audio - NA音频: "Unable to instantiate ASIO. Check if STAThread is set"

标签 audio naudio comexception asio

我正在尝试使用 ASIO 驱动程序将麦克风音频路由到 NAudio 中的扬声器。我在 NAudio 演示项目中取得了成功。将此代码复制到不同的项目(XNA,如果相关)每次都会导致 COMException。这是我写的方法:

    [STAThread]
    void InitAsio()
    {
        if (this.asioOut != null &&
            (this.asioOut.DriverName != "ASIO4ALL v2" ||
            this.asioOut.ChannelOffset != 0))
        {
            this.asioOut.AudioAvailable -= asioOut_AudioAvailable;
            this.asioOut.Dispose();
            this.asioOut = null;
        }

        // create wave input from mic
        // create device if necessary
        if (this.asioOut == null)
        {
            this.asioOut = new AsioOut();
            BufferedWaveProvider wavprov = new BufferedWaveProvider(new WaveFormat(44100, 1));
            this.asioOut.InputChannelOffset = 0;
            this.asioOut.InitRecordAndPlayback(wavprov, 1, 44100);
            this.asioOut.AudioAvailable += asioOut_AudioAvailable;
        }

        //this.fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav");
        //this.writer = new WaveFileWriter(fileName, new WaveFormat(44100, recordChannelCount));
        this.asioOut.Play();
    }

每次,这个方法在AsioDriver.cs中都会失败:

    int hresult = CoCreateInstance(ref ASIOGuid, IntPtr.Zero, CLSCTX_INPROC_SERVER, ref ASIOGuid, out pASIOComObject);
    if ( hresult != 0 )
    {
        throw new COMException("Unable to instantiate ASIO. Check if STAThread is set",hresult);
    }

我不知道发生了什么以及为什么。相同的代码适用于 NAudio 演示项目。有什么想法吗?

最佳答案

STAThread 属性只能应用于应用程序的 Main 方法。它不适用于任何其他方法。

关于audio - NA音频: "Unable to instantiate ASIO. Check if STAThread is set",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778278/

相关文章:

c# - 将 a-law 转换为 pcm

html - 为什么 HTML5 Audio 标签总是有两个来源?

audio - 跨平台SIMD库是否具有与Accelerate Framework类似的API?

c# - 使用NAudio,ADTS AAC转WAV转码,怎么做?

c# - 音频可视化工具 C#

.net - 分配给 Excel.Application.ActivePrinter 时出现 COMException

ASP.NET 4.0 Web 应用程序抛出 "Incorrect function. (Exception from HRESULT: 0x80070001)"

java - (剪辑)和 DataLine.Info 代表什么...?

iphone - 如何设置多个文件播放器音频单元同时启动?