C#:Unity3D 和 NAudio EnumerateAudioEndPoints 抛出 NullReferenceException

发布于 2025-01-06 01:45:28 字数 1991 浏览 1 评论 0原文

我正在尝试在 Unity3D 项目中使用 NAudio 库。调用 MMDeviceEnumerator.EnumerateAudioEndPoints 时,抛出 NullReferenceException。在面向 .NET 2.0 的 Visual Studio 项目中不会发生这种情况。枚举器本身不为空。该方法包装了对 IMMDeviceEnumerator.EnumAudioEndpoints 的调用,这就是直接引发此异常的原因(我将一堆代码从 NAudio 移至我的 Unity3D 项目中)。

这里唯一真正的区别是它在 Unity3D 游戏中运行,而不是在标准 Visual Studio 应用程序中运行。可能是什么原因造成的?请帮忙。

更新:

我的代码抛出(第二行):

MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection collection = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.All);

NAudio代码抛出(第二行方法):

public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask)
{
    IMMDeviceCollection result;
    Marshal.ThrowExceptionForHR(_realEnumerator.EnumAudioEndpoints(dataFlow, dwStateMask, out result));
    return new MMDeviceCollection(result);
}

更新x2:

在NAudio中MMDeviceEnumerator的构造函数中:

_realEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;

新的com object 不为 null,但在强制转换时,它变为 null。同样,这适用于 Visual Studio 项目,但不适用于 Unity3D。以下是每个对象的定义(来自 NAudio 源):

[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
class MMDeviceEnumeratorComObject
{
}

如果需要,我

[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);
}

可以将其他对象发布到那里。我所缺少的可能是 Unity3D(也针对 .NET 2.0)和 Visual Studio 之间的某些内容会发生变化的核心“原因”。

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).

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.

Update:

My code that throws (second line):

MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection collection = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.All);

NAudio code that throws (second line of method):

public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask)
{
    IMMDeviceCollection result;
    Marshal.ThrowExceptionForHR(_realEnumerator.EnumAudioEndpoints(dataFlow, dwStateMask, out result));
    return new MMDeviceCollection(result);
}

Update x2:

In the constructor for MMDeviceEnumerator in NAudio:

_realEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;

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):

[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
class MMDeviceEnumeratorComObject
{
}

and

[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);
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文