⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playthread.java

📁 基于J2ME
💻 JAVA
字号:
package com.yxw.util;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
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.VolumeControl;

import com.yxw.interfaces.PlayListener;

public class PlayThread extends Thread implements PlayerListener{

	private DownloadThread dowmload;
	private Player myPlayer;
	private VolumeControl volume = null;
	private int currentFragNum;//当前播放的段号
	private int playedTime;//已经播放的总时间,单位为s
	private boolean isWaiting=false;//目前状态为暂停,作为同步变量使用
	private boolean isNativeFile=false;//是否为本地文件
	public byte[][] cacheByte=new byte[2][];
	public boolean change=true;
	private boolean isCancel=false;
	private boolean isFinish=false;
	private boolean isCurrentFinish=true;//当前段的是否完成
	private String medieType="audio/mpeg";
	
	private int currentVolumn;
	private PlayListener playListener;
	public PlayThread(String url,long size,boolean isNativeFile,boolean isProxy,int cacheSize){
		if(!isNativeFile){//不是本地文件
			this.dowmload=new DownloadThread(url,size,this,cacheSize);
			this.dowmload.setProxy(isProxy);
			//this.dowmload.setCacheSize(cacheSize);
			this.dowmload.start();
			this.currentFragNum=1;
			//this.cacheByte=new byte[(int)this.dowmload.getInb().length];//播放的缓冲数组
			//System.out.println("fdsf:"+url);
			String type=url.toLowerCase();
			if(type.endsWith(".mp3")){
				this.medieType="audio/mpeg";
			}else if(type.endsWith(".wav")){
				this.medieType="audio/x-wav";
			}else if(type.endsWith(".mid")){
				this.medieType="audio/sp-midi";
			}
			
		}
		System.out.println("medieType:"+this.medieType);
		this.playedTime=0;
		
	}
	public void close(){
		if(this.myPlayer!=null){
			this.myPlayer.close();
		}
		this.isCancel=true;//强制关闭播放线程
		this.myPlayer=null;
		synchronized(this){
			this.notify();
		}
		this.dowmload.close();
		this.dowmload=null;
		System.gc();
	}
	public void run(){
		while(!isFinish&&!isCancel){
			if(!this.isCurrentFinish||!this.dowmload.getFragmentStatus()[this.currentFragNum-1]){
				synchronized(this){
					try {
						
						System.out.println("player is waiting");
						
						this.wait();
						this.isWaiting=true;
						
					} catch (InterruptedException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
				}
			}
			if(isFinish||isCancel){
				this.isWaiting=false;
				System.out.println("player is stop");
				return;
			}
			   // this.cacheByte=new byte[(int)this.dowmload.getInb().length];//播放的缓冲数组
			    //this.arrayCopy(this.dowmload.getInb(), this.cacheByte);
			byte[] temp=null;
			int id=0;
			if(!this.change){
				temp=this.cacheByte[0];id=0;
			}else{
				temp=this.cacheByte[1];id=1;
			}
			    int length=temp.length;
				
				if(this.currentFragNum==this.dowmload.getFragmentNum()){
					length=(int)(this.dowmload.getCurrentEndRange()-this.dowmload.getCurrentStartRange());
				}
				System.out.println("正在播放段"+this.currentFragNum+"cacheID:"+id);
				this.isCurrentFinish=false;
				ByteArrayInputStream bais = new ByteArrayInputStream(temp, 0 ,length);
				DataInputStream dis = new DataInputStream(bais);
			    try {
			    	/*String[] str=Manager.getSupportedContentTypes(null);
					for(int i=0;i<str.length;i++){
						System.out.print(str[i]+" ");
					}
					System.out.println();*/
			    	System.out.println("medieType:"+this.medieType);
			    	int tme= getCurrentFragNum();
			    	System.out.println("getCurrentFragNum() = " + getCurrentFragNum());
			    	this.myPlayer = null;
					this.myPlayer = Manager.createPlayer(dis,this.medieType);
					//this.volume.setLevel(arg0)
					this.myPlayer.addPlayerListener(this);
				    this.myPlayer.realize();
				    this.setVolumn(this.currentVolumn);
				    this.myPlayer.prefetch();
				    
				    this.myPlayer.start();
				   // this.setVolumn(50);
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				} catch (MediaException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
					this.isFinish=true;
					System.out.println("isWaiting:"+this.isWaiting);
					if(this.isWaiting){
						
						synchronized(this){
							this.isWaiting=false;
							this.notify();
						}
					}
					
					if(this.dowmload!=null){
						this.dowmload.cancel();
					}
					if(this.playListener!=null){
						this.playListener.onPlayError("播放出错!");
					}
					//return;
				/*	this.myPlayer=null;
					System.gc();
					this.currentFragNum++;
					this.isCurrentFinish=true;
					if(this.currentFragNum>this.dowmload.getFragmentNum()){
						System.out.println("currentFragNum:"+this.currentFragNum+" finish");
						this.isFinish=true;
						synchronized(this){
							this.notify();
						}
						
					}else{
						
						if(this.dowmload.getFragmentStatus()[this.currentFragNum-1]&&this.isCurrentFinish&&this.dowmload.getCurrentFragment()==this.currentFragNum){
							this.change=!this.change;
							synchronized(this){
								this.notify();
							}
						}
						System.out.println("fjdklsfj:"+this.getCurrentFragNum()+"----->"+this.dowmload.getCurrentFragment());
						if(this.getCurrentFragNum()==this.dowmload.getCurrentFragment()){
							this.dowmload.setNextDownload(true);
							
							synchronized(this.dowmload){
								this.dowmload.notify();
							}
							
							if(this.getCurrentFragNum()==this.dowmload.getFragmentNum()){
								synchronized(this){
									this.notify();
								}
							}
						}
					}
					*/
					
				}
		}
		
		}
	

	public void arrayCopy(byte[] from,byte[] to){
		//to=new byte[from.length];
		for(int i=0;i<to.length;i++){
			to[i]=from[i];
			}
	}
	public void test(){
		try  {
			   Player  musicPlayer;
			   musicPlayer  =  Manager.createPlayer("http://localhost/music.mp3");
			   musicPlayer.addPlayerListener(this);
			   musicPlayer.prefetch();
			   musicPlayer.start();//非阻塞的方法,重新启动线程运行
			 //  System.out.println("fjdsklgjslfsa;dlf");
			   musicPlayer.stop();
			 //  System.out.println(musicPlayer.getMediaTime());//获取当前的播放时间
			   //musicPlayer.setMediaTime(5*1000000);
			   musicPlayer.close();
			}
			   catch  (IOException  ioe)  {
				   ioe.printStackTrace();
			}
			   catch  (MediaException  me)  {
				   
				   me.printStackTrace();
			}finally{
				this.currentFragNum++;
				this.isCurrentFinish=true;
				
				if(this.dowmload.getFragmentStatus()[this.currentFragNum-1]&&this.isCurrentFinish&&this.dowmload.getCurrentFragment()==this.currentFragNum){
					this.change=!this.change;
					this.isCurrentFinish=false;
					synchronized(this){
						this.notify();
					}
				}
				if(this.getCurrentFragNum()==this.dowmload.getCurrentFragment()){
					this.dowmload.setNextDownload(true);
					
					synchronized(this.dowmload){
						this.dowmload.notify();
					}
				}
				
				if(this.currentFragNum>this.dowmload.getFragmentNum()){
					this.isFinish=true;
					synchronized(this){
						this.notify();
					}
				}
			}

		
	}

	//播放监听到事件,事件由外界对player调用相关方法是产生
	public void playerUpdate(Player player, String event, Object eventData){
		//如果开始播放
		if(event==STARTED){
			System.out.println("开始");
			this.isCurrentFinish=false;
			//this.isPlaying=true;
			
		}
		else if(event==STOPPED){
			System.out.println("暂停");
			//this.isPlaying=false;
			
		}
		else if(event==CLOSED){
			System.out.println("关闭");
			this.isCancel=true;
			synchronized(this){
				this.notify();
			}
			
		}
		else if(event == END_OF_MEDIA){//当前段播放完毕,
			System.out.println("结束");
			this.myPlayer=null;
			System.gc();
			this.currentFragNum++;
			this.isCurrentFinish=true;
			if(this.currentFragNum>this.dowmload.getFragmentNum()){
				this.isFinish=true;
				if(this.playListener!=null){
					this.playListener.onPlayFinish();
				}
				synchronized(this){
					this.notify();
				}
				return;
			}
			if(this.dowmload.getFragmentStatus()[this.currentFragNum-1]&&this.isCurrentFinish&&this.dowmload.getCurrentFragment()==this.currentFragNum){
				synchronized(this){
					this.notify();
				}
			}
			System.out.println("fjdklsfj:"+this.getCurrentFragNum()+"----->"+this.dowmload.getCurrentFragment());
			if(this.getCurrentFragNum()==this.dowmload.getCurrentFragment()&&this.dowmload.getFragmentStatus()[this.getCurrentFragNum()-1]){
				this.dowmload.setNextDownload(true);
				this.change=!this.change;
				synchronized(this.dowmload){
					this.dowmload.notify();
				}
				
				if(this.getCurrentFragNum()==this.dowmload.getFragmentNum()){
					synchronized(this){
						this.notify();
					}
				}
			}
			
			
//			Mp3Player.close();
//			closeMusic();
//			ClosePlayer();
//			GGMusicNewW810.ggMusic.playState=0;
		}//end if
	}
	public void setVolumn(int volumn){
		if(this.myPlayer!=null){
			this.volume=(VolumeControl)this.myPlayer.getControl("VolumeControl");
		//	System.out.println("volumn:"+this.volume.getLevel());
			this.volume.setLevel(volumn);
			currentVolumn=volumn;
			
		}
	}
	public int getCurrentFragNum() {
		return currentFragNum;
	}
	public void setCurrentFragNum(int currentFragNum) {
		this.currentFragNum = currentFragNum;
	}
	public boolean isCurrentFinish() {
		return isCurrentFinish;
	}
	public void setCurrentFinish(boolean isCurrentFinish) {
		this.isCurrentFinish = isCurrentFinish;
	}
	public byte[][] getCacheByte() {
		return cacheByte;
	}
	public void setCacheByte(byte[][] cacheByte) {
		this.cacheByte = cacheByte;
	}
	public int getCurrentVolumn() {
		return currentVolumn;
	}
	public void setCurrentVolumn(int currentVolumn) {
		this.currentVolumn = currentVolumn;
	}
	public Player getMyPlayer() {
		return myPlayer;
	}
	public void setMyPlayer(Player myPlayer) {
		this.myPlayer = myPlayer;
	}
	public PlayListener getPlayListener() {
		return playListener;
	}
	public void setPlayListener(PlayListener playListener) {
		this.playListener = playListener;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -