Zaid Jalal Al-Wishah
Zaid Jalal Al-Wishah

Reputation: 27

MediaPlayer getCurrentPosition() issue

Can you help me by this issue? I have a seekbar that streaming from url, i want to display currentTime of mediaplayer in a textView, so how can i update the textview to take the currentTime every second

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

Upvotes: 1

Views: 2011

Answers (2)

Nezneika
Nezneika

Reputation: 147

Just some sample code using AsyncTask like @thiagolr said

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);

Upvotes: 1

thiagolr
thiagolr

Reputation: 7027

Use an AsyncTask:

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

You can update your TextView during the onProgressUpdate!

Upvotes: 1

Related Questions