简体   繁体   中英

Android MediaPlayer seekTo() / getDuration() not working as expected

I am using the android MediaPlayer library to play some MP3 files. I am not in charge of these files and am unable to change them in anyway, only play them.

I currently update a progress bar as to how far through the file the user currently is, setting the maximum value of the progress bar to mediaPlayer.getDuration() . I know that the value returned by this is correct. I postDelayed a Runnable every 500ms to update the progress bar with the value of mediaPlayer.getCurrentPosition . This all works correctly as expected.

The issue occurs when I try to use the seekTo method (however very occasionally it will work as I expect). The media player object I have created has an OnSeekCompleteListener registered with it. When I call mediaPlayer.seekTo(50000) the value that is outputted in the OnSeekCompleteListener using mediaPLayer.getCurrentPosition() is the correct value that I told it to seek to (in this case 51000 assuming that we started from 1000ms in to the file). However when the postDelayed runnable runs to update the progress bar, the value returned by mediaPlayer.getCurrentPosition() is usually (95% of the time) considerably lower than the value that was outputted in the OnSeekCompleteListener.

There does not seem to be any rhyme or reason to the values that end up being returned, as they are different each time even when the exact same circumstances occur.

EDIT: After saying that the values are always different it turns out that isn't quite true. If I attempt to seek to the very end of the audio, it will always jump back to the same point (approx 2-3 hours before if the getCurrentPosition() method is to be believed).

To make matters more complicated, if I take the original file and re-encode it using Audacity and LAME (using the default settings) this issue goes away. I was only able to do this as a test, as when my app goes live I will have no control over encoding the files.

If anyone has any ideas on this they would be greatly appreciated.

For reference: Here are the values of the original file obtained from the afinfo command on Mac:

File:           Test.mp3
File type ID:   MPG3
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration:               35997.544490 sec
audio bytes:                      216835607
audio packets:                    1378031
bit rate:                         48000 bits per second
packet size upper bound:          1052
maximum packet size:              522
audio data file offset:           1630
optimized

And the information from the audio file that I re-encoded that worked correctly:

File:           Test2.mp3
File type ID:   MPG3
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration:                     35997.596735 sec
audio bytes:                            293659748
audio packets:                          1378033
bit rate:                               65000 bits per second
packet size upper bound:                1052
maximum packet size:                    835
audio data file offset:                 1560
optimized
audio 1587491712 valid frames + 576 priming + 1728 remainder = 1587494016

Edit: This is being tested on Android 6.0 on a Sony Xperia

This is a know issue with the mediaplayer, AFAIK there is no solution to it:

https://code.google.com/p/android/issues/detail?id=11590 https://code.google.com/p/android/issues/detail?id=2559

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM