Android TV integration - detect music playing in background

Using the Android TV (ADB) integration, I have (almost) full visibility into the media playing on my Chromecast with Google TV.

However, if playing Spotify (via the app or casting to it), it can play in the background while you are at the main menu or the screen saver. The current app shows either the menu or the screen saver, not spotify.

I have a Lovelace card that only shows media player controls if there is actively media playing. I also have automations set to automatically turn on a TV or the receiver if music is casted and the device is currently off. AndroidTV/ADB integration right now just shows the state is “Standby” and the app_name is “Google TV Launcher” (right now it is the screen saver), so detection is broken for me in this situation.

Based on this discussion How to know, which app is playing music in the background? - Android Enthusiasts Stack Exchange I was able to find an adb command that could tell me if there was media playing and it works correctly

With that help, I was able to come up with an adb command that gives me the status I need.

  1. I need help to know if the command can be improved any. I had to make some assumptions (and I hate making assumptions in scripts)
  2. Is this something that could be added to the integration so we can get proper status of spotify and other music services that can operate in the background?

My command below is what I use to get the application name. I have to assume that the output is always the same and that if state=3 is found that the line I want is 11 before

dumpsys media_session |grep -B 11 "state=3" | grep -E "Media button session is.*userId=0" | sed "s/.*Media button session is//g" |cut -d "/" -f 1 | sed "s/ //g"

here is sample output when my chromecast is on the screensaver but still playing Spotify

sabrina:/ $ dumpsys media_session
MEDIA SESSION SERVICE (dumpsys media_session)

8 sessions listeners.
Global priority session is com.android.server.telecom/HeadsetMediaButton (userId=0)
  HeadsetMediaButton com.android.server.telecom/HeadsetMediaButton (userId=0)
    ownerPid=3914, ownerUid=1000, userId=0
    package=com.android.server.telecom
    launchIntent=null
    mediaButtonReceiver=null
    active=false
    flags=65537
    rating type=0
    controllers: 0
    state=null
    audioAttrs=AudioAttributes: usage=USAGE_VOICE_COMMUNICATION content=CONTENT_TYPE_SPEECH flags=0x800 tags= bundle=null
    volumeType=1, controlType=2, max=0, current=0
    metadata: null
    queueTitle=null, size=0
User Records:
Record for full_user=0, profile_user=10
  Volume key long-press listener: null
  Volume key long-press listener package:
  Media key listener: null
  Media key listener package:
  OnMediaKeyEventDispatchedListener: added 0 listener(s)
  OnMediaKeyEventSessionChangedListener: added 1 listener(s)
    from com.android.bluetooth
  Last MediaButtonReceiver: MBR {pi=PendingIntent{9a9e1e5: PendingIntentRecord{c4724ba com.spotify.tv.android broadcastIntent}}, type=1}
  Media button session is com.spotify.tv.android/spotify-android-tv-media-session (userId=0)
  Sessions Stack - have 1 sessions:
    spotify-android-tv-media-session com.spotify.tv.android/spotify-android-tv-media-session (userId=0)
      ownerPid=18336, ownerUid=10068, userId=0
      package=com.spotify.tv.android
      launchIntent=null
      mediaButtonReceiver=MBR {pi=PendingIntent{9a9e1e5: PendingIntentRecord{c4724ba com.spotify.tv.android broadcastIntent}}, type=1}
      active=true
      flags=3
      rating type=0
      controllers: 7
      state=PlaybackState {state=3, position=2, buffered position=0, speed=1.0, updated=555390121, actions=2368383, custom actions=[], active item id=-1, error=null}
      audioAttrs=AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x800 tags= bundle=null
      volumeType=1, controlType=2, max=0, current=0
      metadata: size=8, description=Tomorrow, Silverchair, Frogstomp (Deluxe Edition) [Remastered]
      queueTitle=null, size=0
Audio playback (lastly played comes first)
  uid=10068 packages=com.spotify.tv.android
Media session config:
  media_button_receiver_fgs_allowlist_duration_ms: [cur: 10000, def: 10000]
  media_session_calback_fgs_allowlist_duration_ms: [cur: 10000, def: 10000]
  media_session_callback_fgs_while_in_use_temp_allow_duration_ms: [cur: 10000, def: 10000]

Have you tried to use the Google Cast integration?

1 Like

Thanks @FPro . I completely forgot. I have both of those integrations set up b/c they both provide what the other does not. I just forgot to check. It would be nice if I could just get everything from one device :confused:

Yes, the google cast integration shows the media playing information.

1 Like