📄 media.java
字号:
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
public class Media //implements PlayerListener
{
public static final int STATE_PLAYING = 0;
public static final int STATE_PAUSE = 1;
public static final int STATE_STOP = 2;
public static final String TYPE_WAVE = "audio/x-wav";
public static final String TYPE_AU = "audio/basic";
public static final String TYPE_MP3 = "audio/mpeg";
public static final String TYPE_MIDI = "audio/midi";
public static final String TYPE_Tone = "audio/x-tone-seq";
protected int state;
protected Player player;
protected String name;
protected String type;
protected boolean loop;
protected boolean isBusy;
public Media(String name,String type,boolean loop) {
this.name=name;
this.type=type;
this.loop=loop;
state=STATE_STOP;
}
private void load() {
try {
if(player!=null)
return ;
player = Manager.createPlayer(this.getClass().getResourceAsStream(name),type);
player.realize();
if(loop) player.setLoopCount(-1);
//player.addPlayerListener(this);
} catch (Exception e){}
}
private void release() {
try {
if(player!=null) {
player.stop();
//player.removePlayerListener(this);
player.deallocate();
player.close();
player=null;
}
} catch(Exception e){}
}
//锟斤拷锟斤拷
public synchronized void play() {
// System.out.println("闊充箰寮?濮嬫挱鏀?1111111111111111");
try {
if(!isBusy && state==STATE_STOP) {
isBusy=true;
load();
state=STATE_PLAYING;
player.prefetch();
//player.setMediaTime(0);
player.start();
}
} catch (Exception e){
// System.out.println("闊砮rr涔愬紑濮嬫挱鏀?3333111111"+e);
}
}
//锟斤拷停
public synchronized void pause() {
try {
if(state==STATE_PLAYING) {
player.stop();
state=STATE_PAUSE;
}
} catch (Exception e){}
}
//锟斤拷锟斤拷
public synchronized void resume() {
try {
if(state==STATE_PAUSE || (!isBusy && state==STATE_STOP && loop)) {
player.start();
state=STATE_PLAYING;
}
} catch (Exception e){}
}
//停止
public synchronized void stop() {
try {
if(state!=STATE_STOP) {
isBusy=false;
release();
state=STATE_STOP;
}
} catch (Exception e){}
}
//锟斤拷锟阶刺?
public synchronized int getState(){return state;}
/* public void playerUpdate (Player player, String event, Object eventData)
{
try{
if(event==PlayerListener.END_OF_MEDIA)
{
if(!loop)
{
isBusy=false;
release();
}
}
}catch(Exception e){}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -