How to get Spotify and other Android music apps to search and play from an intent?

送分小仙女□ 提交于 2020-03-01 18:28:18

问题


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

This corresponds to the code:

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

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.

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"

What do I need in my intent to make it play songs in any major music app (i.e., those supported by Google Assistant)?


回答1:


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

and if that is unsuccessful...

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

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



来源:https://stackoverflow.com/questions/60197217/how-to-get-spotify-and-other-android-music-apps-to-search-and-play-from-an-inten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!