我有MediaMuxer
:
MediaMuxer mMediaMuxer = new MediaMuxer(new File(Environment.getExternalStorageDirectory(), "video.mp4").getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
还有记录视频的代码:
MediaFormat mMediaFormat = MediaFormat.createVideoFormat("video/avc", width, height);
mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);
mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
mMediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, fps);
mMediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
MediaCodec mMediaCodec = MediaCodec.createEncoderByType("video/avc");
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
Surface mSurface = mMediaCodec.createInputSurface();
...
最后一串mMediaCodec.createInputSurface()
,扔给我java.lang.IllegalStateException
。堆栈跟踪:
java.lang.IllegalStateException
at android.media.MediaCodec.createInputSurface(Native Method)
at com.mypackage.Cam$Recorder$VideoRecorder.run(Cam.java:307)
和模拟器的日志:
02-05 07:42:55.024: I/OMXClient(2643): Using client-side OMX mux.
02-05 07:42:55.024: I/SoftAVCEncoder(2643): Construct SoftAVCEncoder
02-05 07:42:55.024: E/SoftAVCEncoder(2643): internalSetParameter: StoreMetadataInBuffersParams.nPortIndex not zero!
02-05 07:42:55.024: E/OMXNodeInstance(2643): OMX_SetParameter() failed for StoreMetaDataInBuffers: 0x80001001
02-05 07:42:55.024: E/ACodec(2643): [OMX.google.h264.encoder] storeMetaDataInBuffers (output) failed w/ err -2147483648
02-05 07:42:55.024: I/ACodec(2643): setupVideoEncoder succeeded
02-05 07:42:55.024: E/OMXNodeInstance(2643): createInputSurface requires COLOR_FormatSurface (AndroidOpaque) color format
02-05 07:42:55.024: E/ACodec(2643): [OMX.google.h264.encoder] onCreateInputSurface returning error -38
02-05 07:42:55.024: W/MediaCodec(2643): createInputSurface failed, err=-38
02-05 07:42:55.024: E/TAG(2643): java.lang.IllegalStateException
02-05 07:42:55.224: I/OMXClient(2643): Using client-side OMX mux.
02-05 07:42:55.254: W/SoftAACEncoder2(2643): Requested bitrate 128000 unsupported, using 48000
我观察到这个字符串
E/OMXNodeInstance(2643): createInputSurface requires COLOR_FormatSurface (AndroidOpaque) color format
说我Surface
必须在COLOR_FormatSurface
,但我已经设置好了
mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);
怎么了?如何解决这个问题呢?帮助。