8

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

Heisenberg
  • 1,556
  • 3
  • 15
  • 33

1 Answers1

12

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

D-Bus with dbus-send

You can control it with DBUS commands manually, but I find it "a little bit" complicated for everyday 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)

playerctl

Or simply use playerctl:

playerctl status
playerctl metadata

Install with apt:

sudo apt install playerctl

Python's D-Bus module

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)
Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
pLumo
  • 26,204
  • 2
  • 57
  • 87
  • 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 09 '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](https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:PlaybackStatus) 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 09 '20 at 11:29
  • Thanks.Also, What's `PlaybackStatus`? How can I execute it? – Heisenberg Dec 09 '20 at 11:32
  • See my updated answer – pLumo Dec 09 '20 at 11:32