[pulseaudio-discuss] execute commands on suspend/resume?
Sean Greenslade
sean at seangreenslade.com
Sat Sep 4 20:13:23 UTC 2021
On Tue, Jun 08, 2021 at 08:17:00PM -0700, Peter Meilstrup wrote:
> Hello pulseaudio list,
>
> I am wondering if it is possible to have a command be executed when a
> sink is suspended or resumed (via module-suspend-on-idle).
> Specifically I want to switch my amplifier on and off automatically
> via home automation. Is this supported in pulseaudio?
I realize this is a reply to a pretty old message, but in case you or
others are still wondering about this:
It's pretty straightforward to do this with a custom pulseaudio client
application. I took a quick stab at it with the "pulsectl" python
library. Example script attached, just modify the sink.name line to
match your target device.
--Sean
-------------- next part --------------
#!/usr/bin/python3
import pulsectl
with pulsectl.Pulse('event-printer') as pulse:
sink_idx = None
sink_was_suspended = False
# Get the desired sink.
for sink in pulse.sink_list():
if sink.name == "alsa_output.usb-Focusrite_Scarlett_Solo_USB-00.analog-stereo":
sink_idx = sink.index
sink_old_state = (sink.state == 'suspended')
def print_events(ev):
#print('Pulse event:', ev)
if sink_idx == ev.index:
raise pulsectl.PulseLoopStop # Break out of the event_listen to process the event in the main app.
pulse.event_mask_set('sink')
pulse.event_callback_set(print_events)
while True:
pulse.event_listen()
sink = pulse.sink_list()[sink_idx]
sink_new_state = (sink.state == 'suspended')
#print(sink_new_state, sink.state)
if sink_new_state != sink_old_state:
print("Sink has changed state")
if sink.state == 'suspended':
print("Now suspended.")
# <<< TAKE ACTION HERE >>>
elif sink.state == 'idle' or sink.state == 'running':
print("Now active.")
# <<< TAKE ACTION HERE >>>
sink_old_state = sink_new_state
More information about the pulseaudio-discuss
mailing list