简体   繁体   中英

Flutter CamerPlugin “camera preview” freezed when app goes to background

Hi iam using flutter cameraPreview to capture photo, the camera preview works fine but when the camera screen goes to background, while on resumed on that screen the camera screen is frezzed, cant able to view cameraPrview onResume.

mycode:

Future<void> _initializeCamera() async {
final cameras = await availableCameras();
final firstCamera = cameras.first;
_controller = CameraController(firstCamera, ResolutionPreset.high);
_initializeControllerFuture = _controller.initialize();
if (!mounted) {
  return;
}
setState(() {
  isCameraReady = true;
});
}


@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: Stack(children: <Widget>[
        FutureBuilder<void>(
          future: _initializeControllerFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              // If the Future is complete, display the preview.
              return CameraPreview(_controller);
            } else {
              return Center(
                  child:
                      CircularProgressIndicator()); // Otherwise, display a loading indicator.
            }
          },
        ),],),);

controller is disposed prpoperly.

I want to know why camera preview is disposed while pause.

Issue is solved by initialised cameraPreview onResume

 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      _controller != null
          ? _initializeControllerFuture = _controller.initialize()
          : null; //on pause camera disposed so we need call again "issue is only for android"
    }
  }

On resuming to the page, _controller.initialize will call, so that cameraPreview will works fine.

This is due to cameraPreview runs for long on onPause, it will be disposed..onAndroid i think so..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM