【发布时间】:2017-05-15 22:10:44
【问题描述】:
Naudio 库: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
发生这种情况是因为我每次都增加计数器,但字节数组test 的大小保持不变。
我一直想知道的是:我需要动态增加数组的大小,还是需要一开始就找出需要的大小并立即设置?
此外,Mp3FileReader 中的方法第一次返回 365,而不是 512。这是整个块的大小。但我正在写完整的 512。我基本上只是使用读取来检查我是否还没有在文件末尾。我是否需要捕获返回值并对其进行处理,还是我在这里做得很好?
【问题讨论】: