hjam40 / Camera.MAUI

A CameraView Control for preview, take photos and control the camera options
MIT License
438 stars 69 forks source link

Android only NullReferenceException on double StartCameraAsync #167

Open rombethor opened 3 weeks ago

rombethor commented 3 weeks ago

Calling cameraView.StartCameraAsync() twice caused a NullReferenceException on Android but not on Windows.

While I know it is not supposed to be called twice, it had no effect on Windows but did not throw any intuitive exception on Android, testing on emulator and device. Perhaps it is idempotent in Windows but not in Android?

Code:


    private void CameraView_CamerasLoaded(object? sender, EventArgs e)
    {
        if (cameraView.NumCamerasDetected > 0)
        {
            if (cameraView.NumMicrophonesDetected > 0)
                cameraView.Microphone = cameraView.Microphones.First();
            cameraView.Camera = cameraView.Cameras.First();
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                if (await cameraView.StartCameraAsync() == CameraResult.Success)
                {
                    playing = true;
                }
                await cameraView.StartCameraAsync(new Size(1280, 720));
            });
        }
    }

It seems that two exceptions were thrown, as follows: First:

 0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception  C#
 0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5    C#
 0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:122,26 C#
 0x5D in Camera.MAUI.Platforms.Android.MauiCameraView.StartPreview  C#
 0x15 in Camera.MAUI.Platforms.Android.MauiCameraView.MyCameraStateCallback.OnOpened    C#
 0x11 in Android.Hardware.Camera2.CameraDevice.StateCallback.n_OnOpened_Landroid_hardware_camera2_CameraDevice_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Hardware.Camera2.CameraDevice.cs:174,5 C#

Second:

0x23 in Camera.MAUI.Platforms.Android.MauiCameraView.StartPreview   C#
0x15 in Camera.MAUI.Platforms.Android.MauiCameraView.MyCameraStateCallback.OnOpened C#
0x11 in Android.Hardware.Camera2.CameraDevice.StateCallback.n_OnOpened_Landroid_hardware_camera2_CameraDevice_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Hardware.Camera2.CameraDevice.cs:174,5  C#
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:121,5    C#

If the call is idempotent in Windows, then really it should be in Android also. A more intuitive exception should be thrown if possible to inform the developer of their mistake.

spasham8234 commented 2 weeks ago

I had the same issue . In IOS its working fine and in Android its crashing. If not calling double times its showing white area instead of camera (only in Android) . Any help is greatly appreciated

ITaluone commented 4 days ago

Probably related (or the same issue?) #170