Search code examples
androidflutterandroid-autojust-audioaudio-service

Controll Flutter app audio from Android auto


I'm working on an Audio Book app that need to be controlled from Android Auto and CarPlay. My app successfully worked with CarPlay. But I can't get it to work with android auto. I'm using just_audio with just_audio_background and Also using audio_service.

I saw some guy acctualy made it work with Android auto, but for some reason I can't. Here's my approch for playing the audio.

 AudioSource audioSource = AudioSource.uri(Uri.file(tmpFile),
            tag: MediaItem(
              id: state.audioBookModel?.partitionKey ?? '12',
              title: state.audioBookModel?.mainTitle ?? 'Audio Book',
              artist: state.audioBookModel?.author ?? 'writer',
              artUri: Uri.parse(
                  'https://arturi.com'),
              playable: true,
              duration: Duration(seconds: state.audioBookModel?.totalDurationInSeconds ?? 0)
            ));
 await audioPlayer?.setAudioSource(
         audioSource,
       );
 await audioPlayer?.play(); 

I also tested my app by uploading it to Internal Testing, and it gives me same result. It play the audio from the app but I can't controll it.


Solution

  • I actually was able to solve it by using audio_session Package.

    In my main.dart I initialized the AudioSession like this.

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration.music());
    

    Also I added this meta-data Into my Android Manifest

    <meta-data
        android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc"/>
    

    automotive_app_desc.xml is located in res/xml folder

    <automotiveApp>
       <uses name="media"/>
    </automotiveApp>