Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Unity3D and NAudio EnumerateAudioEndPoints throws NullReferenceException
    primarykey
    data
    text
    <p>I'm trying to use the NAudio library in a Unity3D project. When calling MMDeviceEnumerator.EnumerateAudioEndPoints, a NullReferenceException is thrown. This does not occur in a Visual Studio project targeting .NET 2.0. The enumerator itself is not null. That method wraps a call to IMMDeviceEnumerator.EnumAudioEndpoints, which is what's directly throwing this exception (I moved a bunch of code from NAudio into my Unity3D project).</p> <p>The only real difference here is that it's running inside a Unity3D game, rather than a standard Visual Studio application. What could be causing this? Please help.</p> <p><strong>Update:</strong></p> <p>My code that throws (second line):</p> <pre><code>MMDeviceEnumerator enumerator = new MMDeviceEnumerator(); MMDeviceCollection collection = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.All); </code></pre> <p>NAudio code that throws (second line of method):</p> <pre><code>public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask) { IMMDeviceCollection result; Marshal.ThrowExceptionForHR(_realEnumerator.EnumAudioEndpoints(dataFlow, dwStateMask, out result)); return new MMDeviceCollection(result); } </code></pre> <p><strong>Update x2:</strong></p> <p>In the constructor for MMDeviceEnumerator in NAudio:</p> <pre><code>_realEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator; </code></pre> <p>The new com object is not null, but when casting, it becomes null. Again, this works in a Visual Studio project, but not Unity3D. Here's the definitions of each (from the NAudio source):</p> <pre><code>[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { } </code></pre> <p>and</p> <pre><code>[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IMMDeviceEnumerator { int EnumAudioEndpoints(DataFlow dataFlow, DeviceState stateMask, out IMMDeviceCollection devices); int GetDefaultAudioEndpoint(DataFlow dataFlow, Role role, out IMMDevice endpoint); int GetDevice(string id, out IMMDevice deviceName); int RegisterEndpointNotificationCallback(IMMNotificationClient client); int UnregisterEndpointNotificationCallback(IMMNotificationClient client); } </code></pre> <p>I can post the other objects in there if necessary. What I'm missing is probably the core "why" something between Unity3D (also targeting .NET 2.0) and Visual Studio would change.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload