gpt4 book ai didi

flutter - 当应用程序进入后台时Flutter CamerPlugin “camera preview”被卡住

转载 作者:行者123 更新时间:2023-12-03 03:39:43 25 4
gpt4 key购买 nike

您好,我使用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上运行了很长时间,它将被废弃。

关于flutter - 当应用程序进入后台时Flutter CamerPlugin “camera preview”被卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58007521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com