我有以下代码,我在其中创建一个Sequencer,给它一个Sequence,给Sequence一个新的Track,将MidiEvents写入Track,然后在我调用sequencer.start()时尝试播放Sequence。我做错了什么?
public static void main(String args[]) {
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.setTempoInBPM(96);
sequencer.setTickPosition(0);
Sequence seq = new Sequence(Sequence.PPQ, 5);
sequencer.setSequence(seq);
sequencer.open();
Track mainTrack = seq.createTrack();
sequencer.recordEnable(mainTrack, 0);
sequencer.startRecording();
ShortMessage msg = new ShortMessage();
ShortMessage msg2 = new ShortMessage();
msg.setMessage(ShortMessage.NOTE_ON, 0, 60, 100);
msg2.setMessage(ShortMessage.NOTE_OFF, 0, 60, 100);
mainTrack.add(new MidiEvent(msg, 0));s
mainTrack.add(new MidiEvent(msg2, 400));
Thread.sleep(5000);
sequencer.stopRecording();
sequencer.start();
Thread.sleep(5000);
sequencer.close();
} catch (MidiUnavailableException | InvalidMidiDataException |
InterruptedException e) {
System.err.println(e.getMessage());
}
}
答案 0 :(得分:0)
public static void main(String args[]) {
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.setTempoInBPM(96);
sequencer.setTickPosition(0);
Sequence seq = new Sequence(Sequence.PPQ, 5);
// sequencer.setSequence(seq);
sequencer.open();
Track mainTrack = seq.createTrack();
// sequencer.recordEnable(mainTrack, 0);
// sequencer.startRecording();
ShortMessage msg = new ShortMessage();
ShortMessage msg2 = new ShortMessage();
msg.setMessage(ShortMessage.NOTE_ON, 0, 60, 100);
msg2.setMessage(ShortMessage.NOTE_OFF, 0, 60, 100);
mainTrack.add(new MidiEvent(msg, 0));
mainTrack.add(new MidiEvent(msg2, 4000));
// Thread.sleep(5000);
// sequencer.stopRecording();
sequencer.setSequence(seq);
Synthesizer synth = MidiSystem.getSynthesizer();
sequencer.getTransmitter().setReceiver(synth.getReceiver());
synth.open();
sequencer.start();
Thread.sleep(5000);
sequencer.close();
} catch (MidiUnavailableException | InvalidMidiDataException | InterruptedException e) {
System.err.println(e.getMessage());
}
}