16

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.

Joel
  • 4,732
  • 9
  • 39
  • 54
lightversusdark
  • 264
  • 4
  • 8
  • There's lots of examples in the sidebar if you need help with MediaPlayer, but it's really simple: `MediaPlayer mMediaPlayer = MediaPlayer.create(context, Uri.parse(url));` `mMediaPlayer.prepare();` `mMediaPlayer.start();` will get you started. If you're connected to Wi-Fi and have 3G coverage, switch off Wi-Fi while streaming and playback will stop. – lightversusdark Jul 30 '12 at 03:35
  • 2
    Yes, I know how to use media player. I was just asking if you tried anything on that behalf and want to share your progress so that we can improve on that. – Erol Jul 30 '12 at 04:04
  • Could you share an example url with us ? does it countain a file-pointer where to resume the stream after the connection is lost/gained ? – Vidar Vestnes Aug 07 '12 at 13:52
  • 1
    What baba and Vidar are saying is, [what have you tried?](http://whathaveyoutried.com) – Justin ᚅᚔᚈᚄᚒᚔ Aug 07 '12 at 14:53

4 Answers4

14

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".

Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
1

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.

Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61
0

I don't know why your media player is stopping, but maybe you could add an onReceive method and put "mp.start()" in the method to make it restart playback.
Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data

You might need to make a separate class, but that should explain how to create a method that is called when you switch networks, at which point you could call "mp.start()" to resume playback (assuming mp is your MediaPlayer).
This assumes, of course, that your MediaPlayer is only being paused when you are switching networks, not stopped.

Community
  • 1
  • 1
gmaster
  • 692
  • 1
  • 10
  • 27
0

As Vidar says, reestablishing the connection will be treated by the server as a new connection.

It appears that I have to double-buffer the audio playback, which means building a custom media player. This can provide continuous audio, but it will still skip when listening to a live stream.

The MP3 file is a bit easier because I can know the playback position. Not so with the live stream.

As gmaster says, I'll need a broadcast receiver to establish a new connection when the network changes. The audio buffer from the previous network connection should continue to playback while a new audio buffer is filled via the new connection.

When the new buffer is full enough to start playback I can switch playback to it. If I am streaming a file, with server support and a little bit of work I can ensure that the current playback position data is in both buffers and switch seamlessly.

As the live stream buffers cannot be synchronized, there will inevitably be a glitch when they switch.

A larger buffer will avoid audio drop-out if the connection takes a while to establish, but will delay the first start of playback. An MP3 file can be downloaded and fill the buffer faster than real time, but the live stream will buffer in real time.

Chris.Jenkins mentions some MediaPlayer methods that can help but points out that this does seem to need a custom framework. It will need to handle the conditions he mentions and others.

If I can make it look pretty I'll post it here. I'm going to keep the question open.

lightversusdark
  • 264
  • 4
  • 8