my code plays a midi java sound file , but i want to play it in a loop constantly ! can somebody help me please !

import javax.sound.midi.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MidiPlayer{

  public static void main(String[] args) {
      try {
          Sequencer sequencer = MidiSystem.getSequencer();
          if (sequencer == null)
              throw new MidiUnavailableException();
          sequencer.open();
          FileInputStream is = new FileInputStream("smb2.mid");
          Sequence Seq = MidiSystem.getSequence(is);
          sequencer.setSequence(Seq);
          sequencer.start();

      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}

Recommended Answers

All 3 Replies

Hey sorry I'm not here to answer your question but to ask one. When you run this code does it play the midi and the code stops running or does it it keep running after it plays the sound. The reason I'm asking is because whenever I run code similar to that it just keeps running after it plays the midi.

Hey sorry I'm not here to answer your question but to ask one. When you run this code does it play the midi and the code stops running or does it it keep running after it plays the sound. The reason I'm asking is because whenever I run code similar to that it just keeps running after it plays the midi.

Well the song palys completly then stops...i want it to keep playing on !

Why not use a for loop:

for (boolean toBreak; toBreak == false; sequencer.start()) {
//check if the user want's to exit, wait a certain amount of milliseconds, whatever.
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.