使用Naudio将MP3文件转换为WAV文件时出现问题 [英] Trouble converting an MP3 file to a WAV file using Naudio

查看:306
本文介绍了使用Naudio将MP3文件转换为WAV文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Naudio库: http://naudio.codeplex.com/

我正在尝试将MP3文件转换为WAV文件,但是遇到了一个小错误.我知道出了什么问题,但是我真的不知道如何解决它.

I'm trying to convert an MP3 file to a WAV file, but I've run in to a small error. I know what's going wrong, but I don't really know how to go about fixing it.

这是我正在运行的代码:

Here's the piece of code I'm running:

private void button1_Click(object sender, EventArgs e) {
    using(Mp3FileReader reader = new Mp3FileReader(@"path\to\MP3")) {
        using(WaveFileWriter writer = new WaveFileWriter(@"C:\test.wav", new WaveFormat())) {
            int counter = 0;
            while(reader.Read(test, counter, test.Length + counter) != 0) {
                writer.WriteData(test, counter, test.Length + counter);
                counter += 512;
            }
        }
    }
}

reader.Read()进入Mp3FileReader类,该方法如下所示:

reader.Read() goes into the Mp3FileReader class, and the method looks like this:

public override int Read(byte[] sampleBuffer, int offset, int numBytes)
{
    if (numBytes % waveFormat.BlockAlign != 0)
        //throw new ApplicationException("Must read complete blocks");
        numBytes -= (numBytes % waveFormat.BlockAlign);
    return mp3Stream.Read(sampleBuffer, offset, numBytes);
}

mp3Stream是Stream类的对象.

mp3Stream is an object of the Stream class.

问题是:我收到ArgumentException. MSDN表示这是因为offset和numBytes之和大于sampleBuffer的长度.

The problem is: I'm getting an ArgumentException. MSDN says that this is because the sum of offset and numBytes is greater than the length of sampleBuffer.

文档: http://msdn.microsoft .com/en-us/library/system.io.stream.read.aspx

发生这种情况是因为我每次都会增加计数器,但是字节数组test的大小保持不变.

This happens because I increase the counter every time, but the size of the byte array test remains the same.

我一直想知道的是:我是否需要动态增加数组的大小,还是需要在开始时找出所需的大小并立即进行设置?

What I've been wondering is: do I need to increase the size of the array dynamically, or do I need to find out the needed size at the beginning and set it right away?

并且,而不是512,Mp3FileReader中的方法第一次返回365.整个块的大小.但是我正在写完整的512.我基本上只是在使用read来检查我是否不在文件末尾.我是否需要获取返回值并进行相应的处理,或者我在这里好吗?

And also, instead of 512, the method in Mp3FileReader returns 365 the first time. Which is the size of a whole block. But I'm writing the full 512. I'm basically just using the read to check if I'm not at the end of the file yet. Do I need to catch the return value and do something with that, or am I good here?

推荐答案

您将需要使用Read()的返回值来确定您实际获得了多少个字节.它不必是512,您已经发现了.请记住,您正在使用流,而不是数组.使它看起来像这样:

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);
            }
        }
    }

这篇关于使用Naudio将MP3文件转换为WAV文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆