您好,我使用flutter cameraPreview捕获照片时,相机预览可以正常工作,但是当相机屏幕进入背景时,在该屏幕上恢复播放时,相机屏幕会变得模糊,无法在恢复状态下查看cameraPrview。

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 配置合理。

我想知道为什么暂停时会丢弃相机预览。

最佳答案

通过初始化相机解决了问题

 @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"
    }
  }

在恢复到页面时,将调用_controller.initialize,以便cameraPreview正常运行。

这是由于cameraPreview在onPause上运行了很长时间,它将被废弃。

10-08 03:27