📄 midiplayer.java
字号:
package midiplayer;import javax.sound.midi.*;import java.io.*;import java.net.*;/** * <p>Title: MIDI播放器</p> * <p>Description: 使用javax.sound.midi包中的类实现MIDI文件的播放</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京师范大学计算机系</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)); // 读网络地址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(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -