2012-08-09 73 views
0

我正在开发一个使用mp3编码/解码的应用程序。虽然原则上它对大多数文件都是正确的,但也有一些例外。我检测到这些文件头部缺失。尝试解码时,我得到一个数组超出界限的异常。我使用了两种方法,但都失败了。无法读取解码的mp3流

第一:

DecodedMpegAudioInputStream dais = (DecodedMpegAudioInputStream) AudioSystem.getAudioInputStream(daisf, ais); 
    byte[] audioData = IOUtils.toByteArray(dais);//exception here 

而第二个:

ByteOutputStream bos = new ByteOutputStream(); 
    // Get the decoded stream. 
    byte[] byteData = new byte[1]; 
    int nBytesRead = 0; 
    int offset = 0; 
    int cnt=1; 

    while (nBytesRead != -1) { 
     System.out.println("cnt="+cnt); 
     nBytesRead = dais.read(byteData, offset, byteData.length);//exception here at first loop 
     if (nBytesRead != -1) { 
      int numShorts = nBytesRead >> 1; 
      for (int j = 0; j < numShorts; j++) { 
       bos.write(byteData[j]); 
      } 
     } 
     cnt+=1; 
    } 
    byte[] audioData = bos.getBytes(); 

似乎没有与标题或它的结构的问题,因为讲台流具有内容/字节。然而,它可以用大胆和ffplay打开,所以我相信应该有一个解决方法。任何想法如何对付它?

回答

1

您可以使用代码冗余来提高可靠性。研究其他库,如Xuggler或JLayer。