StackOverflowed
StackOverflowed

Reputation: 5975

Android - Detecting end of stream as opposed to connection error in MediaPlayer

While listening to a 1 hour mp3 stream, if a user loses connections (or changes from WIFI to 3G and the connection "Drops" for a second) we've noticed that MediaPlayer continues to play the content it has buffered. Once it runs out of buffered content, if it's not completed to the end of the file, I would have assumed it fires an Error event, but instead it changes state to PLAYBACK_COMPLETE. How can we figure out if the end of playback was due to an error in the connection (loss of connection) or actual playback completion? The only thing I've been able to come up with is to check currentPostion vs totalDuration when MediaPlayer changes state to PLAYBACK_COMPLETE. Is there a better way?

Upvotes: 2

Views: 2010

Answers (3)

Shamim Ahmmed
Shamim Ahmmed

Reputation: 8383

You have to make sure that you have a OnErrorListener implemented and inside this onError method you have to return true value. If you return a false value from this method but there was an error in that case media player fires OnCompletionListener onCompletion method.

Upvotes: 2

Alpay
Alpay

Reputation: 1368

It is really difficult to know this. I think your currentPosition & totalDuration approach is great, but I suggest you to listen for the network state of the device, as well. If the connection to the server drops caused by the client, you can know that in this way. This might help

Upvotes: 2

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28737

If it did not fire an onError() event, you can try registering an OnBufferingUpdateListener and check if the buffering percentage reaches 100%. If the connection is lost, on playback completion the percentage would be less than 100%.

Upvotes: 2

Related Questions