NAudio.UWP记录,写.wav

时间:2016-12-28 15:31:23

标签: audio uwp naudio

我知道NAudio.UWP正在进行中,但最新版本(1.8)似乎确实能够编写.wav。

    private IWavePlayer player;
    private WaveStream reader;
    private IWaveIn recorder;
    private MemoryStream recordStream;
    private IRandomAccessStream selectedStream;

    private void Record()
    {
       if (recorder == null)
        {
            recorder = new WasapiCaptureRT();

            recorder.RecordingStopped += RecorderOnRecordingStopped;
            recorder.DataAvailable += RecorderOnDataAvailable;               
        }

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

        recorder.StartRecording();

    }   

 private async void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
    {
        Debug.WriteLine(waveInEventArgs.BytesRecorded);
        if (reader == null)
        {
            recordStream = new MemoryStream();
            reader = new RawSourceWaveStream(recordStream, recorder.WaveFormat);                
        }      

        await recordStream.WriteAsync(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);                      
    }

在哪里/如何创建存储文件名,路径等?没有目标参数...这是示例附带的唯一代码。

**标记您是否看到此Chrome正在阻止1.8代码下载。

2 个答案:

答案 0 :(得分:0)

  

在哪里/如何创建存储文件名,路径等?

这实际上取决于您的要求。例如,您可以使用固定名称和时间戳来命名您的音频文件。

var file = KnownFolders.MusicLibrary.CreateFileAsync("song"+DateTime.Now.ToString("yyyy-MM-DD-hh-mm-ss")+".wav",CreationCollisionOption.GenerateUniqueName);

答案 1 :(得分:0)

您可以将记录流复制到新文件流中。例如:

 var file = await KnownFolders.MusicLibrary.CreateFileAsync("test.wav");
 using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
 {
       recordStream.CopyTo(stream.AsStream());
 }