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

📄 videoplayer.java

📁 jBuilderX无线应用开发源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.util.*;
import java.io.*;

public class VideoPlayer extends MIDlet implements CommandListener,PlayerListener,Runnable {
    
    private final static Command   play = new Command("Play", Command.ITEM, 1);
    private final static Command   stop = new Command("Stop", Command.ITEM, 1);
    private final static Command   exit = new Command("Exit", Command.EXIT, 1);
	
	//指示正在播放
	private boolean playing;

	//显示播放进度
	private Gauge indicator;    

    private Display   display;   
    private Form form;
    private Player player;
    private Thread t;
	private VideoControl vc;
	private Item videoItem;
    private long mediaLength=0,position=0;

    public VideoPlayer(){
		form=new Form("Video Player");
		
		try {
			//创建播放视频的Player
			InputStream is = getClass().getResourceAsStream("/test-mpeg.mpg");
			player = Manager.createPlayer(is,"video/mpeg");	
			player.addPlayerListener(this);
			player.prefetch();

			//从Player中获得VideoControl
			vc = (VideoControl) player.getControl("VideoControl");

			//将VideoControl设置为在Form中显示的模式
			if (vc != null) {
				videoItem = (Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
			}
			form.append(videoItem );

			//得到媒体文件的长度
			mediaLength=player.getDuration();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		} 
		catch (MediaException me) { 
			me.printStackTrace();
		}
		
		//创建指示播放进度的Gauge
		indicator=new Gauge("current",false,(int)(mediaLength/1000000),0);
		indicator.setPreferredSize(180,10);
		form.append(indicator);

		//设置Form中的相关命令
		form.addCommand(play);
		form.addCommand(stop);
		form.addCommand(exit);
		form.setCommandListener(this);
    }

    public void startApp() {
		display  = Display.getDisplay(this);	
		display.setCurrent(form);	
    }

    public void pauseApp() {
		try{
			playing=false;
			player.stop();
		}catch(MediaException me){
			me.printStackTrace();
		}
    }

    public void destroyApp(boolean unconditional){
		player.close();
    }

    public void commandAction(Command c, Displayable s){
       if (c == play && playing == false) {
			try{  
				//开始播放
				playing=true;
				player.start();	
				t=new Thread(this);
				t.start();
			}
			catch(MediaException me){
				me.printStackTrace();
			}
	   }
	   else if (c == stop  && playing == true) {
			try{   
				//停止播放
				playing=false;
				player.stop();
			}
			catch(MediaException me){
				me.printStackTrace();
			}
		}
		else if (c == exit) {
			//退出程序
			playing=false;
			player.close();
			destroyApp(false);
			notifyDestroyed();
		}
    }

	public void playerUpdate(Player player,
                         java.lang.String event,
                         java.lang.Object eventData){
		if (event == PlayerListener.END_OF_MEDIA) {
			//指示播放已停止
			playing=false;
		} 
	}
   
    public void run(){
		while(playing){
			  //延时
			  try{
				Thread.sleep(100);
			  }
			  catch(InterruptedException ie){
				ie.printStackTrace();
			  }
			  //设置当前的播放位置
			  position=player.getMediaTime();
			  if(position!=Player.TIME_UNKNOWN){
				indicator.setValue((int)(position/1000000));
			  }
		}
    }
}

⌨️ 快捷键说明

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