Java播放midi文件

如下面代码,运行后即可播放:

import java.io.File;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class PlayMidi
{
    private static File sound;
    private static Sequence seq;
    private static Sequencer midi;
    
    public static void Play(String filename)
    {
    	 try 
         {
             sound = new File(filename);
             seq = MidiSystem.getSequence(sound);
             midi= MidiSystem.getSequencer();
             midi.open();
             midi.setSequence(seq);
             
             if(!midi.isRunning())
             	midi.start();
             
         } catch (Exception ex) {
         }
    }
    
    public static void Stop()
    {
    	if(midi.isRunning())
			midi.stop();
    	
    	if(midi.isOpen())
    		midi.close();
    }
    
    public static void main(String[] args)
    {
    	PlayMidi.Play("D:\\tianhou.mid");
    	long time = midi.getMicrosecondLength()/1000;

    	try 
    	{
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		PlayMidi.Stop();
    }
} 

  

你可能感兴趣的:(java,thread)