?? playerui.java
字號:
package com.j2medev.chapter7;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.GUIControl;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;
public class PlayerUI extends Canvas implements Runnable,CommandListener,PlayerListener{
private Player player = null;
private MainMIDlet midlet = null;
private int type = 1;
private long ptime = 0;
private String title = "";
private long duration = 0;
private int percent = 0;
private Thread currentThread = null;
private boolean stop = false;
public static final int WAV = 1;
public static final int VIDEO = 2;
private Command backCommand = new Command("Back",Command.BACK,1);
private Command pauseCommand = new Command("pause",Command.OK,2);
private Command playCommand = new Command("Play",Command.OK,2);
public PlayerUI(MainMIDlet midlet) {
this.midlet = midlet;
this.addCommand(pauseCommand);
this.addCommand(backCommand);
this.setCommandListener(this);
}
public void setType(int type){
this.type = type;
}
public void paint(Graphics g){
int w = getWidth();
int h = getHeight();
g.setColor(0xFFFFFF);
g.fillRect(0, 0, w, h);
//如果是wav文件,顯示播放進度
if(type == WAV){
g.setColor(0x000000);
g.drawRect((w-100)/2,h-10,100,6);
g.setColor(255,0,0);
g.fillRect((w-100)/2+1,h-9,percent,5);
}
}
//啟動player
public void startPlayer(){
if(currentThread == null){
stop = false;
currentThread = new Thread(this);
currentThread.start();
}else if(player != null){
try {
player.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
//根據type類型創建不同的播放器
public void createPlayer(){
if(type == WAV){
try{
InputStream is = getClass().getResourceAsStream("/bark.wav");
player = Manager.createPlayer(is,"audio/x-wav");
player.setLoopCount(-1);
}catch(MediaException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}else if(type == VIDEO){
try{
InputStream is = getClass().getResourceAsStream("/moon.mpg");
player = Manager.createPlayer(is,"video/mpeg");
}catch(MediaException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public void run(){
try{
if(player == null){
createPlayer();
}
player.realize();
if(type == WAV){
duration = player.getDuration();
player.prefetch();
player.start();
while(!stop){
try{
long t = player.getMediaTime();
percent = (int)((100*t)/duration);
Thread.sleep(100);
repaint();
}catch(InterruptedException ex){
}
}
}else{
player.addPlayerListener(this);
VideoControl vc = (VideoControl)player.getControl("VideoControl");
/* Form form = new Form("test");
form.append((Item)vc.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE,null));
midlet.setCurrent(form);
*/
if(vc != null){
vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);
//vc.setDisplayFullScreen(true);
vc.setDisplayLocation(0,0);
vc.setVisible(true);
}
player.start();
}
}catch(MediaException ex){
ex.printStackTrace();
}
}
//停止播放器
public void stopPlayer(){
try {
currentThread = null;
stop = true;
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if(player != null){
player.close();
player = null;
}
}
public void pausePlayer(){
if(player != null){
try{
player.stop();
}catch(MediaException ex){
ex.printStackTrace();
}
}
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == pauseCommand){
removeCommand(pauseCommand);
addCommand(playCommand);
ptime = player.getMediaTime();
pausePlayer();
}else if(cmd == playCommand){
removeCommand(playCommand);
addCommand(pauseCommand);
try {
player.setMediaTime(ptime);
player.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
}else if(cmd == backCommand){
stopPlayer();
removeCommand(playCommand);
addCommand(pauseCommand);
midlet.setCurrent(midlet.getMenu());
}
}
protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
VolumeControl vc = (VolumeControl)player.getControl("VolumeControl");
if(action == DOWN){
vc.setLevel(vc.getLevel()-10);
}else if(action == UP){
vc.setLevel(vc.getLevel()+10);
}
}
public void playerUpdate(Player player, String string, Object object) {
if(string.equals(this.END_OF_MEDIA)){
stopPlayer();
midlet.setCurrent(midlet.getMenu());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -