简体   繁体   English

Android MediaPlayer seekTo()/ getDuration()无法正常工作

[英]Android MediaPlayer seekTo() / getDuration() not working as expected

I am using the android MediaPlayer library to play some MP3 files. 我正在使用android MediaPlayer库播放一些MP3文件。 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() . 我目前正在更新进度条,以了解用户当前通过文件的距离,将进度条的最大值设置为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 . 我每500毫秒延迟一次Runnable,以使用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). 当我尝试使用seekTo方法时会出现问题(但是偶尔会按我的预期工作)。 The media player object I have created has an OnSeekCompleteListener registered with it. 我创建的媒体播放器对象已向其注册了OnSeekCompleteListener。 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). 当我调用mediaPlayer.seekTo(50000) ,使用mediaPLayer.getCurrentPosition()在OnSeekCompleteListener中输出的值是我告诉它寻求的正确值(在这种情况下,假设我们从1000ms开始进入文件,则为51000) 。 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. 但是,当postDelayed可运行对象运行以更新进度条时, mediaPlayer.getCurrentPosition()返回的值通常(95%的时间)大大低于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). 如果我尝试搜索音频的结尾,它将始终跳回到同一点(如果相信getCurrentPosition()方法,则大约要提前2-3小时)。

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. 更复杂的是,如果我获取原始文件并使用Audacity和LAME(使用默认设置)对其进行重新编码,则此问题将消失。 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: 供参考:这是从Mac上的afinfo命令获得的原始文件的值:

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 编辑:这正在索尼Xperia的Android 6.0上进行测试

This is a know issue with the mediaplayer, AFAIK there is no solution to it: 这是媒体播放器的已知问题,AFIAK没有解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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