Do all versions of Android support the MediaPlayer

2019-08-10 07:42发布

问题:

As the title says, I'm basically wondering if the MediaPlayer streaming support works for all versions of Android.

Here is the code I'm currently using, which works fine on my Android 2.2 phone:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(SHOUTCAST_STREAM);
mp.prepare();
mp.start();

But will that work on say 1.5 or 1.6? Or would I have to build my own buffering management system for the stream?

* Update *

Just tested it on the 1.6 emulator, and getting the following errors. On the 2.2 emulator it works fine.

23:15:50.074    31  ERROR   PlayerDriver    Command PLAYER_INIT completed with an error or info PVMFFailure
23:15:50.074    282 ERROR   MediaPlayer error (1, -1)
23:15:50.074    282 ERROR   MediaPlayer io error
23:15:50.074    282 ERROR   MediaPlayer java.io.IOException: Prepare failed.: status=0x1
23:15:50.074    282 ERROR   MediaPlayer     at android.media.MediaPlayer.prepare(Native Method)
23:15:50.074    282 ERROR   MediaPlayer     at org.me.bla.Bla.onCreate(Bla.java:38)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3502)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread.access$2200(ActivityThread.java:116)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1798)
23:15:50.074    282 ERROR   MediaPlayer     at android.os.Handler.dispatchMessage(Handler.java:99)
23:15:50.074    282 ERROR   MediaPlayer     at android.os.Looper.loop(Looper.java:123)
23:15:50.074    282 ERROR   MediaPlayer     at android.app.ActivityThread.main(ActivityThread.java:4203)
23:15:50.074    282 ERROR   MediaPlayer     at java.lang.reflect.Method.invokeNative(Native Method)
23:15:50.074    282 ERROR   MediaPlayer     at java.lang.reflect.Method.invoke(Method.java:521)
23:15:50.074    282 ERROR   MediaPlayer     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
23:15:50.074    282 ERROR   MediaPlayer     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
23:15:50.074    282 ERROR   MediaPlayer     at dalvik.system.NativeStart.main(Native Method)

So does this mean it just simply doesn't work on on the older versions of the OS, or is there any work around? I guess I could just do a custom buffering system, but I want to make sure there isn't an easier way first.

回答1:

According to Android References the MediaPlayer class has been available since api level: 1 and there isn't anything about it being deprecated. So I would say you would be safe.

On your 1.6 emulator have you started the browser and made sure you can access the internet?

Was the 1.6 emulator one that you have create some time ago? I have had issues with emulators that I have created after a period of time (the files get corrupted). If that is the case you could create a new one.

Another thing I just thought of... Is there some authentication missing somewhere?

I found this tutorial that might be helpful as well. He does create a buffer in his example.



回答2:

I think the problem is with the setDataSource. its not reading the file properly. The better way is read by FileInputStream and call getFD() method.Ex:

MediaPlayer mPlayer = new MediaPlayer();

FileInputStream stream = mContext.openFileInput("string");

mPlayer.setDataSource(stream.getFD());

stream.close();

mPlayer.setAudioStreamType(ANNOUCE_STREAM);

mAndroidPlayer.prepare();

mAndroidPlayer.start();

I think this will help you.