Received a frame with a sampling rate of 44100, in MP3 format with a sampling rate of 48000. Mp3FileReader does not support changing the sampling rate

I am using NAudio in my Wpf app for the first time.

Steps: 1) Writing to MemoryStream using NAudio (C #, Wpf). This is my writing 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, I insert the array of the recorded stream (MemoryStream.ToArray ()) into the database (SQLite).

3) Getting from the database and converting it to a stream for playback:

Stream stream = new MemoryStream(bytes); 
var mp3Reader = new Mp3FileReader(stream);

      

Mp3FileReader throws an exception: Received frame with sample rate 44100, in MP3 format with sample rate 48000. Mp3FileReader does not support changing the sample rate.

Can anyone tell me where I am doing wrong please. I found some questions but they didn't help me. Sorry If there is a duplicate question. Thanks to

+3


source to share


1 answer


You saved a WAV file, not an MP3 file, so you need to use WaveFileReader

instead Mp3FileReader

to play it.



+2


source







All Articles