0

I am developing an application that uses mp3 encoding/decoding. While in principle it behaves correctly for most of the files, there are some exceptions. I detected that these files have a missing header. I get an array out of bound exception when attempting to decode. I used two approaches but both failed.

The first:

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

And the second:

    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();

It seems that there is an issue with the headers or the structure of it, because the dais stream has content/bytes. However it can be opened with audacity and ffplay so I believe there should be a workaround. Any ideas how to counter it?

Potney Switters
  • 2,902
  • 4
  • 33
  • 51

1 Answers1

1

You could use code redundancy to improve reliability. Look into alternative libraries, such as Xuggler or JLayer.

Dan Gravell
  • 7,855
  • 7
  • 41
  • 64