4

I want to get a list of all media playing. Somewhat like the notification bar shows you. Is there a command to do the same?

enter image description here

4

That feature s implemented with MPRIS (Media Player Remote Interfacing Specification), a standard D-Bus interface.

You can control it with DBUS commands manually, but I find it "a little bit" complicated for every day use:

# Get current Status
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
  /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
  string:'org.mpris.MediaPlayer2.Player' \
  string:'PlaybackStatus'

# Get Metadata of currently playing song (if Playing)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
  /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
  string:'org.mpris.MediaPlayer2.Player' \
  string:'Metadata'

(spotify is the player, change that accordingly)

or simply use playerctl:

playerctl status
playerctl metadata

Install with apt:

sudo apt install playerctl

You can also control players with python's dbus module:

#!/usr/bin/env python3
import dbus
bus = dbus.SessionBus()
for service in bus.list_names():
    if service.startswith('org.mpris.MediaPlayer2.'):
        player = dbus.SessionBus().get_object(service, '/org/mpris/MediaPlayer2')

        status=player.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus', dbus_interface='org.freedesktop.DBus.Properties')
        print(status)

        metadata = player.Get('org.mpris.MediaPlayer2.Player', 'Metadata', dbus_interface='org.freedesktop.DBus.Properties')
        print(metadata)
4
  • Thanks. Can you help me a bit more? This was not in the question but its what I'm trying to achieve. Say, you have 2 audio playing. And then you pause the 1st audio. Now only the 2nd audio is playing. So, when you press F5 (to pause music) the 2nd audio should gets paused, right? Well, it doesn't. The first audio then plays with the 2nd audio. So, I want the audio which was last played to be play/pause by the F5 key, but this isn't the default gnome behavior. Do you think you can help me? – Heisenberg Dec 9 '20 at 11:15
  • Pause the playing only is easy, as you can check PlaybackStatus, which will return Playing. But not sure if it's possible to know which player was playing last (the Interface declaration does not have yield like that). You may track the Status with a never-ending loop or so, but it seems not very nice to me. – pLumo Dec 9 '20 at 11:29
  • Thanks.Also, What's PlaybackStatus? How can I execute it? – Heisenberg Dec 9 '20 at 11:32
  • See my updated answer – pLumo Dec 9 '20 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.