问题
I am trying to use the WasapiLoopbackCapture class (NAudio 1.7.1.17) and ending up with the COMException (0x88890003). The recording format is WaveFormat(44100, 16, 2)
. I have multiple playback devices on my system and have tried setting each one as the default device with the same results. I have also verified that each of those devices has (44100, 16, 2)
listed as a supported format.
Console Output:
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)
Code:
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);
}
}
}
Any tips would be appreciated.
回答1:
You can't set the capture for WASAPI loopback capture - you have to use the system mix format, which will use 32 bit floating point samples. Just use the WasapiLoopbackCapture
class directly and it will work.
来源:https://stackoverflow.com/questions/24566152/naudio-wasapiloopbackcapture-comexception-0x88890003