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

1

3

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

Isagogics answered 12/2, 2020 at 21:34 Comment(0)
S
0

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

Spalla answered 21/2, 2020 at 17:32 Comment(2)
Hmm - could possibly a bug in the App versions...are you up-to date? (sorry if that is obvious and tried)Spalla
I did not have the latest version, but updating (as I should have done before posting) made no difference. Thanks anyway.Isagogics

© 2022 - 2024 — McMap. All rights reserved.