Search code examples
androidandroid-mediaplayerandroid-auto

How to detect if Android Auto is starting, so I can set the initial PlaybackState?


According to the Android app quality for cars guideline MA-1:

Media apps must not autoplay on startup or without user initiated action to select the app or app media..

The guidelines suggest reading Implement play commands which refers to instructions to Set initial PlaybackState. This states:

Your app should not automatically start playing music when Android Automotive OS or Android Auto connect to your media browser service.

This seems clear - when starting Android Auto, music should not auto play.

However, I can't see an obvious way that you can detect when Android Auto starts and connects to the app.

My media app has functionality that auto plays when you use it on a mobile phone and it was last left in a "playing" state. So I need to disable this when Android Auto connects.

When Android Auto does connect, MediaSessionCompat.Callback:onPlay is invoked. However, the same callback to onPlay is made if the user clicks the "Play" icon. Obviously, if a user clicks the Play icon, the music should play.

So how do I know if the user has manually pressed play, or Android Auto has connected to the app?


Solution

  • You can do it in MediaBrowserCompat.ConnectionCallback(). You most likely have a media browser service set up for your Android Auto implementation. It has an onConnected() method specifically for this purpose, and you can use it to process any logic whenever your app initiates that connection