Android Auto - Voice - Cannot perform "Play [x] on [y]"
Simply declare an intent-filter
for MEDIA_PLAY_FROM_SEARCH in an Activity declaration. It is not mandatory for Android Auto to actually handle the intent, since Android Auto will call the MediaSession.Callback.onPlayFromSearch
. The declaration in the manifest serves to flag your app as available to respond media voice commands. However, you might want to handle it properly, because other non-Auto environments, like Google Now, will submit voice searches via that intent.
The best way to handle the intent is by calling MediaController.TransportControls.playFromSearch
, so you handle it in a consistent way no matter how the voice search was triggered.
See this snippet from uAmp AndroidManifest.xml:
<!-- Main activity for music browsing on phone -->
<activity
android:name=".ui.MusicPlayerActivity"
android:label="@string/app_name">
[...]
<!-- Use this intent filter to get voice searches, like "Play The Beatles" -->
<intent-filter>
<action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And this is how the intent is handled:
@Override
protected void onMediaControllerConnected() {
if (mVoiceSearchParams != null) {
String query = mVoiceSearchParams.getString(SearchManager.QUERY);
getMediaController().getTransportControls().playFromSearch(query, mVoiceSearchParams);
mVoiceSearchParams = null;
}
getBrowseFragment().onConnected();
}
One caveat: you need to publish your app with the intent-filter and wait a few days to get it flagged and indexed for the "Play [x] on [y]" type of query. It is not instantaneous.