I am streaming audio using MediaPlayer on Android.
When the device moves from Wi-Fi to the cell network or vice-versa, the MediaPlayer stops playback.
Typically there are a few seconds-worth of audio in the buffer, so playback does not cease immediately.
Ideally I would like to pick up the stream for uninterrupted playback, but I cannot see how to do it.
I am working with both mp3 files hosted on the server and a live broadcast stream.
From a servers point of view, changing network mode from WiFi to 3G (visa versa), will look like a brand new connection from a separate IP's (client).
If the server that you are downloading from does not support tracking the stream (e.g number of seconds, sequence, byte) (unlike media-servers),It will have to start serving your mp3 from 0 byte again.
If your URL is pointing to a MP3 file located at a standard HTTP server, your situation will be what to expect. You should look into using a Media streaming server, so you could resume downloading/streaming at your choice. When you receive the intent that the connection is lost/resume, you could point your mediaplayer to the new URL with file-position in the URL (e.g seconds=19, bytes=57365).
Not sure if this helps you, but it explains a bit whats going on "behind the scenes".
Try setting your setOnCompletionListener
and setOnErrorListener
. In on complete with a live stream you can just call prepareAsync()
again and this will kick off the stream again. There is no graceful way of doing this really unless you write your own media framework.
You can also listen in you onError()
for the MEDIA_ERROR_SERVER_DIED
you can then fire off the prepareAsync()
again.
You'll find that the MediaPlayer will either Error or Complete. If you handle both these callbacks the very least you can do is restart the stream on change of network, as for smooth playback.. that would require custom mediaframework as the android one is pretty shoddy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With