The issue is if the Video Button is used more than once the program appears to crash when the Stop button is pressed.
Its caused by the session.RunMethod("abortCaptures", Null)
Adding Sleep(100)after the line cures the issue for some phones I use and not others
I have found for the cameras I use that Sleep (XXX) does not fix then, that changing the code to
if CameraMode<>legacy then session.RunMethod("abortCaptures", Null) then the issue goes away.
I haven't seen any performance issue with the line missing yet. Perhaps others will/have.
When the crash appears
Msg Box shows "Camera2 Example has stopped"
In fact, clicking the bottom right-most button shows the APP is currently running.
And the log also shows the APP has paused
Selecting the App brings it back, recovered.
I was hoping that adding a watchdog would allow me to recover the APP, then I would run the fix mentioned above only on affected cameras. I am slightly hesitant to run the fix on every Legacy in case there are performance issues.
see https://www.b4x.com/android/forum/threads/simple-watchdog-not-working.109361/
Detail of changes to prevent crash on affected Legacy phones
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.hardware.camera2.params.OutputConfiguration.getSurfaces()' on a null object reference
at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onCaptureErrorLocked(CameraDeviceImpl.java:2095)
at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onDeviceError(CameraDeviceImpl.java:1765)
at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraCallbackThread$CallbackHandler.handleMessage(CameraDeviceUserShim.java:309)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.os.HandlerThread.run(HandlerThread.java:65)
** Activity (main) Pause, UserClosed = true **
Ignoring event: camera_surfaceready
Sleep not resumed (context is paused): b4a.example3.camex2$ResumableSub_CloseSession
Sleep not resumed (context is paused): b4a.example3.camex2$ResumableSub_CloseSession
			
			Its caused by the session.RunMethod("abortCaptures", Null)
Adding Sleep(100)after the line cures the issue for some phones I use and not others
I have found for the cameras I use that Sleep (XXX) does not fix then, that changing the code to
if CameraMode<>legacy then session.RunMethod("abortCaptures", Null) then the issue goes away.
I haven't seen any performance issue with the line missing yet. Perhaps others will/have.
When the crash appears
Msg Box shows "Camera2 Example has stopped"
In fact, clicking the bottom right-most button shows the APP is currently running.
And the log also shows the APP has paused
Selecting the App brings it back, recovered.
I was hoping that adding a watchdog would allow me to recover the APP, then I would run the fix mentioned above only on affected cameras. I am slightly hesitant to run the fix on every Legacy in case there are performance issues.
see https://www.b4x.com/android/forum/threads/simple-watchdog-not-working.109361/
Detail of changes to prevent crash on affected Legacy phones
			
				B4X:
			
		
		
		Added
In Sub Process_Globals
   Public Supported_Camera_Hardware_Level As String= "Unknown"
   Public Video_About_To_Stop As Boolean=False
In Sub Activity_Create(FirstTime As Boolean)
   Supported_Camera_Hardware_Level=cam.SupportedHardwareLevel
   Log(Supported_Camera_Hardware_Level)
Changes
Private Sub CloseSession
   Dim session As JavaObject = jcamera.GetFieldJO("captureSession")
   If session.IsInitialized Then
       session.RunMethod("stopRepeating", Null)
       If Main.Video_About_To_Stop=True And Main.Supported_Camera_Hardware_Level="LEGACY" Then
           'Avoid abortCaptures during Video Stop
       Else
           session.RunMethod("abortCaptures", Null)
       End If
       jcamera.SetField("captureSession", Null)
   End If
End Sub
Public Sub StopVideoRecording (MyTaskIndex As Int)
   Main.Video_About_To_Stop=True
   CloseSession
   Main.Video_About_To_Stop=False
   Try
       MediaRecorder.RunMethod("stop", Null)
   Catch
       Log(LastException)
   End Try
   RecordingVideo = False
End Sub
	java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.hardware.camera2.params.OutputConfiguration.getSurfaces()' on a null object reference
at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onCaptureErrorLocked(CameraDeviceImpl.java:2095)
at android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onDeviceError(CameraDeviceImpl.java:1765)
at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraCallbackThread$CallbackHandler.handleMessage(CameraDeviceUserShim.java:309)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.os.HandlerThread.run(HandlerThread.java:65)
** Activity (main) Pause, UserClosed = true **
Ignoring event: camera_surfaceready
Sleep not resumed (context is paused): b4a.example3.camex2$ResumableSub_CloseSession
Sleep not resumed (context is paused): b4a.example3.camex2$ResumableSub_CloseSession
			
				Last edited: