问题描述:

I am trying to overlap two audio files with m4a format using MediaCodec. I have sucessfully mixed the files, combining them into an array of short. I can play the mixed short array using Android AudioTrack, however, i need to create a new audio file using MediaMuxer. I have used an Encoder to read the mixed short array and tried to use the muxer to create the file. The file is created but no sound is actually played. Here is the code i am currently using to write on MediaMuxer from the encoder i have created.

 private void writeOnMuxer(MediaCodec codec, ByteBuffer[] codecInputBuffers,

ByteBuffer[] codecOutputBuffers, ByteBuffer data, MediaMuxer mux, int length){

MediaCodec.BufferInfo outBuffInfo = new MediaCodec.BufferInfo();

MediaFormat outputFormat = null;

int inputBufIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_IN_MS);

if (inputBufIndex >= 0) {

ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];

dstBuf.clear();

dstBuf.put(data);

long presentationTimeUs = 1000000l * (length / 2) / SAMPLING_RATE;

codec.queueInputBuffer(inputBufIndex, 0, length, (long) presentationTimeUs, 0);

}

// Drain audio

int outputBufIndex = 0;

while (outputBufIndex != MediaCodec.INFO_TRY_AGAIN_LATER) {

outputBufIndex = codec.dequeueOutputBuffer(outBuffInfo, CODEC_TIMEOUT_IN_MS);

if (outputBufIndex >= 0) {

ByteBuffer encodedData = codecOutputBuffers[outputBufIndex];

encodedData.position(outBuffInfo.offset);

encodedData.limit(outBuffInfo.offset + outBuffInfo.size);

if ((outBuffInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0 && outBuffInfo.size != 0) {

codec.releaseOutputBuffer(outputBufIndex, false);

} else {

mux.writeSampleData(0, encodedData, outBuffInfo);

codec.releaseOutputBuffer(outputBufIndex, false);

}

} else if (outputBufIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {

outputFormat = codec.getOutputFormat();

Log.v("test", "Output format changed - " + outputFormat);

mux.addTrack(outputFormat);

mux.start();

} else if (outputBufIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {

Log.e("test", "Output buffers changed during encode!");

} else if (outputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {

// NO OP

} else {

Log.e("test", "Unknown return code from dequeueOutputBuffer - " + outputBufIndex);

}

}

}

// Encoder config

MediaCodec encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");

encoder.configure(outputFormat, null, null,

MediaCodec.CONFIGURE_FLAG_ENCODE);

encoder.start();

ByteBuffer[] codecInputBuffers3 = encoder.getInputBuffers();

ByteBuffer[] codecOutputBuffers3 = encoder.getOutputBuffers();

// Mixing short array

short[] shortArrayThree = mix(shortArrayOne, shortArrayTwo);

if (shortArrayThree.length > 0) {

//track.write(shortArrayThree, 0, shortArrayThree.length);

ByteBuffer byteBuf = ByteBuffer.allocate(2 * shortArrayThree.length);

int index;

for (index = 0; index != shortArrayThree.length; index++) {

byteBuf.putShort(shortArrayThree[index]);

}

//muxer.writeSampleData(0, byteBuf, info);

writeOnMuxer(encoder,codecInputBuffers3,codecOutputBuffers3,byteBuf, muxer,shortArrayThree.length);

}

相关阅读:
Top