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

📄 canvasvideoplayer.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 CanvasVideoPlayer extends MIDlet implements CommandListener,PlayerListener {
    
    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 final static Command   fullScreen = new Command("Full Screen", Command.ITEM, 1);

    //指示正在播放
	private boolean playing;

	//指示是否为全屏模式
	private boolean fullScreenMode;

    private Display   display;	
	private Canvas canvas;
    private Player player;
	private VideoControl vc;

    public CanvasVideoPlayer(){
		canvas=new VideoCanvas();
		
		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");
			if (vc != null) {
				//将VideoControl设置为在Canvas中显示的模式
				vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
				
				//设置在屏幕中央显示视频
				int videoWidth=vc.getDisplayWidth();
				int videoHeight=vc.getDisplayHeight();
				int canvasWidth=canvas.getWidth();
				int canvasHeight=canvas.getHeight();
				vc.setDisplayLocation((canvasWidth-videoWidth)/2,(canvasHeight-videoHeight)/2);
				vc.setVisible(true);
			}
			fullScreenMode=false;
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		} 
		catch (MediaException me) { 
			me.printStackTrace();
		}
		
		//设置相关命令
		canvas.addCommand(play);
		canvas.addCommand(stop);
		canvas.addCommand(exit);
		canvas.addCommand(fullScreen);
		canvas.setCommandListener(this);
    }

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

    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();	
				canvas.repaint();
			}
			catch(MediaException me){
				me.printStackTrace();
			}
	   }
	   else if (c == stop  && playing == true) {
			try{   
				//停止播放
				playing=false;
				player.stop();
				canvas.repaint();
			}
			catch(MediaException me){
				me.printStackTrace();
			}
		}
		else if(c == fullScreen){
			try{
				//如果不是全屏模式,则将其设置为全屏模式
				if(fullScreenMode == false){
					fullScreenMode=true;
					vc.setDisplayFullScreen(true);	
					canvas.repaint();
				}
				//如果是全屏模式,则将其设置为非全屏模式
				else{
					fullScreenMode=false;
					vc.setDisplayFullScreen(false);
					canvas.repaint();
				}
			}
			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;
			canvas.repaint();
		} 
	}
   
}

class VideoCanvas extends Canvas{
	public void paint(Graphics g){
		g.setColor(0,0,255);
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		g.setColor(255,0,0);
		g.drawString("paused",50,50,Graphics.LEFT|Graphics.TOP);
	}
}

⌨️ 快捷键说明

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