📄 simpleplayergui.java
字号:
if (player == null || player.getState() >= Player.STARTED) { return; } updateLoop(); // auto-rewind /* Not Supported try { long duration = player.getDuration(); if (duration != Player.TIME_UNKNOWN && player.getMediaTime() >= duration) { player.setMediaTime(0); } } catch (MediaException e) {}*/ if (player.getMediaTime() == 0) { setupKaraokeLines(0, 0); updateKaraoke(); } else { clearKaraoke(); } setStatus("Starting..."); player.start(); setStatus("Playing"); setFeedback(""); // tempo may have changed due to new position updateTempo(null); } catch (Throwable t) { error(t); } } public void closePlayer() { targetState = Player.CLOSED; if (player != null) { setStatus("Stopping..."); try { player.stop(); } catch (MediaException e) {} setStatus("Closing..."); player.close(); setStatus("Closed"); player = null; initialize(); } } public void pausePlayer() { if (player != null) { int state = player.getState(); if (state >= Player.PREFETCHED) targetState = Player.PREFETCHED; else if (state >= Player.REALIZED) targetState = Player.REALIZED; else targetState = Player.UNREALIZED; setStatus("Stopping..."); try { player.stop(); } catch (MediaException e) {} clearKaraoke(); setStatus("Stopped"); setFeedback(""); } } public void togglePlayer() { if (player!=null) { if (player.getState() == Player.STARTED) { pausePlayer(); } else { startPlayer(); } } } /** fast forward or fast backward */ public void skip(boolean backwards) { if (player != null) { long mTime = player.getMediaTime(); // default 10 sek long jump = 10000000; long duration = player.getDuration(); if (duration >= 0) { // skip 5% jump = duration / 20; } if (backwards) { // Jump backward setMediaTime(mTime-jump); } else { // Jump forward setMediaTime(mTime+jump); } } } public void stopAfterTime() { int delay = 5000000; // 5 seconds StopTimeControl stc = getStopTimeControl(); if (stc != null) { try { stc.setStopTime(StopTimeControl.RESET); stc.setStopTime(player.getMediaTime()+delay); setFeedback("Stop in "+(delay/1000000)+" seconds."); } catch (IllegalStateException me) { error(me); } } else { setFeedback("No StopTimeControl!"); } } public void changeRate(boolean slowdown) { int diff = 10000; // 10% if (slowdown) { diff = -diff; } RateControl rc = getRateControl(); if (rc != null) { int ocr = rc.getRate(); int ncr = ocr + diff; if (ncr >= rc.getMinRate() && ncr <= rc.getMaxRate()) { int ecr = rc.setRate(ncr); setFeedback("New rate: "+toFloatString(ecr, 3)+"%"); updateRate(rc); // rate changes effective tempo. updateTempo(null); } } else { setFeedback("No RateControl!"); } } public void setMediaTime(long time) { return;/* Not Supported
if (player == null) { return; } try { player.setMediaTime(time); setFeedback("Set MediaTime to "+timeDisplay(player.getMediaTime())); updateTime(); clearKaraoke(); updateTempo(null); } catch (MediaException me) { error(me); }*/ } public void changeVolume(boolean decrease) { int diff = 10; if (decrease) { diff = -diff; } VolumeControl vc = getVolumeControl(); if (vc != null) { int cv = vc.getLevel(); cv += diff; vc.setLevel(cv); updateVolume(vc); } else { setFeedback("No VolumeControl!"); } } public void toggleMute() { VolumeControl vc = getVolumeControl(); if (vc != null) { vc.setMute(!vc.isMuted()); updateVolume(vc); } else { setFeedback("No VolumeControl!"); } } public void transpose(boolean down) { int diff = 1000; // 1 semitone if (down) { diff = -diff; } PitchControl pc = getPitchControl(); if (pc != null) { pc.setPitch(pc.getPitch()+diff); updatePitch(pc); } else { // if no PitchControl, use FramePositioningControl if (getFramePositioningControl()!=null) { skipFrame(down); } 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); setFeedback("Skipped: "+res+" frames to "+fpc.mapTimeToFrame(player.getMediaTime())); } else { setFeedback("No FramePositioningControl!"); } } private void startRecording(String locator) { try { if (locator != null) { // 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!"); } } else { // display the screen to enter the locator Utils.query("Enter a record locator", recordLocator, 200, TextField.URL, this, parent); } } catch (Exception e) { Utils.error(e, parent); } } private void stopRecording() { try { // Stop recording RecordControl rc = getRecordControl(); if (rc != null) { rc.stopRecord(); rc.commit(); Utils.FYI("Recorded "+recordLocator+" successfully.", parent); } else { throw new MediaException("Could not get RecordControl!"); } } catch (Exception e) { Utils.error(e, parent); } } ///////////////////////////// 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) { /* Not Supported 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 == startRecordCommand) { startRecording(null); } else if (c == stopRecordCommand) { stopRecording(); } else if (s != display) { // e.g. when list item in MetaData display list is pressed goBack(); } } public void playerUpdate(Player plyr, String evt, Object evtData) { try { if ( (evt == END_OF_MEDIA) || (evt == STOPPED_AT_TIME) ) targetState = Player.PREFETCHED; // 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 == END_OF_MEDIA || evt == STARTED || evt == STOPPED_AT_TIME || evt == STOPPED) { if (timeDisplayTask!=null) { timeDisplayTask.cancel(); timeDisplayTask=null; updateTime(); } } if (evt == END_OF_MEDIA || evt == STOPPED_AT_TIME || evt == STOPPED) { display.removeCommand(stopCommand); display.addCommand(playCommand); changeSongDisplayCounter = 0; currSongDisplay = 0; updateSongDisplay(); } 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; } timeDisplayTask = new SPTimerTask(); guiTimer.scheduleAtFixedRate(timeDisplayTask, 0, timerInterval); 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) { int[] newDim = (int[]) evtData; setFeedback("Resize to "+newDim[0]+"x"+newDim[1]); } 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 } } } // interface Utils.QueryListener // used for entering the filename of a recording public void queryOK(String text) { startRecording(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 TIMER_INTERVAL * 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; } } } } } /** * 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 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 + -