我有一个Java应用程序,可以在PC的扬声器上生成,然后播放一些简单的单轨MIDI音乐。它在几个版本的Windows上运行得非常好,但我最近在Windows 10上尝试过它并且它失败了:
代码(我刚刚更新了它,因此它是可测试的,更接近最小的例子):
import javax.sound.midi.*;
class zik2 {
static Track track;
static int VEL=50;
static int PROG=21; // 19: church organ, 21: harmonica
static int[] gamme = {60,62,64,65,67,69,71,72,74,76,77,79,81,83,84};
public static void main(String[] args) {
try {
Sequence seq=new Sequence(Sequence.PPQ,1);
track=seq.createTrack();
long tick=0;
ShortMessage sm;
sm=new ShortMessage();
sm.setMessage(ShortMessage.PROGRAM_CHANGE,PROG,0);
track.add(new MidiEvent(sm,tick));
for(int k=0; k<7; k++) {
int l=gamme[k];
sm.setMessage(ShortMessage.NOTE_ON,0,l,VEL);
track.add(new MidiEvent(sm,tick++));
sm.setMessage(ShortMessage.NOTE_OFF,0,l,0);
track.add(new MidiEvent(sm,tick+1));
}
Sequencer player=MidiSystem.getSequencer();
player.open();
player.setSequence(seq);
player.setTempoInBPM(60);
player.start();
System.out.println("Hello (world)");
do { Thread.sleep(50); } while(player.isRunning());
player.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
更新:以下简短测试有效,所以问题在于音序器?
import javax.sound.midi.*;
public class test {
public static void main(String[] args)
{
try {
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
MidiChannel[] channels = synthesizer.getChannels();
channels[0].noteOn(60, 60);
Thread.sleep(1000);
channels[0].noteOff(60);
Thread.sleep(1000);
synthesizer.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}