Search code examples
androidkotlinandroid-autoandroid-automotive

Android Automotive Unable to create media player and setDataSource failed


I have been trying to use a simple audio audio player for android auto and just stream a audio from a link. But when the function is called the media player cant set the data source from url. and it will return the following error.

Code :

class HelloWorldScreen(carContext: CarContext) : Screen(carContext) {
override fun onGetTemplate(): Template {
    val mGridIcon = IconCompat.createWithResource(
        carContext, R.drawable.mainscreenlogo
    )

    val gridItemCar = GridItem.Builder()
        .setTitle("Car Info")
        .setImage(
            CarIcon.Builder(mGridIcon).build(),
            GridItem.IMAGE_TYPE_LARGE
        ).setOnClickListener(this::player).build()
    val gridList = ItemList.Builder()
        .addItem(gridItemCar).build()
   return GridTemplate.Builder()
        .setSingleList(gridList)
        .build()
}
private fun player(){
    var mediaPlayer = MediaPlayer()

    var audioUrl = "https://url"

    var customUri: Uri = Uri.parse(audioUrl)


    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
    mediaPlayer.setAudioAttributes(
        AudioAttributes. Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build()
    )
    if(!mediaPlayer.isPlaying){
        try {

            mediaPlayer.setDataSource(audioUrl)
            mediaPlayer.prepareAsync()
            mediaPlayer.setOnPreparedListener {
                mediaPlayer.start()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        Log.d("Player" , "Audio started playing..") }
    else {
        if (mediaPlayer.isPlaying) {
            mediaPlayer.stop()
            mediaPlayer.reset()
            //  mediaPlayer.release()
            Log.d("Player" , "Player Released")
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)


        } else {
            Log.d("Player" , "Audio not played.. Check Error")
        }
    }
}}

I used the player function to invoke the player .

The Error:

E/MediaPlayerNative: Unable to create media player W/System.err: java.io.IOException: setDataSource failed.: status=0x80000000 W/System.err: at android.media.MediaPlayer.nativeSetDataSource(Native Method) W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1173) W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1160) W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1125) W/System.err: at com.auslanka.app.common.HelloWorldService.player(HelloWorldService.kt:42) W/System.err: at com.auslanka.app.common.HelloWorldService.onCreateSession(HelloWorldService.kt:14) W/System.err: at androidx.car.app.CarAppService.onCreateSession(CarAppService.java:283) W/System.err: at androidx.car.app.CarAppBinder.lambda$onAppCreate$0$androidx-car-app-CarAppBinder(CarAppBinder.java:115) W/System.err: at androidx.car.app.CarAppBinder$$ExternalSyntheticLambda6.dispatch(Unknown Source:8) W/System.err: at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$0(RemoteUtils.java:149) W/System.err: at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda2.run(Unknown Source:6) W/System.err: at android.os.Handler.handleCallback(Handler.java:883) W/System.err:
at android.os.Handler.dispatchMessage(Handler.java:100) W/System.err: at android.os.Looper.loop(Looper.java:214) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356) W/System.err: at java.lang.reflect.Method.invoke(Native Method) W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Am I doing this wrong, if so a small code guide of how to play audio in android auto will be much appreciated. Thank you.


Solution

  • Technically this happened due to my own mistake of not adding the network the permissions in manifest. It turns out both Mediaplayer and HLS link I was using supported in Automotive Emulator just fine. If anyone out there see this question, make sure you add the network permissions properly. Thanks.

    <uses-permission android:name="android.permission.INTERNET"/>
    

    and

    <application....
    
    android:usesCleartextTraffic="true" .../>