Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to tell when MediaPlayer is buffering

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?

like image 678
Daniel Avatar asked Dec 16 '10 21:12

Daniel


People also ask

How do I play music through mediaplayer?

Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.


2 Answers

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

like image 66
ljian Avatar answered Sep 30 '22 06:09

ljian


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.

like image 44
Daniel Avatar answered Sep 30 '22 08:09

Daniel