mediacodec异步模式

	/**
	 * 异步模式解码器
	 * @param surface
	 */
	public void setupAsynDecoder(Surface surface){
		try {
			mediaCodec = MediaCodec.createDecoderByType("video/avc");
		} catch (IOException e) {
			e.printStackTrace();
		}

		mediaCodec.setCallback(new MediaCodec.Callback() {
			@Override
			public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
				int length = 0;
				ByteBuffer inputBuffer = mediaCodec.getInputBuffer(index);
				Log.i(TAG, "inputbufferIndex:" + index);
				try {
					byte[] frameBuffer = ParamUtil.vedioQueue.consume();
					inputBuffer.clear();
					if(frameBuffer[0]==0 && frameBuffer[1]==0 && frameBuffer[2]==0 && frameBuffer[3]==1){
						inputBuffer.put(frameBuffer,0,frameBuffer.length);
						length = frameBuffer.length;
					}
					else {
						inputBuffer.put(frameBuffer,16,frameBuffer.length-16);
						length = frameBuffer.length - 16;
					}
					mediaCodec.queueInputBuffer(index, 0, length, mCount, 0);
					mCount = mCount +100;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull BufferInfo info) {
				ByteBuffer outputBuffer = mediaCodec.getOutputBuffer(index);
				MediaFormat bufferFormat = mediaCodec.getOutputFormat(index);
				Log.i(TAG, "ouputbufferIndex:" + index);
				mediaCodec.releaseOutputBuffer(index,true);
			}

			@Override
			public void onError(@NonNull MediaCodec codec, @NonNull MediaCodec.CodecException e) {

			}

			@Override
			public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {

			}
		});

		MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc",320, 240);
		mediaCodec.configure(mediaFormat, surface, null, 0);

		mediaCodec.start();

	}

你可能感兴趣的:(视频)