从麦克风n音讯录制声音,然后保存 [英] naudio record sound from microphone then save

查看:409
本文介绍了从麦克风n音讯录制声音,然后保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与n音讯并保存录音的一些问题。该代码我现在有工作的地方它保存为wav文件的地步,但是当我打开它,Windows媒体播放器将返回一个错误:Windows Media Player中遇到的问题,同时播放该文件



我有两个按钮,一个记录按钮,它的压制之后变成停止按钮。我有一个保存按钮,点击它时,保存录音 sample.wav

  NAudio.Wave.WaveIn的SourceStream = NULL; 
NAudio.Wave.DirectSoundOut waveout的= NULL;
NAudio.Wave.WaveFileWriter waveWriter = NULL;

私人无效recordButton_Click(对象发件人,EventArgs五)
{
INT deviceNumber = sourceList.SelectedItems [0]的.index;

的SourceStream =新NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFor​​mat =新NAudio.Wave.WaveFor​​mat(44100,NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

NAudio.Wave.WaveInProvider waveIn =新NAudio.Wave.WaveInProvider(的SourceStream);

waveout的=新NAudio.Wave.DirectSoundOut();
waveOut.Init(waveIn);

sourceStream.StartRecording();
waveOut.Play();

recordButton.Visible = FALSE;
stopRecord.Visible = TRUE;
}

私人无效saveResponse_Click(对象发件人,EventArgs五)
{
INT deviceNumber = sourceList.SelectedItems [0]的.index;

串saveLocation =C:\\wav\\sample.wav

的SourceStream =新NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFor​​mat =新NAudio.Wave.WaveFor​​mat(44100,NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

sourceStream.DataAvailable + =新的EventHandler< NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
waveWriter =新NAudio.Wave.WaveFileWriter(saveLocation,sourceStream.WaveFor​​mat);

sourceStream.StartRecording();

MessageBox.Show(记录保存成功。);
}

私人无效sourceStream_DataAvailable(对象发件人,NAudio.Wave.WaveInEventArgs E)
{
如果(waveWriter == NULL)回报;

waveWriter.WriteData(e.Buffer,0,e.BytesRecorded);
waveWriter.Flush();
}
私人无效stopRecord_Click(对象发件人,EventArgs五)
{
如果(waveout的!= NULL)
{
waveOut.Stop();
waveOut.Dispose();
waveout的= NULL;
}
如果(的SourceStream!= NULL)
{
sourceStream.StopRecording();
sourceStream.Dispose();
的SourceStream = NULL;
}
如果(waveWriter!= NULL)
{
waveWriter.Dispose();
waveWriter = NULL;
}

recordButton.Visible = TRUE;
stopRecord.Visible = FALSE;
saveResponse.Enabled = TRUE;
}


解决方案

recordButton_Click 代码没有记录,这是一个从管道数据 WaveIn waveout的 ,这将播放从信号源(麦克风)传来的数据直接输出(扬声器)。它不会从一个到另一个保留该数据供以后使用,它只是管它。如果您想随后保存数据到磁盘上,你需要自己缓冲吧。



saveResponse_Click 的另一方面是的启动的数据从麦克风到磁盘上的波形文件直接录制。如果单击保存响应按钮,稍等一下,然后点击你的停止按钮,你应该得到一个记录波形文件。



如果你想直接录制到磁盘,这是好的。如果你想记录到内存中,然后有选择地写入到磁盘,那么你需要保存的数据,因为它的用武之地。也许用一个内存流,以存放在记录数据,然后写在 WaveFileWriter 当谈到时间来保存文件。






下面是我用来测试直接代码记录到磁盘上的波形文件:

 公共WaveIn waveSource = NULL; 
公共WaveFileWriter waveFile = NULL;

私人无效StartBtn_Click(对象发件人,EventArgs五)
{
StartBtn.Enabled = FALSE;
StopBtn.Enabled = TRUE;

waveSource =新WaveIn();
waveSource.WaveFor​​mat =新WAVEFORMAT(44100,1);

waveSource.DataAvailable + =新的EventHandler< WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped + =新的EventHandler< StoppedEventArgs>(waveSource_RecordingStopped);

waveFile =新WaveFileWriter(@C:\Temp\Test0001.wav,waveSource.WaveFor​​mat);

waveSource.StartRecording();
}

私人无效StopBtn_Click(对象发件人,EventArgs五)
{
StopBtn.Enabled = FALSE;

waveSource.StopRecording();
}

无效waveSource_DataAvailable(对象发件人,WaveInEventArgs E)
{
如果(waveFile!= NULL)
{
waveFile.Write (e.Buffer,0,e.BytesRecorded);
waveFile.Flush();
}
}

无效waveSource_RecordingStopped(对象发件人,StoppedEventArgs E)
{
如果(waveSource!= NULL)
{
waveSource.Dispose();
waveSource = NULL;
}

如果(waveFile!= NULL)
{
waveFile.Dispose();
waveFile = NULL;
}

StartBtn.Enabled = TRUE;
}


I'm having some issues with naudio and saving sound recordings. The code I currently have works to the point where it saves the wav file, but when I open it up, Windows Media Player returns an error: "Windows Media Player encountered a problem while playing the file"

I have two buttons, a "Record" button, which turns into the stop button after it's pressed. And I have a "Save" button which when clicked, saves the recording to sample.wav.

NAudio.Wave.WaveIn sourceStream = null;
NAudio.Wave.DirectSoundOut waveOut = null;
NAudio.Wave.WaveFileWriter waveWriter = null;

private void recordButton_Click(object sender, EventArgs e)
{
    int deviceNumber = sourceList.SelectedItems[0].Index;

    sourceStream = new NAudio.Wave.WaveIn();
    sourceStream.DeviceNumber = deviceNumber;
    sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

    NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream);

    waveOut = new NAudio.Wave.DirectSoundOut();
    waveOut.Init(waveIn);

    sourceStream.StartRecording();
    waveOut.Play();

    recordButton.Visible = false;
    stopRecord.Visible = true;
}

private void saveResponse_Click(object sender, EventArgs e)
{
    int deviceNumber = sourceList.SelectedItems[0].Index;

    string saveLocation = "c:\\wav\\sample.wav";

    sourceStream = new NAudio.Wave.WaveIn();
    sourceStream.DeviceNumber = deviceNumber;
    sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

    sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
    waveWriter = new NAudio.Wave.WaveFileWriter(saveLocation, sourceStream.WaveFormat);

    sourceStream.StartRecording();

    MessageBox.Show("Recording successfully saved.");
}

private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
    if (waveWriter == null) return;

    waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
    waveWriter.Flush();
}
private void stopRecord_Click(object sender, EventArgs e)
{
    if (waveOut != null)
    {
        waveOut.Stop();
        waveOut.Dispose();
        waveOut = null;
    }
    if (sourceStream != null)
    {
        sourceStream.StopRecording();
        sourceStream.Dispose();
        sourceStream = null;
    }
    if (waveWriter != null)
    {
        waveWriter.Dispose();
        waveWriter = null;
    }

    recordButton.Visible = true;
    stopRecord.Visible = false;
    saveResponse.Enabled = true;
}

解决方案

Your recordButton_Click code isn't recording, it's piping data from a WaveIn to a WaveOut, which will play the data coming from your source (microphone) directly to the output (speakers). It doesn't retain that data for later use, it just pipes it from one to the other. If you want to subsequently save that data to disk, you need to buffer it yourself.

The saveResponse_Click on the other hand is starting the direct recording of data from the microphone to a wave file on disk. If you click your Save Response button, wait for a bit, then click your Stop button, you should get a recorded wave file.

If you want to record directly to disk, this is fine. If you want to record to memory, then optionally write to disk, then you need to save the data as it comes in. Perhaps use a memory stream to hold the data while recording, then write that to the WaveFileWriter when it comes time to save the file.


Here's the code I used for testing direct recording to a wave file on disk:

public WaveIn waveSource = null;
public WaveFileWriter waveFile = null;

private void StartBtn_Click(object sender, EventArgs e)
{
    StartBtn.Enabled = false;
    StopBtn.Enabled = true;

    waveSource = new WaveIn();
    waveSource.WaveFormat = new WaveFormat(44100, 1);

    waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
    waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

    waveFile = new WaveFileWriter(@"C:\Temp\Test0001.wav", waveSource.WaveFormat);

    waveSource.StartRecording();
}

private void StopBtn_Click(object sender, EventArgs e)
{
    StopBtn.Enabled = false;

    waveSource.StopRecording();
}

void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
    if (waveFile != null)
    {
        waveFile.Write(e.Buffer, 0, e.BytesRecorded);
        waveFile.Flush();
    }
}

void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
    if (waveSource != null)
    {
        waveSource.Dispose();
        waveSource = null;
    }

    if (waveFile != null)
    {
        waveFile.Dispose();
        waveFile = null;
    }

    StartBtn.Enabled = true;
}

这篇关于从麦克风n音讯录制声音,然后保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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