package test;
import javax.sound.midi.*;
import javax.sound.sampled.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: SinoSoft</p>
* @author pete
* @version 1.0
*/
public class PlayMidi implements Runnable{
File sound;
Sequence seq;
Sequencer midi;
Thread runner;
public PlayMidi(String f){
try {
sound=new File(f);
seq=MidiSystem.getSequence(sound);
midi=MidiSystem.getSequencer();
midi.open();
midi.setSequence(seq);
}
catch (Exception ex) {
}
}
public void run(){
try {
while(true){
if(!midi.isRunning())midi.start();
Thread.sleep(1000);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void playMidi(){
try
{
//midi.start();
if(runner==null){
runner=new Thread(this);
runner.start();
}
}
catch (Exception ex)
{
}
}
public void stopMidi(){
try
{
runner.stop();
runner=null;
midi.stop();
}
catch (Exception ex)
{
}
}
public static void main(String[] args)
{
PlayMidi playMidi1 = new PlayMidi("mytest.mid");
playMidi1.playMidi();
playMidi1.stopMidi();
}
}