Open usausa opened 1 year ago
For example, could this problem be avoided by using TaskCompletionSource to prevent StartCameraAsync() from exiting until CameraDevice.StateCallback.OnOpened() is called?
internal async Task<CameraResult> StartRecordingAsync(string file, Microsoft.Maui.Graphics.Size Resolution)
{
...
if (OperatingSystem.IsAndroidVersionAtLeast(28))
cameraManager.OpenCamera(cameraView.Camera.DeviceId, executorService, stateListener);
else
cameraManager.OpenCamera(cameraView.Camera.DeviceId, stateListener, null);
started = true;
// await CameraDevice.StateCallback.OnOpened()
await stateListener.Completion.Task;
...
private class MyCameraStateCallback : CameraDevice.StateCallback
{
public TaskCompletionSource<bool> Completion { get; } = new();
private readonly MauiCameraView cameraView;
public MyCameraStateCallback(MauiCameraView camView)
{
cameraView = camView;
}
public override void OnOpened(CameraDevice camera)
{
if (camera != null)
{
cameraView.cameraDevice = camera;
cameraView.StartPreview();
}
// Open completed
Completion.TrySetResult(true);
}
...
It is also desirable to have status checks and exception handling in StartPreview().
Have you got any solution? I am also faciing same issue.
You can try my pull (https://github.com/hjam40/Camera.MAUI/pull/107). It should have resolved almost all the crashes.
In the Android environment, Quickly and repeatedly calling StartCameraAsync()/StopCameraAsync() while adding/removing a CameraView to the parent will result in a Java.Lang.IllegalStateException: 'CameraDevice was already closed' exception.