title: Looping a MIDI sequence in Java
body: <p>I'm trying to loop a MIDI sequence in a java game I'm making and I'm having some issues.</p>
  
  <p>The current code I have does repeat the sequence, but there is a large delay between the end of the sequence and the restart. How can I eliminate this?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>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();}
  </code></pre>