Potney Switters
Potney Switters

Reputation: 3062

Decoded mp3 stream can not be read

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?

Upvotes: 0

Views: 133

Answers (1)

Dan Gravell
Dan Gravell

Reputation: 8240

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

Upvotes: 1

Related Questions