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

📄 playvideo.java

📁 java me的一些源码,包括多媒体(声音、视频)
💻 JAVA
字号:
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.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class PlayVideo extends MIDlet implements CommandListener {
    private Display display;
    Form form = new Form("视频播放演示");
    
    Command cmdPlayItem = new Command("通过Item播放", Command.SCREEN, 1);
    Command cmdPlayCanvas = new Command("通过Canvas播放", Command.SCREEN, 1);
    Command cmdExit = new Command("退出", Command.EXIT, 1);
    
    Player player = null;
    public PlayVideo() {
        super();
        String prop2 = System.getProperty("audio.encodings");
        if (prop2 != null) {
            System.out.println("照相机支持的图像格式:"+prop2);
        }

        
        form.addCommand(cmdPlayItem);
        form.addCommand(cmdPlayCanvas);
        form.addCommand(cmdExit);
        form.setCommandListener(this);
    }

    protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }

    protected void pauseApp() {
        close();
    }

    protected void destroyApp(boolean arg0)
        throws MIDletStateChangeException {
        close();
    }
    
    private void playItem() {        
        try {
            InputStream is = getClass().getResourceAsStream("/test.mpg");
            
            player = Manager.createPlayer(is,"Video/mpeg");            
            player.realize();
            
            //获得视频控制器
            VideoControl vc = (VideoControl)player.getControl("VideoControl");
            Item item = (Item)vc.initDisplayMode(
                    VideoControl.USE_GUI_PRIMITIVE, null);
            form.append(item);
            player.start();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    private void playCanvas() { 
        try {
            InputStream is = getClass().getResourceAsStream("/test.mpg");
            
            player = Manager.createPlayer(is,"Video/mpeg");            
            player.realize();
            
            MyCanvas mc = new MyCanvas();
            //获得视频控制器
            VideoControl vc = (VideoControl)player.getControl("VideoControl");
            vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, mc);
            display.setCurrent(mc);
            player.start();
        } catch(Exception e) {
            e.printStackTrace();
        }        
    }
    
    class MyCanvas extends Canvas {
        protected void paint(Graphics p) {            
        }
    }
    
    /**
     * 命令按钮事件
     */
    public void commandAction(Command c, Displayable s) {
        if (c == cmdExit) {
            notifyDestroyed();
        } else if (c == cmdPlayItem) {
            display.setCurrent(form);
            new Thread() {
                public void run() {
                    playItem();
                }
            }.start();
        }else if (c == cmdPlayCanvas) {
            new Thread() {
                public void run() {
                    playCanvas();
                }
            }.start();            
        }
    }
    
    private void close() {
        if (player != null) {
            player.close();
            player = null;
        }
    }
}

⌨️ 快捷键说明

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