Naudio录音麦克风X秒钟 [英] Naudio record microphone for X amount of seconds

查看:41
本文介绍了Naudio录音麦克风X秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我可以使用Naudio .dll录制麦克风输入:

Currently I can record the microphone input using the Naudio .dll with this:

public void recordInput()
{
    Console.WriteLine("Now recording...");
    waveSource = new WaveIn();
    waveSource.WaveFormat = new WaveFormat(16000, 1);

    waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
    waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
    //string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav");

    string tempFile = Path.Combine(@"C:\Users\Nick\Desktop",  "test.wav");


    waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);

    waveSource.StartRecording();

    myTimer.Interval = 5000;
    myTimer.Tick += new EventHandler(myTimer_Tick);
    myTimer.Start();

}

目前,我使用事件处理程序等待5秒钟,然后onTick停止记录.但是,这引起了问题,因为在我调用的代码的主要部分中,我无法等待录制任务完成:

Currently I used an eventhandler to wait for 5 seconds and then onTick I stop recording. However this is causing problems because I am having trouble waiting for the recording task to finish in the main part of the code where I call:

    public string voiceToText()
    {
        recordInput();

        //Somehow wait here until record is done then proceed.

        convertToFlac();
        return "done";


    }

我尝试将Naudio放入线程中并使用 waitOne(); ,但是将 waveSource.StartRecording(); 放入事件处理程序中,并将期望值放入线程.我还尝试过使用 Thread.Sleep(5000)并在该线程结束后停止记录,但是由于某种原因,音频仅记录了前500mS的音频.

I've tried putting the Naudio in a thread and using a waitOne(); but waveSource.StartRecording(); is put in an eventhandler and thread an expection. I've also tried using a Thread.Sleep(5000) and stop recording after that thread finishes but the audio only records the first 500mS of audio for some reason.

我对C#还是很陌生,并且不完全了解线程,因此欢迎任何帮助或单独的方法.

I'm pretty new to c# and don't fully understand threading so any help or seperate approach is welcome.

推荐答案

如果您不是在Windows Forms或WPF应用程序中运行,则应改用 WaveInEvent ,以便有一个后台线程设置为处理回调. WaveIn 的默认构造函数使用Windows消息.

If you aren't running in a Windows Forms or WPF application, then you should use WaveInEvent instead, so that a background thread is set up to handle the callbacks. The default constructor for WaveIn uses Windows messages.

这篇关于Naudio录音麦克风X秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆