Android - How to tell when MediaPlayer is buffering
Asked Answered
C

6

38

I've got to be missing something obvious here, but I can't seem to find anything to allow me to determine when MediaPlayer is buffering audio. I'm streaming internet audio and I want to display a buffering indicator, but nothing I've tried allows me to know when MediaPlayer interrupts the audio to buffer, so I can't properly display a buffering indicator. Any clues?

Circumrotate answered 16/12, 2010 at 21:22 Comment(1)
Any updates on this? I am trying to make a streaming video player. All comments welcome.Oxy
C
7

@Daniel, per your comment on @JRL's answer, you could probably get this working by spinning up a thread and waiting for a timeout.

Something like DetectBufferTimeout.java (untested) would do nicely.

I do, however, agree that spinning up this separate thread is a bit of a hack. Perhaps OnBufferingUpdateListener could make a guarantee as to how often it calls onBufferingUpdate() regardless of whether a change in the buffering progress has occurred so we can detect if we're getting the same value over and over.

Cosec answered 12/4, 2011 at 19:19 Comment(2)
Thanks, Matt. The problem I'm having is that there's no notification of when the MediaPlayer actually pauses the track to buffer. But your suggestion is still good. I think the same technique would apply... rather than checking the buffering progress, I should check the play progress. If it hasn't updated and it's not paused or stopped from my end, then the MediaPlayer instance must have paused it in order to buffer some more audio data. Thanks!Circumrotate
Ah yes, I didn't realize you wanted to know when the track had paused specifically because it had run out through its buffer.Solarism
D
59

Like below (API level ≥ 9):

mp.setOnInfoListener(new OnInfoListener() {

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                loadingDialog.setVisibility(View.VISIBLE);
                break;
            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                loadingDialog.setVisibility(View.GONE);
                break;
        }
        return false;
    }
});

NOTE : There is a known bug in Android. When playing HLS stream it's just never calls OnInfoListener or OnBuffering. check this link OnInfoListener bug

Damar answered 11/4, 2013 at 4:23 Comment(3)
Does the fact that mediaPlayer has started buffering guarantee that it has stopped playback ? I am not sure about this.Entreat
I owe you a million ;)Overweary
@Entreat - According to the docs (developer.android.com/reference/android/media/…), yes.Thou
C
10

Ok, I feel a little vindicated now. I checked out the Pandora app and it doesn't display a buffering indicator. When music is interrupted for buffering, it just sits there as if nothing happened and the UI looks like it's still playing. So I've come to the conclusion that if you're using MediaPlayer, it's just not possible to determine if the track is temporarily paused for buffering.

However, I did notice that there are a couple MediaPlayer constants that could be of use: MEDIA_INFO_BUFFERING_START and MEDIA_INFO_BUFFERING_END. But they're only available in API level 9+, and the docs don't say anything about them. I'm assuming they can be used with an OnInfoListener.

I'm disappointed, but at least I can stop spinning my wheels now and move on to something else.

Circumrotate answered 17/12, 2010 at 16:49 Comment(0)
C
7

@Daniel, per your comment on @JRL's answer, you could probably get this working by spinning up a thread and waiting for a timeout.

Something like DetectBufferTimeout.java (untested) would do nicely.

I do, however, agree that spinning up this separate thread is a bit of a hack. Perhaps OnBufferingUpdateListener could make a guarantee as to how often it calls onBufferingUpdate() regardless of whether a change in the buffering progress has occurred so we can detect if we're getting the same value over and over.

Cosec answered 12/4, 2011 at 19:19 Comment(2)
Thanks, Matt. The problem I'm having is that there's no notification of when the MediaPlayer actually pauses the track to buffer. But your suggestion is still good. I think the same technique would apply... rather than checking the buffering progress, I should check the play progress. If it hasn't updated and it's not paused or stopped from my end, then the MediaPlayer instance must have paused it in order to buffer some more audio data. Thanks!Circumrotate
Ah yes, I didn't realize you wanted to know when the track had paused specifically because it had run out through its buffer.Solarism
S
1

Register an OnBufferingUpdate listener.

Sagitta answered 16/12, 2010 at 21:23 Comment(1)
Thanks. I've done that, but that only seems to tell me the percentage of the audio track that has been buffered. I tried comparing with the current play time of the track. I thought maybe I could see if the current buffer percentage is <= to the current play time percentage. But that doesn't work because MediaPlayer seems to have some buffering code baked in so that it buffers before the buffer runs completely out. So I still don't have a way to tell (or control) when the track is buffering. Hopefully I'm missing something?Circumrotate
B
1

You can use a thread that checks the current position of the MediaPlayer. If the position doesnt change, you can conclude that the media is in buffering state. Here is the complete tutorial that i wrote: http://www.ottodroid.net/?p=260

Bootblack answered 9/1, 2014 at 20:51 Comment(0)
P
0

Just like @JRL said, register a OnBufferUpdateListener but register it on the MediaPlayer object in OnPreparedListener, that way anytime the music is buffering it'll always indicate. as this listener is always called when mediaplayer is buffering. like so:

player.setOnPreparedListener(mediaPlayer -> {
               mediaPlayer.setOnBufferingUpdateListener((mediaPlayer1, percent) -> {
                    if (percent<=99)view.showMusicBuffer();

                    else view.hideMusicBuffer();
                });
                view.setTrackDuration(mediaPlayer.getDuration());
                mediaPlayer.start();

                changeTrackBarProgress();
            });
Photocompose answered 8/7, 2020 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.