我可以使用NAudio在Blazor应用中录制音频吗?

时间:2019-10-01 15:59:32

标签: naudio blazor

在dotnetcore 3.0 Blazor应用程序中,我希望能够使用NAudio录制音频,而不是在Javascript中实现HTML5控件。 NAudio支持吗?

我已经尝试了以下示例代码。我正在将一个0字节的wave文件写入桌面。

@inject IJSRuntime JsRuntime
@using System
@using System.IO
@using NAudio.Wave

<button @ref="recordButton" @onclick="Record">Record</button>
<button @ref="stopRecording" @onclick="StopRecording">Stop</button>

@code {

    ElementReference recordButton;
    ElementReference stopRecording;

    WaveInEvent waveIn;

    public void Record()
    {        
        var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");
        Directory.CreateDirectory(outputFolder);
        var outputFilePath = Path.Combine(outputFolder, "recorded.wav");

        waveIn = new WaveInEvent();

        WaveFileWriter writer = new WaveFileWriter(outputFilePath, waveIn.WaveFormat);
        waveIn.StartRecording();

        waveIn.DataAvailable += (s, a) =>
        {
            writer.Write(a.Buffer, 0, a.BytesRecorded);
        };

    }

    public void StopRecording()
    {
        waveIn.StopRecording();
    }

}

0 个答案:

没有答案