简体   繁体   English

如何使用naudio将音频录制到byte []而不是file

[英]How to record audio using naudio onto byte[] rather than file

I am able to capture audio using naudio into file, now i want it on byte[] or Stream in c#. 我能够使用naudio将音频捕获到文件中,现在我希望它在byte []或stream in c#中。

this.writer = new WaveFileWriter(this.outputFilename, this.waveIn.WaveFormat);

What i tried so far is instead of passing output filename in WaveFileWriter constructor, passed MemoryStream object. 到目前为止我尝试的是在WaveFileWriter构造函数中传递输出文件名,而不是传递MemoryStream对象。 With reference to stream object i try to play it using Soundplayer once recording gets over. 参考流对象,一旦录制结束,我尝试使用Soundplayer播放它。

private IWaveIn waveIn;
private WaveFileWriter writer;
private string outputFilename;

private Stream memoryStream;

public void onRecord(object inputDevice, string fileName)
{
    if (this.waveIn == null)
    {
            this.outputFilename = fileName;
            this.waveIn = new WasapiLoopbackCapture((MMDevice)inputDevice);

            if(memoryStream == null)
                   memoryStream = new MemoryStream();

            this.writer = new WaveFileWriter(this.memoryStream,this.waveIn.WaveFormat);
            this.waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(this.OnDataAvailable);
            this.waveIn.RecordingStopped += new EventHandler<StoppedEventArgs>(this.OnRecordingStopped);
            this.waveIn.StartRecording();
    }
}

private void OnDataAvailable(object sender, WaveInEventArgs e)
{
    this.writer.Write(e.Buffer, 0, e.BytesRecorded);
}

public void OnRecordingStopped(object sender, StoppedEventArgs e)
{
    if (this.waveIn != null)
    {
            this.waveIn.Dispose();
        this.waveIn = null;
    }

    if (this.writer != null)
    {
        this.writer.Close();
        this.writer = null;
    } 
}

For testing purpose, i created this below code to check whether it able to play the recorded audio. 出于测试目的,我创建了以下代码以检查它是否能够播放录制的音频。

System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer();

memoryStream.Position = 0; 
soundPlayer.Stream = null;
soundPlayer.Stream = memoryStream;
soundPlayer.Play();

But when i try with above manner, i got System.ObjectDisposedException: Cannot access a closed Stream. 但是当我尝试以上方式时,我得到System.ObjectDisposedException:无法访问封闭的Stream。 On the line memoryStream.Position = 0; 在行memoryStream.Position = 0; .I didn't dispose the stream object, Don't know where exactly it gets disposed. 我没有丢弃流对象,不知道它究竟在哪里处理。

正如马克暗示,我包裹memoryStreamIgnoreDisposeStream和它的作品。

this.writer = new WaveFileWriter(new IgnoreDisposeStream(memoryStream),this.waveIn.WaveFormat);

Do not create Memory stream. 不要创建内存流。 Use your System.Media.SoundPlayer as Stream. 将System.Media.SoundPlayer用作Stream。

byte[] buff = new byte[1024];   //or bigger...
System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer();
soundPlayer.Stream.Read(buff, 0, (buff.Length - 1));

The SoundPlayer class is not great for this since it reads the entire stream before starting playback. SoundPlayer类不是很好,因为它在开始播放之前读取整个流。 It also requires the stream to be in Wave file format, so you'll have to use the WaveFileWriter to write out all your audio data before you can use the stream with the SoundPlayer. 它还要求流为Wave文件格式,因此您必须使用WaveFileWriter写出所有音频数据,然后才能将该流与SoundPlayer一起使用。

I would recommend that you use NAudio's WaveOutEvent (or similar) to do your audio output. 我建议您使用NAudio的WaveOutEvent(或类似)来进行音频输出。 Doing so removes the need for a Stream and allows you to play back the recorded audio in near real-time (it'll have a delay; how much depends on your hardware and on the size of all the buffers involved). 这样做可以消除对Stream的需求,并允许您近乎实时地播放录制的音频(它会有延迟;多少取决于您的硬件和所涉及的所有缓冲区的大小)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM