本文共 2626 字,大约阅读时间需要 8 分钟。
android AAC的音频硬解码
上一篇写了AAC音频的硬编码,今天补充硬解码,其实就是硬编码反过来,非常简单
初始化AAC硬解码资源:
public int Start() {
int iRet = 0;
_miVersion = android.os.Build.VERSION.SDK_INT;
if(_miVersion < 16) {
iRet = ErrorInfo.ERR_ANDROID_VER_NOT_SUPPORT;
return iRet;
}
if(mMediaDecode != null) {
return ErrorInfo.ERR_MEDIACODEC_EXIST_ALREADY;
}
if(_audioTrack != null) {
return ErrorInfo.ERR_AUDIOTRACK_EXIST_ALREADY;
}
mMediaDecode = MediaCodec.createDecoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, _miSamplingRate);
format.setInteger(MediaFormat.KEY_BIT_RATE, _miBitRate);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
mMediaDecode.configure(format, null, null, 0);
mMediaDecode.start();
_audioTrack = new MyAudioTrack(8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
_audioTrack.init();
inputBuffers = mMediaDecode.getInputBuffers();
outputBuffers = mMediaDecode.getOutputBuffers();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
_mbStartFlag = true;
_mbInitFlag = false;
return iRet;
}
对收到的AAC数据进行解码,并用_audioTrack进行播放:
需要注意的是, AAC音频第一个报文开始是一个两个字节的数据(0x15,0xF8),需要自己手动填写以下:
public void DecodeAudio(byte[] audioData) {
int iRet = 0; _miVersion = android.os.Build.VERSION.SDK_INT; if(_miVersion < 16) { return; } if(audioData == null) { return; } if(!_mbStartFlag) { return; } int iAudioDataLen = audioData.length; try { inputBufferIndex = mMediaDecode.dequeueInputBuffer(WAIT_TIME); if (inputBufferIndex >= 0) { inputBuffer = inputBuffers[inputBufferIndex]; inputBuffer.clear(); if(_mbInitFlag == false) { _mbInitFlag = true; byte[] fisrtData = new byte[2]; fisrtData[0] = 21; fisrtData[1] = -120; inputBuffer.put(fisrtData); }else { inputBuffer.put(audioData); } mMediaDecode.queueInputBuffer(inputBufferIndex, 0, iAudioDataLen, System.nanoTime()/1000, 0); } outputBufferIndex = mMediaDecode.dequeueOutputBuffer(bufferInfo, WAIT_TIME); while (outputBufferIndex >= 0) { outputBuffer = outputBuffers[outputBufferIndex]; outputBuffer.position(0); outData = new byte[bufferInfo.size]; outputBuffer.get(outData); Log.d(TAG, outData.length + " bytes decoded"); if (_audioTrack != null) { _audioTrack.playAudioTrack(outData, 0, outData.length); } mMediaDecode.releaseOutputBuffer(outputBufferIndex, false); outputBufferIndex = mMediaDecode.dequeueOutputBuffer(bufferInfo, WAIT_TIME); } } catch (Exception e) { Log.e(TAG, "Exception: "+Log.getStackTraceString(e)); } return; }
转载地址:http://iiima.baihongyu.com/