当采样率为44100时,可以解码aac字节数组,并且i 可以发出声音,但其他人无法正确解码。我不知道 为什么呢?
在编码之前,我设置了MediaFormat.csd-0是来自其他样本的副本,并且将显示MediaFormat参数。
private boolean prepare() {
String codecName = null;
for (int i = 0; i < MediaCodecList.getCodecCount(); i++) {
MediaCodecInfo mediaCodecInfo = MediaCodecList.getCodecInfoAt(i);
for (String type : mediaCodecInfo.getSupportedTypes()) {
if (TextUtils.equals(type, MIME_TYPE)
&& mediaCodecInfo.isEncoder()) {
codecName = mediaCodecInfo.getName();
break;
}
}
if (null != codecName) {
break;
}
}
try {
mBufferInfo = new MediaCodec.BufferInfo();
mEncoder = MediaCodec.createByCodecName(codecName);
MediaFormat mediaFormat = MediaFormat.createAudioFormat(MIME_TYPE,
KEY_SAMPLE_RATE, KEY_CHANNEL_COUNT);
mediaFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, KEY_BIT_RATE);
mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE,
KEY_AAC_PROFILE);
mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, mFrameSize);
byte[] data = new byte[]{(byte) 0x11, (byte) 0x90};
ByteBuffer csd_0 = ByteBuffer.wrap(data);
mediaFormat.setByteBuffer("csd-0", csd_0);
mEncoder.configure(mediaFormat, null, null,
MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoder.start();
} catch (IOException e) {
e.printStackTrace();
return false;
}
mBuffer = new byte[mFrameSize];
int minBufferSize = AudioRecord.getMinBufferSize(KEY_SAMPLE_RATE, CHANNEL_IN,
AUDIO_FORMAT);
mRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
KEY_SAMPLE_RATE, CHANNEL_IN, AUDIO_FORMAT, minBufferSize * 2);
mRecord.startRecording();
return true;
}
使用android MediaCodec api对aac进行编码,while.encoderCallback将被MediaCodec调用解码。
long presentationTimeUs = 0;
private void encode(byte[] data) {
//api > 21
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
int inputBufferIndex = mEncoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = mEncoder.getInputBuffer(inputBufferIndex);
inputBuffer.clear();
inputBuffer.put(data);
inputBuffer.limit(data.length);
mEncoder.queueInputBuffer(inputBufferIndex, 0, data.length,
computePresentationTime(presentationTimeUs), BUFFER_FLAG_CODEC_CONFIG);
presentationTimeUs+=1;
}
int outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = mEncoder.getOutputBuffer(outputBufferIndex);
outputBuffer.position(mBufferInfo.offset);
outputBuffer.limit(mBufferInfo.offset + mBufferInfo.size);
int length = mBufferInfo.size + 7;
if (mFrameByte == null || mFrameByte.length < length) {
mFrameByte = new byte[length];
}
addADTStoPacket(mFrameByte, length);
outputBuffer.get(mFrameByte, 7, mBufferInfo.size);
if (encoderCallback!=null){
encoderCallback.encode(mFrameByte);
mFrameByte = null;
}
mEncoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);
}
}
添加adts标题。
private void addADTStoPacket(byte[] packet, int packetLen) {
int profile = KEY_AAC_PROFILE; //AAC LC
int freqIdx = FREQ_IDX; //44.1KHz
int chanCfg = CHAN_CFG; //CPE
packet[0] = (byte) 0xFF;
packet[1] = (byte) 0xF9;
packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
packet[6] = (byte) 0xFC;
}
这些是关于采样率的参数。
public interface AudioCodec {
String MIME_TYPE = MediaFormat.MIMETYPE_AUDIO_AAC;
int KEY_CHANNEL_COUNT = 2;
int KEY_BIT_RATE = 64 * 1024;
int KEY_SAMPLE_RATE = 44100;
int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
int WAIT_TIME = 10000;
int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
int BUFFFER_SIZE = 2048;
int FREQ_IDX = 4;
int CHAN_CFG = 1;
}
答案 0 :(得分:0)
也许我的英语很差,所以有人无法理解我的描述。现在,我已经找到了AAC文件不起作用或以半速播放的原因.AAC的csd-0配置决定是否成功。
我将params设置为MediaCodec,用于编码和解码。采样率为44100.
Mongo db
csd-0设置为通过MediaCodec从pcm编码aac。
int KEY_CHANNEL_COUNT = 2;
int KEY_BIT_RATE = 64*1024;
int KEY_SAMPLE_RATE = 44100;
int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
int WAIT_TIME = 10000;
int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
int BUFFFER_SIZE = 2048;
int FREQ_IDX = 4;
int CHAN_CFG = 2;
byte CSD_0 = 0x12;
byte CSD_1 = (byte) 0x12;
csd-0可以通过您的采样率进行更改。例如,您的采样率为8000。
byte[] data = new byte[]{CSD_0, CSD_1};
ByteBuffer csd_0 = ByteBuffer.wrap(data);
mediaFormat.setByteBuffer("csd-0", csd_0);