I'am a beginner. I would like to write an app, which has many activities and one othe them is for setting video recording on background but within camera preview. So when I leave the activity the recording must continue.
- I have a service started from the main activity. This service manage the camera and video recording.
- The service containes initialized and "prepared" MediaRecorder instance, "recorder". It's set to source from surface like recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE).
- Camera is opened by cameraManager.openCamera(cameraId, etc.) and cameraDevice.createCaptureSession(surfaceList, etc.) where surfaceList contains surface from the MediaRecorder instance and surface from a SurfaceTexture instance created in the onCreate function of this service.
- Capturing is done by cameraDevice.createCaptureRequest(some arguments) and cameraCaptureSession.setRepeatingRequest(some arguments). Of course I don't forget add surfaces by requestBuilder.addTarget().
- I pass the SurfaceTexture instance to the upper activity, where I pass it to the existing TextureView UI element like textureView.setSurfaceTexture(surfaceTextureFromTheService). Works well.
- I can start recording video to the file by recorder.start(). Recording works well, camera preview too.
- NOW! I would like to completely leave the activity (but still stay in the app) and somehow remove the surfaceTextureFromTheService from the activity UI. If I simply leave the activity I got an exception:
E/BufferQueueProducer: [SurfaceTexture-0-29154-1] dequeueBuffer: BufferQueue has been abandoned 03-04 22:08:47.525 29154-29440/com.example.hakka.androidcarhelper I/Adreno: DequeueBuffer: dequeueBuffer failed 03-04 22:08:47.525 29154-29440/com.example.hakka.androidcarhelper 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:530) at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:523) at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:727) 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 understand I do something wrong if I try to leave activity without releasing the surfaceTextureFromTheService. But how to do it? How can I release the surfaceTextureFromTheService but still to be able reuse it again after I return back to the activity? And of course without calling cameraDevice.createCaptureRequest and cameraCaptureSession.setRepeatingRequest again in the service because if the MediaRecorder is recording I don't want to stop it everytime I leave and return to the activity.
I have spent about 6 days 12 hours a day with solving this problem. But still no luck.
Thank you.