Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer.getDuration() returning wrong duration

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?

like image 619
JMRboosties Avatar asked Jan 01 '26 05:01

JMRboosties


1 Answers

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:

<?xpacket begin="п»ї" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.2-c063 53.351735, 2008/07/22-18:04:26        ">

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.

like image 95
Artem Svystun Avatar answered Jan 02 '26 18:01

Artem Svystun