📄 midiplayer.java
字号:
/*
* Created on 2005-9-28 by pcy
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package a.a.a.mmedia;
import javax.microedition.media.Control;
import javax.microedition.media.MediaException;
import javax.microedition.media.PlayerListener;
import javax.sound.midi.*;
public class MidiPlayer extends BasicPlayer implements Runnable {
private Sequence sequence = null;
private Sequencer sequencer;
private Thread thread;
public MidiPlayer() {
super();
}
protected void doRealize() throws MediaException {
try{
if(stream!=null){
sequence=MidiSystem.getSequence(stream);
sequencer=MidiSystem.getSequencer();
sequencer.setSequence(sequence);
sequencer.open();
}
}catch(Exception e){
throw new MediaException(e.getMessage());
}
}
protected void doPrefetch() throws MediaException {
}
protected boolean doStart() {
if(thread!=null){
thread.interrupt();
thread=null;
}
thread=new Thread(this);
thread.start();
return true;
}
protected void doStop() {
if(sequencer!=null&&sequencer.isRunning()){
sequencer.stop();
}
}
protected void doDeallocate() {
if(sequencer!=null&&sequencer.isOpen()){
sequencer.close();
}
sequence=null;
sequencer=null;
if(thread!=null){
thread.interrupt();
thread=null;
}
}
protected void doClose() {
if(sequencer!=null&&sequencer.isOpen()){
sequencer.close();
}
}
protected long doSetMediaTime(long now) throws MediaException {
sequencer.setMicrosecondPosition(now);
return sequencer.getMicrosecondPosition();
}
protected long doGetMediaTime() {
return sequencer.getMicrosecondPosition();
}
protected long doGetDuration() {
return sequencer.getMicrosecondLength();
}
protected Control doGetControl(String type) {
if ((getState() >= REALIZED) &&
type.equals("javax.microedition.media.control.VolumeControl")) {
return this;
}
return null;
}
protected int doSetLevel(int vol) {
try{
MidiChannel[] channels=MidiSystem.getSynthesizer().getChannels();
if(channels!=null){
for(int i=0; i<channels.length; ++i){
if(vol<=0){
channels[i].allNotesOff();
channels[i].allSoundOff();
channels[i].setMute(true);
}else{
channels[i].controlChange(0x7,0);
channels[i].controlChange(0x27,0);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return vol;
}
public void run() {
if(sequencer!=null&&sequencer.isOpen()){
sequencer.start();
while (sequencer.isRunning() && thread != null) {
try {
Thread.sleep(300);
}catch (Exception e) {
break;
}
}
this.sendEvent(PlayerListener.END_OF_MEDIA,null);
}
if(thread!=null){
thread.interrupt();
thread=null;
}
}
public String getContentType() {
chkClosed(true);
return "audio/midi";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -