如何让Spotify和其他Android音乐应用从意图中搜索和播放?
我想创建一个 MEDIA_PLAY_FROM_SEARCH
(或其他)意图,可以在任何主要的Android音乐应用中搜索并播放歌曲.我希望以下命令行可以在多个应用程序上运行:
I would like to create a MEDIA_PLAY_FROM_SEARCH
(or other) intent that will search for and play a song in any major Android music app. I expected the following command line to work on multiple apps:
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音乐,它将播放Yellow Submarine.如果我选择Spotify,即使我有Spotify高级订阅,它也会搜索但不会播放. YouTube音乐也只进行搜索.
This launches a chooser on my Pixel 2 listing different apps that can handle the request. If I select Google Play Music, it plays Yellow Submarine. If I select Spotify, it does a search but does not play, even though I have a Spotify premium subscription. YouTube Music also does only a search.
我故意不指定查询是艺术家还是歌曲(如本例所示),因为我想将决定权留给音乐应用.
I purposely don't specify whether the query is an artist or song (or both, as in this example), since I want to leave the determination to the music app.
如果我删除MediaStore.EXTRA_MEDIA_FOCUS
extra和标志,则行为是相同的(在Google Play音乐和Spotify中):
The behavior is the same (in Google Play Music and Spotify) if I remove the MediaStore.EXTRA_MEDIA_FOCUS
extra and the flag:
$ adb shell am start -a "android.media.action.MEDIA_PLAY_FROM_SEARCH" -e query "yellow\ submarine\ by\ the\ beatles"
要在任何主要音乐应用程序(即Google Assistant支持的音乐应用程序)中播放歌曲,我需要什么?
What do I need in my intent to make it play songs in any major music app (i.e., those supported by Google Assistant)?
我怀疑这是由于特定的Apps如何处理焦点所致. 您可以尝试以下吗?
I suspect that it is due to how the specific Apps are handling the focus. Could you try the following?
adb shell am start -a "android.media.action.MEDIA_PLAY_FROM_SEARCH" -e android.intent.extra.focus "vnd.android.cursor.item/playlist" -e query "yellow\ submarine\ by\ the\ beatles" -f 268435456
如果不成功...
adb shell am start -a "android.media.action.MEDIA_PLAY_FROM_SEARCH" -e android.intent.extra.focus "vnd.android.cursor.item/audio" -e query "yellow\ submarine\ by\ the\ beatles" -f 268435456
使用"vnd.android.cursor.item/*" 应该根据明智的选择播放一些音乐,但是它是非结构化的,因此Apps应该在可能的情况下使用更具体的搜索模式,例如播放列表/音频
Using "vnd.android.cursor.item/*" should play some music based on a smart choice, but it is unstructured and Apps should use more specific search modes when possible, e.g. playlist/audio