答案是这样的,我终于想出来了:
Instrument instruments[];
Soundbank sb=synthesizer.getDefaultSoundbank();
if (sb!=null) instruments=synthesizer.getDefaultSoundbank().getInstruments();
...
try
{
Sequence sequence=MidiSystem.getSequence(file); // From file
int trackNumber=0;
for (Track track : sequence.getTracks())
{
System.out.print("Track "+(trackNumber++)+" : ");
MidiEvent event=track.get(0);
MidiMessage message=event.getMessage();
if (message instanceof ShortMessage)
{
ShortMessage sm=(ShortMessage)message;
if (sm.getCommand()==192) Out("sm.getChannel()="+sm.getChannel()+" sm.getData1()="+sm.getData1()+" "+instruments[sm.getData1()]);
}
}
输出如下所示:
Track 0 : sm.getChannel()=0 sm.getData1()=0 Instrument: Piano 1 bank #0 preset #0
Track 1 : sm.getChannel()=1 sm.getData1()=16 Instrument: Organ 1 bank #0 preset #16
Track 2 : sm.getChannel()=2 sm.getData1()=56 Instrument: Trumpet bank #0 preset #56
Track 3 : sm.getChannel()=3 sm.getData1()=25 Instrument: Steel-str.Gt bank #0 preset #25