Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioSessionEvents is receiving multiple events for a single real event after refreshing the audio sessions list. #99

Open
TJ-59 opened this issue Oct 8, 2024 · 0 comments

Comments

@TJ-59
Copy link

TJ-59 commented Oct 8, 2024

  • OS version : Windows 10 22H2
  • architecture : 64Bits
  • Python version : 3.11.8
  • pycaw version : 20240210

I'm using pycaw to watch sound volume changes, I have a AudioSessionCallback class that inherit pycaw's AudioSessionEvents, and is given to the session via register_notification().
The callback instance has a few attributes added, like the ID of the corresponding UI, the session instance identifier, AND a pseudo unique ID (generated at __init__) just to be able to differentiate "who" is receiving the events.
This was done after seeing multiple instances of the prints happening for a single event (like, "one single click in the windows audio mixer to change that session's volume" single event).
The sessions are obtained with AudioUtilities.GetAllSessions(), each is given to a custom UI panel instance, and depending on the process the session is from, may or may not have a callback created and registered to it.
The registration is done (in the UI panel code) with :

    self.ASCB = AudioSessionCallback(self, self.session.InstanceIdentifier)
    self.session.register_notification(self.ASCB)

This register_notification() , defined in picaw/utils.py 's AudioSession class, internally uses :

    def register_notification(self, callback):
        if self._callback is None:
            self._callback = callback
            self._ctl.RegisterAudioSessionNotification(self._callback)

Same for the unregister_notification(), done with this code :

    self.session.unregister_notification()
    del self.ASCB

which is defined just after in utils.py :

    def unregister_notification(self):
        if self._callback:
            self._ctl.UnregisterAudioSessionNotification(self._callback)

For various reasons, the user may need to refresh the sessions, when that happens, the callbacks are all removed using the code above, the ui panels are removed and destroyed, and then, the same process, used initially to get the sessions and give each of them a panel, is used again to obtain one panel per session.
But when an event happens, like a volume change, there are now multiple prints happening, all from events very close to each other temporally, but ALL are printed by the NEW callback for that session, proof being the pseudo unique ID it generated during its __init__ being the same for all events.

I'd have understood, if the pseudo unique IDs were different, that the event is still getting picked up by old callbacks that would somehow not be correctly unregistered, but those are all received by the current callback, like "something above it" was counting "how many copies of the single event they're supposed to send out", not caring who's concerned or receiving it.

Any idea how to avoid being spammed by duplicate events ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant