📄 videoplayer.java
字号:
/* * @(#)VideoPlayer.java 1.3 03/02/24 * * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms */package example.mmademo;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;import java.io.InputStream;/** * Play Video/Capture using MMAPI * */public class VideoPlayer extends Form implements Runnable, CommandListener, PlayerListener { private static String TITLE_TEXT = "MMAPI Player"; static Player player = null; static Image logo = null; int idx = 0; Display parentDisplay; long duration; private Command backCommand = new Command("Back", Command.BACK, 1); private Command playCommand = new Command("Play", Command.ITEM, 1); private Command snapCommand = new Command("Snapshot", Command.ITEM, 1); private Command pauseCommand = new Command("Pause", Command.ITEM, 10); Item videoItem; private StringItem status; private StringItem audioStatus; private StringItem time; VolumeControl vc; RateControl rc; Thread th; int currentVolume; boolean muted; int currentRate = 100000; VideoControl vidc; public VideoPlayer(Display parentDisplay) { super(TITLE_TEXT); this.idx = 0; this.parentDisplay = parentDisplay; initialize(); } void initialize() { addCommand(backCommand); addCommand(snapCommand); setCommandListener(this); try { logo = Image.createImage("/icons/logo.png"); } catch (Exception ex) { logo = null; } if ( logo == null) System.out.println("can not load logo.png"); } /* * Respond to commands, including back */ public void commandAction(Command c, Displayable s) { if (s == this) { if (c == backCommand) { close(); parentDisplay.setCurrent(VideoTest.getList()); } else if (videoItem != null && c == snapCommand) { doSnapshot(); } else if (videoItem == null && c == pauseCommand) { removeCommand(pauseCommand); addCommand(playCommand); pause(); } else if (videoItem == null && c == playCommand) { start(); removeCommand(playCommand); addCommand(pauseCommand); } } } public void run() { while (player != null) { try { Thread.sleep(200); } catch (InterruptedException ie) { } synchronized (this) { if (player == null) return; if (vc != null) { if (vc.getLevel() != currentVolume || vc.isMuted() != muted) { muted = vc.isMuted(); currentVolume = vc.getLevel(); audioStatus.setText("Volume: " + currentVolume + "% " + (muted?" (muted)":"")); } } if (rc != null) { if (rc.getRate() != currentRate) { currentRate = rc.getRate(); updateStatus(); } } long k = player.getMediaTime(); time.setText("Pos: " + (k / 1000000) + "." + ((k / 10000) % 100)); } } } public void open(String url) { try { synchronized (this) { if ( player == null ) { if (url.startsWith("resource:")) { InputStream ins = getClass().getResourceAsStream(url.substring(9)); player = Manager.createPlayer(ins, "video/mpeg"); } else { player = Manager.createPlayer(url); } player.addPlayerListener(this); } } player.realize(); if ((vidc = (VideoControl) player.getControl("VideoControl")) != null) { videoItem = (Item)vidc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null); //vidc.setDisplaySize(240, 140); } else if (logo != null) { append(new ImageItem("", logo, ImageItem.LAYOUT_CENTER,"")); } Control [] controls = player.getControls(); for (int i = 0; i < controls.length; i++) { if (controls[i] instanceof GUIControl && controls[i] != vidc) { append((Item) controls[i]); } if (controls[i] instanceof VolumeControl) { vc = (VolumeControl) controls[i]; } if (controls[i] instanceof RateControl) { rc = (RateControl) controls[i]; } } status = new StringItem("Status: ",""); append(status); if (vc != null) { audioStatus = new StringItem("", "Volume:"); append(audioStatus); } append( time = new StringItem("","") ); player.prefetch(); if (videoItem == null) addCommand(pauseCommand); else append(videoItem); Thread t = new Thread(this); t.start(); } catch (Exception me) { player = null; } } public void start() { if (player == null) return; try { duration = player.getDuration(); player.start(); } catch (Exception ex) { player = null; } } public void close() { synchronized (this) { pause(); if (player != null) { player.close(); player = null; } } VideoTest.getInstance().nullPlayer(); } public void pause() { if ( player != null) { try { player.stop(); } catch (MediaException e) {} } } private synchronized void updateStatus() { if (player == null) return; status.setText((player.getState() == Player.STARTED ? "Playing, ": "Paused, ") + "Rate: " + (currentRate/1000) + "%\n"); } public void playerUpdate(Player plyr, String evt, Object evtData) { if ( evt == END_OF_MEDIA ) { try { player.setMediaTime(0); player.start(); } catch (MediaException ex) {} } else if (evt == STARTED || evt == STOPPED) { updateStatus(); } } private void doSnapshot() { try { byte [] snap = vidc.getSnapshot("encoding=png"); if (snap != null) { Image im = Image.createImage(snap, 0, snap.length); ImageItem imi = new ImageItem("", im, 0, ""); append(imi); } } catch (MediaException me) { System.err.println(me); } } public synchronized void stopVideoPlayer() { player.deallocate(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -