Android - How to tell when MediaPlayer is buffering

Daniel picture Daniel · Dec 16, 2010 · Viewed 28.3k times · Source

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?

Answer

ljian picture ljian · Apr 11, 2013

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