1

EDIT: I removed and rewrote most of the post to make the question more direct and to make the post a lot shorter. You can of course view the edit history to get the previous version.

Using bspwm on Arch Linux. playerctl to control media, sxhkd to bind media keys to playerctl commands.

I'm trying to find a way to get the latest active media player so when I use the media keys to play/pause a song/movie/.. , my pc automatically controls the latest active mediaplayer.

For instance, when Spotify happens to be open in the background and I'm watching something on VLC, it knows to control VLC and not Spotify when I press media keys. Right now, if both are open, VLC always gets priority from playerctl.

What I need is a way to ask dbus which mediaplayer is currently playing a song, so I can keep it in a file.


EDIT: I found a way to ask each spotify and vlc using:

qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus

and

qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus

but I'd like to automatically ask all mediaplayers, not each one by name. I tried to do it with org.mpris.MediaPlayer2.* but that doesn't work.

Any ideas?

0

I did it this way:

This is a script that always runs to check which player is currently active/was last active, and stores that player in a file:

#!/bin/sh

while true ; do
    for m in `qdbus | egrep -i 'org.mpris.MediaPlayer2|plasma-browser-integration'` ; do
        if [[ `qdbus $m /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus` == 'Playing' ]]; then
            echo $m > ~/.config/activePlayer/currentPlaying.txt
        fi
    done
    sleep 1
done

and this is a script that sends dbus commands like play/pause/etc/ to the player that is currently active (based on that file). The script gets called by sxhkd which listens to my keypresses.

#!/bin/sh

#Get command
if [[ $1 == 'play-pause' ]]; then
    cmd='/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause'
elif [[ $1 == 'next' ]]; then
    cmd='/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next'
elif [[ $1 == 'previous' ]]; then
    cmd='/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous'
elif [[ $1 == 'stop' ]]; then
    cmd='/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop'
fi

#Send command to qdbus
if [[ `qdbus | egrep -i 'org.mpris.MediaPlayer2|plasma-browser-integration' | wc -l` -eq 1 ]]; then
    qdbus `qdbus | egrep -i 'org.mpris.MediaPlayer2|plasma-browser-integration'` $cmd
else
    qdbus `cat ~/.config/activePlayer/currentPlaying.txt` $cmd
fi

unset $cmd

Is this a good way to do this?

  • 1
    Is your setup using pulseaudio ? If so pacmd list-sink-inputs will print (among other stuff) something like application.process.binary = "totem" ... there's your currently playing app name... – don_crissti Nov 9 '18 at 18:54
  • For me it displays the currently opened apps (it shows spotify and firefox, while both are not playing anything), not the one playing. – zjeffer Nov 9 '18 at 19:07
  • 1
    Well, you should only look for the ones with state: RUNNING... – don_crissti Nov 9 '18 at 19:09
  • I see. But what would the difference be with what I'm doing now? It works perfectly so far... – zjeffer Nov 9 '18 at 19:20

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.