📄 simpleplayercanvas.java
字号:
/* * @(#)SimplePlayerCanvas.java 1.2 03/01/22 * * Copyright (c) 2000-2003 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms */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.*;/** * MMA player main window for media files, implemented as a Canvas * * @author Florian Bomers * @version 1.10 */public class SimplePlayerCanvas extends Canvas implements SimplePlayerGUI.Parent, 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 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; } 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) { setFeedback("No fullscreen mode!"); // nothing to do } //////////////////////////////// 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(); } /////////////////////////// implementation ///////////////////////////////////////////////// private synchronized SimplePlayerGUI getGUI() { if (gui == null) { gui = new SimplePlayerGUI(); gui.initialize(title, this); } return gui; } private void doHandle() { // START HACK // 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(); //midiPlayer.setIndex(i); //midiPlayer.startPlayer(); //display.callSerially(this); new Thread(this).start(); } public void run() { gui.startPlayer(); } // END HACK 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.setMediaTime(0); break; case KEY_NUM5: gui.pausePlayer(); break; case KEY_NUM9: break; case KEY_NUM4: gui.changeRate(true); break; case KEY_NUM8: fullScreen(true); break; case KEY_NUM6: gui.changeRate(false); break; case KEY_NUM0: gui.toggleMute(); break; case KEY_STAR: gui.changeVolume(true); //gui.stopAfterTime(); break; case KEY_POUND: gui.changeVolume(false); break; default: int code = getGameAction(keycode); if (code == RIGHT) { gui.transpose(false); } else if (code == LEFT) { gui.transpose(true); } } } catch (Throwable t) { Utils.error(t, parent); } } private boolean intersects(int clipY, int clipHeight, int y, int h) { return (clipY<=y+h && clipY+clipHeight>=y); } public void paint(Graphics g) { try { if (displayHeight==-1) { displayWidth = getWidth(); displayHeight = getHeight(); textHeight = g.getFont().getHeight(); if (logo == null && gui != null) { logo = gui.getLogo(); } int currTop = PLAYER_TITLE_TOP + textHeight; if (logo != null) { currTop += LOGO_GAP; logoTop = currTop; currTop += logo.getHeight(); } currTop += SONG_TITLE_GAP; songTitleTop = currTop; currTop += TIME_GAP + textHeight; timeRateTop = currTop; timeWidth = g.getFont().stringWidth("0:00:0 "); currTop += textHeight+KARAOKE_GAP; // feedback: before-last line feedbackTop = displayHeight - 2*textHeight - STATUS_GAP; // karaoke: squeeze as many lines as possible in between rate and feedback maxKaraokeLines = (feedbackTop - currTop) / (textHeight + KARAOKE_GAP); karaokeHeight = maxKaraokeLines * (textHeight + KARAOKE_GAP); karaokeTop = currTop + ((feedbackTop - currTop - karaokeHeight) / 2); // status: last line. statusTop = displayHeight - textHeight; } int clipX=g.getClipX(); int clipY=g.getClipY(); int clipWidth=g.getClipWidth(); int clipHeight=g.getClipHeight(); // background g.setColor(0); g.fillRect(clipX, clipY, clipWidth, clipHeight); // title if (intersects(clipY, clipHeight, PLAYER_TITLE_TOP, textHeight)) { g.setColor(0xFF7f00); g.drawString(title, displayWidth>>1, PLAYER_TITLE_TOP, Graphics.TOP | Graphics.HCENTER); } // logo if (logo != null && intersects(clipY, clipHeight, logoTop, logo.getHeight())) { g.drawImage(logo, displayWidth/2, logoTop, Graphics.TOP | Graphics.HCENTER); } // song name (+ duration) if (intersects(clipY, clipHeight, songTitleTop, textHeight)) { g.setColor(0xFF7F00); g.drawString(fileTitle, displayWidth>>1, songTitleTop, Graphics.TOP | Graphics.HCENTER); } if (gui!=null) { // time and rate/tempo display if (intersects(clipY, clipHeight, timeRateTop, textHeight)) { if (intersects(clipX, clipWidth, 0, timeWidth)) { g.setColor(0xF0F0F0); g.drawString(gui.getMediaTimeStr(), 0, timeRateTop, Graphics.TOP | Graphics.LEFT); } if (intersects(clipX, clipWidth, timeWidth+1, displayWidth)) { // tempo/rate display if (gui.hasTempoControl()) { g.setColor(0xF0F0F0); g.drawString(gui.getTempoStr(), displayWidth, timeRateTop, Graphics.TOP | Graphics.RIGHT); } else { g.setColor(0xF0F0F0); g.drawString(gui.getRateStr(), displayWidth, timeRateTop, Graphics.TOP | Graphics.RIGHT); } } } // Karaoke text if (intersects(clipY, clipHeight, karaokeTop, karaokeHeight)) { String[] lines = gui.getKaraokeStr(karaokeParams); int currTop = karaokeTop; int currLine = karaokeParams[SimplePlayerGUI.KARAOKE_LINE]; int lineCount = karaokeParams[SimplePlayerGUI.KARAOKE_LINE_COUNT]; int thisLine = 0; if (lineCount > maxKaraokeLines) { thisLine = currLine - 1; if (thisLine < 0) { thisLine = 0; } if (thisLine + maxKaraokeLines > lineCount) { thisLine = lineCount - maxKaraokeLines; } else if (lineCount - thisLine > maxKaraokeLines) { lineCount = thisLine + maxKaraokeLines; } } int syllLen = karaokeParams[SimplePlayerGUI.KARAOKE_SYLLABLE_LENGTH]; int currLinePos = karaokeParams[SimplePlayerGUI.KARAOKE_LINE_INDEX]; for (; thisLine < lineCount; thisLine++) { if (currLine != thisLine || syllLen == 0) { if (thisLine < currLine) { // && syllLen > 0 // already sung text in yellow g.setColor(0xFFFF30); } else { // other stuff in grey g.setColor(0x909090); } g.drawString(lines[thisLine], 0, currTop, Graphics.TOP | Graphics.LEFT); } else { // first draw any text before current position int xPos = 0; String currText; if (currLinePos > 0) { currText = lines[thisLine].substring(0, currLinePos); g.setColor(0xFFFF30); // yellow g.drawString(currText, 0, currTop, Graphics.TOP | Graphics.LEFT); xPos += g.getFont().stringWidth(currText); } // colorize the current syllable g.setColor(0xFFFF30); currText = lines[thisLine].substring(currLinePos, currLinePos + syllLen); g.drawString(currText, xPos, currTop, Graphics.TOP | Graphics.LEFT); if (currLinePos + syllLen < lines[thisLine].length()) { xPos += g.getFont().stringWidth(currText); currText = lines[thisLine].substring(currLinePos + syllLen); g.setColor(0x909090); // grey g.drawString(currText, xPos, currTop, Graphics.TOP | Graphics.LEFT); } } currTop += textHeight + KARAOKE_GAP; } } } // Feedback if (intersects(clipY, clipHeight, feedbackTop, textHeight)) { g.setColor(0xE0E0FF); g.drawString(feedback, 0, feedbackTop, Graphics.TOP | Graphics.LEFT); } // Status if (intersects(clipY, clipHeight, displayHeight-textHeight, textHeight)) { g.setColor(0xFAFAFA); g.drawString(status, 0, displayHeight, Graphics.BOTTOM | Graphics.LEFT); } } catch (Throwable t) { debugOut("in paint(): "+Utils.friendlyException(t)); } } // for debugging public String toString() { return "SimplePlayerCanvas"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -