Javability (Java, Zaurus, Linux, Live) by Jean-Marc Autexier, Saarland/Germany
cat /dev/www | egrep 'Java|Linux|Zaurus|ITnews|Live' > blog

16.12.04 18:43 Java: Play midi file in a loop until stop ( , , , )

The code below allows to select a midi file for endless loop playback until stopPlay() is called. It is more or less identical to this one.

The special thing is: how to loop a MIDI file?
The simplest way is to register as MetaEventListener and catch Midi events (see Midi Event class diagramm)

MetaMessage type 47 signals end of track. When called, playback will be restarted.


private Sequencer sm_sequencer = null;
File midiFile = null;

public class LoopMidiPlayback implements MetaEventListener
{
public LoopMidiPlayback(String strFilename)
{
midiFile = new File(strFilename);
}
public void startPlay()
{
try
{
Sequence sequence = MidiSystem.getSequence(midiFile);
sm_sequencer = MidiSystem.getSequencer();
sm_sequencer.open();
sm_sequencer.setSequence(sequence);
sm_sequencer.addMetaEventListener(this) ;
             
sm_sequencer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
     
public void stopPlay()
{
sm_sequencer.stop();
sm_sequencer.close();
}

public void meta(MetaMessage event)
{
if (event.getType() == 47)
{
startPlay();
}
}
}

posted by Jean-Marc Autexier | 0 comments | Permalink | Send to Friends | Google it!
Subscribe

Locations of visitors to this page
selected blogs
ressources
Security
Unsorted
Fun
Free&Open Software
archives
This is a personal web page. Things said here do not represent the position of my employer.
RSS icons by: FastIcon.com