Android MediaPlayer works fine in Custom audio Streaming application up to Android 2.1 but not in higher versions

Yaqub Ahmad picture Yaqub Ahmad · Dec 29, 2011 · Viewed 20.2k times · Source

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)

Answer

Yaqub Ahmad picture Yaqub Ahmad · Jan 12, 2012

The problem is that content type "audio/aacp" streaming is not supported directly. Some decoding libraries can be used to play "aacp", please see the solution below:

Freeware Advanced Audio (AAC) Decoder for Android

How to use this library?

Consider legal issues while using it.

[T]he project http://code.google.com/p/aacplayer-android/ is licensed under GPL, so you can create commercial apps on top of it, but you need to fullfill the GPL - mainly it means to publish your code as well. If you use the second project http://code.google.com/p/aacdecoder-android/ , then you do not need to publish your code (the library is licensed under LGPL).