I am experiencing a strange behavior while streaming music on some devices. When the audio first starts playing, the position jumps around and plays erratically until the buffering has fully completed.
I began with the sample code from Tutorials Point.
I am creating MediaPlayer in OnCreate()
as such:
// Create Media Player
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
prepareMediaPlayer();
prepareMediaPlayer
sets the data like this:
private void prepareMediaPlayer() {
try {
mediaPlayer.setDataSource(myurl);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Toast.makeText(getApplicationContext(),
"Playing sound",Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
and onBufferingUpdate
prints out the buffer progress:
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int bufferingProgress) {
Log.v(TAG, "onBufferingUpdate() "+bufferingProgress +
"\ncurrentposition: "+mediaPlayer.getCurrentPosition());
seekbar.setSecondaryProgress(bufferingProgress);
}
And the log output I get shows how the position jumps around while the media player is still buffering. I've tested on a few devices, and this only happens on a Moto G. Is this a bug in the firmware or am I doing something wrong?
08-29 12:50:17.934 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 4
currentposition: 0
08-29 12:50:17.934 277/com.test.mediaplayer D/MediaPlayer: setSubtitleAnchor in MediaPlayer
08-29 12:50:17.976 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 4
currentposition: 0
08-29 12:50:18.014 27927-27945/com.test.mediaplayer V/RenderScript: 0xb7912ee8 Launching thread(s), CPUs 4
08-29 12:50:21.414 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 8
currentposition: 7
08-29 12:50:34.930 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 11
currentposition: 9
08-29 12:50:50.085 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 15
currentposition: 4
08-29 12:51:32.991 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 19
currentposition: 2
08-29 12:53:25.036 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 22
currentposition: 5
08-29 12:53:51.976 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 26
currentposition: 1
08-29 12:56:26.004 27927-27946/com.test.mediaplayer I/MediaHTTPConnection: proxyName: 0.0.0.0 0
08-29 12:56:34.651 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 30
currentposition: 10
08-29 12:56:35.862 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 33
currentposition: 1102
08-29 12:56:38.211 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 37
currentposition: 4
08-29 12:56:40.097 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 41
currentposition: 9
08-29 12:56:43.034 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 44
currentposition: 9
08-29 12:56:45.130 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 48
currentposition: 3
08-29 12:56:49.841 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 52
currentposition: 8
08-29 12:56:52.500 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 55
currentposition: 8
08-29 12:56:55.748 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 63
currentposition: 7
08-29 12:56:57.080 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 74
currentposition: 1223
08-29 12:56:59.072 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 81
currentposition: 979
08-29 12:57:00.073 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 81
currentposition: 1979
08-29 12:57:03.596 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 88
currentposition: 5
08-29 12:57:05.862 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 92
currentposition: 4
08-29 12:57:08.291 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 95
currentposition: 8
08-29 12:57:13.903 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 99
currentposition: 9
08-29 12:57:14.904 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 100
currentposition: 668
08-29 12:57:34.261 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 100
currentposition: 224068
There is a note related to this in the developer docs:
There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after reset() is called. It is a programming error to invoke methods such as getCurrentPosition() ... in the Idle state for both cases
It also adds:
It is important to note that the Preparing state is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in the Preparing state is undefined.
It seems it is not safe for you to call getCurrentPosition()
until after the onPrepared
callback. The solution is probably to pause the playback while buffering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With