You are not logged in.

#1 2018-12-16 14:36:36

Registered: 2018-10-31
Posts: 47

[i3] Showing current progress in a playing track on Spotify

Hello all,   

So I have been trying to write a shell script with my shallow knowledge to control spotify and see what is currently playing on my i3blocks sidebar. I successfully binded some keys for play/pause-next-previous. And also wrote a python script using dbus library to get current track's information such as song name, artist album.

import dbus
import sys

session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus, "org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player","Metadata")
print(str(metadata['xesam:artist'][0]), '~',  metadata['xesam:title'])

Now I would like to display the progress of the current track. For instance 1:02 / 5:12.

But I have no idea how I can access it. Any ideas on how I can somehow print such information?


Be nice and live for others.


#2 2018-12-18 19:26:29

Registered: 2006-09-23
Posts: 360

Re: [i3] Showing current progress in a playing track on Spotify

It looks like the track length is available in the metadata - stored as microseconds - but the current position in a track is not available. There is a position attribute you can access but it is always 0. From some forum posts on, it seems like this hasn't worked in years.

For the length of the current track:

length = int(metadata['mpris:length'])
mins = length / 1000000 // 60
seconds = length / 1000000 % 60

For the position that is always 0:

position = spotify_properties.Get("org.mpris.MediaPlayer2.Player","Position")


#3 2018-12-19 01:23:09

Registered: 2012-02-12
Posts: 142

Re: [i3] Showing current progress in a playing track on Spotify

Here is how I get the current track position in a BASH script for i3-status.  Saved in a variable called "time" which I then use to display.  I assume i3-blocks would or could use something similar.

time=$(dbus-send --print-reply=literal --session --dest=org.mpris.MediaPlayer2.mbmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.getPositionInSeconds)

Substitute "Spotify" for "mbmp" in the  command.  MBMP is a homegrown thing, but does have an mpris interface.


Board footer

Powered by FluxBB