📄 simpleplayergui.java
字号:
} else { setFeedback("No PitchControl!"); } } } public void skipFrame(boolean back) { int diff = 1; // 1 frame if (back) { diff = -diff; } FramePositioningControl fpc = getFramePositioningControl(); if (fpc != null) { int res = fpc.skip(diff); updateTime(); setFeedback("Skipped: "+res+" frames to "+fpc.mapTimeToFrame(player.getMediaTime())); } else { setFeedback("No FramePositioningControl!"); } } private void queryRecording() { try { // display the screen to enter the locator Utils.query("Enter a record locator", recordLocator, 200, TextField.URL, new RecordTask(), parent); } catch (Exception e) { Utils.error(e, parent); } } private void startRecording(String locator) { try { if (locator == null || locator == "") { Utils.error("No locator set!", parent); } else { // user entered the locator. recordLocator = locator; //Start recording RecordControl rc = getRecordControl(); if (rc != null) { rc.stopRecord(); rc.reset(); rc.setRecordLocation(locator); rc.startRecord(); } else { throw new MediaException("Could not get RecordControl!"); } } } catch (Exception e) { Utils.error(e, parent); } } private void stopRecording() { try { // Stop recording final RecordControl rc = getRecordControl(); if (rc != null) { rc.stopRecord(); // The commit() must be performed in a thread that is not this one: // we are running on the event dispatcher thread. // rc.commit() MIGHT display a security dialog (depending on the URL // that the audio is being recorded to) and the MIDP spec recommends // performing actions that could bring up a security dialog // on a thread that is NOT the event dispatcher thread. new Thread(new Runnable() { public void run() { try { rc.commit(); Utils.FYI("Recorded "+recordLocator+" successfully.", parent); } catch (Exception e) { Utils.error(e, parent); } } }).start(); } else { throw new MediaException("Could not get RecordControl!"); } } catch (Exception e) { Utils.error(e, parent); } } private void showHelp() { // only available for canvas player if (parent instanceof SimplePlayerCanvas) { ((SimplePlayerCanvas) parent).showHelp(); } } // /////////////////////////// EVENT HANDLERS ///////////////////////////// public void commandAction(Command c, Displayable s) { if (c == backCommand) { goBack(); } else if (c == muteCommand || c == unmuteCommand) { mutePressed(); } else if (c == volCommand) { volPressed(); } else if (c == metaCommand) { metaPressed(); } else if (c == loopCommand) { loopPressed(); } else if (c == stcCommand) { stopAfterTime(); } else if (c == playCommand || c == stopCommand) { togglePlayer(); } else if (c == skipFwCommand) { skip(false); } else if (c == skipBwCommand) { skip(true); } else if (c == rewindCommand) { setMediaTime(0); } else if (c == rateCommand) { ratePressed(); } else if (c == tempoCommand) { tempoPressed(); } else if (c == pitchCommand) { pitchPressed(); } else if (c == fullScreenCommand) { fullScreenPressed(); } else if (c == normalScreenCommand) { normalScreenPressed(); } else if (c == startRecordCommand) { queryRecording(); } else if (c == stopRecordCommand) { stopRecording(); } else if (c == helpCommand) { showHelp(); } else if (s != display) { // e.g. when list item in MetaData display list is pressed goBack(); } } private synchronized void startDisplayTimer() { if (timeDisplayTask == null) { timeDisplayTask = new SPTimerTask(); guiTimer.scheduleAtFixedRate(timeDisplayTask, 0, timerInterval); } } private synchronized void stopDisplayTimer() { if (timeDisplayTask != null) { timeDisplayTask.cancel(); timeDisplayTask = null; updateTime(); } } public void playerUpdate(Player plyr, String evt, Object evtData) { try { // special case: end-of-media, but loop count>1 ! if (evt == END_OF_MEDIA && plyr.getState() == Player.STARTED) { setFeedback("Looping"); return; } if (evt == CLOSED || evt == ERROR || evt == END_OF_MEDIA || evt == STOPPED_AT_TIME || evt == STOPPED) { stopDisplayTimer(); } if (evt == END_OF_MEDIA || evt == STOPPED_AT_TIME || evt == STOPPED || evt == ERROR) { display.removeCommand(stopCommand); display.addCommand(playCommand); changeSongDisplayCounter = 0; currSongDisplay = 0; updateSongDisplay(); } // Sun-specific event for karaoke lyrics if (evt.equals("com.sun.midi.lyrics")) { // META data byte[] data=(byte[]) evtData; if (data!=null && (evtData instanceof byte[]) && data.length>0) { if (Utils.DEBUG) System.out.println("META event 0x"+Integer.toHexString(data[0] & 0xFF)); switch (data[0]) { case 0x01: // Text (commonly used for Karaoke, but not sent if Player is in Karaoke mode) // fall through case 0x05: // Lyrics (isn't this meant for Karaoke ??) if (karaokeMode) { break; } // fall through if not Karaoke case 0x06: // marker // fall through case 0x07: // Cue point String text = new String(data, 1, data.length-1); setFeedback(text); if (Utils.DEBUG) System.out.println("META event 0x"+Integer.toHexString(data[0] & 0xFF)+": "+text); break; case 0x51: // Tempo updateTempo(null); break; case LYRICS_EVENT: // inofficial lyrics event: data 1-3 pos, 4-6 length int kPos = (data[1] << 16) | (data[2] << 8) | (data[3] & 0xFF); int kLen = (data[4] << 16) | (data[5] << 8) | (data[6] & 0xFF); setupKaraokeLines(kPos, kLen); updateKaraoke(); break; //case 0x58: // Time Signature //case 0x59: // Key Signature //case 0x7F: // Proprietary } } } if (evt == STARTED) { if (songDisplayNames.length > 1) { changeSongDisplayCounter = SONG_DISPLAY_COUNTER * 2; } startDisplayTimer(); display.addCommand(stopCommand); display.removeCommand(playCommand); } else if (evt == DEVICE_UNAVAILABLE) { setFeedback("Audio device not available!"); } else if (evt == BUFFERING_STARTED) { setFeedback("Buffering started"); } else if (evt == BUFFERING_STOPPED) { setFeedback("Buffering stopped"); } else if (evt == CLOSED) { setFeedback("Closed"); } else if (evt == DURATION_UPDATED) { setFeedback("Duration updated"); durationUpdated(); } else if (evt == END_OF_MEDIA) { setStatus("End of media."); setFeedback(""); } else if (evt == ERROR) { setFeedback("Error: " + ((String) evtData)); } else if (evt == RECORD_STARTED) { isRecording=true; display.addCommand(stopRecordCommand); display.removeCommand(startRecordCommand); setFeedback("Recording Started"); } else if (evt == RECORD_STOPPED) { isRecording=false; display.addCommand(startRecordCommand); display.removeCommand(stopRecordCommand); setFeedback("Recording Stopped"); } else if (evt == SIZE_CHANGED) { VideoControl vc = (VideoControl)evtData; setFeedback("Resize to " + vc.getDisplayWidth() + "x" + vc.getDisplayHeight()); } else if (evt == STOPPED_AT_TIME) { setStatus("Stopped at time."); setFeedback(""); } else if (evt == VOLUME_CHANGED) { VolumeControl vc = (VolumeControl) evtData; setFeedback("New volume: " + vc.getLevel()); updateVolume(vc); } } catch (Throwable t) { if (Utils.DEBUG) System.out.println("Uncaught Exception in SimplePlayerGUI.playerUpdate()"); error(t); } } public void itemStateChanged(Item item) { if (item!=null) { if (item == gauge) { switch (gaugeMode) { case GAUGE_VOLUME: VolumeControl vc = getVolumeControl(); if (vc != null) { vc.setLevel(gauge.getValue()); updateVolume(vc); } break; case GAUGE_RATE: RateControl rc = getRateControl(); if (rc != null) { rc.setRate((gauge.getValue() * 1000) + rc.getMinRate()); updateRate(rc); } break; case GAUGE_TEMPO: TempoControl tc = getTempoControl(); if (tc != null) { tc.setTempo((gauge.getValue() + 1) * 1000); updateTempo(tc); } break; case GAUGE_PITCH: PitchControl pc = getPitchControl(); if (pc != null) { pc.setPitch((gauge.getValue() * 1000) + pc.getMinPitch()); updatePitch(pc); } break; } // switch } } } // ///////////////// PAUSE / RESUME ///////////////////////// // /** * Stop the player and the display thread. * Some VM's may stop players and threads * on their own, but for consistent user * experience, it's a good idea to explicitly * stop and start resources such as player * and threads. */ public synchronized void pauseApp() { if (player != null && player.getState() >= Player.STARTED) { // player was playing, so stop it. try { player.stop(); } catch (MediaException e) { // nothing we can do here } // make sure that the timer does not keep on // displaying (we're not sure that the STOPPED // event will reach us before the VM becomes // suspended) stopDisplayTimer(); // make sure to restart upon resume restartOnResume = true; } else { restartOnResume = false; } } /** * If the player was playing when the MIDlet was paused, * then the player will be restarted here. */ public synchronized void resumeApp() { if (player != null && restartOnResume) { try { player.start(); } catch (MediaException me) { error(me); } // player.start() will trigger the display timer // to be started, so we don't need to explicitely // call startDisplayTimer(). } restartOnResume = false; } // ///////////////// INNER CLASSES /////////////////// // /** * Inner class for handling the query listener * when entering the recording URL */ private class RecordTask implements Utils.QueryListener { private RecordTask() { } public void start(String location) { startRecording(location); } // interface Utils.QueryListener // used for entering the filename of a recording public void queryOK(String text) { start(text); } public void queryCancelled() { // don't do anything if query is cancelled. // the previous visible Displayable will be // displayed automatically } } /** * The timer task that will be called every timerInterval * milliseconds */ private class SPTimerTask extends TimerTask { public void run() { updateTime(); if (redisplayKaraokeCounter>0) { redisplayKaraokeCounter--; if (redisplayKaraokeCounter == 0) { displayNextKaraokePhrase(); } } if (changeSongDisplayCounter > 0 && songDisplayNames.length>0) { changeSongDisplayCounter--; if (changeSongDisplayCounter == 0) { currSongDisplay = (currSongDisplay + 1) % songDisplayNames.length; updateSongDisplay(); changeSongDisplayCounter = SONG_DISPLAY_COUNTER; if (currSongDisplay == 0) { changeSongDisplayCounter *= 2; } } } } } // ////////////////// INNER INTERFACES ///////////////////// // /** * This interface is implemented by the Displayable * which uses this class. It provides the main screen. */ interface Parent extends Utils.BreadCrumbTrail { public Utils.BreadCrumbTrail getParent(); public String getTitle(); // callbacks for display matters of the main player screen public void setupDisplay(); // called after the media is prefetched public void setStatus(String s); public void setFeedback(String s); public void setFileTitle(String s); public void updateKaraoke(); public void updateTime(); public void updateRate(); // and tempo public void updateDisplay(); public void fullScreen(boolean value); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -