Record input from NAudio WaveIn, and output to NAudio WaveOut

旧城冷巷雨未停 提交于 2019-11-27 15:08:23

问题


I want to be able to get input from a microphone device via NAudio.WaveIn, and then output that exact input to an output device via NAudio.WaveOut.

How would I do this?


回答1:


Here is the code that worked for me:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.CoreAudioApi;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {

        private BufferedWaveProvider bwp;

        WaveIn wi;
        WaveOut wo;
        public Form4()
        {
            InitializeComponent();
            wo = new WaveOut();
            wi = new WaveIn();

            wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);

            bwp = new BufferedWaveProvider(wi.WaveFormat);
            bwp.DiscardOnBufferOverflow = true;

            wo.Init(bwp);
            wi.StartRecording();
            wo.Play();

        }

        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);

        }
    }
}



回答2:


The best way would be to use a BufferedWaveProvider as the input to WaveOut. Then in the DataAvailable callback of WaveIn, supply the data recorded to the BufferedWaveProvider

void DataAvailable(object sender, WaveInEventArgs args)
{
    bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}

You need to be aware that the default buffer sizes will result in a noticeable delay, so if you were hoping for low latency you might need to experiment a bit with buffer sizes to see how low you can get it.



来源:https://stackoverflow.com/questions/5694326/record-input-from-naudio-wavein-and-output-to-naudio-waveout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!