The MediaPlayer's getDuration()
method is giving me an incorrect value for some audio files. I think the common trait for all these files is that they were manipulated using Audacity or some other audio editing tool. This is a problem when trying to tie MediaPlayer progress to a Progress Bar.
I went ahead and logged it:
while(mPlayer.isPlaying())
Log.i("progress/total",
mPlayer.getCurrentPosition() +
"/" + mPlayer.getDuration());
and found this:
I/progress/total(643): 14615/14620
I/progress/total(643): 14647/14620
This is only two log line of thousands, but the point is after the progress passes what getDuration() believes to be the total duration of the song, it just keeps going. Because the MediaPlayer can in fact give the correct total for duration, is there a way to use this to get a proper maximum for my ProgressBar?
I had similar problem when MediaPlayer.getDuration() returned 542434 ms for mp3 file (HTC Desire C with ICS 4.0.3). File itself was around 89 seconds, difference is too big. I checked mp3 file content and saw some strange xml like:
After saving this file as new one that xml was dropped and getDuration() returned correct value. I know that it will not help those who need playing files you can't modify, but for those who can - it should help.
I was trying to play demo player a few while ago ,when i tested in Android emulator ,its behavior was the same as like you mentioned in your question but when i tried in some real device it gave me accurate value of media duration.
If your intention is only play media syncing with seekbar then you can do something like below ,
I have encountered a similar situation. In my case the time difference between
mPlayer.getDuration()
tomPlayer.getCurrentPosition()
was around 80 seconds.After reading few posts on the subject, I used third party software to convert the
mp3
's sample rate from22,000 kHz
to44,100 kHz
. Once converted, the result ofgetDuration()
andgetCurrentPosition()
are the almost the same (0.0012s constant error).Here is the test used: