Android MediaPlayer.getCurrentPosition() returns incorrect values
Asked Answered
G

2

8

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?

Gnaw answered 3/4, 2017 at 14:18 Comment(2)
Does the same problem occurs if your set UI_UPDATE_FREQ to 500ms. Apparently this is a well known bug. Try to change your audio file sampling rates.Uhhuh
The problem's gone after increasing UI_UPDATE_FREQ=80. If I change recording sampling rate, UI_UPDATE_FREQ also changes (up or down depending on sampling rate) to avoid the issueGnaw
G
1

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

Gnaw answered 4/4, 2017 at 14:44 Comment(0)
M
0

I believe one of my apps had the same issue (EBT Music Player on Google Play).

During playback, calling MediaPlayer.getCurrentPosition() periodically (1x/sec) would sometimes return a too-large value. When the first inflated value was returned, the values would be inflated until playback completed on that track.

https://github.com/EricTerrell/EBTMusicPlayer

Calling MediaPlayer.seekTo(0) before playback seems to fix the problem. I was able to consistently reproduce the problem, and I haven't seen the problem since adding the seekTo call.

Morganica answered 23/10, 2022 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.