Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer.getCurrentPosition() returns incorrect values

I'm writing an audio player for short audio duration (typically 1-5 seconds) as following

//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);
        }

    }
};

The problem is that mediaPlayer.getCurrentPosition() returns glitter values

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)

This causes the UI laggy/buggy. I saw an report on Android issues but there is no given solution: https://code.google.com/p/android/issues/detail?id=2559.

Any help?

like image 547
Bao Le Avatar asked Apr 03 '17 14:04

Bao Le


1 Answers

ExoPlayer is a good alternative to MediaPlayer. It resolves my issue at least.

like image 56
Bao Le Avatar answered Nov 14 '22 23:11

Bao Le