Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play and Show NowPlaying MediaItem when clicked on Android Auto #991

Open
wants to merge 1 commit into
base: minor
Choose a base branch
from

Conversation

bensalcie
Copy link

@bensalcie bensalcie commented Jan 18, 2023

Adding this snippet enables you to play MediaItems on Android Auto, and shows the correct Playing information on click.

    @override
    Future<void> playFromMediaId(String mediaId,
        [Map<String, dynamic>? extras]) async {
      //Get the index of the item on the queue
  
      var index = queue.value.indexWhere((element) => element.id == mediaId);
  
      // Skip to the item to start playing.
      await skipToQueueItem(index);
    }

image

@ryanheise
Copy link
Owner

Should we have some state to check whether we are in Android Auto mode or not?

@bensalcie
Copy link
Author

bensalcie commented Jan 18, 2023 via email

@ryanheise
Copy link
Owner

I guess a more important question is why this override doesn't actually make a call to play the audio.

@bensalcie
Copy link
Author

Yaaa, tried to check on a couple of hacks suggested on stackoverflow and google auto docs , the package seems to have followed the pattern quite correctly.

However skipping to the index of the items already added to the queue can be a workaround for now. Probably we can pass the client type on MediaItem extras ,so that its easer to combine the check for the index and client type before we skip to the item via Android Auto.

@federico2390
Copy link

Hello I have an app with radio stations list and work as well in iOS CarPlay but I don't know why in android auto I see just the app icon and when I open the app I dont see nothing. the screen is empty. how I can pass the mediaItem from flutter to display in a list in android auto and when I click on one item this go to play?
plz

@bensalcie
Copy link
Author

Hello I have an app with radio stations list and work as well in iOS CarPlay but I don't know why in android auto I see just the app icon and when I open the app I dont see nothing. the screen is empty. how I can pass the mediaItem from flutter to display in a list in android auto and when I click on one item this go to play?
plz

Hi, please checkout this example from the library. It is pretty clear on how you can do it on Android Auto.
https://github.com/ryanheise/audio_service/blob/minor/audio_service/example/lib/example_multiple_handlers.dart

@federico2390
Copy link

@bensalcie ok thx, I try tomorrow and I'll tell you how it went.

@federico2390
Copy link

@bensalcie this works. thx. I need last help plz. do u know why if I have added my tracks on mediaItems why inside android auto I see blank screen?
And also exist a way to hide/remove the time of the tracks because for the streaming I don't need this.
thx

Screenshot 2023-02-05 alle 03 43 06

Screenshot 2023-02-05 alle 03 43 09

@bensalcie
Copy link
Author

@bensalcie this works. thx. I need last help plz. do u know why if I have added my tracks on mediaItems why inside android auto I see blank screen? And also exist a way to hide/remove the time of the tracks because for the streaming I don't need this. thx

Screenshot 2023-02-05 alle 03 43 06 Screenshot 2023-02-05 alle 03 43 09
  1. When you run the default example
    https://github.com/ryanheise/audio_service/blob/master/audio_service/example/lib/example_multiple_handlers.dart
    and your android Auto shows a blank screen, then that could be a problem with the version of Android Auto you are using on your phone, try to update it or use an external downloaded android auto APK to test that. The default example should show media items out of the box. So based on that you could build your media items using the same architecture of Subscribing to BehaviourSubject of Media Library items.
  2. To remove the progress bar from the Now Playing template in an Android Auto media app, you can modify your media session metadata to exclude the media item's duration. The progress bar in the Now Playing template is based on the duration of the media item, so by excluding the duration, the progress bar will not be displayed.
  • You can also add a flag to your extras in your media item to remove the PorgressBar e.g

 

MediaItem(
                id: e['id'].toString(),
                title: e['title'],
                artist: e['title'],
                displayTitle: e['title'],
                displaySubtitle: e['description'],
                displayDescription: e['description'],
                artUri: Uri.parse(e['albumArtUrl']),
                duration:null,
                extras: {
                  'androidx.media.MediaItem.Extras.COMPLETION_PERCENTAGE': 0.4,
                  'url': e['playableUrl'],
                  'showId': int.parse(e['showId'].toString()),
                  'episodeId': e['id'].toString(),
                  'episodeDate': e['publishedAt'],
                  'shareableUrl': e['shareableUrl'],
                  'albumArtUrl': e['albumArtUrl'],
                  'startFromDuration': e['durationPlayed'],
                  'isLiveStream': false,
                  'isLocalFile': Uri.parse(e['playableUrl']).scheme == '',
                  'isRadio': true,
                },
              )


Get the extras flags from here
https://developer.android.com/reference/androidx/media/utils/MediaConstants

@federico2390
Copy link

federico2390 commented Apr 21, 2023

Thanks @bensalcie, now the android auto app shows the list of my songs but how can I hide the time and the seek bar inside the "Now Playing" screen? Your previous example disable only the dot on the seek bar but don't hide it and the time too

@federico2390
Copy link

Hello do u know why if I connect the smartphone to the car radio, if I don't open the app before and open directly the android auto, this show me an empty list and then when I open the app stuck on launch screen?

@bensalcie
Copy link
Author

Hello do u know why if I connect the smartphone to the car radio, if I don't open the app before and open directly the android auto, this show me an empty list and then when I open the app stuck on launch screen?

@federico2390 Make sure you architecture your Audio Service correctly so that your media items can be available to Android Auto even in the background. The cause could be that your media items depend on app performing some tasks on foreground before going to background. The example on Multiple Handlers can fetch the media items even when the app was closed and no longer running.

@federico2390
Copy link

@bensalcie Hi solved but remaining the problem that if I open first android auto inside the car and later I want to open the app, this stuck on launch screen. I'have read some your issue on git now but I haven't found solutions. Do u know have can I solve the problem? I have followed the readme file of the plug-in but I use mainactivity.it and not mainactivity.Java. Do u think that this is not the problem? I don't know how can do

@bensalcie
Copy link
Author

@bensalcie Hi solved but remaining the problem that if I open first android auto inside the car and later I want to open the app, this stuck on launch screen. I'have read some your issue on git now but I haven't found solutions. Do u know have can I solve the problem? I have followed the readme file of the plug-in but I use mainactivity.it and not mainactivity.Java. Do u think that this is not the problem? I don't know how can do

@federico2390 I think the problem is with the theme style used on Android (Portrait and Landscape restrictions). (Especially if you're using the native splash, you gotta check on that.)

@ahmedtanjim
Copy link

@bensalcie If i don't use audio handler will my audio be controllable from android auto?

@bensalcie
Copy link
Author

@bensalcie If i don't use audio handler will my audio be controllable from android auto?

Yes, if you decide to use an Alternative Audio Management , still will work. As long as you conform to media architecture and the framework will assist you.

https://developer.android.com/guide/topics/media/legacy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants