Search code examples
alsapulseaudio

Watch for volume changes in ALSA/Pulseaudio


How do you listen to changes in volume on the Master channel on the default sound card? I'd like to be notified through dbus or a callback or something that the volume has changed.

I have tried looking and the ALSA and PulseAudio APIs and they only seem to allow you to set and get the volume, but not listen for changes in the volume.

Any programming language is fine.


Solution

  • This is possible with the ALSA API.

    When you have a control device, call snd_ctl_subscribe_events() to enable events. Then use snd_ctl_read() to read events; to wait for them, use blocking mode or poll(). If the event is of type SND_CTL_EVENT_ELEM, and if its event bit mask contains SND_CTL_EVENT_MASK_VALUE, that element's value has changed.

    See the implementation of amixer monitor for an example.