我无法使用javax.sound.midi.ShortMessage.setMessage()方法更改乐器。通道0-15为钢琴,其中9为鼓。但8-15应该是定音鼓。当我将通道设置为16或更多(范围从0到127),我会收到javax.sound.midi.InvalidMidiDataException:通道超出范围:16。请问,我该如何解决这个问题?
import javax.sound.midi.*;
public class TestMidi{
public void play(){
try{
Sequencer player = MidiSystem.getSequencer();
System.out.println("Sequencer created succesfully.");
player.open();
Sequence s = new Sequence(Sequence.PPQ, 4);
Track track = s.createTrack();
addShortMessage(track,1,192,1,102,0);
addShortMessage(track,1,144,1,44,100);
addShortMessage(track,16,128,1,44,100);
player.setSequence(s);
player.start();
} catch(Exception e){
System.out.println("Fail.");
e.printStackTrace();
}
}
public static void main(String[] args) {
TestMidi tm = new TestMidi();
tm.play();
}
void addShortMessage(Track track, long tick, int command, int channel, int data1, int data2) throws Exception {
ShortMessage msg = new ShortMessage(command, channel, data1, data2);
MidiEvent event = new MidiEvent(msg, tick);
track.add(event);
}
}