听不到任何来自 MIDI 的声音

Can't hear any sounds from MIDI

我从 java 开始,并尝试使用 midi 播放一些声音。我正在关注 "Head First" 这本书。问题是我听不到任何声音,这是我的代码

package pakedz;
import javax.sound.midi.*;

public class odtwarzaczMuzyki {
  public void graj(){
   try {
    Sequencer sekwenser = MidiSystem.getSequencer();
    System.out.println("Mamy sekwenser");
    sekwenser.open();

    Sequence sekwencja = new Sequence(Sequence.PPQ,4);
    Track sciezka = sekwencja.createTrack();

    ShortMessage a = new ShortMessage();
    a.setMessage(144, 1, 20, 100);
    MidiEvent nutaP = new MidiEvent(a, 1);
    sciezka.add(nutaP);
    ShortMessage b = new ShortMessage();
    b.setMessage(128, 1, 44, 100);
    MidiEvent nutaK = new MidiEvent(b, 16);
    sciezka.add(nutaK);

     sekwenser.setSequence(sekwencja);

 } catch (Exception ex) { 
    System.out.println("KUTASMARIAN");
}

 };

 public static void main (String[] args){
 odtwarzaczMuzyki radio = new odtwarzaczMuzyki();
 radio.graj();

 }

} 

我正在使用 java 8.0

需要调用Sequencer中的start()方法。下面是修改后的(未经测试的)代码。

建议检查Javadoc and official tutorials

package pakedz;
import javax.sound.midi.*;

public class odtwarzaczMuzyki {
  public void graj(){
    try {
      Sequencer sekwenser = MidiSystem.getSequencer();
      System.out.println("Mamy sekwenser");
      sekwenser.open();

      Sequence sekwencja = new Sequence(Sequence.PPQ,4);
      Track sciezka = sekwencja.createTrack();

      ShortMessage a = new ShortMessage();
      a.setMessage(144, 1, 20, 100);
      MidiEvent nutaP = new MidiEvent(a, 1);
      sciezka.add(nutaP);
      ShortMessage b = new ShortMessage();
      b.setMessage(128, 1, 44, 100);
      MidiEvent nutaK = new MidiEvent(b, 16);
      sciezka.add(nutaK);

      sekwenser.setSequence(sekwencja);
      sekwenser.start();

    }
    catch (Exception ex) { 
      System.out.println("KUTASMARIAN");
    }
  };

  public static void main (String[] args) {
    odtwarzaczMuzyki radio = new odtwarzaczMuzyki();
    radio.graj();
  }
}