Reputation: 21
I have the following issue with getduration() method of android MediaPlayer. For example with the following file(the real duration of the file is 5747000 ms) :
After MediaPlayer is prepared, I'm printing in log cat current position and getduration value in the following code :
Log.d("Podcast", "getDuration:"+mediaPlayer.getDuration());
Log.d("Podcast","getCurrentPosition:"+mediaPlayer.getCurrentPosition());
In log cat I can see the following :
D/Podcast﹕ getDuration:2885642
D/Podcast﹕ getCurrentPosition:3288059
I found another post with information about that but I have not found a solution. Similar issue found on stackoverflow
How is it possible? Can anyone help me on this?
Thanks. Regards.
Upvotes: 2
Views: 3851
Reputation: 238
You have to convert it into miliseconds..
public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
String mp3Minutes = "";
// Convert total duration into time
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
// menor que 10
if (minutes < 10) {
mp3Minutes = "0" + minutes;
} else {
mp3Minutes = "" + minutes;
}
finalTimerString = finalTimerString + mp3Minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}
Then,
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if (mediaPlayer != null && isPlaying) {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
musicSongDurationStart.setText("" + utils.milliSecondsToTimer(currentDuration) + "/" + utils.milliSecondsToTimer(totalDuration));
}
if (!isPause) {
musicSongDurationStart.postDelayed(mUpdateTimeTask, 100);
}
// Running this thread after 100 milliseconds
}
};
I hope it will help you.
Upvotes: 1
Reputation: 912
According to this thread: https://code.google.com/p/android/issues/detail?id=2559 The mp3 file should be on 44,100 Hz. Otherwise, the MediaPlayer will scale the time by the ratio of the MP3's actual sample rate to 44,100 Hz.
Upvotes: 1