I’m using the MediaPlayer to stream MP3 files via http and it works great in the emulator, here’s the heart of the code I’m using (targeting sdk version 8):
// play selected track
if(mediaplayer.isPlaying()){
mediaplayer.reset();
}
try {
mediaplayer.setDataSource(selectedTrack.url);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.start();
However, when I try to debug it on a device (Motorola Droid RAZR running 2.3.5), I get the error below:
03-23 08:51:02.873: E/MediaPlayer(9442): error (1, -1004)
03-23 08:51:02.873: W/System.err(9442): java.io.IOException: Prepare failed.: status=0x1
Here’s the full stack trace for the run:
03-23 08:50:44.842: W/ActivityThread(9442): Application com.murfie.murfdroid is waiting for the debugger on port 8100...
03-23 08:50:44.850: I/System.out(9442): Sending WAIT chunk
03-23 08:50:44.858: I/dalvikvm(9442): Debugger is active
03-23 08:50:45.045: I/System.out(9442): Debugger has connected
03-23 08:50:45.045: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:45.248: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:45.451: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:45.654: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:45.850: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:46.053: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:46.256: I/System.out(9442): waiting for debugger to settle...
03-23 08:50:46.451: I/System.out(9442): debugger has settled (1490)
03-23 08:50:46.865: D/dalvikvm(9442): GC_EXTERNAL_ALLOC freed 52K, 44% free 3066K/5379K, external 2756K/2773K, paused 28ms
03-23 08:50:52.803: D/dalvikvm(9442): GC_EXTERNAL_ALLOC freed 83K, 42% free 3194K/5447K, external 3453K/3470K, paused 26ms
03-23 08:50:55.912: D/dalvikvm(9442): GC_EXTERNAL_ALLOC freed 21K, 42% free 3208K/5447K, external 4323K/4499K, paused 36ms
03-23 08:50:55.975: D/dalvikvm(9442): GC_EXTERNAL_ALLOC freed 3K, 42% free 3205K/5447K, external 5259K/5399K, paused 25ms
03-23 08:51:02.873: E/MediaPlayer(9442): error (1, -1004)
03-23 08:51:02.873: W/System.err(9442): java.io.IOException: Prepare failed.: status=0x1
03-23 08:51:02.904: W/System.err(9442): at android.media.MediaPlayer.prepare(Native Method)
03-23 08:51:02.912: W/System.err(9442): at com.murfie.murfdroid.Murfdroid.playSelectedTrack(Murfdroid.java:162)
03-23 08:51:02.912: W/System.err(9442): at com.murfie.murfdroid.Murfdroid.access$3(Murfdroid.java:151)
03-23 08:51:02.912: W/System.err(9442): at com.murfie.murfdroid.Murfdroid$4.onClick(Murfdroid.java:130)
03-23 08:51:02.920: W/System.err(9442): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:932)
03-23 08:51:02.920: W/System.err(9442): at android.widget.AdapterView.performItemClick(AdapterView.java:290)
03-23 08:51:02.928: W/System.err(9442): at android.widget.ListView.performItemClick(ListView.java:3602)
03-23 08:51:02.928: W/System.err(9442): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1838)
03-23 08:51:02.928: W/System.err(9442): at android.os.Handler.handleCallback(Handler.java:587)
03-23 08:51:02.936: W/System.err(9442): at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 08:51:02.936: W/System.err(9442): at android.os.Looper.loop(Looper.java:130)
03-23 08:51:02.936: W/System.err(9442): at android.app.ActivityThread.main(ActivityThread.java:3859)
03-23 08:51:02.944: W/System.err(9442): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 08:51:02.944: W/System.err(9442): at java.lang.reflect.Method.invoke(Method.java:507)
03-23 08:51:02.944: W/System.err(9442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
03-23 08:51:02.944: W/System.err(9442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
03-23 08:51:02.951: W/System.err(9442): at dalvik.system.NativeStart.main(Native Method)
03-23 08:51:02.951: E/MediaPlayer(9442): start called in state 0
03-23 08:51:02.951: E/MediaPlayer(9442): error (-38, 0)
03-23 08:51:02.975: E/MediaPlayer(9442): Error (-38,0)
I’m new to Android development so I may be missing something obvious here, but my guess is that it may be some sort of platform-specific issue based on other bits of info I’ve found googling around; if targeting a higher SDK version would change that, that may be a viable option but I’d prefer to make the app compatible with as many Android devices as possible.
Ok, here’s the solution I arrived at:
After reading this thread:
http://code.google.com/p/android/issues/detail?id=17553
It became obvious that what we were trying to do would never work on pre 4.x Android and that Google had no intention of fixing it. Given this, made two changes to my app to get it working:
With these two changes the app now works on the Droid RAZR, Kindle Fire and any other piece of Android hardware I can come up with.