AudioSwitcher icon indicating copy to clipboard operation
AudioSwitcher copied to clipboard

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

Open EverBS2014 opened this issue 6 years ago • 22 comments

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 avatar Nov 29 '17 22:11 EverBS2014

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

EverBS2014 avatar Nov 29 '17 22:11 EverBS2014

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.

xenolightning avatar Nov 29 '17 23:11 xenolightning

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

EverBS2014 avatar Nov 30 '17 00:11 EverBS2014

The same exception messages?

xenolightning avatar Nov 30 '17 00:11 xenolightning

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 avatar Nov 30 '17 08:11 EverBS2014

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

EverBS2014 avatar Nov 30 '17 08:11 EverBS2014

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"));

xenolightning avatar Nov 30 '17 19:11 xenolightning

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

MorGuux avatar Oct 29 '18 12:10 MorGuux

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

xenolightning avatar Oct 29 '18 20:10 xenolightning

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?

xenolightning avatar Oct 29 '18 20:10 xenolightning

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 avatar Oct 29 '18 21:10 MorGuux

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

MorGuux avatar Oct 29 '18 21:10 MorGuux

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

xenolightning avatar Oct 29 '18 21:10 xenolightning

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

MorGuux avatar Oct 29 '18 21:10 MorGuux

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

MorGuux avatar Oct 29 '18 21:10 MorGuux

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

xenolightning avatar Oct 29 '18 21:10 xenolightning

Yeah no problems

MorGuux avatar Oct 29 '18 21:10 MorGuux

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

JoshWobbles avatar Dec 19 '18 15:12 JoshWobbles

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.

MorGuux avatar Dec 19 '18 16:12 MorGuux

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.

JoshWobbles avatar Dec 19 '18 16:12 JoshWobbles

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?

Jagreaper avatar Mar 13 '19 05:03 Jagreaper

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

jesusvallejo avatar Feb 09 '20 16:02 jesusvallejo