我正在尝试在我正在制作的java游戏中循环MIDI序列,我遇到了一些问题。
我当前的代码确实重复了序列,但序列结束和重启之间存在很大的延迟。我该怎样消除这个?
这是我的代码:
try
{
// From file
final Sequence sequence = MidiSystem.getSequence(new File("main menu.mid"));
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(new MetaEventListener() {
public void meta(MetaMessage msg) {
if (msg.getType() == 47) { // End of track
sequencer.setTickPosition(0);
try
{
sequencer.setSequence(sequence);
} catch(InvalidMidiDataException e) {e.printStackTrace();}
sequencer.start();
}
}
});
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
} catch (IOException e) {e.printStackTrace();}
catch (MidiUnavailableException e) {e.printStackTrace();}
catch (InvalidMidiDataException e) {e.printStackTrace();}
答案 0 :(得分:1)
这个改编自Java Sound tag Wiki的音源播放MIDI,循环之间没有“长时间延迟”,这表明你听到的延迟是现有音轨的静音前奏/外音的一部分。
import javax.sound.midi.*;
import javax.swing.JOptionPane;
import java.net.URL;
class LoopMidi {
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/EverLove.mid");
Sequence sequence = MidiSystem.getSequence(url);
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
//sequencer.setLoopStartPoint(2000);
//sequencer.setLoopEndPoint(4000);
sequencer.setLoopCount(5);
sequencer.start();
JOptionPane.showMessageDialog(null, "Everlasting Love");
}
}
解决方案在于: