spotifyd
spotifyd copied to clipboard
run this before entering active state
Hi ... I use AirPlay using shairport-sync which has this function in .config
run_this_before_entering_active_state = “home/jlp/script/amp_on.py”;
run_this_after_exiting_active_state = “home/jlp/script/amp_off.py”;
active_state_timeout = 300.0;
which then runs this amp_on.py
#!/bin/python3
from gpiozero import OutputDevice
import sys
import os
os.system("pkill amp_off.py")
AMP_TRIGGER_PIN = 21
amp = OutputDevice(AMP_TRIGGER_PIN, active_high=False, initial_value=False)
amp.on()
sys.exit()
and amp_off.py
#!/bin/python3
from gpiozero import OutputDevice
from signal import pause
AMP_TRIGGER_PIN = 21
amp = OutputDevice(AMP_TRIGGER_PIN, active_high=False, initial_value=False)
amp.off()
pause()
this allows me to turn on/off my amplifier when I play/stop something through shairport-sync. Is something similar possible with spotifyd?
You could put a script up for on_song_change_hook and use the DBUS/MPRIS interface to check the current playback state each time the script is called. Depending on that state you can turn on/off your amp.
You could put a script up for on_song_change_hook and use the DBUS/MPRIS interface to check the current playback state each time the script is called. Depending on that state you can turn on/off your amp.
Thanks for reply. It seems to be beyond my ability. Could you perhaps guide me in the direction of some examples?
Expanding upon what @Exceen said: You don't necessarily need to query MPRIS for the playback state, spotifyd provides most of the relevant information already in environment variables, whenever the on_song_change_hook is called.
So you could write something like the following and tell spotifyd via the on_song_change_hook config parameter about the script:
#!/bin/bash
case "$PLAYER_EVENT" in
play)
echo "Turning AMP on"
/home/jlp/script/amp_on.py ;;
pause)
echo "Turning AMP off"
/home/jlp/script/amp_off.py ;;
esac
You can have a look at this code, which is responsible for setting the environment variables, to learn which other variables you can use / events you can listen for.
I have not tested the script above and I'm especially not sure, wether play and pause are the events that you'd want to use in your case, but it should give you the idea. There is also this page which contains some usage examples for the hook.
If you have further questions, feel free to ping me, and I'll reopen this issue.