📄 playmedia6.java
字号:
package com.wy.ch12;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public class PlayMedia6 extends MIDlet
implements PlayerListener,CommandListener,ItemStateListener{
private Display display;
private Form f;
private Command command1;
private Command command2;
private Command command3;
private Player p;
private Gauge gauge;
private VolumeControl vc;
public PlayMedia6(){
display = Display.getDisplay(this);
f = new Form("音乐播放测试");
command1 = new Command("播放", Command.ITEM, 1);
command2 = new Command("停止", Command.ITEM, 2);
command3 = new Command("重放", Command.ITEM, 3);
f.addCommand(command1);
f.setCommandListener(this);
gauge=new Gauge("音量控制", true, 10, 5);
f.append(gauge);
gauge.setLayout(Item.LAYOUT_CENTER);
f.setItemStateListener(this);
try {
InputStream is = getClass().getResourceAsStream("/test-wav.wav");
p = Manager.createPlayer(is, "audio/X-wav");
p.realize();
vc=(VolumeControl)p.getControl("VolumeControl");
vc.setLevel(50);
p.addPlayerListener(this);
} catch (Exception e){
}
}
public void startApp() {
display.setCurrent(f);
}
public void commandAction(Command command, Displayable displayable) {
if(command==command1){//播放
f.removeCommand(command1);
f.addCommand(command2);
f.addCommand(command3);
if(p!=null){
try{
p.start();
}catch (Exception e){
}
}//if
}//if
if(command==command2){//停止
f.removeCommand(command2);
f.addCommand(command1);
f.addCommand(command3);
if(p!=null){
try{
p.stop();
}catch (Exception e){
}
}//if
}//if
if(command==command3){//重放
f.removeCommand(command3);
f.addCommand(command2);
if((p!=null)&&(p.getState()==Player.PREFETCHED)){
try{
p.setMediaTime(-1);
p.start();
}catch (Exception e){
}
}//if
}//if
}
public void itemStateChanged(Item item){
int volume=gauge.getValue()*10;
if(vc!=null){
vc.setLevel(volume);
}
}
public void playerUpdate(Player player,String event,Object eventData){
if(event==PlayerListener.CLOSED){
System.out.println("播放器被关闭");
}
if(event==PlayerListener.DEVICE_AVAILABLE){
System.out.println("设备可用");
}
if(event==PlayerListener.DEVICE_UNAVAILABLE){
System.out.println("设备不可用");
}
if(event==PlayerListener.DURATION_UPDATED){
System.out.println("播放器更新");
}
if(event==PlayerListener.END_OF_MEDIA){
System.out.println("歌曲播放完毕");
}
if(event==PlayerListener.STARTED){
System.out.println("歌曲播放开始");
}
if(event==PlayerListener.STOPPED){
System.out.println("歌曲播放停止");
f.removeCommand(command2);
f.addCommand(command1);
f.addCommand(command3);
}
if(event==PlayerListener.VOLUME_CHANGED){
System.out.println("播放音量被改变");
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
if(p!=null){
p.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -