📄 simpleplayergui.java
字号:
/* * @(#)SimplePlayerGUI.java 1.5 03/03/12 * * 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.*;/** * GUI functionality for SimplePlayer. * This class is used by SimplePlayerCanvas and SimplePlayerForm and * provides the actual logic. * SimplePlayerCanvas and SimplePlayerForm provide the interaction of * the main window with the user. * * @author Florian Bomers * @version 1.26 */public class SimplePlayerGUI implements CommandListener, PlayerListener, ItemStateListener, Utils.QueryListener { // loop private static final int LOOP_ONCE = 0; private static final int LOOP_3 = 1; private static final int LOOP_INF = 2; private int loopmode = LOOP_ONCE; // timer (for time display and karaoke asynchronous event) private static final int TIMER_INTERVAL = 50; static int timerInterval = TIMER_INTERVAL; // gauge private static final int GAUGE_NONE = -1; private static final int GAUGE_VOLUME = 0; // gauge is used as volume slider private static final int GAUGE_RATE = 1; // gauge is used as rate slider private static final int GAUGE_TEMPO = 2; // gauge is used as tempo slider private static final int GAUGE_PITCH = 3; // gauge is used as pitch slider private int gaugeMode = GAUGE_NONE; private Gauge gauge = null; private StringItem gaugeLabel = null; private Form gaugeForm = null; // display timer private Timer guiTimer=null; private TimerTask timeDisplayTask=null; // Commands private Command backCommand = new Command("Back", Command.BACK, 1); private Command playCommand = new Command("Play", Command.ITEM, 1); private Command stopCommand = new Command("Stop", Command.ITEM, 1); private Command metaCommand = new Command("META data", Command.ITEM, 3); private Command volCommand = new Command("Volume", Command.ITEM, 2); private Command muteCommand = new Command("Mute", Command.ITEM, 1); private Command unmuteCommand = new Command("Unmute", Command.ITEM, 1); private Command loopCommand = new Command("Loopmode", Command.ITEM, 4); private Command stcCommand = new Command("Stop in 5 sec", Command.ITEM, 4); private Command selectCommand = new Command("Select", Command.ITEM, 1); private Command skipFwCommand = new Command("Skip Forward", Command.ITEM, 5); private Command skipBwCommand = new Command("Skip Backward", Command.ITEM, 5); private Command rewindCommand = new Command("Rewind", Command.ITEM, 5); private Command rateCommand = new Command("Rate", Command.ITEM, 5); private Command tempoCommand = new Command("Tempo", Command.ITEM, 5); private Command pitchCommand = new Command("Pitch", Command.ITEM, 5); private Command fullScreenCommand = new Command("Full Screen", Command.ITEM, 5); private Command normalScreenCommand = new Command("Full Screen", Command.ITEM, 5); private Command startRecordCommand = new Command("Start Recording", Command.ITEM, 5); private Command stopRecordCommand = new Command("Stop Recording", Command.ITEM, 5); // display private Parent parent; private Displayable display; private static Image logo = null; // full screen private boolean fullScreen = false; // recording private boolean isRecording = false; private String recordLocator = "file:recording.wav"; // the player instance private Player player=null; // song descriptors private String title; private InputStream songInputStream; private String durationStr; private String songContentType=""; private String songLocator=""; private String songName=""; private String[] songDisplayNames = new String[0]; private int currSongDisplay = 0; private int changeSongDisplayCounter = 0; private static final int SONG_DISPLAY_COUNTER = 2000 / TIMER_INTERVAL; // 2 seconds // karaoke support private final static int LYRICS_EVENT = 0x60; // not an official event private final static String LYRICS_KEY = "lyrics"; // not an official key for MetaDataControl // indexes in params array returned by getKaraokeStr() public final static int KARAOKE_LINE_COUNT = 0; public final static int KARAOKE_LINE = 1; public final static int KARAOKE_LINE_INDEX = 2; public final static int KARAOKE_SYLLABLE_LENGTH = 3; private boolean karaokeMode = false; private String karaokeLyrics = ""; private int currKaraokeLine = 0; private int currKaraokeLinePos = 0; private int currKaraokeLength = 0; private String[] karaokeLines = null; private int[] karaokeLinePos = null; // start position in karaokeLyrics for each line private int karaokeLineCount = 0; private int redisplayKaraokeCounter = 0; // asynchronous switch to next Karaoke page private int nextKaraokePos = 0; // for asynchronous display of next line/phrase private int targetState = Player.UNREALIZED; private static void debugOut(String s) { Utils.debugOut("SimplePlayerGUI: "+s); } ///////////////////////////// INITIALIZATION ////////////////////////////// /** Note: parent MUST be Displayable */ public SimplePlayerGUI() { // initialize(String title, Parent parent) must be called after this } public void initialize(String title, Parent parent) { this.title = title; setParent(parent); initialize(); } public void setParent(Parent parent) { this.parent = parent; if (!(parent instanceof Displayable)) { throw new RuntimeException("parent must be instanceof Displayable!"); } display = (Displayable) parent; display.addCommand(backCommand); display.setCommandListener(this); updateTime(); updateRate(null); updateTempo(null); durationUpdated(); parent.updateKaraoke(); parent.updateDisplay(); } private void initialize() { karaokeMode = false; karaokeLyrics = ""; currKaraokeLine = 0; currKaraokeLinePos = 0; currKaraokeLength = 0; karaokeLines = null; karaokeLinePos = null; karaokeLineCount = 0; redisplayKaraokeCounter = 0; removeCommands(); setStatus(""); durationStr = ""; setSong("No song loaded", ""); guiTimer=new Timer(); } public void setSong(String name, String locator) { songLocator = locator; songInputStream = null; songContentType = ""; doSetSong(name); } public void setSong(String name, InputStream is, String contentType) { songLocator = ""; songInputStream = is; songContentType = contentType; doSetSong(name); } public void setSong(String name, Player player) { songLocator = ""; songInputStream = null; songContentType = ""; doSetSong(name); this.player = player; } private void doSetSong(String name) { songName = name; songDisplayNames = new String[1]; songDisplayNames[0] = name; currSongDisplay = 0; closePlayer(); setStatus(""); setFeedback(""); clearKaraoke(); updateTempo(null); updateTime(); updateSongDisplay(); } ///////////////////////////// VISUAL FEEDBACK ////////////////////////////// private void error(Throwable e) { error(Utils.friendlyException(e)); if (Utils.DEBUG) e.printStackTrace(); } private void error(String s) { setFeedback(s); } private void setStatus(String s) { parent.setStatus(s); if (Utils.DEBUG) System.out.println("Status: "+s); } private void setFeedback(String s) { parent.setFeedback(s); if (Utils.DEBUG) System.out.println("Feedback: "+s); } private void updateKaraoke() { parent.updateKaraoke(); } private void updateTime() { parent.updateTime(); } private void updateRate(RateControl c) { parent.updateRate(); // if the rate gauge is activated, update it if (gauge != null && gaugeMode == GAUGE_RATE) { if (c==null) { c = getRateControl(); } if (c != null) { String disp = getRateStr(); gauge.setValue((c.getRate() - c.getMinRate())/1000); gaugeLabel.setLabel(disp); } } } private void updateTempo(TempoControl c) { parent.updateRate(); // if the tempo gauge is activated, update it if (gauge != null && gaugeMode == GAUGE_TEMPO) { if (c==null) { c = getTempoControl(); } if (c != null) { String disp = getTempoStr(); gauge.setValue((c.getTempo()/1000)-1); gaugeLabel.setLabel(disp); } } } private void updateVolume(VolumeControl c) { if (c==null) { c = getVolumeControl(); } if (c != null) { int l = c.getLevel(); String disp = "Volume: "+String.valueOf(l); if (c.isMuted()) { disp += " (muted)"; } if (gauge != null && gaugeMode == GAUGE_VOLUME) { gauge.setValue(l); gaugeLabel.setLabel(disp); } setFeedback(disp); } } private void updatePitch(PitchControl c) { if (c==null) { c = getPitchControl(); } if (c != null ) { String disp = "Transpose: "+toFloatString(c.getPitch(), 3)+" semi tones"; // if the pitch gauge is activated, update it if (gauge != null && gaugeMode == GAUGE_PITCH) { gauge.setValue((c.getPitch() - c.getMinPitch())/1000); gaugeLabel.setLabel(disp); } setFeedback(disp); } } private void updateLoop() { if (player != null) { try { int loop=1; switch (loopmode) { case LOOP_ONCE: loop=1; break; case LOOP_3: loop=3; break; case LOOP_INF: loop=-1; break; } boolean restart = false; if (player.getState() == player.STARTED) { player.stop(); restart = true; } player.setLoopCount(loop); if (restart) player.start(); } catch (Exception e) { error(e); } } } private void updateSongDisplay() { String add = ""; if (currSongDisplay == 0) { add = durationStr; } parent.setFileTitle(songDisplayNames[currSongDisplay] + add); } private void durationUpdated() { if (player != null) { // include duration in song name long duration = player.getDuration(); if (duration >= 0) { durationStr = " ("+timeDisplay(duration)+")"; } else { durationStr = ""; } updateSongDisplay(); } } private void clearKaraoke() { karaokeLineCount = 0; nextKaraokePos = -1; redisplayKaraokeCounter = 0; updateKaraoke(); } ///////////////////////////// DISPLAY UTILITIES ////////////////////////////// public Image getLogo() { if (logo==null) { try { logo = Image.createImage("/icons/logo.png"); } catch (Exception ex) { logo = null; } if (logo == null) { debugOut("can not load logo.png"); } } return logo; } private String toFloatString(int number, int digitsAfterDot) { StringBuffer ret=new StringBuffer(String.valueOf(number)); while (ret.length()<(digitsAfterDot+1)) { ret.insert(0, '0'); } ret.insert(ret.length()-digitsAfterDot, '.'); return ret.toString(); } private String formatNumber(long num, int len, boolean leadingZeros) { StringBuffer ret=new StringBuffer(String.valueOf(num)); if (leadingZeros) { while (ret.length()<len) { ret.insert(0, '0'); } } else { while (ret.length()<len) { ret.append('0'); } } return ret.toString(); } private String timeDisplay(long us) { long ts = us/100000; return formatNumber(ts/600l, 2, true)+":"+formatNumber(((ts % 600) / 10), 2, true)+"."+String.valueOf(ts % 10); } public String getMediaTimeStr() { if (player!=null) { return timeDisplay(player.getMediaTime()); } return "--:--:-"; } public String getTempoStr() { if (player!=null) { TempoControl tc = getTempoControl(); if (tc != null) { int tempo = tc.getTempo(); return toFloatString((tempo+50)/100,1) +"bpm (eff: " +toFloatString((int) (((((long) tc.getRate())*((long) tempo)/100000)+50)/100l), 1)+")"; } } return ""; } public String getRateStr() { if (player!=null) { RateControl rc = getRateControl(); if (rc!=null) { return "Rate: "+toFloatString(rc.getRate(),3)+"%"; } } return ""; } /** * switch to next karaoke screen */ private void displayNextKaraokePhrase() { if (karaokeLyrics != null && karaokeLineCount > 0 && karaokeLines != null && nextKaraokePos >= 0) { if (nextKaraokePos < karaokeLyrics.length() && nextKaraokePos > karaokeLinePos[currKaraokeLine] ) { setupKaraokeLines(nextKaraokePos, 0); updateKaraoke(); } } } private void addKaraokeLine(int start, int end) { if (karaokeLines == null || karaokeLines.length <= karaokeLineCount) { String[] newKL = new String[karaokeLineCount+4]; if (karaokeLines != null) { System.arraycopy(karaokeLines, 0, newKL, 0, karaokeLineCount); } karaokeLines = newKL; int[] newKLP = new int[karaokeLineCount+5]; // one more than karaokeLines.length if (karaokeLinePos != null) { System.arraycopy(karaokeLinePos, 0, newKLP, 0, karaokeLineCount+1); } karaokeLinePos = newKLP; } karaokeLines[karaokeLineCount] = karaokeLyrics.substring(start, end); karaokeLinePos[karaokeLineCount++] = start; } private void setupKaraokeLines(int pos, int syllableLen) { if (karaokeLyrics == null) { debugOut("lyrics = null"); return; } // cancel automatic display of next line if it is already displayed nextKaraokePos = -1; int len = karaokeLyrics.length(); if (pos < 0 || pos >= len) { karaokeLineCount = 0; debugOut("< out of bounds! setupKaraokeLines"); return; } // the strings in karaokeLines never start with a control character while (karaokeLyrics.charAt(pos) == '\\' || karaokeLyrics.charAt(pos) == '/') { pos++; syllableLen--; } if (syllableLen<0) { syllableLen=0; } if (karaokeLinePos == null || karaokeLineCount == 0 || pos < karaokeLinePos[0] || pos >= karaokeLinePos[karaokeLineCount]) { // need to re-setup karaoke lines // first find the start of this phrase int startPos = pos; while (karaokeLyrics.charAt(startPos) != '\\' && startPos > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -