androidapivideo-capturecamera2

android camera2 api MediaRecorder stop() error


I am very new in android and I am trying to create a simple video recorder app using android camera2 api. By following a tutorial on youtube I managed to preview the camera in a TextureView and start capturing with MediaRecorder, but I am having trouble on stopping the recorder and saving the video to my device storage.

This error appears on MediaRecorder.stop()

W/Adreno-EGLSUB: : dequeue native buffer fail: No such device, buffer=0x0, handle=0x0 W/Adreno-EGL: : EGL_BAD_SURFACE E/CameraDeviceGLThread-0: Received exception on GL render thread: java.lang.IllegalStateException: swapBuffers: EGL error: 0x300d at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:544) at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:537) at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:741) at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:148) at android.os.HandlerThread.run(HandlerThread.java:61) I/CameraDeviceState: Legacy camera service transitioning to state ERROR

I am using an Android 6.0 api23 device to run the app.

Thanks

EDIT: Here is the stopRecording function.

                try{
                    previewSession.stopRepeating();
                    previewSession.abortCaptures();
                }catch(CameraAccessException e){
                    e.printStackTrace();
                }
                recorder.stop();
                recorder.reset();
                isRecording = false;
                Toast.makeText(getApplicationContext(),"Recording Stopped",Toast.LENGTH_SHORT).show();

...based on @NewOne's answer, I included the try catch before the recorder.stop() then I have a new error

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tingker.daryl.camera2, PID: 9014 Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo} java.lang.IllegalStateException: Session has been closed; further changes are illegal. at android.hardware.camera2.impl.CameraCaptureSessionImpl.checkNotClosed(CameraCaptureSessionImpl.java:606) at android.hardware.camera2.impl.CameraCaptureSessionImpl.stopRepeating(CameraCaptureSessionImpl.java:272) at com.tingker.daryl.camera2.MainActivity$2.onClick(MainActivity.java:121) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21158) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Solution

  • I don't know, currently, what is there in your stop recording method. If you could share, may be I can help better. Anyways make sure you have the below lines of code before you call the stop method.

    private void stopRecording()) {
    
            .....
    
            try {
                preview.stopRepeating();
                preview.abortCaptures();
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
    
            try{
               mediaRecorder.stop();
            }catch(RuntimeException e){
               //handle the exception
            }
            mediaRecorder.reset();
    
            ......
        }
    

    Check this link. You may catch the exception if you are stopping the recorder immediately after starting it.