C#Unity 3D NAudio在检查默认音频端点时抛出NullReferenceException [英] C# Unity 3D NAudio throws NullReferenceException while checking for default audio endpoint

查看:396
本文介绍了C#Unity 3D NAudio在检查默认音频端点时抛出NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了Unity 3D,因为我想制作一个简单的游戏. NAudio库有一个问题.

I've just installed Unity 3D because I wanted to make a simple game. There is one problem with the NAudio library though.

这是我的代码:

var enumerator = new MMDeviceEnumerator();

if (enumerator.HasDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia)) //<--- crashes here
{
   //...
}

这实际上是Unity在尝试启动游戏时所说的:

This is literally all Unity says when trying to launch the game:

NullReferenceException:对象引用未设置为的实例 对象NAudio.CoreAudioApi.MMDeviceEnumerator.HasDefaultAudioEndpoint (DataFlow dataFlow,角色角色)

NullReferenceException: Object reference not set to an instance of an object NAudio.CoreAudioApi.MMDeviceEnumerator.HasDefaultAudioEndpoint (DataFlow dataFlow, Role role)

我不明白为什么它会在这里引发这种错误. 可能是该库可能与此版本的Net Framework(版本3.5)不兼容吗?

I don't understand why it throws this kind of error here. Could it be that the library might not be compatible with this version of net framework (version 3.5)?

编辑:变量枚举器不能为null,因此请停止将此帖子标记为什么是NullReferenceException,以及如何解决?"的重复项.

the variable enumerator can't be null, so stop flagging this post as a duplicate of 'What is a NullReferenceException, and how do I fix it?'

我也尝试使用此库: https://code.google.com/archive/p/naudio-with-unity3d/downloads 该库仅不包含HasDefaultAudioEndpoint()函数,因此我只是尝试通过调用GetDefaultAudioEndpoint()直接获取音频端点,但是它仍然在该行上引发了相同的错误.

I've also tried using this libary: https://code.google.com/archive/p/naudio-with-unity3d/downloads That library only doesn't contain the HasDefaultAudioEndpoint() function, so I just tried getting the audio endpoint directly by calling GetDefaultAudioEndpoint() but it still threw that same error on that line.

推荐答案

问题不是您的代码.如果您查看 HasDefaultAudioEndpoint ,看起来像这样:

The problem is not your code. If you look at the source code for HasDefaultAudioEndpoint, it looks like this:

public bool HasDefaultAudioEndpoint(DataFlow dataFlow, Role role)
{
    IMMDevice device = null;
    int hresult = ((IMMDeviceEnumerator)realEnumerator).GetDefaultAudioEndpoint(dataFlow, role, out device);
    ...
}

realEnumerator所在的位置:

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

它不是在检查是否realEnumerator != null,这很可能是因为未考虑Unity且它不支持某些COM +互操作.

It's not checking whether realEnumerator != null, most likely because Unity was not considered and it doesn't support some COM+ interop.

如果您查看此问题从2012年开始,四年后,OP回答它从未起作用,因此必须为此创建一个c ++ DLL.

If you look at this question from 2012, 4 years later the OP answered it never worked, and had to create a c++ DLL for this.

这篇关于C#Unity 3D NAudio在检查默认音频端点时抛出NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆