我使用MediaCodec
自 API 级别 16 以来 Android SDK 提供的类和OMX.SEC.aac.enc
编码器将音频编码为文件。我从AudioRecord
课堂上得到音频输入。我的类实例AudioRecord
配置如下:
bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_DEFAULT, bufferSize);
我能够播放AudioRecord
实例中的原始数据,所以问题不存在。
AudioRecord
我将实例的输出写入ByteBuffer
实例,并将其从编码器传递到可用的输入缓冲区。编码器的输出被写入 SD 卡上的文件。
这些是我的MediaCodec
实例的配置参数:
codec = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
VLC 告诉我我的 aac 文件中没有流。该命令FFMPEG -i @filename@
给了我以下错误:Invalid data found when processing input。我测试的所有媒体播放器都无法播放我的文件。
为什么我无法播放我的文件?我没有收到任何OpenMAX
错误,LogCat
并且应用程序在编码时不会崩溃。我写了一个基于相同原理工作的视频编码器,它可以工作。
这是将数据从AudioRecord
实例读取到缓冲区的代码:
new Thread() {
public void run() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
int read = 0;
while (isRecording) {
read = recorder.read(byteBuffer, bufferSize);
if(AudioRecord.ERROR_INVALID_OPERATION != read){
encoder.add(byteBuffer);
}
}
recorder.stop();
}
}.start();
我的编码器中的 add 函数将一个缓冲区的内容复制到另一个缓冲区:
public void add(ByteBuffer input) {
if (!isRunning)
return;
if (tmpInputBuffer == null)
tmpInputBuffer = ByteBuffer.allocate(input.capacity());
if (!tmpBufferClear)
Log.e("audio encoder", "deadline missed"); //TODO lower bit rate
synchronized (tmpInputBuffer) {
tmpInputBuffer.clear();
tmpInputBuffer.put(input);
tmpInputBuffer.notifyAll();
Log.d("audio encoder", "pushed data into tmpInputBuffer");
}
}
以下代码用于占用编码器的输入缓冲区:
new Thread() {
public void run() {
while (isRunning) {
if (tmpInputBuffer == null)
continue;
synchronized (tmpInputBuffer) {
if (tmpBufferClear) {
try {
Log.d("audio encoder", "falling asleep");
tmpInputBuffer.wait(); //wait when no input is available
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
ByteBuffer[] inputBuffers = codec.getInputBuffers();
int inputBufferIndex;
do
inputBufferIndex = codec.dequeueInputBuffer(-1);
while (inputBufferIndex < 0);
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
Log.d("input buffer size", String.valueOf(inputBuffer.capacity()));
Log.d("tmp input buffer size", String.valueOf(tmpInputBuffer.capacity()));
inputBuffer.put(tmpInputBuffer.array());
tmpInputBuffer.clear();
codec.queueInputBuffer(inputBufferIndex, 0, tmpInputBuffer.capacity(), 0, 0);
tmpBufferClear = true;
Log.d("audio encoder", "added to input buffer");
}
}
}
}.start();
我将编码器的输出写入本地文件,如下所示:
new Thread() {
public void run() {
while (isRunning) {
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, -1);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
try {
fileWriter.write(outData, 0, outData.length);
}
catch (IOException e) {
e.printStackTrace();
}
codec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, 0);
Log.d("audio encoder", "removed from output buffer");
}
}
codec.stop();
try {
fileWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}.start();
tmpBufferClear = true;
Log.d("audio encoder", "added to input buffer");
}
}
}
}.start();