@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 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(); } }