📄 videocanvas.java
字号:
// unnamed packageimport javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;import java.io.*;class VideoCanvas extends Canvas implements CommandListener, PlayerListener{ private final MediaSamplerMIDlet midlet; private final String videoFile; private final Command stopCommand; private final Command exitCommand; private Player player; private boolean playPending = false; VideoCanvas(MediaSamplerMIDlet midlet, String videoFile) { this.midlet = midlet; this.videoFile = videoFile; stopCommand = new Command("Stop", Command.SCREEN, 2); exitCommand = new Command("Exit", Command.EXIT, 1); addCommand(stopCommand); addCommand(exitCommand); setCommandListener(this); } // defer actual playing until we're shown, see showNotify() void prepareToPlay() { playPending = true; } // Play video only when we're displayed. Use playPending flag to // avoid restarting if a system screen momentarily obscures us public void showNotify() { if (playPending) { playPending = false; play(); } } public void paint(Graphics g) { g.setColor(0x00FFFF00); // yellow g.fillRect(0, 0, getWidth(), getHeight()); } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { stop(); midlet.videoExit(); } else if (c == stopCommand) { stop(); } } public void keyPressed(int keyCode) { if (getGameAction(keyCode) == FIRE) { stop(); } } void stop() { if (player != null) { player.close(); } } private void play() { if (videoFile == null) { midlet.alertError("No video file specified"); } else { try { InputStream is = getClass().getResourceAsStream(videoFile); player = Manager.createPlayer(is, "video/3gpp"); player.addPlayerListener(this); player.realize(); // get the video control and attach it to our canvas VideoControl videoControl = (VideoControl)(player.getControl("VideoControl")); if (videoControl == null) { midlet.alertError("VideoControl not supported"); } else { videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this); // centre video, letting it be clipped if it's too big int canvasWidth = getWidth(); int canvasHeight = getHeight(); int displayWidth = videoControl.getDisplayWidth(); int displayHeight = videoControl.getDisplayHeight(); int x = (canvasWidth - displayWidth) / 2; int y = (canvasHeight - displayHeight) / 2; videoControl.setDisplayLocation(x, y); videoControl.setVisible(true); player.start(); } } catch (IOException ioe) { discardPlayer(); midlet.alertError("IOException: " + ioe.getMessage()); } catch (MediaException me) { discardPlayer(); midlet.alertError("MediaException: " + me.getMessage()); } catch (SecurityException se) { discardPlayer(); midlet.alertError("SecurityException: " + se.getMessage()); } } } // Called in case of exception to make sure invalid players are closed private void discardPlayer() { if (player != null) { player.close(); player = null; } } public void playerUpdate(final Player p, final String event, final Object eventData) { // queue a call to updateEvent in the user interface event queue Display display = midlet.getDisplay(); display.callSerially(new Runnable() { public void run() { VideoCanvas.this.updateEvent(p, event, eventData); } }); } private void updateEvent(Player p, String event, Object eventData) { if (event == END_OF_MEDIA) { p.close(); } else if (event == CLOSED) { player = null; midlet.videoDone(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -