Camera2Video black preview depending on video resolution
I'm having this problem with Huawei phones (P40, P40 pro, Mate 30 Pro). If I choose 1280x720 or 640x480 it will show a black preview and it will throw this exception:
2020-11-11 11:51:42.551 24901-5788/com.android.example.camera2.video E/CameraFragment: Camera 1 error: (4) Fatal (device) java.lang.RuntimeException: Camera 1 error: (4) Fatal (device) at com.example.android.camera2.video.fragments.CameraFragment$openCamera$$inlined$suspendCancellableCoroutine$lambda$1.onError(CameraFragment.kt:351) at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.notifyError(CameraDeviceImpl.java:2022) at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.lambda$Sm85frAzwGZVMAK-NE_gwckYXVQ(Unknown Source:0) at android.hardware.camera2.impl.-$$Lambda$CameraDeviceImpl$CameraDeviceCallbacks$Sm85frAzwGZVMAK-NE_gwckYXVQ.accept(Unknown Source:8) at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:271) at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:195) at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:86) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.os.HandlerThread.run(HandlerThread.java:67) 2020-11-11 11:51:43.792 24901-5788/com.android.example.camera2.video E/CameraCaptureSession: Session 0: Exception while stopping repeating: android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): The camera device has encountered a serious error at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:2534) at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1131) at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:526) at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:737) at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:242) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.os.HandlerThread.run(HandlerThread.java:67)
I found that the problem is in the creation of recorderSurface, when it execute createRecorder and it set setVideoSize with the chosen resolutions it will throw the exception and shows a black preview. If I set setVideoSize with 1920x1080 resolution in the creation of recourderSurface and the chosen resolution in recorder it will be just fine but I don't know if it will crash in other devices different than huawei brand...I've tried in a xiaomi mi 9 se and it works fine.
update: this workaround is not working with no huawei phones...and when you record a video with a huawei phone with a lower resolution than 1080p the result video will be cropped