?? midiplayer.java
字號:
package midiplayer;import javax.sound.midi.*;import java.io.*;import java.net.*;/** * <p>Title: MIDI播放器</p> * <p>Description: 使用javax.sound.midi包中的類實現(xiàn)MIDI文件的播放</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京師范大學(xué)計算機(jī)系</p> * @author 曾文琪 * @version 1.0 */public class MIDIPlayer { private static String midiFile = "canyon.mid"; private static String midiURI = "http://.../canyon.mid"; private Sequence sequence =null; public MIDIPlayer() { this.loadAndPlay(); } public void loadAndPlay(){ try { sequence = MidiSystem.getSequence(new File(midiFile)); // 讀網(wǎng)絡(luò)地址URL中MIDI文件// sequence = MidiSystem.getSequence(new URL("http://hostname/midifile")); Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); double durationInSecs = sequencer.getMicrosecondLength() / 1000000.0; System.out.println("the duration of this audio is "+durationInSecs+"secs."); double seconds = sequencer.getMicrosecondPosition() / 1000000.0; System.out.println("the Position of this audio is "+seconds+"secs."); if (sequencer instanceof Synthesizer) { Synthesizer synthesizer = (Synthesizer)sequencer; MidiChannel[] channels = synthesizer.getChannels(); double gain = 0.9D; for (int i=0; i<channels.length; i++) { channels[i].controlChange(7, (int)(gain * 127.0)); } } sequencer.start(); Thread.currentThread().sleep(5000); seconds = sequencer.getMicrosecondPosition() / 1000000.0; System.out.println("the Position of this audio is "+seconds+"secs."); sequencer.addMetaEventListener( new MetaEventListener() { public void meta(MetaMessage event) { if (event.getType() == 47) { System.out.println("Sequencer is done playing."); } } }); }catch (MalformedURLException e) { }catch (IOException e) { }catch (MidiUnavailableException e) { }catch (InvalidMidiDataException e) { }catch (InterruptedException e){ } } public static void main(String[] args) { MIDIPlayer midiplayer = new MIDIPlayer(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -