2

I working with C# script to control android device media. I have been able to check and set volume as well as send commands to pause, play, skip, etc for media apps like Spotify or YouTube

One thing I have not been able to figure out however is how to get the current time of the media. For example, if Spotify is playing a two-minute song, and it is currently at 1:16, I'd like to get that information. So far, I have not been able to find documentation on how to do this.

Is there a way to get the current time of media that's playing. In addition, is there a way to get the total time of the media?

Thank you

1 Answer 1

3

Issue this command:

adb shell dumpsys media_session | grep "state=PlaybackState"      # assuming that only one media player or alike app is playing audio, such as Spotify.

Demo output:

state=PlaybackState {state=2, position=44912, buffered position=0, speed=0.0, updated=14800717, actions=142132, custom actions=[], active item id=-1, error=null}

The highlighted position shows the position of the playing media in milliseconds.

Issues:

1) The position does not reflect real-time media position, and it gets updated in these conditions (based on my observation) only:

  • when the media is played from fresh or from paused state
  • when the media is paused
  • when the media's position is changed manually by the user (such as, by dragging the position slider)

2) There is no maximum media position available, so this is all you get.


You can try briefly pausing and then playing the media using adb command to update the position to reflect that in the script. It would be disruptive, but would work.

Tip: you can get meta information, mostly the description, of the playing media from the output of adb shell dumpsys media_session too.

1
  • The expected way to reliably track the media position to watch the state and the clock. When the state transitions from PAUSED (2) to PLAYING (3), capture the position and increment it by 1 second every second. When it transitions to PAUSED, resynchronize your position.
    – Stefan
    Sep 17, 2020 at 23:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .