javaandroidandroid-activityandroid-camera

Camera stops after one minute application suspend


I am using CameraX in my app. When I put application to background after one minute I got following logs and then when I resume the application the camera shows no preview (black screen). Probably I have configured something wrong with my activity, but have now no idea what it is.

public class AndroidLauncher extends AndroidApplication implements ImageAnalysis.Analyzer {
public class CustomLifecycle implements LifecycleOwner {
lifecycle = new CustomLifecycle();
cameraProvider.bindToLifecycle(this.lifecycle, cameraSelector, preview, imageCapture);

This log is being diplayed all the time while the app is in the background:

D/SurfaceView@e6a8dbc: updateSurface: surface is not valid
I/SurfaceView@e6a8dbc: releaseSurfaces: viewRoot = ViewRootImpl@99cd854[AndroidLauncher]

And after one minute this logs appears:

D/DeferrableSurface: use count-1,  useCount=1 closed=false androidx.camera.core.SurfaceRequest$2@58a2ee3
D/DeferrableSurface: use count-1,  useCount=0 closed=false androidx.camera.core.impl.ImmediateSurface@f89dc55
D/DeferrableSurface: Surface no longer in use[total_surfaces=3, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@f89dc55}
D/Camera2CameraImpl: CameraDevice.onError(): 0 failed with ERROR_CAMERA_DISABLED while in OPENED state. Will attempt recovering from error.
E/Camera2CameraImpl: Error observed on open (or opening) camera device 0: ERROR_CAMERA_DISABLED closing camera.

Then when I back to the app the camera shows no preview, but the whole app works fine.


Solution

  • Well... Probably solved. Needed to change whole main activity part.

    public class AndroidLauncher extends AppCompatActivity implements ImageAnalysis.Analyzer, AndroidFragmentApplication.Callbacks {
    

    And put LibGDX part to Fragment

    arr = new ArrowGame(this, new AndroidInterfaceClass());
    GameFragment lg = new GameFragment(arr);
    
    getSupportFragmentManager().beginTransaction().
       add(R.id.linear1, lg).
       commit();
    
    public class GameFragment extends AndroidFragmentApplication {
        AndroidLauncher al;
        ArrowGame arrowGame;
    
        public GameFragment(ArrowGame arrowGame) {
            this.arrowGame = arrowGame;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // return the GLSurfaceView on which libgdx is drawing game stuff
            return initializeForView(new ArrowGame(al, arrowGame, new AndroidInterfaceClass()));
        }
    }