I'm using NAudio in my with my Wpf app first time.
Steps: 1) Recording to MemoryStream using NAudio (C#, Wpf). This is my recording code:
public void StartRecording()
{
this.waveSource = new WaveIn();
if (Stream == null)
{
Stream = new MemoryStream();
}
waveSource.WaveFormat = new WaveFormat(44100, 2);
this.waveFile = new WaveFileWriter(this.Stream, this.waveSource.WaveFormat);
this.waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
this.waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
this.waveSource.StartRecording();
}
private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
int secondsRecorded = (int)(waveFile.Length / waveFile.WaveFormat.AverageBytesPerSecond);
waveFile.Flush();
}
}
private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
}
2) After stopping it I am inserting array of recorded stream (MemoryStream.ToArray()) to Database (SQLite).
3) Getting from database and converting it to stream to play it:
Stream stream = new MemoryStream(bytes);
var mp3Reader = new Mp3FileReader(stream);
Mp3FileReader is throwing an exception: Got a frame at sample rate 44100, in an MP3 with sample rate 48000. Mp3FileReader does not support sample rate changes.
Could someone tell me where I am doing wrong, please. I found some questions but they didn't help me. Sorry If there any duplicate question. Thanks