@Override public void run() { while (true) { try { Thread.sleep(100); if (mediaPlayer != null) { int status = MediaManage.getMediaManage().getPlayStatus(); // 只有当前正在播放才去刷新页面,如果当前正在快进,则不要刷新页面,免得页面出现闪烁 if (songInfo != null && status == MediaManage.PLAYING) { long progress = Math.round(mediaPlayer.getMediaTime().getNanoseconds() * 1.0 / 1000000); // double rate = mediaPlayer.getMediaTime() // .getSeconds() // * 1.00 // / mediaPlayer.getDuration().getSeconds(); // long progress = Math.round(songInfo.getDuration() // * rate); songInfo.setPlayProgress(progress); SongMessage msg = new SongMessage(); msg.setSongInfo(songInfo); msg.setType(SongMessage.SERVICEPLAYINGMUSIC); ObserverManage.getObserver().setMessage(msg); } } } catch (InterruptedException e) { e.printStackTrace(); } } }
/** * 解析歌词通过数据流 * * @param kscInputStream * @throws IOException * @throws NumberFormatException */ public static void parserKscLyricsByKscInputStream( String sid, InputStream kscInputStream, int type) throws NumberFormatException, IOException { mSid = sid; if (mKscLyricsParser == null) { mKscLyricsParser = new KscLyricsParserUtil(); } BufferedReader br = new BufferedReader(new InputStreamReader(kscInputStream, "GB2312")); mKscLyricsParser.parserksc(br); SongMessage songMessage = new SongMessage(); if (type == SongMessage.KSCTYPELRC) { songMessage.setType(SongMessage.LRCKSCDOWNLOADED); } else if (type == SongMessage.KSCTYPEDES) { songMessage.setType(SongMessage.DESKSCDOWNLOADED); } else if (type == SongMessage.KSCTYPELOCK) { songMessage.setType(SongMessage.LOCKKSCDOWNLOADED); } // songMessage.setKscFilePath(kscFilePath); songMessage.setSid(sid); // 通知 ObserverManage.getObserver().setMessage(songMessage); }
/** 初始化播放器 */ private void initMusic() { try { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.close(); mediaPlayer = null; SongMessage msg = new SongMessage(); msg.setSongInfo(songInfo); msg.setType(SongMessage.SERVICEPAUSEEDMUSIC); ObserverManage.getObserver().setMessage(msg); } if (lrcThread != null) { lrcThread = null; } } catch (Exception e) { e.printStackTrace(); } }
/** * 加载ksc歌词文件 * * @param context * @param sid * @param title * @param singer * @param kscUrl */ public static void loadKsc( String sid, String title, String singer, String displayName, String kscUrl, int type) { // 先判断本地有没有歌词文件 // 如果有,则加载歌词文件 // 如果没有,则判断kscUrl是否为空 // 如果kscUrl为空,则从服务器获取kscUrl的路径,然后下载ksc歌词文件 // 如果kscUrl不为空,则直接下载ksc歌词文件 String kscFilePath = Constants.PATH_KSC + File.separator + displayName + ".ksc"; File kscFile = new File(kscFilePath); if (!kscFile.exists()) { return; } else { SongMessage songMessage = new SongMessage(); if (type == SongMessage.KSCTYPELRC) { songMessage.setType(SongMessage.LRCKSCLOADED); } else if (type == SongMessage.KSCTYPEDES) { songMessage.setType(SongMessage.DESKSCLOADED); } else if (type == SongMessage.KSCTYPELOCK) { songMessage.setType(SongMessage.LOCKKSCLOADED); } songMessage.setKscFilePath(kscFilePath); songMessage.setSid(sid); // 通知 ObserverManage.getObserver().setMessage(songMessage); } }
/** * 播放歌曲 * * @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(); } }