/** * 快进 * * @param progress */ private void seekTo(int progress) { try { if (mediaPlayer != null) { // float playedRate = (float) progress / songInfo.getDuration(); // mediaPlayer.seekTo(playedRate); // mediaPlayer.stop(); double totalTime = mediaPlayer.getDuration().getSeconds(); double rate = progress * 1.00 / songInfo.getDuration(); mediaPlayer.setMediaTime(new Time(totalTime * rate)); // mediaPlayer.start(); MediaManage.getMediaManage().setPlayStatus(MediaManage.PLAYING); } } catch (Exception e) { e.printStackTrace(); } }
/** * 播放歌曲 * * @param songInfo */ private void playInfoMusic(SongInfo msongInfo) { this.songInfo = msongInfo; if (songInfo == null) { SongMessage msg = new SongMessage(); msg.setType(SongMessage.SERVICEERRORMUSIC); msg.setErrorMessage(SongMessage.ERRORMESSAGEPLAYSONGNULL); ObserverManage.getObserver().setMessage(msg); return; } File songFile = new File(songInfo.getFilePath()); if (!songFile.exists()) { logger.error("播放文件不存在!"); // 下一首 SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); return; } try { if (mediaPlayer == null) { // mediaPlayer = new MediaPlayer(); // // mediaPlayer.setDataSource(songInfo.getFilePath()); // // // // float playedRate = (float) songInfo.getPlayProgress() // // / songInfo.getDuration(); // // mediaPlayer.seekTo(playedRate); // mediaPlayer.setMediaLocator(new MediaLocator(songInfo // .getFilePath())); File file = new File(songInfo.getFilePath()); mediaPlayer = Manager.createRealizedPlayer(new MediaLocator(file.toURL())); initVolume(); mediaPlayer.prefetch(); // 获取媒体数据 double totalTime = mediaPlayer.getDuration().getSeconds(); double rate = songInfo.getPlayProgress() * 1.00 / songInfo.getDuration(); mediaPlayer.setMediaTime(new Time(totalTime * rate)); mediaPlayer.prefetch(); mediaPlayer.start(); } } catch (Exception e) { logger.error("不能播放此文件:" + songInfo.getFilePath()); e.printStackTrace(); SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); } if (mediaPlayer != null) { mediaPlayer.addControllerListener( new ControllerListener() { @Override public void controllerUpdate(ControllerEvent e) { // 当媒体播放结束时 if (e instanceof EndOfMediaEvent) { mediaPlayer.setMediaTime(new Time(0)); mediaPlayer = null; SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); } } }); // mediaPlayer.setOnCompletionListener(new OnCompletionListener() { // // @Override // public void onProgressChanged() { // // float playedRate = mediaPlayer.getPlayedRate(); // // long playProgress = (long) (songInfo.getDuration() * playedRate); // // if (songInfo != null) { // songInfo.setPlayProgress(playProgress); // SongMessage msg = new SongMessage(); // msg.setSongInfo(songInfo); // msg.setType(SongMessage.SERVICEPLAYINGMUSIC); // ObserverManage.getObserver().setMessage(msg); // } // } // // @Override // public void onCompletion() { // // SongMessage songMessage = new SongMessage(); // songMessage.setType(SongMessage.NEXTMUSIC); // ObserverManage.getObserver().setMessage(songMessage); // } // }); } if (lrcThread == null) { lrcThread = new Thread(new LrcRunable()); lrcThread.start(); } }