xenolightning / AudioSwitcher

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

"System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll #40

Open EverBS2014 opened 6 years ago

EverBS2014 commented 6 years ago

I'm using VS2017 on Windows 10. My WPF application is using the lastest version of this API and has a Realtek onboard sound card

I want to set the volume of the windows setting to max like that:

 CoreAudioDevice defaultCaptureDevice = new CoreAudioController().DefaultCaptureDevice;
 defaultCaptureDevice.Volume = 100;

So far it's working but after the first call my CPU usage is increasing and I constantely receive this message in the VS2017 output window:

"System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.NullReferenceException" in AudioSwitcher.AudioApi.CoreAudio.dll ....

It's not stopping. What is wrong?

I'm using AudioSwitcher.AudioApi.CoreAudio. 3.0.0.1 And I have to problem with Win 7 and Win 10

My WPF application has as target framework .NET 4.5.2

EverBS2014 commented 6 years ago

In the other link you suggested to try the version 4, how can I do that? In NuGet in the VS2017 I see only the version 3.0.0.1

xenolightning commented 6 years ago

You can include pre-release versions, there's a checkbox at the top of the nuget window in VS.

https://www.nuget.org/packages/AudioSwitcher.AudioApi.CoreAudio/4.0.0-alpha5

You can see all the versions down the bottom.

EverBS2014 commented 6 years ago

Thank you for the link. I still get a few the exception messages when I set the Mic or Speaker to 100 but it's not an endless loop anymore

xenolightning commented 6 years ago

The same exception messages?

EverBS2014 commented 6 years ago

Yes I get this message a few times at the same time:

"System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll "System.ArgumentNullException" in AudioSwitcher.AudioApi.CoreAudio.dll

but then it stopps at least and is not printed in an endless loop. Unfortunately my catch (Exception ex) isn't working so I don't see any further informations. I don't know why this is so

EverBS2014 commented 6 years ago

When I call this:

CoreAudioController Controller = new CoreAudioController();

I get that message 13 times.

this gives me an IOException "System.IO.IOException" in System.dll defaultCaptureDevice = Controller.DefaultCaptureDevice;

but nevertheless this seems to work defaultCaptureDevice.SetVolumeAsync(100);

Is it possible to loop through a list of all available speakers and change volume only for a special one? The same for the mirophones? I only want to change the volume of the extermal Mic & Speaker (both USB devices) and not the system devices

xenolightning commented 6 years ago

Might be something to do with the realtek device. I can't replicate it. As long as it doesn't break anything I'm not too worried about it, the exceptions are probably being caught, and handled for a reason.

You can try debugging the api project if you want to get more information.

var playback = await Controller.GetPlaybackDevicesAsync(DeviceState.Active);
var specialDevices = playback.Where(x => x.Name.Contains("Special"));
MorGuux commented 5 years ago

I just debugged the error as I too have multiple ArgumentNullExceptions on startup, with the pre-release version.

The error thrown is in the CoreAudioDevice.Internal.cs Line 115 _audioMeterInformationPtr = Marshal.GetIUnknownForObject(result); System.ArgumentNullException: 'Value cannot be null. Parameter name: o'

System.ArgumentNullException HResult=0x80004003 Message=Value cannot be null. Parameter name: o Source=mscorlib StackTrace: at System.Runtime.InteropServices.Marshal.GetIUnknownForObjectNative(Object o, Boolean onlyInContext) at AudioSwitcher.AudioApi.CoreAudio.CoreAudioDevice.LoadAudioMeterInformation() in C:\Users\morga\Downloads\AudioSwitcher-master\AudioSwitcher.AudioApi.CoreAudio\CoreAudioDevice.Internal.cs:line 115

xenolightning commented 5 years ago

@MorGuux Are you able to share the audio devices you are running in your system? And the version of Windows?

xenolightning commented 5 years ago

I wonder if the device doesn't have audio meter support, in which case it's not returning anything from the call to Activate.

And to double check, these exceptions are being handled internally? They aren't an unhandled exception outside of the Api?

MorGuux commented 5 years ago

Windows 10 Pro Version 10.0.16299 Logitech G430 Headset Oculus Rift NVIDIA High Definition Audio (TV through HDMI) Realktek High Definition Audio (Speakers)

MorGuux commented 5 years ago

The exception is handled with a try catch, I just allowed VS to break on ArgumentNullExceptions

xenolightning commented 5 years ago

Are there any disabled devices too? I wonder if it's trying to call that on a disabled/disconnected device and then throwing an exception.

There's definitely a nicer way to handle this without throwing an exception too

MorGuux commented 5 years ago

Yeah, one capture device, Stereo Mix. I've not tried with that enabled.

MorGuux commented 5 years ago

There's plenty of disconnected devices, that'll never be connected again ;)

xenolightning commented 5 years ago

I wonder if that's what is causing it. It would make sense if it is.

The solution would be to null check the COM Activate() call. I'll push up a commit if you'd like to test it

MorGuux commented 5 years ago

Yeah no problems

JoshWobbles commented 5 years ago

I too am getting an endless loop of these messages. Is there a fix yet?

MorGuux commented 5 years ago

Try updating to the latest pre-release version instead of the stable version. It doesn't have the endless loop bug, just a few on startup.

JoshWobbles commented 5 years ago

Yup that fixes the loop bug. It doesn't seem to be specific to hardware persay but more likely driver as I never had this bug when developing my app but I recently did a fresh format and am starting fresh and now have it happen on the same hardware, all that I can imagine is different is some different drivers.

Jagreaper commented 5 years ago

Hi Guys,

All of a sudden today I starting getting this issue when building new binaries. My binaries built from the exact same commit prior to encountering this problem work fine. Has anyone figured out what is causing the issue?

jesusvallejo commented 4 years ago

I've got the same issue from time to time, strangely CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; throws the exception but after doing this CoreAudioDevice defaultCaptureDevice = new CoreAudioController().DefaultCaptureDevice; defaultCaptureDevice.Volume = 100; it worked again like it restarted something no clue why mb this info helps to identify why this occurs