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

时间:2017-04-03 14:18:28

标签: android android-mediaplayer

我按照以下

编写音频播放器的音频时间很短(通常为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

任何帮助?

1 个答案:

答案 0 :(得分:1)

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