【发布时间】:2020-05-28 12:50:15
【问题描述】:
我想创建一个MEDIA_PLAY_FROM_SEARCH(或其他)intent,用于在任何主要的 Android 音乐应用中搜索和播放歌曲。我希望以下命令行适用于多个应用程序:
adb shell am start -a "android.media.action.MEDIA_PLAY_FROM_SEARCH" -e android.intent.extra.focus "vnd.android.cursor.item/*" -e query "yellow\ submarine\ by\ the\ beatles" -f 268435456
这个对应的代码:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "vnd.android.cursor.item/*");
intent.putExtra(SearchManager.QUERY, "yellow submarine by the beatles");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这会在我的 Pixel 2 上启动一个选择器,列出可以处理请求的不同应用。如果我选择 Google Play Music,它会播放 Yellow Submarine。如果我选择 Spotify,它会搜索但不播放,即使我订阅了 Spotify 高级版。 YouTube Music 也只进行搜索。
我故意不指定查询是艺术家还是歌曲(或两者,如本例所示),因为我想将决定权留给音乐应用。
如果我删除 MediaStore.EXTRA_MEDIA_FOCUS 额外和标志,行为是相同的(在 Google Play 音乐和 Spotify 中):
$ adb shell am start -a "android.media.action.MEDIA_PLAY_FROM_SEARCH" -e query "yellow\ submarine\ by\ the\ beatles"
我需要什么才能让它在任何主要音乐应用(即 Google 助理支持的应用)中播放歌曲?
【问题讨论】: