1

我正在尝试使用 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);
        }
    }
}

任何提示将不胜感激。

4

1 回答 1

2

您不能为 WASAPI 环回捕获设置捕获 - 您必须使用系统混合格式,它将使用 32 位浮点样本。只需WasapiLoopbackCapture直接使用该类,它就可以工作。

于 2014-07-04T08:23:53.803 回答