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

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101

1 Answers1

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

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Hmm - could possibly a bug in the App versions...are you up-to date? (sorry if that is obvious and tried) – Fraser Feb 21 '20 at 22:30
  • 1
    I did not have the latest version, but updating (as I should have done before posting) made no difference. Thanks anyway. – Ellen Spertus Feb 22 '20 at 00:27