瑙迪奥图书馆:http://naudio.codeplex.com/ http://naudio.codeplex.com/
我正在尝试将 MP3 文件转换为 WAV 文件,但遇到了一个小错误。我知道出了什么问题,但我真的不知道如何解决它。
这是我正在运行的代码片段:
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 类,该方法如下所示:
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 类的对象。
问题是:我收到 ArgumentException。 MSDN说这是因为offset和numBytes之和大于sampleBuffer的长度。
文档:http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
发生这种情况是因为我每次都增加计数器,但是字节数组的大小test
保持不变。
我一直想知道的是:我是否需要动态增加数组的大小,或者我是否需要在开始时找出所需的大小并立即设置它?
而且,Mp3FileReader 中的方法第一次返回 365,而不是 512。这是整个块的大小。但我正在写完整的 512。我基本上只是使用读取来检查我是否尚未到达文件末尾。我是否需要捕获返回值并对其执行某些操作,或者我在这里做得好吗?