Android Auto - 语音 - 无法执行“在 [y] 上播放 [x]”

jul*_*cer 7 android android-auto

我在 [app] 上播放 [song] 命令时遇到问题;特别是 Android Auto 无法识别“应用程序”。

我收到一条语音语音消息:“不知道如何帮助在应用程序上播放歌曲”

所以语音识别工作得很好(就像我说的歌曲和应用程序一样),但是与应用程序的匹配不起作用或者 Auto 并不明显我的应用程序可以处理这个问题。

在模拟器中,搜索从搜索菜单选项开始,所以我假设我的意图过滤器是正确的。

我的清单有以下内容:

    <!-- - - - - - - - - - - - - - - - - - - - - -->
    <!-- Auto -->
    <!-- - - - - - - - - - - - - - - - - - - - - -->
    <meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc"/>
    <meta-data android:name="com.google.android.gms.car.notification.SmallIcon" android:resource="@drawable/brand_icon_white" />
    <service android:name=".library.service.auto.MyMediaBrowserService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService"/>
        </intent-filter>
    </service>
Run Code Online (Sandbox Code Playgroud)

我曾尝试将以下内容放入我的服务声明或YouTube DevByte 中所示的活动中,但似乎都不起作用:

    <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
    <category android:name="android.intent.category.DEFAULT" />
Run Code Online (Sandbox Code Playgroud)

我希望它在服务中,因为没有与服务相关的活动。我想确定这应该在哪里。

我可能错过了一些东西或误解了一些东西。一个问题可能是应用程序的名称中包含一个数字(例如 1up)。

man*_*ini 8

只需在活动声明中intent-filterMEDIA_PLAY_FROM_SEARCH声明一个。Android Auto 不强制实际处理意图,因为 Android Auto 将调用MediaSession.Callback.onPlayFromSearch. 清单中的声明用于将您的应用标记为可用于响应媒体语音命令。但是,您可能希望正确处理它,因为其他非 Auto 环境(例如 Google Now)将通过该意图提交语音搜索。

处理意图的最佳方法是调用MediaController.TransportControls.playFromSearch,因此无论语音搜索如何触发,您都可以以一致的方式处理它。

请参阅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>
Run Code Online (Sandbox Code Playgroud)

这是怎样的意图进行处理:

@Override
protected void onMediaControllerConnected() {
    if (mVoiceSearchParams != null) {
        String query = mVoiceSearchParams.getString(SearchManager.QUERY);
        getMediaController().getTransportControls().playFromSearch(query, mVoiceSearchParams);
        mVoiceSearchParams = null;
    }
    getBrowseFragment().onConnected();
}
Run Code Online (Sandbox Code Playgroud)

一个警告:您需要使用意图过滤器发布您的应用程序并等待几天,以便为“Play [x] on [y] ”类型的查询进行标记和索引。它不是瞬间的。


Hea*_*ers 5

正如曼吉尼的回答所暗示的那样,我通过仅将意图过滤器添加到我的主要活动中来实现此目的:

<activity
  android:name=".LandingActivity"
  android:label="@string/app_name"
  android:noHistory="true"
  android:launchMode="singleTop"
  android:theme="@style/SplashTheme">

  <!-- for the launcher -->          
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.DEFAULT"/>
  </intent-filter>

  <!-- for media search (Play X in Y) -->
  <intent-filter>
    <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

我不必添加任何其他代码。androidx.media简直神奇地起作用了。当我的应用程序在 Android Auto 前台运行时说“Play X”时,我的MediaSessionCompat.Callback子类收到onPlayFromSearch回调,当我Search Results在 Android Auto 中点击时,我的MediaBrowserServiceCompat子类收到onSearch回调。

我还尝试将其添加intent-filter到我的service声明中AndroidManifest.xml,但这不起作用:

<service
  android:name=".BackgroundAudioNotificationService"
  android:exported="true">
  <intent-filter>
    <action android:name="android.media.browse.MediaBrowserService" />
    <action android:name="android.intent.action.MEDIA_BUTTON" />
    <!-- Tried to do "Play X" while running in Android Auto, and this didn't work -->
    <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
  </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)