最近测试手机的硬件编解码,经常遇到以下错误:

/net.quantum6.codectest W/System.err: java.lang.IllegalStateException
at android.media.MediaCodec.native_dequeueInputBuffer(Native Method)
at android.media.MediaCodec.dequeueInputBuffer(MediaCodec.java:2726)
at net.quantum6.mediacodec.AndroidVideoCodec.process(AndroidVideoCodec.java:116)
at net.quantum6.codectest.AbstractCodecHelper.processData(AbstractCodecHelper.java:146)
at net.quantum6.codectest.AbstractCameraHelper.onPreviewFrame(AbstractCameraHelper.java:167)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1288)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
net.quantum6.codectest W/System.err: java.lang.IllegalStateException
at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:2789)
at net.quantum6.mediacodec.AndroidVideoCodec.process(AndroidVideoCodec.java:142)
at net.quantum6.codectest.AbstractCodecHelper.processData(AbstractCodecHelper.java:146)
at net.quantum6.codectest.AbstractCameraHelper.onPreviewFrame(AbstractCameraHelper.java:167)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1288)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

  具体代码可以看:​​https://github.com/quantum6/Android-CodecTest​

  有时有,有时没有,是解码器产生的。什么原因吾亦搞不清楚。于是吾在收到异常时返回-1,由上层释放解码器,再重新初始化。