8

我在 [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>

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

    <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
    <category android:name="android.intent.category.DEFAULT" />

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

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

4

2 回答 2

8

只需在 Activity 声明中声明一个intent-filterfor MEDIA_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>

就是意图的处理方式:

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

一个警告:您需要使用意图过滤器发布您的应用程序并等待几天才能将其标记为“Play [x] on [y] ”类型的查询并为其编制索引。它不是瞬时的。

于 2015-08-12T21:55:32.490 回答
2

正如mangini 的回答所暗示的,我通过仅将意图过滤器添加到我的主要活动中来实现这一点:

<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>

我不必添加任何其他代码。androidx.media只是神奇地工作。当我的应用在 Android Auto 前台运行时说“播放 X”时,我的MediaSessionCompat.Callback子类收到了onPlayFromSearch回调,当我点击Search ResultsAndroid 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>
于 2020-10-07T18:44:58.297 回答