Got a frame at sample rate 44100, in an MP3 with sample rate 48000. Mp3FileReader does not support sample rate changes
Asked Answered
D

2

5

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

Drupelet answered 16/7, 2015 at 11:39 Comment(1)
This may be helpful: Look at this link: codeproject.com/Tips/56882/MP-Sound-Recording-ToolAbbottson
P
2

You have saved a WAV file, not an MP3 file, so you need to use WaveFileReader instead of Mp3FileReader in order to play it back.

Patronize answered 17/7, 2015 at 13:59 Comment(3)
Is there any way to convert Wav file to Mp3? So I should use Mp3FileReader because in my database all files are Mp3 files. ThanksDrupelet
Or is it possible to record mp3 file?Drupelet
I tend to use LAME.exe to convert WAV to MP3. If you are on Win 8 you can do it with MediaFoundationEncoder insteadPatronize
M
6

I also got this exception message just like you. I solved like this:

MediaFoundationReader readers = new MediaFoundationReader(filepath);

instead of

Mp3FileReader readers = new Mp3FileReader(filepath);

When I use the MediaFoundationReader, I didn't get that exception message.

Machination answered 22/7, 2020 at 21:31 Comment(1)
FYI, if you are working with a MemoryStream and not a file, you can also use the stream version of the class: new StreamMediaFoundationReader(memoryStream)Antwanantwerp
P
2

You have saved a WAV file, not an MP3 file, so you need to use WaveFileReader instead of Mp3FileReader in order to play it back.

Patronize answered 17/7, 2015 at 13:59 Comment(3)
Is there any way to convert Wav file to Mp3? So I should use Mp3FileReader because in my database all files are Mp3 files. ThanksDrupelet
Or is it possible to record mp3 file?Drupelet
I tend to use LAME.exe to convert WAV to MP3. If you are on Win 8 you can do it with MediaFoundationEncoder insteadPatronize

© 2022 - 2024 — McMap. All rights reserved.