23

I'm trying to write a light-weight HTTP server in my app to feed dynamically generated MP3 data to the built-in Android MediaPlayer. I am not permitted to store my content on the SD card.

My input data is essentially of an infinite length. I tell MediaPlayer that its data source should basically be something like "http://localhost/myfile.mp3". I've a simple server set up that waits for MediaPlayer to make this request. However, MediaPlayer isn't very cooperative. At first, it makes an HTTP GET and tries to grab the whole file. It times out if we try and simply dump data into the socket so we tried using the HTTP Range header to write data in chunks. MediaPlayer doesn't like this and doesn't keep requesting the subsequent chunks.

Has anyone had any success streaming data directly into MediaPlayer? Do I need to implement an RTSP or Shoutcast server instead? Am I simply missing a critical HTTP header? What strategy should I use here?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Rob Szumlakowski
  • 947
  • 3
  • 8
  • 18
  • Did you also use this approach to playback video files? Did it perform the same as audio files? – Mathias Conradt Oct 11 '11 at 13:47
  • Hey Rob have you got any workaround for the issue? can you share sample code ? – Ashwin N Bhanushali Apr 04 '13 at 14:33
  • i know it late. but this is for those who end up here in the future. this [**Link**](http://stackoverflow.com/questions/5343730/mediaplayer-stutters-at-start-of-mp3-playback/5432091#5432091) has code to start with. – Samuel Apr 21 '11 at 06:57

1 Answers1

4

The HTTP Server was indeed hosted on the phone itself. It was very simple: just a thread listening on a socket for an HTTP GET request. When it got the HTTP request, it would one a new socket, write back some HTTP headers and start dumping the MP3 audio data back to the socket. This HTTP server didn't do anything else.

The Android Media Player was playing the music as I was streaming to it. The Media Player behaved very poorly if its playback buffer was emptied while it was playing audio. It was very important for me to make sure my HTTP server kept writing data into that socket. I moved bytes into the socket in small chunks (10 kB). The headers on my HTTP response ended up looking like this:

// Build response headers
StringBuilder sb = new StringBuilder();
sb.append( "HTTP/1.1 200 OK\r\n");
sb.append( "Content-Type: audio/mpeg\r\n");
sb.append( "Connection: close\r\n" );
sb.append( "Accept-Ranges: bytes\r\n" );
sb.append( "Content-Length: " + totalFileSize + "\r\n" );
sb.append( "Content-Disposition: inline; filename=xxxxx.mp3\r\n\r\n");

As long as I kept the pipe stoked, the Android Media Player kept consuming it without complaint. Playing audio only required one request and response. It ended up working pretty well.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    Any chance the full code is hosted anywhere? I'm trying to do the same with live video coming from the MediaRecorder + Camera and am exhausted with chasing down different attempts and libraries. If not, what size bounty would it take to add more detail here? – ChiefTwoPencils Aug 27 '14 at 23:51