在MIDI的播放中,一个类是比较重要的,那就是MidiSystem类,它负责整个MIDI播放设备等的管理,其实就是Seqencer,它就是一个MIDI播放设置,用于播放MIDI序列的,还有一个类叫Seqence,它就是MIDI的序列,MIDI的序列可以自己由程序生成,也可以从文件中或者URL中读取。
下面我们来看一个例子吧:
* Test5.java
*
* Created on 2007-9-22, 11:16:22
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test1;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
/**
*
* @author hadeslee
*/
public class Test5 implements Runnable{
private Sequencer midi;
private String[] names={"1.mid","2.mid","3.mid","4.mid","5.mid"};
private int i;
private Map<String,Sequence> map;
public Test5(){
initMap();
new Thread(this).start();
}
private void initMap(){
try {
map = new Hashtable<String, Sequence>();
midi = MidiSystem.getSequencer(false);
midi.open();
for (String s : names) {
try {
Sequence s1 = MidiSystem.getSequence(new File(s));
map.put(s, s1);
} catch (InvalidMidiDataException ex) {
Logger.getLogger(Test5.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test5.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (MidiUnavailableException ex) {
Logger.getLogger(Test5.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void createPlayer(String name){
try {
Sequence se=map.get(name);
midi.setSequence(se);
midi.start();
}catch (InvalidMidiDataException ex) {
Logger.getLogger(Test5.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run(){
while(true){
try {
System.out.println("换文件了."+(++i));
String name=names[(int)(Math.random()*names.length)];
createPlayer(name);
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(Test5.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
new Test5();
}
}