【问题标题】:Destination array was not long anough when reading mp3 file with NAudio使用 NAudio 读取 mp3 文件时,目标数组不够长
【发布时间】:2012-09-19 20:54:39
【问题描述】:

我有这个代码:

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 上查看此问题

【问题讨论】:

    标签: naudio


    【解决方案1】:

    不要试图一键读取整个文件。相反,一次阅读大约一秒钟,然后继续阅读,直到 Read 返回 0。

    【讨论】:

    • 嗨,马克,您已经在 [Codeplex 线程] (naudio.codeplex.com/workitem/16345) 上回答了这个问题。问题是 NAudio 不能正确处理不同采样率的帧,由于缓冲区长度,它会抛出上面的异常。实际上以较小的块读取文件并不能解决问题,所以我不会将此标记为答案。如果您愿意,请发布对 codeplex 线程的引用,我会将其标记为答案。谢谢
    猜你喜欢
    • 2014-02-22
    • 2014-05-11
    • 2012-05-08
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2017-06-20
    相关资源
    最近更新 更多