From 34a5b011cd3fe3b231770a3bae984175d2c983e2 Mon Sep 17 00:00:00 2001 From: ztenghui <ztenghui@google.com> Date: Fri, 31 May 2013 10:50:04 -0700 Subject: MediaMuxer sample code update, no functional change bug:9213533 Change-Id: I0d42ec58576d9fd797dba9c9c9f5ebed1170a818 --- media/java/android/media/MediaMuxer.java | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'media') diff --git a/media/java/android/media/MediaMuxer.java b/media/java/android/media/MediaMuxer.java index e49a00e..774964e 100644 --- a/media/java/android/media/MediaMuxer.java +++ b/media/java/android/media/MediaMuxer.java @@ -35,20 +35,29 @@ import java.util.Map; * It is generally used like this: * * <pre> - * MediaMuxer muxer = new MediaMuxer(...); + * MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); + * // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat() + * // or MediaExtractor.getTrackFormat(). * MediaFormat audioFormat = new MediaFormat(...); * MediaFormat videoFormat = new MediaFormat(...); * int audioTrackIndex = muxer.addTrack(audioFormat); * int videoTrackIndex = muxer.addTrack(videoFormat); - * ByteBuffer inputBuffer = ByteBuffer.allocate(...); + * ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize); + * boolean finished = false; + * BufferInfo bufferInfo = new BufferInfo(); + * * muxer.start(); - * while(inputBuffer has new data) { - * if (new data is audio sample) { - * muxer.writeSampleData(audioTrackIndex, inputBuffer, ...); - * } else if (new data is video sample) { - * muxer.writeSampleData(videoTrackIndex, inputBuffer, ...); + * while(!finished) { + * // getInputBuffer() will fill the inputBuffer with one frame of encoded + * // sample from either MediaCodec or MediaExtractor, set isAudioSample to + * // true when the sample is audio data, set up all the fields of bufferInfo, + * // and return true if there are no more samples. + * finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo); + * if (!finished) { + * int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex; + * muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo); * } - * } + * }; * muxer.stop(); * muxer.release(); * </pre> -- cgit v1.1