A Google audio player code sample that successfully plays mp3 audio from an online stream fails as soon as the project is migrated to AndroidX.
I'm attempting to follow the best practices in building a Media Player app as detailed here: https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app
At the bottom of this page is a link to this sample code: https://github.com/googlesamples/android-MediaBrowserService/
This sample creates a simple media player app that plays local mp3 files, rather than a stream.
The smallest possible change I can make to the sample so that it plays a stream rather than a file is to change the following in com.example.android.mediasession.service.players.MediaPlayerAdapter:
AssetFileDescriptor assetFileDescriptor = mContext.getAssets().openFd(mFilename);
mMediaPlayer.setDataSource(
assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength());
to
mMediaPlayer.setDataSource("http://109.228.17.230:80/");
This works successfully. The stream plays when the Play button is pressed.
However this code sample has not been migrated to AndroidX. It is still using support library v27.0.2. I have successfully migrated the code sample to AndroidX in Android Studio, and confirmed that after doing this the sample app still successfully opens and plays local mp3 files.
If however, I make the above code change after migrating the project to AndroidX, AndroidX, MediaPlayer.prepare() fails with the following stack trace:
E/MediaPlayerNative: error (1, -2147483648)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.mediasession, PID: 11609
java.lang.RuntimeException: Failed to prepare stream:
at com.example.android.mediasession.service.players.MediaPlayerAdapter.playFile(MediaPlayerAdapter.java:130)
at com.example.android.mediasession.service.players.MediaPlayerAdapter.playFromMedia(MediaPlayerAdapter.java:87)
at com.example.android.mediasession.service.MusicService$MediaSessionCallback.onPlay(MusicService.java:145)
at android.support.v4.media.session.MediaSessionCompat$Callback$StubApi21.onPlay(MediaSessionCompat.java:1404)
at android.support.v4.media.session.MediaSessionCompatApi21$CallbackProxy.onPlay(MediaSessionCompatApi21.java:196)
at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1486)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1282)
at com.example.android.mediasession.service.players.MediaPlayerAdapter.playFile(MediaPlayerAdapter.java:128)
at com.example.android.mediasession.service.players.MediaPlayerAdapter.playFromMedia(MediaPlayerAdapter.java:87)
at com.example.android.mediasession.service.MusicService$MediaSessionCallback.onPlay(MusicService.java:145)
at android.support.v4.media.session.MediaSessionCompat$Callback$StubApi21.onPlay(MediaSessionCompat.java:1404)
at android.support.v4.media.session.MediaSessionCompatApi21$CallbackProxy.onPlay(MediaSessionCompatApi21.java:196)
at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1486)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I have researched other reports of the "Prepare failed.: status=0x1" error online, but there does not seem to be a consensus as to the cause.
I've tried making the code change to use a stream before migrating to AndroidX, and after. The result is the same, with the app failing as above when playing a stream, but only when using AndroidX.
UPDATE: Further debugging has shown that I don't need to perform a full migration to AndroidX to trigger this error. Simply updating the support libraries used by the code sample from v27.0.2 to v28.0.0 in build.gradle is sufficient to stop the stream being played. Streaming still works when they are upgraded only to v27.1.1. However I'll leave the Title of this question as is, because the most likely way in which a developer might trigger this issue is by migrating to AndroidX as recommended by Google.