📄 playerui.java
字号:
package com.yxw.view;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.MediaException;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;
import com.yxw.model.PlayList;
import com.yxw.util.PlayThread;
import com.yxw.util.RMSDao;
import com.yxw.control.*;
import com.yxw.interfaces.*;
public class PlayerUI extends Canvas implements CommandListener,PlayListener{
private PlayThread player;//播放线程
//播放器菜单
private Command command_add;
private Command command_shunxu;
private Command command_xunhuan;
private Command command_exit;
private Command command_back;
// 控制播放器
private int volumNum=100;
private boolean isPause=false;//暂停
private boolean isStop=false;//停止
private boolean isError=false;
private int addTime=0;
// 显示时间
private long currentTime=0;//当前时间
private long eachCurrentTime=0;//单个段的当前时间
private long eachDurationTime=0;//该段的持续时间
private long totalDurationTime=0;//从开始播放到current的持续时间
private String displayTime="00:00";
// 显示给用户的状态
private int displayToUser=0; //1-网络连接中 2-缓冲 3-播放
public static int musicState=2; //0-缓冲中 1-播放中 2-结束 3-暂停
private boolean endLoadFirst=false;
// 播放文件的信息
private int currentFileId;//当前播放的文件在列表中的id
private PlayList[] list=null;//播放列表
private long haveDown;
private long allHaveDown;
private long currentSize=0;
private String percent="0%";
private long process=1;
private long size=-1;//当前播放文件的大小
private String messageShow="正在播放....";
private boolean isNewFile=false;//播放的文件url是否已经在播放列表中存在
private String fileName;
private String url;
private boolean isLoop=false;//是否循环播放
//屏幕元素
private int height;
private int width;
private Image left;
private Image right;
private Image pause;
private Image play;
//系统配置
private boolean isProxy=false;
private int cacheSize=200;
public PlayerUI(String fileName,long size,String url,int index){
//super(false);
try {
left=Image.createImage("/left.png");
right=Image.createImage("/right.png");
pause=Image.createImage("/pause.png");
play=Image.createImage("/play.png");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
this.height=this.getHeight();
this.width=this.getWidth();
RMSDao rmsDao=new RMSDao();
this.list=rmsDao.getFileList("playList");
byte[] config=rmsDao.getConfig("config");
rmsDao=null;
if(config==null){
this.cacheSize=200;
this.isProxy=false;
}else{
ByteArrayInputStream inputStream=new ByteArrayInputStream(config);
DataInputStream dis=new DataInputStream(inputStream);
try {
this.cacheSize=dis.readInt();
this.isProxy=dis.readBoolean();
inputStream.reset();
inputStream.close();
dis.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
this.cacheSize=200;
this.isProxy=false;
e.printStackTrace();
}
}
if((list==null||list.length==0)&&(url==null||url.equals(""))){
this.messageShow="初始化失败!无播放文件";
this.isError=true;
return;
}
this.currentFileId=index;
if(url!=null&&!url.equals("")){//如果播放新文件
if(this.isNewFile(url)){
this.isNewFile=true;
}else{
this.isNewFile=false;
}
this.fileName=fileName;
this.size=size;
this.url=url;
}else{
this.isNewFile=false;
this.fileName=this.list[this.currentFileId].getFileName();
this.size=this.list[this.currentFileId].getSize();
this.url=this.list[this.currentFileId].getUrl();
}
//System.out.println(this.width+"x"+this.height);
command_add=new Command("收 藏",Command.ITEM,2);
command_shunxu=new Command("顺序播放",Command.ITEM,2);
command_xunhuan=new Command("循环播放",Command.ITEM,2);
command_exit=new Command("退出播放器",Command.ITEM,2);
command_back=new Command("返 回",Command.ITEM,1);
this.setCommandListener(this);
this.addCommand(command_back);
if(this.isNewFile){
this.addCommand(command_add);
}
this.addCommand(command_shunxu);
this.addCommand(command_xunhuan);
this.addCommand(command_exit);
this.haveDown=0;
this.isStop=false;
this.startPlay();
StringBuffer sb=new StringBuffer();
sb.append("正在播放:");
sb.append("\n");
sb.append(this.fileName);
sb.append(" 大小:");
sb.append(this.size/1024);
sb.append("KB");
this.messageShow=sb.toString();
}
private boolean isNewFile(String url){
for(int i=0;i<this.list.length;i++){
if(url.equalsIgnoreCase(this.list[i].getUrl())){
return false;
}
}
return true;
}
protected void paint(Graphics g) {
// TODO 自动生成方法存根
g.setColor(222,235,198);
g.fillRect(0,0, this.width, this.height);
g.setColor(125);
g.drawString(this.messageShow,this.width/2,this.height/2,Graphics.HCENTER|Graphics.BASELINE);
g.drawString("缓冲:"+percent, 30, this.height-35, Graphics.HCENTER|Graphics.BASELINE);
g.drawRect(2, this.height-30, this.width-5, 5);
//g.setColor(120);
//g.fillRect(30, this.height-32, 10, 8);
g.setColor(180);
g.fillRect(2, this.width-30, (int)process, 5);
g.drawImage(left, 8,this.height-10, Graphics.HCENTER|Graphics.VCENTER);
if(this.isPause){
g.drawImage(play, 35,this.height-10, Graphics.HCENTER|Graphics.VCENTER);
}else{
g.drawImage(pause, 35,this.height-10, Graphics.HCENTER|Graphics.VCENTER);
}
g.drawImage(right, 65,this.height-10, Graphics.HCENTER|Graphics.VCENTER);
g.drawString("音量:"+this.volumNum, this.width-50, this.height-10, Graphics.HCENTER|Graphics.BASELINE);
//g.drawString("fsdfs", arg1, arg2, arg3)
//this.flushGraphics();
}
protected void keyPressed(int key){
//System.out.println("key pressed");
switch(getGameAction(key)){
case Canvas.UP:if(this.player==null)return;this.volumNum+=10;if(this.volumNum>100)this.volumNum=100;this.player.setVolumn(this.volumNum);repaint();break;
case Canvas.DOWN:if(this.player==null)return;this.volumNum-=10;if(this.volumNum<0)this.volumNum=0;this.player.setVolumn(this.volumNum);this.repaint();break;
case Canvas.LEFT:this.currentFileId--;if(this.currentFileId<0){this.currentFileId=this.list.length-1;}this.setParams();this.stopPlay();this.startPlay();break;
case Canvas.RIGHT:this.currentFileId++;if(this.currentFileId>=this.list.length){this.currentFileId=0;}this.setParams();this.stopPlay();this.startPlay();break;
case Canvas.FIRE:if(!this.isPause){try {
this.player.getMyPlayer().stop();
} catch (MediaException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}}else{try {
this.player.getMyPlayer().start();
} catch (MediaException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}}this.isPause=!this.isPause;this.repaint();break;
}
}
public void setParams(){
this.fileName=this.list[this.currentFileId].getFileName();
this.url=this.list[this.currentFileId].getUrl();
this.size=this.list[this.currentFileId].getSize();
}
public void startPlay(){//调用之前需要设置好将要播放的文件参数
// if(this.isNewFile){//播放列表以外的文件
// this.player=new PlayThread(this.url,this.size,false);
// }else{
/*long temp_size=this.list[this.currentFileId].getSize();
if(temp_size>0){
this.size=temp_size;
}*/
this.player=new PlayThread(this.url,this.size,false,this.isProxy,this.cacheSize);
// }
this.player.setPlayListener(this);
this.player.start();
this.player.setCurrentVolumn(this.volumNum);
StringBuffer sb=new StringBuffer();
sb.append("正在播放:");
sb.append("\n");
sb.append(this.fileName);
sb.append(" 大小:");
sb.append(this.size/1024);
sb.append("KB");
this.messageShow=sb.toString();
}
public void stopPlay(){
if(this.player!=null){
this.player.close();
}
this.player=null;
System.gc();
this.size=-1;
this.allHaveDown=0;
this.process=1;
this.percent="0%";
}
public void commandAction(Command arg0, Displayable arg1) {
// TODO 自动生成方法存根
if(arg0==this.command_back){
MainMidlet.goBack();
}else if(arg0==this.command_exit){
this.stopPlay();
this.isStop=true;
MainMidlet.goBack();
}else if(arg0==this.command_shunxu){
this.isLoop=false;
}else if(arg0==this.command_xunhuan){
this.isLoop=true;
}else if(arg0==this.command_add){
RMSDao rmsDao=new RMSDao();
int t=rmsDao.addRecord("playList", this.fileName, this.size, this.url);
this.list=null;
this.list=rmsDao.getFileList("playList");
rmsDao=null;
System.out.println("添加操作:"+1);
this.isNewFile=false;
this.removeCommand(this.command_add);
}
}
public void onPlayError(String message) {
// TODO 自动生成方法存根
this.isError=true;
long m=Runtime.getRuntime().freeMemory();
System.out.println("freeMemory:"+m);
System.out.println(message);
this.messageShow=message;
this.stopPlay();
this.repaint();
}
public void onPlayFinish() {//播放完毕后判断是否播放下一首
// TODO 自动生成方法存根
System.out.println("onPlayFinish");
this.messageShow="该曲播放完毕";
this.repaint();
this.stopPlay();
this.currentFileId++;
if(this.currentFileId>=this.list.length){
if(this.isLoop){
this.currentFileId=0;
this.startPlay();
}else{
this.currentFileId--;
return;
}
}
}
public void onError(int code, String message) {
// TODO 自动生成方法存根
this.messageShow=message;
this.isError=true;
this.stopPlay();
System.out.println("onError"+message);
this.repaint();
}
public void onFinish(byte[] data, int size) {
// TODO 自动生成方法存根
System.out.println("onFinish:"+size);
this.allHaveDown+=size;
}
public void onProgress(int percent) {
// TODO 自动生成方法存根
this.haveDown=percent;
// System.out.println("allhaveDown:"+this.allHaveDown+"size:"+this.size);
this.process=((this.width-7)*(this.allHaveDown+percent))/this.size;
this.percent=String.valueOf((this.haveDown*100)/this.currentSize)+"%";
this.repaint();
//System.out.println("onProgress:"+percent);
}
public void onSetSize(int size,long fileSize) {
// TODO 自动生成方法存根
//System.out.println("onSetSize:"+size);
this.currentSize=size;
if(fileSize>0&&this.size==-1){
this.size=fileSize;
StringBuffer sb=new StringBuffer();
sb.append("正在播放:");
sb.append("\n");
sb.append(this.fileName);
sb.append(" 大小:");
sb.append(this.size/1024);
sb.append("KB");
this.messageShow=sb.toString();
}
}
public void onGetFileSize(long size) {
// TODO 自动生成方法存根
this.size=size;
//System.out.println("allhaveDown:"+this.allHaveDown+"size:"+this.size);
}
public void recordAdded(RecordStore arg0, int arg1) {
// TODO 自动生成方法存根
try {
if(arg0.getName().equals("playList")){
RMSDao rmsDao=new RMSDao();
this.list=null;
this.list=rmsDao.getFileList("playList");
rmsDao=null;
this.currentFileId=0;
}
} catch (RecordStoreNotOpenException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public PlayList[] getList() {
return list;
}
public void setList(PlayList[] list) {
this.list = list;
}
public boolean isStop() {
return isStop;
}
public int getCurrentFileId() {
return currentFileId;
}
public void setCurrentFileId(int currentFileId) {
this.currentFileId = currentFileId;
}
public boolean isError() {
return isError;
}
public void setError(boolean isError) {
this.isError = isError;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -