xenolightning / AudioSwitcher

.NET Library which facilitates interacting with Audio Devices on Windows
Microsoft Public License
248 stars 53 forks source link

avoid unnecessarily triggering STA COM thread check #63

Closed pengowray closed 1 year ago

pengowray commented 1 year ago

This is another tiny patch. it's also a little lazy: I haven't got the library to compile yet so haven't checked if it 100% fixes the problem.

This time it's for the most recent release. (I went and updated my code to 4.0-alpha)

For background, here's my client code which I'm having trouble with:

AudioSession.StateChanged.Subscribe(this); // OK
AudioSession.MuteChanged.Subscribe(this); // OK
AudioSession.VolumeChanged.Subscribe(this); // OK
AudioSession.Disconnected.Subscribe(this); // OK
AudioSession.PeakValueChanged.Subscribe(this); // ❌ InvalidThreadException: This operation must be run on a STA COM Thread

It's not the .Subscribe(this) that fails, but the CoreAudioSession.PeakValueChanged getter.

During PeakValueChanged's getter routine, there's a null check for a variable (MeterInformation), and during MeterInformation's getter there's a ComThread.Assert(); which fails.

This patch simply bypasses the getter (MeterInformation -> _meterInformation). I'm assuming a STA thread isn't needed here.

I was going to try compiling it first, but I couldn't get that set up immediately, and might not get a chance to do that for a few days so thought I'd post a PR now while it's still fresh in my head.

Edit: Had another go and got it compiling and got it going, and the problem it's meant to fix has indeed gone away with this patch.

pengowray commented 1 year ago

I've been using this AudioSwitcher patch since soon after this post and it's been working great with no problems for my project (Streamling Overlay). It's a tiny fix but it's required to make the library usable for me. Would be great if it could be incorporated to make a 4.0.0-alpha6 (or as a release version, whatever).

Cheers.

xenolightning commented 1 year ago

@pengowray - for some reason I thought I'd done this.

I'll merge and drop another version out there, probably a full version rather than an alpha too.

I've been working on a fully ".net core" version of this library, if I get enough time to sit down to get it at better state I'll tag you there too! :D

amadeo-alex commented 6 months ago

Hello, I've been trying to use this library as a replacement to "NAudio" and "CoreAudio" as this one has much better approach to memory handling and does not slowly leak resources. However I've stumbled on the same issue as described here, the versioning and update dates of nuget packages are a bit confusing, has this fix been incorporated into any nuget releases?

pengowray commented 6 months ago

Hi @amadeo-alex , this was merged into the main AudioSwitcher but I don't think it made it into a nuget release. Might need to give @xenolightning a little nudge?