My keyboard media keys have no control on players, so let fix that by adding custom shortcuts in KDE (need the KHotKeys package installed on the system).
With the help of package playerctl it is simple to make the media keys work. In KDE custom shortcuts, simply create all the global shortcuts for command/URL for the following actions Play/Pause, Stop, Next, Previous, and assign the corresponding media keys. Then set the command for each actions:
Play/Pause : playerctl play-pause
Stop : playerctl stop
Next : playerctl next
Previous: playerctl previous
As Spotify will run multiple instances every time it is called, I'm using a script to check if it is started, and depending on the result it starts Spotify, or gives focus to its window:
#!/bin/bash
# spotify_instance_checker.sh
# https://github.com/dahlo/spotify_instance_checker
# custom shortcut command to open spotify properly if already started or not (media key)
# need also playerctl command to set all shortcuts in KDE for media keys, this script is only to start spotify
# check if spotify is already running
if pgrep -x "spotify" > /dev/null
then
# if it is, switch focus to it (changed path to /opt/spotify/spotify)
wmctrl -ia $(wmctrl -lp | awk -vpid=$(ps ax | grep "/opt/spotify/spotify$" | grep -v grep | awk '{print $1}') '$3==pid {print $1; exit}')
else
# if not, start it (changed to start spotify adblock)
LD_PRELOAD=/usr/lib/spotify-adblock.so spotify &
fi
I just need to create a new global shortcut to call this script for the desired media key (I use the Tools key which on my keyboard represents a circled play button), which will result in starting or giving focus to Spotify:
Spotify : ~/Scripts/spotify_instance_checker.sh
While I was at it (I actually started by that.. but I tell my story as I want..), I replaced the CTRL+ESCAPE shortcut which was starting KSysGuard (I disabled it), by a custom shortcut to start the new Plasma System Monitor:
Plasma System Monitor : plasma-systemmonitor
And cherry on the cake, I fixed the issue with Spotify muting itself when a system sound pops. I commented the line loading the cork module in Pulse Audio config file /etc/pulse/default.pa:
### Cork music/video streams when a phone stream is active
#disabled so system sound doesn't auto mute Spotify (when lowering the volume the 'pop' sound auto mutes Spotify for example)
#load-module module-role-cork
Alternatively, simply running this command fixes the issue for the current session without needing to reboot:
pactl unload-module module-role-cork