节目不产生Midi声音

时间:2017-09-01 21:05:26

标签: java midi

我一直在阅读Head First Java,这是练习制作音序器和播放midi的代码。事情是,我没有听到任何我通过帖子搜索的内容,我听说有一个soundbank.gm文件,我得到的文件仍然没有。无法判断这是我的代码还是我的电脑,因为这本书还没有达到解释这一切的作用。

import javax.sound.midi.*;
public class MiniMiniMusicApp {
    public static void main(String[] args){
        MiniMiniMusicApp  mini = new MiniMiniMusicApp();
        mini.play();
    }

    public void play(){
        try{
            Sequencer player = MidiSystem.getSequencer(); //get a sequencer
            player.open(); //open it
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            Track track = seq.createTrack(); //ask the sequence for a track;
            ShortMessage b = new ShortMessage(); //Put some midi events into the Track.
            b.setMessage(128,1,44,100);
            MidiEvent noteOff = new MidiEvent(b, 16);
            track.add(noteOff);
            player.setSequence(seq); //give the Sequence to the sequencer
            player.start(); // press play.
            Thread.sleep(2000);
            player.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你错过了一个note_on。例如:

ShortMessage b = new ShortMessage(); 
b.setMessage(144, 0, 60, 127);
MidiEvent noteOn = new MidiEvent(b, 2);
track.add(noteOn);

// Here's your note_off
ShortMessage b1 = new ShortMessage(); 
b1.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b1, 16);
track.add(noteOff);