Bitstream bitstream = new Bitstream(file.read());
MP3Decoder decoder = new MP3Decoder();
try {
OutputBuffer outputBuffer = null;
int sampleRate = -1, channels = -1;
while (true) {
Header header = bitstream.readFrame();
if (header == null) break;
if (outputBuffer == null) {
channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2;
outputBuffer = new OutputBuffer(channels, false);
decoder.setOutputBuffer(outputBuffer);
sampleRate = header.getSampleRate();
}
try {
decoder.decodeFrame(header, bitstream);
} catch (Exception ignored) {
// JLayer's decoder throws ArrayIndexOutOfBoundsException sometimes!?
}
bitstream.closeFrame();
output.write(outputBuffer.getBuffer(), 0, outputBuffer.reset());
}
bitstream.close();
setup(output.toByteArray(), channels, sampleRate);
} catch (Throwable ex) {
throw new GdxRuntimeException("Error reading audio data.", ex);