EDIT:
Android 2.2 MediaPlayer is working fine with one SHOUTcast URL but not with the other one
I need to play audio files from external URLs(shoutcast stream). Currently the audio files are downloaded incrementally & are played as soon as we get enough audio in phone local temporary storage. i am using the StreamingMediaPlayer class.
Check this piece of code:
private MediaPlayer createMediaPlayer(File mediaFile)
throws IOException {
MediaPlayer mPlayer = new MediaPlayer();
//example of mediaFile =/data/data/package/cache/playingMedia0.dat
FileInputStream fis = new FileInputStream(mediaFile);
mPlayer.setDataSource(fis.getFD());
mPlayer.prepare();
return mPlayer;
}
Current status:
1- It works fine from Android 1.6 to 2.1 but not in the higher versions like Android 2.2.
2- The “mPlayer.setDataSource(fis.getFD())” is the line which throws the error.
3- The error is “Unable to to create media player”
Other Solution tried:
I tried below alternate solution but nothing worked so far.
Android 2.2 MediaPlayer is working fine with one SHOUTcast URL but not with the other one
What i am looking for?
My goal is to have a peace of code which can work on Android 2.1 & higher.
This issue is also discussed here:
1- Inconsistent 2.2 Media Player Behavior
2- android code for streaming shoutcast stream breaks in 2.2
3- This issue is also discussed in a lot of questions on this site, but i found the answer no where.
4- markmail.org
LogCat trace:
Unable to to create media player
Error copying buffered conent.
java.lang.NullPointerException
com.ms.iradio.StreamingMediaPlayer.startMediaPlayer(StreamingMediaPlayer.java:251)
com.ms.iradio.StreamingMediaPlayer.access$2(StreamingMediaPlayer.java:221)
com.ms.iradio.StreamingMediaPlayer$2.run(StreamingMediaPlayer.java:204)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:3683)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
dalvik.system.NativeStart.main(Native Method)
The
StreamingMediaPlayerclass is using a double-buffering technique to get around limitations in pre-1.2 releases of Android. All production releases of Android OS have included a MediaPlayer that supports streaming media(1). I would recommend doing that rather than using this double-buffering technique to get around the problem.Android OS 2.2 replaced the old media player code with the FrightCast player which probably is acting differently in this case.
The line numbers in your stack trace don’t map to the file you link to, so I assume there’s a different version that you’re actually using. I’m going to guess that that
NullPointerExceptionis being reported byMediaPlayerbut neither theFileInputStreamnor the returnedFileDescriptorcan benull.(1) Prior to version 2.2 the media player wouldn’t recognize ShoutCast streams with an “ICY/1.1” version header in the response. By creating a proxy that replaces this with “HTTP/1.1” you can resolve that. See the StreamProxy class here for an example.