In a dotnetcore 3.0 Blazor app I want to be able to record audio using NAudio, instead of implementing an HTML5 control in Javascript. Does NAudio support this?
I have already tried the sample code below. I am getting a 0 byte wave file written to my Desktop.
@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();
}
}