The Linux version of Spotify provides a way to control the music using the DBus org.mpris.MediaPlayer2.spotify service. This lets you skip tracks, play, and pause the music. It doesn't let you change the volume though.
If you are using PulseAudio or PipeWire then you can use the pactl
command to identify the Sink Input number of Spotify and modify the volume that way. The following shell script will check each Sink Input number, and if the application associated with it is Spotify then it will change the volume or toggle mute depending on the command line arguments.
#!/bin/bash
# check command line arguments first
if [ $# != 1 ]; then
echo "Usage ${0} up|down|mute"
exit 1
fi
if [ $1 != "up" -a $1 != "down" -a $1 != "mute" ]; then
echo "Usage ${0} up|down|mute"
exit 1
fi
# go through each line of the sink-inputs output
pactl list sink-inputs | while read line; do
# Update current sink input id
[[ $line =~ ^Sink\ Input\ \#([[:digit:]]+) ]] && this_input=${BASH_REMATCH[1]}
# check if it is spotify
if [[ $line =~ "application.name = \"spotify\"" ]]; then
echo "Spotify is sink-input ${this_input}"
case $1 in
up)
pactl set-sink-input-volume ${this_input} +5%
;;
down)
pactl set-sink-input-volume ${this_input} -5%
;;
mute)
pactl set-sink-input-mute ${this_input} toggle
;;
*)
echo "Error"
;;
esac
fi
done
This script can then be bound to the keyboard shortcuts that are triggered when you press your volume buttons. In Openbox I used the keybinds XF86AudioMute, XF86AudioRaiseVolume, and XF86AudioLowerVolume and bound them to the mute, up, and down commands.