J2ME视频播放实现的简单代码
在J2ME中有关视频播放的步骤:
1.加载视频资源;
2.创建Player;
3.将视频到"以实现"状态(player.realize());
4.vidc.initDisplayMode(参数1,参数2);由于参数2需要Canvas对象,所以编写的类需要继承Canvas类;
5.视频播放并显示.
package com.mopietek;import javax.microedition.lcdui.Display;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class MediaPlayerApp extends MIDlet{public Display display;public static MediaPlayerApp midlet;public VideoCanvas videoCanvas;public MediaPlayerApp(){ midlet = this; display = Display.getDisplay(this); videoCanvas = new VideoCanvas(display); videoCanvas.open("/up1.mpg"); display.setCurrent(videoCanvas);}protected void destroyApp(boolean arg0) throws MIDletStateChangeException {// TODO Auto-generated method stub}protected void pauseApp() {// TODO Auto-generated method stub}protected void startApp() throws MIDletStateChangeException {// TODO Auto-generated method stub}}
package com.mopietek;import java.io.InputStream;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Graphics;import javax.microedition.media.Control;import javax.microedition.media.Manager;import javax.microedition.media.Player;import javax.microedition.media.PlayerListener;import javax.microedition.media.control.FramePositioningControl;import javax.microedition.media.control.RateControl;import javax.microedition.media.control.VideoControl;import javax.microedition.media.control.VolumeControl;public class VideoCanvas extends Canvas implements Runnable,CommandListener,PlayerListener{private Display videodisplay;private int screenWidth;private int screenHeight;public static Command exitCommand = new Command("退出",Command.EXIT,1);public static Command playCommand = new Command("播放",Command.OK,1);public static Command pauseCommand = new Command("暂停",Command.ITEM,10);private VolumeControl vc;private Player player;private VideoControl vidc;private RateControl rc;private FramePositioningControl fpc;public VideoCanvas(){}public VideoCanvas(Display display){ this.videodisplay = display; screenWidth = this.getWidth(); screenHeight = this.getHeight(); initialize();}//添加命令public void initialize(){addCommand(exitCommand);addCommand(playCommand);addCommand(pauseCommand);setCommandListener(this);}protected void paint(Graphics g) {//可以编写视频的外框,进度条等}public void open(String url){try{if(player == null){InputStream is = getClass().getResourceAsStream(url);player = Manager.createPlayer(is,"video/mpeg");//可以添加player的监听,实现视频的暂停、快放、快退等功能player.addPlayerListener(this);}player.realize(); //player的“以实现”状态if((vidc = (VideoControl) player.getControl("VideoControl")) != null){vidc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);vidc.setDisplayFullScreen(true);vidc.setVisible(true);}Control [] controls = player.getControls();for(int i=0; i<controls.length;i++){if(controls[i] instanceof VolumeControl)vc = (VolumeControl) controls[i];if(controls[i] instanceof RateControl)rc = (RateControl) controls[i];if(controls[i] instanceof FramePositioningControl)fpc = (FramePositioningControl) controls[i];}player.prefetch(); //"预抓取"状态if(vidc == null) addCommand(pauseCommand);if(player == null){return;}else{player.start(); //开始播放视频}}catch(Exception e){System.err.println(e.toString());}}public void run() {// TODO Auto-generated method stub}public void commandAction(Command c, Displayable d) {// TODO Auto-generated method stub}public void playerUpdate(Player arg0, String arg1, Object arg2) {// TODO Auto-generated method stub}}