9

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
Evelyn
  • 2,588
  • 3
  • 22
  • 47
  • Why setOnPreparedListener is called two times on the same mediaPlayer instance? First time in onCreate() method, like `mediaPlayer.setOnPreparedListener(this)`, and then in prepareMediaPlayer() method you are creating and setting a new one `mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()`. – Stasys Skliutas Sep 13 '17 at 06:25
  • @StasysSkliutas You're right, the first `setOnPreparedListener` was a mistake. I removed it, but the result has not changed. – Evelyn Sep 13 '17 at 07:56
  • Some additional information would be helpful. On which android version code ran successfully?, on which version of android the code failed? What is the source of the stream, I assume from the code, that it is a remote resource? Does this behavior happen for any file, for instance song1.mp3, song2.mp3, ... or just for one particular file? – Stasys Skliutas Sep 13 '17 at 09:02
  • At what point in all that is the prepared listener called? It may be helpful to see a log from that callback. – Dave Sep 14 '17 at 11:44
  • @StasysSkliutas This error happens for all remote source files that I have tried. The Moto G, on which the error occurs, is running Android 6.0.1. Other phones, including emulators, that I have tried, do not experience this problem. – Evelyn Sep 15 '17 at 08:35

1 Answers1

2

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.

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Good information, but even when I remove the log line which calls `getCurrentPosition()`, it still plays erratically. The MediaPlayer does not start playing until `mediaPlayer.start()` in the `onPrepared` listener, so nothing is called when in the Preparing or Idle state. – Evelyn Sep 15 '17 at 08:52
  • So you're seeing the thumb jump, or hearing the audio? Do you have a sample app somewhere? – Nick Cardoso Sep 15 '17 at 10:09
  • Both the thumb and the audio jump. The link to the sample code is in the question, the only code I added is shown above. – Evelyn Sep 16 '17 at 14:17