Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mediaplayer endless buffering

I'm developing online streaming player on Android. I found that mediaplayer wont ever stop buffering! It reads 100% of buffer and keeps doing 'something', because stream can only be read only once and there nothing left to buffer... I see that

public void onBufferingUpdate(MediaPlayer player, int percent)

is called endlessly with percent = 100. It drains battery over one night. Am I doing something wrong or there is some reasonable explanation on this? It seems that buffering can not be canceled, the only way to stop it is to reset mediaplayer itself. I need some way to stop this endless 'buffering'!

This how I init mediaplayer

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(streamUrl);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnInfoListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.prepareAsync();

Log messages I see:

10-30 08:45:21.169: D/MediaPlayer(13217): Message: MEDIA_BUFFERING_UPDATE(3), ext1=93, ext2=0x0
10-30 08:45:22.169: D/MediaPlayer(13217): Message: MEDIA_BUFFERING_UPDATE(3), ext1=100, ext2=0x0
10-30 08:45:23.169: D/MediaPlayer(13217): Message: MEDIA_BUFFERING_UPDATE(3), ext1=100, ext2=0x0
10-30 08:45:24.172: D/MediaPlayer(13217): Message: MEDIA_BUFFERING_UPDATE(3), ext1=100, ext2=0x0
10-30 08:45:25.172: D/MediaPlayer(13217): Message: MEDIA_BUFFERING_UPDATE(3), ext1=100, ext2=0x0
...
like image 509
Arvydas Avatar asked Oct 30 '13 06:10

Arvydas


1 Answers

First, it's not a good idea to leave the MediaPlayer paused while not in use. It may work sometimes, but Android is going to take back those resources eventually. It's simply bad practice not to clean up when your app isn't active.

That said, I believe what it's doing is maintaining an open socket with the server, probably sending the buffering notification whenever it pings to make sure the socket is still open. HTTP streaming is too simple a protocol to do what you ask. But you have at least one option to make it work. You can insert a proxy that will allow you to cache the stream and playback simultaneously. You will have to be clever about dealing with what happens when the user exits before the stream is fully cached, but it can work. At that point, the MediaPlayer is reduced to local file playback, which you can stop and seek to the correct point when the app is opened again. It provides a means of managing resources and network communication better.

like image 54
Dave Avatar answered Oct 19 '22 02:10

Dave