Android MediaPlayer crash
ProblemEver wondered why your app isn't able to stream a radio stream on eg. HTC EVO 3D but works on almost every other android phone?
Does the following error message seem familiar?
SolutionAfter a lot of fiddling around I found that the key was the line
10-20 10:47:05.076: WARN/NuHTTPDataSource(28325): Server did not give us the content length!.
Even though it's just a warning, and even though it works on almost every phone, somehow the HTC EVO 3D fails to play sound.
So what can we do about this? Well there are 2 options.
1. Add a content-length header on the server reply
2. Add a custom header to the response and then feed it to MediaPlayer.
Since we already have loads of clients connected to our servers we didn't want them to suddenly break and thus creating more work for us, we decided to go with option 2. But how does one feed a header to MediaPlayer? Well we ended up using the idea of StreamProxy which basically is a local proxy which downloads the stream and is able to modify the headers which are then buffered and sent to the MediaPlayer. This way we also got a nice set of stream-bits that we could use to fetch the meta-data from without using multiple connections to the stream-server which should preserve some batteries as doing http requests is somewhat expensive on a phone. I haven't measured if and how much battery-life is gained by using 1 connection instead of polling form the server, but I sure do like that I now have full control of what goes into MediaPlayer!
Also as a sidenote to the content-header. I tried setting an 'unlimited' content-header eg. -1 or 0. The results were quite dissapointing.
|Phone||content-length = -1||content-length = 0||content-length = a_large_number|
|HTC 3D EVO||Runs fine||Stops at 0sec||Runs fine|
|Other Android phones||Doesn't run at all||Stops at 0sec||Runs fine|
So your last question might be What is the maximun length of content-length?. Google told me 2TB, so I went with something around those numbers. FYI setting it to 3TB (I had to test) did in fact not work at all.