本文主要是介绍MediaCodec Demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/dahuaishu2010_/article/details/14103299
MediaCodec Demo
分类:
public class AvcEncoder {private MediaCodec mediaCodec;
private BufferedOutputStream outputStream;public AvcEncoder() { File f = new File(Environment.getExternalStorageDirectory(), "Download/video_encoded.264");touch (f);try {outputStream = new BufferedOutputStream(new FileOutputStream(f));Log.i("AvcEncoder", "outputStream initialized");} catch (Exception e){ e.printStackTrace();}mediaCodec = MediaCodec.createEncoderByType("video/avc");MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);mediaCodec.start();
}public void close() {try {mediaCodec.stop();mediaCodec.release();outputStream.flush();outputStream.close();} catch (Exception e){ e.printStackTrace();}
}// called from Camera.setPreviewCallbackWithBuffer(...) in other class
public void offerEncoder(byte[] input) {try {ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);if (inputBufferIndex >= 0) {ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];inputBuffer.clear();inputBuffer.put(input);mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);}MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo,0);while (outputBufferIndex >= 0) {ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];byte[] outData = new byte[bufferInfo.size];outputBuffer.get(outData);outputStream.write(outData, 0, outData.length);Log.i("AvcEncoder", outData.length + " bytes written");mediaCodec.releaseOutputBuffer(outputBufferIndex, false);outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);}} catch (Throwable t) {t.printStackTrace();}}
- 顶
- 0
- 踩
这篇关于MediaCodec Demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!