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?
Asked
Active
Viewed 4,128 times
1 Answers
16
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
- 17,552
pLumo
- 28,011
- 2
- 65
- 97
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:15PlaybackStatus, 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 09 '20 at 11:29PlaybackStatus? How can I execute it? – Heisenberg Dec 09 '20 at 11:32