[英]MediaPlayer.getDuration() returning wrong duration
The MediaPlayer's getDuration()
method is giving me an incorrect value for some audio files. MediaPlayer的
getDuration()
方法为某些音频文件提供了不正确的值。 I think the common trait for all these files is that they were manipulated using Audacity or some other audio editing tool. 我认为所有这些文件的共同特点是它们是使用Audacity或其他一些音频编辑工具进行操作的。 This is a problem when trying to tie MediaPlayer progress to a Progress Bar.
尝试将MediaPlayer进度绑定到进度条时,这是一个问题。
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. 这只是两个千分之一的日志行,但重点是在进度过去之后getDuration()认为是歌曲的总持续时间,它只是继续前进。 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?
因为MediaPlayer实际上可以提供正确的持续时间总数,有没有办法使用它来获得我的ProgressBar的适当最大值?
I had similar problem when MediaPlayer.getDuration() returned 542434 ms for mp3 file (HTC Desire C with ICS 4.0.3). 当MediaPlayer.getDuration()为mp3文件(HTC Desire C with ICS 4.0.3)返回542434 ms时,我遇到了类似的问题。 File itself was around 89 seconds, difference is too big.
文件本身大概是89秒,差异太大了。 I checked mp3 file content and saw some strange xml like:
我检查了mp3文件内容并看到了一些奇怪的xml:
<?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. 将此文件保存为新文件后,xml被删除,getDuration()返回正确的值。 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. 我几次尝试玩演示播放器,当我在Android模拟器中测试时,它的行为与您在问题中提到的相同,但是当我尝试使用某些真实设备时,它给了我准确的媒体持续时间值。
If your intention is only play media syncing with seekbar then you can do something like below , 如果您的意图只是播放媒体与seekbar同步,那么您可以执行以下操作,
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
handler.post(new SeekbarRefresh(seekbar));
//Class to update progress of seekbar according to music player
private class SeekbarRefresh implements Runnable {
SeekBar seekBar;
public SeekbarRefresh(SeekBar seekBar, ImageView imageView) {
this.seekBar = seekBar;
}
@Override
public void run() {
if (mediaPlayer != null) {
if (mediaPlayer.getDuration() > 0) {
int currentDuration = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentDuration);
if (mediaPlayer.isPlaying()){
handler.post(this);
isAudioPlaying = true;
}
else {
handler.removeCallbacks(this);
isAudioPlaying = false;
}
}
}
}
}
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mediaPlayer.seekTo(progress);
}
});
I have encountered a similar situation. 我遇到过类似的情况。 In my case the time difference between
mPlayer.getDuration()
to mPlayer.getCurrentPosition()
was around 80 seconds. 在我的例子中,
mPlayer.getDuration()
与mPlayer.getCurrentPosition()
之间的mPlayer.getDuration()
约为80秒。
After reading few posts on the subject, I used third party software to convert the mp3
's sample rate from 22,000 kHz
to 44,100 kHz
. 在阅读了关于这个主题的几篇文章后,我使用第三方软件将
mp3
的采样率从22,000 kHz
为44,100 kHz
。 Once converted, the result of getDuration()
and getCurrentPosition()
are the almost the same (0.0012s constant error). 转换后,
getDuration()
和getCurrentPosition()
的结果几乎相同(0.0012s恒定误差)。
Here is the test used: 这是使用的测试:
dur = mp.getDuration();
Log.d("dur", dur + " <- getDuration");
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// finish current activity
Log.d("dur", mp.getCurrentPosition() + " <- getCurrentPostion");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.