【问题标题】:How to get Spotify and other Android music apps to search and play from an intent?如何让 Spotify 和其他 Android 音乐应用根据意图进行搜索和播放?
【发布时间】: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 助理支持的应用)中播放歌曲?

【问题讨论】:

    标签: android-intent spotify


    【解决方案1】:

    我怀疑这是由于特定应用程序处理焦点的方式。 您可以尝试以下方法吗?

    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/*”应该根据明智的选择播放一些音乐,但它是非结构化的,应用程序应尽可能使用更具体的搜索模式,例如播放列表/音频

    【讨论】:

    • 嗯 - 可能是应用程序版本中的错误......你是最新的吗? (对不起,如果这很明显并尝试过)
    • 我没有最新版本,但更新(正如我应该在发布之前所做的那样)没有任何区别。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多