MediaPlayer getCurrentPosition()问题

时间:2013-05-27 13:57:50

标签: android android-mediaplayer

你能帮我解决这个问题吗?我有一个从url流式传输的搜索栏,我想在textView中显示mediaTimeyer的currentTime,那么如何更新textview以每秒获取当前时间

      new Thread(new Runnable()
      {
          public void run()
          {
              while(mediaPlayer!=null )
              {
                  seekprogress.setProgress(mediaPlayer.getCurrentPosition());
                  Message msg=new Message();
                  int millis = mediaPlayer.getCurrentPosition();

                  msg.obj=millis/1000;

                  try {
                      Thread.sleep(5000);
                  }
                  catch (InterruptedException e) {
                     e.printStackTrace();
                  }
                  seekprogress.setProgress(millis);
              }
          }
      }).start();

2 个答案:

答案 0 :(得分:1)

使用AsyncTask:

http://developer.android.com/reference/android/os/AsyncTask.html

您可以在onProgressUpdate期间更新TextView!

答案 1 :(得分:1)

使用像@thiagolr这样的AsyncTask的一些示例代码

Timer seekBarTimer = new Timer();
TimerTask seekbarTimerTask = new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // your condition and code here
                if (seekbar progress changes){
                    yourTextView.SetText(newString);
                }
            }
        });
    }
};
seekBarTimer.scheduleAtFixedRate(seekbarTimerTask, 1000, 1000);