我正在尝试为我的应用程序实现暂停/恢复功能,该功能使用MediaCodec记录显示捕获.
我已经尝试过mEncoder.stop()然后调用mEncoder.start()而不调用mEncoder.release()但是没有用.再次调用mEncoder.start()时,我得到IllegalStateException.
现在我实施了一个解决方法,我在捕获完成后合并视频的和平,但合并需要很长时间.
任何人都可以帮助我吗?也许有人已经实现了这个东西?
我已经尝试过mEncoder.stop()然后调用mEncoder.start()而不调用mEncoder.release()但是没有用.再次调用mEncoder.start()时,我得到IllegalStateException.
现在我实施了一个解决方法,我在捕获完成后合并视频的和平,但合并需要很长时间.
任何人都可以帮助我吗?也许有人已经实现了这个东西?
Initialazation:
MediaCodec mEncoder; mEncoder = MediaCodec.createEncoderByType(Preferences.MIME_TYPE); mEncoder.configure(mFormat,null,MediaCodec.CONFIGURE_FLAG_ENCODE); mInputSurface = new InputSurface(mEncoder.createInputSurface(),mSavedEglContext); mEncoder.start(); try { String fileId = String.valueOf(System.currentTimeMillis()); mMuxer = new MediaMuxer(dir.getPath() + "/Video" + fileId + ".mp4",MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); videoParts.add(fileId); } catch (IOException ioe) { throw new RuntimeException("MediaMuxer creation Failed",ioe); } isRecording = true;
暂停:
public void pauseRecordPressed() { if (isRecording){ isRecording = false; drainEncoder(false); if (mEncoder != null) { mEncoder.stop(); } } }
取消暂停:
public void resumeRecordPressed() { mEncoder.start(); isRecording = true; }
例外:
01-09 15:34:27.980: E/AndroidRuntime(21467): FATAL EXCEPTION: main 01-09 15:34:27.980: E/AndroidRuntime(21467): Process: com.example.poc,PID: 21467 01-09 15:34:27.980: E/AndroidRuntime(21467): java.lang.IllegalStateException: start Failed 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.media.MediaCodec.start(Native Method) 01-09 15:34:27.980: E/AndroidRuntime(21467): at com.example.poc.MyRenderer.resumeRecordPressed(MyRenderer.java:501) 01-09 15:34:27.980: E/AndroidRuntime(21467): at com.example.poc.MyGLSurfaceView.resumeRecordPressed(MyGLSurfaceView.java:243) 01-09 15:34:27.980: E/AndroidRuntime(21467): at com.example.poc.MainActivity.onClick(MainActivity.java:775) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.view.View.performClick(View.java:4438) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.view.View$PerformClick.run(View.java:18422) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.os.Handler.handleCallback(Handler.java:733) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.os.Handler.dispatchMessage(Handler.java:95) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.os.Looper.loop(Looper.java:136) 01-09 15:34:27.980: E/AndroidRuntime(21467): at android.app.ActivityThread.main(ActivityThread.java:5017) 01-09 15:34:27.980: E/AndroidRuntime(21467): at java.lang.reflect.Method.invokeNative(Native Method) 01-09 15:34:27.980: E/AndroidRuntime(21467): at java.lang.reflect.Method.invoke(Method.java:515) 01-09 15:34:27.980: E/AndroidRuntime(21467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 01-09 15:34:27.980: E/AndroidRuntime(21467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 01-09 15:34:27.980: E/AndroidRuntime(21467): at dalvik.system.NativeStart.main(Native Method)
MediaFormat:
mFormat = createMediaFormat(); private static MediaFormat createMediaFormat() { MediaFormat format = MediaFormat.createVideoFormat( Preferences.MIME_TYPE,mScreenWidth,mScreenHeight); format.setInteger(MediaFormat.KEY_COLOR_FORMAT,MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_BIT_RATE,Preferences.BIT_RATE); format.setInteger(MediaFormat.KEY_FRAME_RATE,Preferences.FRAME_RATE); format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,Preferences.IFRAME_INTERVAL); return format; }