1

我正在尝试在 C# 中使用 NAudio 录制语音,但我被困在两个地方:

1. A crash:

通过THIS SO page 中稍微修改的代码形式,我得到了一个NullReferenceException. 这是崩溃日志:

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at NAudio.Wave.WaveIn.Callback(IntPtr waveInHandle, WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved)
   at NAudio.Wave.WaveWindow.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

代码是:

using System;
using System.Windows.Forms;
using System.Threading;
using NAudio.Wave;

public class FOO
{
    static WaveIn s_WaveIn;

    [STAThread]
    static void Main(string[] args)
    {
        init();
        Application.Run();
    }

    public static void record()
    {
    while (true)
    {
        Console.WriteLine("Hit Enter to START Recording.\n");
        Console.ReadLine();

        s_WaveIn.StartRecording();

        Console.WriteLine("Hit Enter to STOP recording.\n");
        Console.ReadLine();

        s_WaveIn.StopRecording();
    }
    }

    public static void DeviceInit(int rate, int channels)
    {
    s_WaveIn = new WaveIn();
    s_WaveIn.WaveFormat = new WaveFormat(rate, channels);

    s_WaveIn.BufferMilliseconds = 1000;
    s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
    }

    public static void init()
    {
    DeviceInit(44100, 2);

    Thread t1 = new Thread(delegate() {
        record();
        });
    t1.Start();
    }

    static void SendCaptureSamples(object sender, WaveInEventArgs e)
    {
    Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
    }
}

大多数情况下,这发生在我开始第三次录制时。知道是什么原因造成的吗?

*2. Modifying rate and channels at runtime.*

在我的实际代码中,我s_WaveIn.WaveFormat = new WaveFormat(new_rate, new_channels);在调用之前使用重置波形格式StartRecording()。我没有打电话Dispose(),因为这需要重置DataAvailable回调,为此,我需要另一个消息循环。这种方法是否正确,或者我应该先调用 Dispose,然后用新格式重新初始化 s_WaveIn?

谢谢你。

4

4 回答 4

1

你的Main方法看起来很奇怪。线程是干什么用的?

只需使用这个:

[STAThread]
static void Main(string[] args)
{
    init();
    Application.Run();
}

您的init方法还运行应用程序。为什么?
尝试将其更改为:

public static void init()
{
    DeviceInit(44100, 2);

    Thread t1 = new Thread(delegate() {
        record();
    });
    t1.Start();
}

return;在方法返回的末尾不需要void.

于 2011-08-18T12:00:47.910 回答
1

似乎即使缓冲区为空,也会调用 DataAvailable 回调。

我修改了WaveIn.cs文件中的一个函数,现在它工作正常。我不确定这是否正确,但现在,这对我有用。

private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved)
{
    if (message == WaveInterop.WaveMessage.WaveInData)
    {
    GCHandle hBuffer = (GCHandle)waveHeader.userData;
    WaveInBuffer buffer = (WaveInBuffer)hBuffer.Target;
if (buffer != null)
{
    if (DataAvailable != null)
    {
    DataAvailable(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded));
    }
    if (recording)
    {
    buffer.Reuse();
    }
}
else
{
    if (RecordingStopped != null)
    {
    RecordingStopped(this, EventArgs.Empty);
    }
}
}

}

于 2011-08-23T10:32:21.293 回答
0

我认为,必须以这种方式完成:

GCHandle hBuffer = (GCHandle)waveHeader.userData;
WaveInBuffer buffer = (WaveInBuffer)hBuffer.Target;

if (buffer == null)
{
    return; // with this new line, everything works fine
}

if (DataAvailable != null)
{
    DataAvailable(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded));
}

if (recording)
{
    buffer.Reuse();
}
else
{
    if (RecordingStopped != null)
    {
        RecordingStopped(this, EventArgs.Empty);
    }
}
于 2011-11-14T10:31:08.073 回答
0

我得到了相同的 NullReferenceException。作者没有在建议的类WaveIn中添加行。据我了解,不提供这种用途。示例中的作者库不调用StopRecording (),只是停止记录传入的信息,而是继续处理它(例如测量音量),并调用StopRecording ()完全停止接收数据到数据WaveIn . 因此,我认为有必要在调用StopRecording ()之后使用新的 WaveIn 。

于 2013-09-20T07:08:19.920 回答