Skip to content

Commit 9e247bb

Browse files
committed
🚸 Initialize a new controller if failed to stop recording
Fix #39
1 parent 108796a commit 9e247bb

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/src/widget/camera_picker.dart

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class CameraPickerState extends State<CameraPicker>
253253

254254
/// Available cameras.
255255
/// 可用的相机实例
256-
late final List<CameraDescription> cameras;
256+
late List<CameraDescription> cameras;
257257

258258
/// Current exposure offset.
259259
/// 当前曝光值
@@ -763,16 +763,16 @@ class CameraPickerState extends State<CameraPicker>
763763
/// 将被取消,并且状态会重置。
764764
void recordDetectionCancel(PointerUpEvent event) {
765765
_recordDetectTimer?.cancel();
766-
if (controller.value.isRecordingVideo) {
767-
_lastShootingButtonPressedPosition = null;
768-
stopRecordingVideo();
769-
safeSetState(() {});
770-
}
771766
if (isShootingButtonAnimate) {
772767
safeSetState(() {
773768
isShootingButtonAnimate = false;
774769
});
775770
}
771+
if (controller.value.isRecordingVideo) {
772+
_lastShootingButtonPressedPosition = null;
773+
safeSetState(() {});
774+
stopRecordingVideo();
775+
}
776776
}
777777

778778
/// Set record file path and start recording.
@@ -790,8 +790,10 @@ class CameraPickerState extends State<CameraPicker>
790790
realDebugPrint('Error when start recording video: $e');
791791
if (controller.value.isRecordingVideo) {
792792
controller.stopVideoRecording().catchError((Object e) {
793-
realDebugPrint('Error when stop recording video: $e');
794-
throw e;
793+
realDebugPrint(
794+
'Error when stop recording video after an error start: $e',
795+
);
796+
stopRecordingVideo();
795797
});
796798
}
797799
throw e;
@@ -802,6 +804,12 @@ class CameraPickerState extends State<CameraPicker>
802804
/// Stop the recording process.
803805
/// 停止录制视频
804806
Future<void> stopRecordingVideo() async {
807+
void _handleError() {
808+
_recordCountdownTimer?.cancel();
809+
isShootingButtonAnimate = false;
810+
safeSetState(() {});
811+
}
812+
805813
if (controller.value.isRecordingVideo) {
806814
controller.stopVideoRecording().then((XFile file) async {
807815
final AssetEntity? entity = await CameraPickerViewer.pushToViewer(
@@ -814,16 +822,20 @@ class CameraPickerState extends State<CameraPicker>
814822
);
815823
if (entity != null) {
816824
Navigator.of(context).pop(entity);
817-
} else {
818-
safeSetState(() {});
819825
}
820826
}).catchError((Object e) {
821827
realDebugPrint('Error when stop recording video: $e');
828+
realDebugPrint('Try to initialize a new CameraController...');
829+
initCameras();
830+
_handleError();
822831
throw e;
823832
}).whenComplete(() {
824833
isShootingButtonAnimate = false;
834+
safeSetState(() {});
825835
});
836+
return;
826837
}
838+
_handleError();
827839
}
828840

829841
////////////////////////////////////////////////////////////////////////////

lib/src/widget/camera_picker_viewer.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
369369
@override
370370
Widget build(BuildContext context) {
371371
if (hasErrorWhenInitializing) {
372-
return Center(child: Text(Constants.textDelegate.loadFailed));
372+
return Center(
373+
child: Text(
374+
Constants.textDelegate.loadFailed,
375+
style: const TextStyle(inherit: false),
376+
),
377+
);
373378
}
374379
if (!hasLoaded) {
375380
return const SizedBox.shrink();

0 commit comments

Comments
 (0)