📄 simpleplayercanvas.java
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package example.mmademo;import java.util.*;import java.io.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;/** * MMAPI player main window for media files, implemented as a Canvas * * @version 1.7 */public class SimplePlayerCanvas extends Canvas implements SimplePlayerGUI.Parent, Utils.Interruptable, Utils.ContentHandler, Runnable { private static int PLAYER_TITLE_TOP = 2; private static int LOGO_GAP = 2; private static int SONG_TITLE_GAP = 2; private static int KARAOKE_GAP = 1; private static int TIME_GAP = 2; private static int RATE_GAP = 2; private static int STATUS_GAP = 2; private String title; private Image logo = null; private SimplePlayerGUI gui; // default: null private Utils.BreadCrumbTrail parent; private VideoControl videoControl; private String status=""; private String feedback=""; private String fileTitle=""; int displayWidth = -1; int displayHeight = -1; int textHeight=10; int logoTop = 0; int songTitleTop = 0; int timeRateTop = 0; int timeWidth = 0; int karaokeTop = 0; int karaokeHeight = 0; int maxKaraokeLines = 0; int feedbackTop = 0; int statusTop = 0; int[] karaokeParams = new int[4]; private static void debugOut(String s) { Utils.debugOut("SimplePlayerCanvas: "+s); } public SimplePlayerCanvas(String title, Utils.BreadCrumbTrail parent) { super(); this.parent = parent; this.title = title; } // //////////////////////////// interface Utils.BreadCrumbTrail /////////// public Displayable go(Displayable d) { return parent.go(d); } public Displayable goBack() { return parent.goBack(); } public Displayable replaceCurrent(Displayable d) { return parent.replaceCurrent(d); } public Displayable getCurrentDisplayable() { return parent.getCurrentDisplayable(); } // ///////////////////////// interface SimplePlayerGUI.Parent ///////////// public Utils.BreadCrumbTrail getParent() { return parent; } // called after the media is prefetched public void setupDisplay() { // if there is video control, change display layout videoControl = gui.getVideoControl(); if (videoControl != null) { Utils.debugOut("Initializing display mode."); try { videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this); } catch (Exception e) { setFeedback(Utils.friendlyException(e)); videoControl = null; } } // force recalculation of layout displayHeight = -1; updateDisplay(); } public String getTitle() { return title; } public void setStatus(String s) { status=s; repaint(0, statusTop, displayWidth, textHeight); serviceRepaints(); } public void setFeedback(String s) { feedback=s; repaint(0, feedbackTop, displayWidth, textHeight); serviceRepaints(); } public void setFileTitle(String s) { fileTitle = s; repaint(0, songTitleTop, displayWidth, textHeight); serviceRepaints(); } public void updateKaraoke() { repaint(0, karaokeTop, displayWidth, karaokeHeight); serviceRepaints(); } public void updateTime() { repaint(0, timeRateTop, timeWidth, textHeight); serviceRepaints(); } public void updateRate() { repaint(timeWidth, timeRateTop, displayWidth, textHeight); serviceRepaints(); } public void updateDisplay() { repaint(); serviceRepaints(); } public void fullScreen(boolean value) { // may not display the other items // when going back to small video repaint(); } // ////////////////////////////// interface Utils.ContentHandler ////////// public synchronized void close() { if (gui != null) { gui.closePlayer(); gui = null; } } public boolean canHandle(String url) { return true; } public void handle(String name, String url) { Utils.debugOut("SimplePlayerCanvas: handle "+url); getGUI().setParent(this); gui.setSong(name, url); doHandle(); } public void handle(String name, InputStream is, String contentType) { getGUI().setParent(this); gui.setSong(name, is, contentType); doHandle(); } public void handle(String name, Player player) { getGUI().setParent(this); gui.setSong(name, player); doHandle(); } // ///////////////////////// interface Utils.ContentHandler //////////////// // private synchronized SimplePlayerGUI getGUI() { if (gui == null) { gui = new SimplePlayerGUI(); gui.initialize(title, this); } return gui; } private void doHandle() { // IMPL NOTE: // I want to display the player first, and THEN start prefetching. // the only way I was able to achieve this was by creating a new thread. repaint(); serviceRepaints(); new Thread(this).start(); } public void run() { gui.startPlayer(); } // ///////////////////////// Canvas callbacks /////////////////////////////// protected void keyPressed(int keycode) { try { SimplePlayerGUI gui = getGUI(); switch (keycode) { case KEY_NUM1: // Jump backward gui.skip(true); break; case KEY_NUM2: gui.togglePlayer(); break; case KEY_NUM3: // Jump forward gui.skip(false); break; case KEY_NUM7: gui.stepFrame(-1); break; case KEY_NUM9: gui.stepFrame(+1); break; case KEY_NUM5: gui.pausePlayer(); gui.setMediaTime(0); setFeedback("Player Stopped."); break; case KEY_NUM4: gui.changeRate(true); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -