我正在尝试使用 JLayer java lib 来解码 mp3 数据流。当下一个 mp3 数据块从网络到达时,我有一个异步调用的回调。到达的每个块都包含 4 个byte[]
格式的 mp3 帧。将此数据传递给short[] decode(byte[] mp3_data)
要解码的,输出是一个short[]
pcm 音频缓冲区。使用该concatArray()
方法将缓冲区附加到 while 循环内,直到用完所有 mp3 帧。我遇到的问题是前 2 帧或有时 3 帧数据返回填充零的 pcm 缓冲区,而最后 2 帧或 1 帧返回有效的 16 位音频值。
public short[] decode(byte[] mp3_data) throws IOException {
SampleBuffer output = null;
InputStream inputStream = new ByteArrayInputStream(mp3_data);
short[] pcmOut = {};
try {
Bitstream bitstream = new Bitstream(inputStream);
Decoder decoder = new Decoder();
boolean done = false;
int i = 0;
while (! done) {
Header frameHeader = bitstream.readFrame();
if (frameHeader == null) {
done = true;
} else {
output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
short[] next = output.getBuffer();
pcmOut = concatArrays(pcmOut, next);
}
bitstream.closeFrame();
i++;
}
return pcmOut;
} catch (BitstreamException e) {
throw new IOException("Bitstream error: " + e);
} catch (DecoderException e) {
Log.w(LOG_TAG, "Decoder error", e);
}
return null;
}
short[] concatArrays(short[] A, short[] B) {
int aLen = A.length;
int bLen = B.length;
short[] C= new short[aLen+bLen];
System.arraycopy(A, 0, C, 0, aLen);
System.arraycopy(B, 0, C, aLen, bLen);
return C;
}
日志输出
Frame 0 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 1 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 2 len: 2304, First 10 samples: [-4128, -4158, -4252, -3934, -4452, -3775, -4799, -3762, -5430, -4092]
Frame 3 len: 2304, First 10 samples: [-18050, -19711, -18184, -19753, -18143, -19595, -17046, -18362, -14773, -15933]
Frame 0 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 1 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 2 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 3 len: 2304, First 10 samples: [2455, 2345, 5253, 5129, 6716, 6442, 7475, 6866, 8461, 7444]
Frame 0 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 1 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 2 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 3 len: 2304, First 10 samples: [951, 1322, 1497, 1929, 1615, 2198, 1320, 2134, 1040, 2114]
Frame 0 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 1 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 2 len: 2304, First 10 samples: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Frame 3 len: 2304, First 10 samples: [-10213, -9578, -11691, -10867, -13686, -12770, -14837, -13874, -15619, -14574]
如您所见,打印出每个 4 帧 mp3 块的 pcm 缓冲区,您可以看到前 2 - 3 个缓冲区被零填充。有没有人对 JLayer 有任何可以看出我的方法存在明显问题的经验?