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

📄 videocanvas.java

📁 j2me视频播放小程序的源代码
💻 JAVA
字号:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;

/** Acquires the video content and renders it */
public class VideoCanvas extends Canvas implements CommandListener, PlayerListener, Runnable {

	private VideoPlayer parent;
	private Display display;
		
	private Player player;
	private VideoControl videoControl;
	
	private String url;
	private Thread initializer;
	
	private Command close;
	private Command rePlay;
	
	
	public VideoCanvas(VideoPlayer parent){
		super();
		this.parent = parent;
		display = Display.getDisplay(parent);
		close = new Command("close", Command.SCREEN, 1);
		addCommand(close);
		setCommandListener(this);			
	}
	
	
	public void initializeVideo(String url){	
		this.url = url;
		initializer = new Thread(this);	
		initializer.start();
	}
	
	
	public void run(){
		
		try {							
			player = Manager.createPlayer(url);
			parent.updateGauge();						
			player.addPlayerListener(this); 			
			player.realize();
			parent.updateGauge();								
			player.prefetch();
			parent.updateGauge();						

        } catch (IOException ioe) {
			Alert alert = new Alert("IOException thrown", ioe.getMessage(), null, AlertType.ERROR);
			display.setCurrent(alert);
		
        } catch (MediaException me) {
			Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
			display.setCurrent(alert);
		}
		
		playVideo();
	}
					
	
	public void playVideo(){
		try {

            // Get the video control and set it to the current display.
            videoControl = (VideoControl)player.getControl("VideoControl");
            if (videoControl != null) {				
				videoControl.initDisplayMode(videoControl.USE_DIRECT_VIDEO, this);										                
            }
			
			parent.updateGauge();
			
			int cHeight = this.getHeight();
			int cWidth = this.getWidth();			
			videoControl.setDisplaySize(cWidth, cHeight);     	
																	
			display.setCurrent(this);
			videoControl.setVisible(true);
            player.start();

        
        } catch (MediaException me) {
			Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
			display.setCurrent(alert);
		}
	} 
		
		
	/** Paints background color */
	public void paint(Graphics g){
		g.setColor(128, 128, 128);
		g.fillRect(0, 0, getWidth(), getHeight());		
	}
	
	
	public void playerUpdate(Player p, String event, Object eventData) {
		//add "Replay" option when video is finished
		if (event == PlayerListener.END_OF_MEDIA)
		{
			if (rePlay == null)
			{
				rePlay = new Command("re-play", Command.SCREEN, 1);	
				addCommand(rePlay);
			}			
		}				
		
	}
	
	
	public void commandAction(Command c, Displayable s) {		
		if(c == rePlay){		
			try{
				player.start();	
			} catch (MediaException me) {
				Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
				display.setCurrent(alert);
			}			
		}
				
		else if(c == close){												
			player.close();
			parent.form.delete(1);
			display.setCurrent(parent.form);
			url=null;
			parent=null;
		}	
			
	}
 
		
}

⌨️ 快捷键说明

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