コード例 #1
0
    @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();
        }
      }
    }
コード例 #2
0
  /** 初始化播放器 */
  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();
    }
  }