📄 videoplayer.java
字号:
package example.mmademo;import java.io.ByteArrayOutputStream;import java.io.InputStream;import javax.microedition.lcdui.Alert;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.Image;import javax.microedition.lcdui.ImageItem;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.Spacer;import javax.microedition.lcdui.StringItem;import javax.microedition.media.Control;import javax.microedition.media.Manager;import javax.microedition.media.MediaException;import javax.microedition.media.Player;import javax.microedition.media.PlayerListener;import javax.microedition.media.control.GUIControl;import javax.microedition.media.control.RateControl;import javax.microedition.media.control.RecordControl;import javax.microedition.media.control.VideoControl;import javax.microedition.media.control.VolumeControl;/** * Play Video/Capture in a Form using MMAPI * */public class VideoPlayer extends Form implements Runnable, CommandListener, PlayerListener { private static final String TITLE_TEXT = "MobileVideoIP Player (Form) "; private static Player player = null; private static boolean isCapturePlayer; private static Image logo = null; private Display parentDisplay; private long duration; private final Command backCommand = new Command("Back", Command.BACK, 1); private final Command playCommand = new Command("Play", Command.ITEM, 1); private final Command snapCommand = new Command("Snapshot", Command.ITEM, 1); private final Command pauseCommand = new Command("Pause", Command.ITEM, 10); private Item videoItem; private StringItem status; private StringItem audioStatus; private StringItem time;// private RateControl rc; private int currentVolume; private boolean muted; private int currentRate = 100000; private VideoControl vidc; // pause/resume support private boolean suspended = false; private boolean restartOnResume = false; private long restartMediaTime; public VideoPlayer(Display parentDisplay) { super(TITLE_TEXT); this.parentDisplay = parentDisplay; initialize(); } void initialize() { addCommand(backCommand); addCommand(snapCommand); setCommandListener(this); try { if (logo == null) 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) { //try { 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) { startPlaying(); removeCommand(playCommand); addCommand(pauseCommand); } } //} catch (Exception e) { // System.out.println("DEBUG: GOT EXCEPTION in VideoPlayer.commandAction("+c.toString()+","+s.toString()+")!"); // e.printStackTrace(); //} } /** * Loops to see if volume and rate have been changed. */ public void run() { P.rint("VideoPlayer:run()"); //try { while (player != null) { P.rint("VideoPlayer:run():while loop"); // sleep 200 millis. If suspended, // sleep until MIDlet is restarted do { try { Thread.sleep(200); } catch (InterruptedException ie) { } } while (player != null && suspended); synchronized (this) { if (player == null) return; long k = player.getMediaTime(); time.setText("Pos: " + (k / 1000000) + "." + ((k / 10000) % 100)); } }//while //} catch (Exception e) { // System.out.println("DEBUG: GOT EXCEPTION in VideoPlayer.run()!"); // e.printStackTrace(); //} }//run /* */ /** * Called by MIDlet after instantiating this object. * Creates the player. XXX */ public void open(String url) { //capture://video theCamera P.rint("VideoPlayer:open()"); try { synchronized (this) { if ( player == null ) { player = Manager.createPlayer(url); //creating player for the url player.addPlayerListener(this); isCapturePlayer = url.startsWith("capture:"); } } player.realize(); //realise //set to use_gui_primitive 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]); } } status = new StringItem("Status: ",""); status.setLayout(Item.LAYOUT_NEWLINE_AFTER); append(status); time = new StringItem("",""); time.setLayout(Item.LAYOUT_NEWLINE_AFTER); append(time); player.prefetch(); if (videoItem == null) addCommand(pauseCommand); else { Spacer spacer = new Spacer(3, 10); spacer.setLayout(Item.LAYOUT_NEWLINE_BEFORE); append(spacer); append(videoItem); ///append the videoItem, if don't append snapShot will get blank screen } //Thread t = new Thread(this); //t.start(); //loops to set display of volume and rate } catch (Exception ex) { error(ex); ex.printStackTrace(); close(); } }//open /**/ /** * Start */ //XXX public void startPlaying() { P.rint("VideoPlayer:startPlaying()"); if (player == null) return; try { duration = player.getDuration(); //P.rint("VideoPlayer:start(): duration is "+duration); player.start(); //without this will get black square } catch (Exception ex) { System.err.println(ex); error(ex); close(); } }//startPlaying // general purpose error method, displays on screen as well to output public void error(Exception e) { Alert alert = new Alert("Error"); alert.setString(e.getMessage()); parentDisplay.setCurrent(alert); e.printStackTrace(); } public void error(String s) { Alert alert = new Alert("Error"); alert.setString(s); alert.setTimeout(2000); parentDisplay.setCurrent(alert, this); } public void close() { synchronized (this) { pause(); //stop() if (player != null) { player.close(); player = null; } } VideoTest.getInstance().nullPlayer(); } public void pause() { if ( player != null) { try { player.stop(); } catch (MediaException me) { System.err.println(me); } } } /** * Sets the text to show playing-state and rate. */ private synchronized void updateStatus() { if (player == null) return; status.setText(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -