使用NAudio读取mp3文件时目标数组不长

时间:2012-04-04 09:09:29

标签: naudio

我有这段代码:

static void Main(string[] args)
{
    try
    {
        Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
        long count = reader.Length;
        if (count <= int.MaxValue)
        {
             byte[] info = new byte[count];
             reader.Read(info, 0, (int)count);
             Console.WriteLine("Succesfull read");
        }
        else
             Console.WriteLine("Could not read");
     }
     catch(Exception ex)
     {
         Console.WriteLine(ex);
     }
}

打印出以下异常消息:

System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds. 
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) 
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length) 
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50 
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338 
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30

我已经下载了NAudio代码并且我一直在调试它但是我找不到错误的原因,但是,你可以在堆栈跟踪中看到的是 NAudio-Source \ NAudio \ FileFormats \ Mp3 \ Mp3FrameDecompressor.cs:第50行

我做错了吗?顺便说一句,它只发生在几个mp3文件,其他人读得很好。如果需要,我可以发送一个冲突的文件。

-------------------------编辑参考NAUDIO CODEPLEX的评论-------------- ------------------

在Codeplex Same question on Codeplex NAudio

上查看此问题

1 个答案:

答案 0 :(得分:0)

不要试图在一次点击中读取整个文件。相反,一次读取大约一秒钟值并继续读取,直到读取返回0。