我正在尝试使用 javax.sound.midi 编写一个简单的程序,该程序可以读取、编辑然后通过 FluidSynth 播放 midi 文件。这是我的代码片段:
Synthesizer synth;
// Look through the available midi devices for a software synthesizer
MidiDevice.Info[] deviceInfo = MidiSystem.getMidiDeviceInfo();
for(MidiDevice.Info currentDevice : deviceInfo){
if(currentDevice.getName().matches("FluidSynth virtual port.*"))
synth = (Synthesizer) MidiSystem.getMidiDevice(currentDevice);
}
// If FluidSynth is not found, use default synth
if(synth == null){
synth = MidiSystem.getSynthesizer();
System.out.println("Using default synth");
}
// Do stuff with synth
代码编译成功,但是当我运行它时,出现以下异常:
Exception in thread "main" java.lang.ClassCastException: java.desktop/com.sun.media.sound.MidiOutDevice cannot be cast to java.desktop/javax.sound.midi.Synthesizer
at SynthesizerDemo.main(SynthesizerDemo.java:49)
我期待返回 Synthesizer 类,但我不明白com.sun.media.sound.MidiOutDevice
该类是什么。如果我将合成器切换到MidiDevice
类,播放工作,但我无法访问Synthesizer
类中的所有方法。知道我缺少什么吗?