NAudio WasapiLoopbackCapture ComException(0x88890003)

时间:2014-07-04 03:09:19

标签: c# .net audio naudio wasapi

我正在尝试使用WasapiLoopbackCapture类(NAudio 1.7.1.17)并最终使用COMException(0x88890003)。录制格式为WaveFormat(44100, 16, 2)。我的系统上有多个播放设备,并尝试将每个设备设置为具有相同结果的默认设备。我还验证了每个设备都(44100, 16, 2)列为支持的格式。

控制台输出:

WasapiCapture_RecordingStopped.

Exception: System.Runtime.InteropServices.COMException (0x88890003): Exception from HRESULT: 0x88890003
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at NAudio.CoreAudioApi.AudioClient.get_AudioCaptureClient()
   at NAudio.CoreAudioApi.WasapiCapture.DoRecording(AudioClient client)
   at NAudio.CoreAudioApi.WasapiCapture.CaptureThread(AudioClient client)

代码:

public static class Program
{
    private static int Index = 0;
    private static int TotalBytesRecorded = 0;
    private static bool RecordingStopped = false;

    private static void Main (string [] args)
    {
        var device = NAudio.Wave.WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice();

        using (var capture = new NAudio.CoreAudioApi.WasapiCapture(device))
        {
            capture.WaveFormat = new NAudio.Wave.WaveFormat(44100, 16, 2);
            capture.ShareMode = NAudio.CoreAudioApi.AudioClientShareMode.Shared;
            capture.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(Program.WasapiCapture_DataAvailable);
            capture.RecordingStopped += new EventHandler<NAudio.Wave.StoppedEventArgs>(Program.WasapiCapture_RecordingStopped);

            Program.Index = 0;
            Program.TotalBytesRecorded = 0;
            Program.RecordingStopped = false;

            capture.StartRecording();
            Thread.Sleep(TimeSpan.FromSeconds(10));
            capture.StopRecording();

            while (!Program.RecordingStopped)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(10));
            }
        }

        Console.WriteLine();
        Console.WriteLine();
        Console.Write("TotalBytesRecorded: {0}.", Program.TotalBytesRecorded.ToString("N0"));
    }

    private static void WasapiCapture_DataAvailable (object sender, NAudio.Wave.WaveInEventArgs e)
    {
        Program.Index++;
        Program.TotalBytesRecorded += e.BytesRecorded;

        Console.WriteLine();
        Console.Write
        (
            "Index: {0}, BytesRecorded: {1}, Buffer Length: {2}, TotalBytesRecorded: {3}.",
            Program.Index.ToString("N0").PadLeft(10, ' '),
            e.BytesRecorded.ToString("N0").PadLeft(10, ' '),
            e.Buffer.Length.ToString("N0").PadLeft(10, ' '),
            Program.TotalBytesRecorded.ToString("N0").PadLeft(10, ' ')
        );
    }

    private static void WasapiCapture_RecordingStopped (object sender, NAudio.Wave.StoppedEventArgs e)
    {
        Program.RecordingStopped = true;

        Console.WriteLine();
        Console.WriteLine();
        Console.Write("WasapiCapture_RecordingStopped.");

        if (e.Exception != null)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Exception: {0}", e.Exception);
        }
    }
}

任何提示都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您无法设置WASAPI环回捕获的捕获 - 您必须使用系统混合格式,它将使用32位浮点样本。只需直接使用WasapiLoopbackCapture课程即可。