Trouble converting an MP3 file to a WAV file using Naudio

本小妞迷上赌 提交于 2019-12-06 12:28:02

You'll need to use the return value of Read() to determine how many bytes you actually got. It doesn't have to be 512, you already found that out. And keep in mind that you are working with streams, not arrays. Make it look similar to this:

   using (var reader = new Mp3FileReader(@"path\to\MP3")) {
        using (var writer = new WaveFileWriter(@"C:\test.wav", new WaveFormat())) {
            var buf = new byte[4096];
            for (;;) {
                var cnt = reader.Read(buf, 0, buf.Length);
                if (cnt == 0) break;
                writer.WriteData(buf, 0, cnt);
            }
        }
    }

You're not actually performing any conversion from MP3 into WAV in your example. You need the WaveFormatConversionStream. Try something like this:

private void button1_Click(object sender, EventArgs e) {
    using(Mp3FileReader reader = new Mp3FileReader(@"path\to\MP3")) {
        using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader)) {
            WaveFileWriter.CreateWaveFile(outputFileName, convertedStream);
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!