Home > Back-end >  Android Automotive Unable to create media player and setDataSource failed
Android Automotive Unable to create media player and setDataSource failed

Time:12-05

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.

CodePudding user response:

In my opinion: There could be several reasons why the media player is unable to create and set the data source from the URL. First, check that you have the correct URL. Make sure it is not a broken or invalid URL. Also, make sure that the URL has been whitelisted in the Android Auto app settings, if applicable. Additionally, make sure your app has the required permissions to access the URL. Finally, make sure the audio format is supported by Android Auto. If all of the above fail, then it could be an issue with the Android Auto app itself, in which case you should contact the developer for assistance.

See this example maybe can help you out:

// Create a MediaSource instance
MediaSource source = new MediaSource.Factory(dataSourceFactory)
        .createMediaSource(uri);

// Create an AudioSource instance
AudioSource audioSource = new AudioSource.Factory(dataSourceFactory)
        .setTag(TAG)
        .createAudioSource();

// Create a media player instance
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context);

// Prepare the player with the MediaSource
player.prepare(source);

// Set the audio source
player.setAudioSource(audioSource);

// Set the playback parameters
player.setPlaybackParameters(new PlaybackParameters(speed, pitch));

// Start playback
player.playWhenReady(true);

CodePudding user response:

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" .../>
  • Related