Reputation: 307
friends, I don't know how to get songs remaining and total duration from the media player. In my code, I want to get songs total duration and remaining duration and show in text view with proper time formatting:
updateseekbar=new Thread(){
@Override public void run(){
int totalduration=mediaPlayer.getDuration();
int currnetpostion=0;
while (currnetpostion<totalduration){
try{
sleep(500);
currnetpostion=mediaPlayer.getCurrentPosition();
seekBar.setProgress(currnetpostion);
}catch (Exception e) {
e.printStackTrace();
}
}
}
};
What do I need to do in order to achieve this?
Upvotes: 0
Views: 2764
Reputation: 219
As i see in your code....
totalduration= the total time of the song;
but if you want to current position of the media player. You have to put your code inside the runnable and update the current position at every second....
final Handler handler=new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int duration=mediaplayer.getcurrentPosition();
//and update your seekbar from handler
//change your int to time format...
String time = String.format("%02d:%02d ", TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
updateseekbar();
handler.postDelayed(this,1000);
}
});
if you are not getting it ask this again
Upvotes: 1
Reputation: 822
You can acheive this by substracting current duration from total duration, In your code I can see you have both Total and current duration.
remainingDuration = totalduration - currnetpostion=mediaPlayer.getCurrentPosition();
May be work your case
Upvotes: 0