gpt4 book ai didi

Android MediaPlayer.getCurrentPosition() 返回不正确的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:35 25 4
gpt4 key购买 nike

我正在编写一个短音频持续时间(通常为 1-5 秒)的音频播放器,如下所示

//start media player
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, Uri.fromFile(audioCache));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPlaying(true);
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
startPlayingTimer();
}
});
mediaPlayer.prepareAsync();


int UI_UPDATE_FREQ = 1000/ 60 + 1;

private void startPlayingTimer() {
playerRunnable = new Runnable() {
@Override
public void run() {
uiHandler.post(updateWaveRunnable);
handler.postDelayed(this, UI_UPDATE_FREQ);
}
};
handler.postDelayed(playerRunnable, UI_UPDATE_FREQ);

}

Runnable updateWaveRunnable = new Runnable() {
@Override
public void run() {
int position = mediaPlayer.getCurrentPosition();
updateDurationLabel(position);
}

}
};

问题是 mediaPlayer.getCurrentPosition() 返回闪光值

curPos/total: 1/1003 /(AudioView.java:323)
curPos/total: 13/1003 /(AudioView.java:323)
curPos/total: 34/1003 /(AudioView.java:323)
curPos/total: 55/1003 /(AudioView.java:323)
curPos/total: 76/1003 /(AudioView.java:323)
curPos/total: 97/1003 /(AudioView.java:323)
curPos/total: 117/1003 /(AudioView.java:323)
curPos/total: 139/1003 /(AudioView.java:323)
curPos/total: 159/1003 /(AudioView.java:323)
curPos/total: 179/1003 /(AudioView.java:323)
curPos/total: 199/1003 /(AudioView.java:323)
curPos/total: 226/1003 /(AudioView.java:323)
curPos/total: 246/1003 /(AudioView.java:323)
-> values jump back to smaller one
curPos/total: 162/1003 /(AudioView.java:323)
curPos/total: 185/1003 /(AudioView.java:323)
curPos/total: 203/1003 /(AudioView.java:323)
curPos/total: 223/1003 /(AudioView.java:323)
curPos/total: 242/1003 /(AudioView.java:323)
curPos/total: 263/1003 /(AudioView.java:323)
curPos/total: 290/1003 /(AudioView.java:323)
curPos/total: 312/1003 /(AudioView.java:323)
curPos/total: 332/1003 /(AudioView.java:323)
curPos/total: 352/1003 /(AudioView.java:323)
curPos/total: 372/1003 /(AudioView.java:323)
curPos/total: 392/1003 /(AudioView.java:323)
curPos/total: 412/1003 /(AudioView.java:323)
curPos/total: 431/1003 /(AudioView.java:323)
curPos/total: 452/1003 /(AudioView.java:323)
curPos/total: 472/1003 /(AudioView.java:323)
curPos/total: 492/1003 /(AudioView.java:323)
curPos/total: 512/1003 /(AudioView.java:323)
curPos/total: 534/1003 /(AudioView.java:323)
curPos/total: 554/1003 /(AudioView.java:323)
curPos/total: 574/1003 /(AudioView.java:323)
curPos/total: 595/1003 /(AudioView.java:323)
curPos/total: 615/1003 /(AudioView.java:323)
curPos/total: 635/1003 /(AudioView.java:323)
curPos/total: 655/1003 /(AudioView.java:323)
curPos/total: 675/1003 /(AudioView.java:323)
curPos/total: 697/1003 /(AudioView.java:323)
curPos/total: 716/1003 /(AudioView.java:323)
curPos/total: 736/1003 /(AudioView.java:323)
curPos/total: 756/1003 /(AudioView.java:323)
curPos/total: 776/1003 /(AudioView.java:323)
curPos/total: 796/1003 /(AudioView.java:323)
curPos/total: 821/1003 /(AudioView.java:323)
curPos/total: 837/1003 /(AudioView.java:323)
curPos/total: 857/1003 /(AudioView.java:323)
curPos/total: 877/1003 /(AudioView.java:323)
curPos/total: 897/1003 /(AudioView.java:323)
curPos/total: 917/1003 /(AudioView.java:323)
curPos/total: 938/1003 /(AudioView.java:323)
curPos/total: 958/1003 /(AudioView.java:323)
curPos/total: 978/1003 /(AudioView.java:323)
curPos/total: 981/1003 /(AudioView.java:323)

这会导致 UI 延迟/错误。我看到一份关于 Android 问题的报告,但没有给出解决方案:https://code.google.com/p/android/issues/detail?id=2559 .

有什么帮助吗?

最佳答案

ExoPlayer是 MediaPlayer 的一个很好的替代品。它至少解决了我的问题。

关于Android MediaPlayer.getCurrentPosition() 返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43187087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com