WebcamTexture on Android, how do I check if the device cameras are switching?
I'm using WebCamTexture to display images streamed from the camera. I have a button with which the users can change camera, similar to this (there's more to it but this is basically it):
bool firstCamera = true;
private WebCamTexture webcamTexture;
private WebCamDevice[] webcamDevices;
public void Start() {
webcamDevices = WebCamTexture.devices;
webcamTexture = new WebCamTexture( webcamDevices[ 0 ].name, width, height);
}
// Button triggers this
public void OnChangeCameraPressed() {
firstCamera = !firstCamera
webcamTexture.Stop();
webcamTexture.deviceName = firstCamera ? webcamDevices[0].name : webcamDevices[1].name;
webcamTexture.Play();
}
The issue is that a user can spam-click the button and that leads to the app crashing. I get a crash similar to this:
android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError CameraDeviceImpl.java:2432
android.hardware.camera2.impl.CameraDeviceImpl.createCaptureRequest CameraDeviceImpl.java:783
com.unity3d.player.a$2.onConfigured Unknown Source:36
android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.lambda$onConfigured$0 CallbackProxies.java:53
android.hardware.camera2.impl.-$$Lambda$CallbackProxies$SessionStateCallbackProxy$soW0qC12Osypoky6AfLaP2-TeDw.run Unknown Source:4
android.os.Handler.handleCallback Handler.java:873
android.os.Handler.dispatchMessage Handler.java:99
android.os.Looper.loop Looper.java:214
android.os.HandlerThread.run HandlerThread.java:65
My guess is that it is a race condition in the Android framework that occurs when switching cameras quickly back-and-forth. If I add a cooldown to the button the app never crashes. Has anyone else experienced this? How can I check whether the device cameras are already switching before trying to switch cameras again?
Did you find a way to fix this? Seems likely to be related to issue #1262468, which appears to be fixed in Unity2020.2, but we're experiencing something similar in Unity2018.
Your answer
Follow this Question
Related Questions
Facebook Audience Network conflicts with WebCamTexture 0 Answers
How to turn off webCam in Android? 0 Answers
Unity Android camera crash 0 Answers
WebCam Texture Rotation 0 Answers