Skip to content

Commit f9da37b

Browse files
authored
[camera_android_camerax] Fix video recording after backgrounding app (flutter#12145)
Clears the cached recording when `dispose` is called to ensure that the video recording state is cleared when the app backgrounds/resumes. This action does not impact the recording saving; it just updates the state so the plugin knows that the camera is available to start a new recording. Fixes flutter#183880. ----- Normal pull request information above the line. This pull request is an attempt to "oneshot" fixing flutter#183880. Using Antigravity to execute a plan that is collaboratively worked on. The "rules" are to not allow human authored code to `camera_android_camerax` and to not rely on human code review feedback for code iteration. We can add documentation, skills, mcp servers, presubmit test etc. Basically any "support infrastructure" for development is allowed but the package changes must be generated as a single pass. When we discover that the plan is not good enough we will blow the package changes away and either modify the plan or add more support infrastructure and try again. For more information see the working doc in [go/flutter-project-one-shot](http://goto.google.com/flutter-project-one-shot). The agent-authored implementation plan this oneshot was made with can be seen at this commit: flutter/packages@e5f6c95 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 8dfde8f commit f9da37b

5 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎packages/camera/camera_android_camerax/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.4+4
2+
3+
* Fix `NullPointerException` when disposing camera during active video recording.
4+
15
## 0.7.4+3
26

37
* Updates `ResolutionPreset.max` to prefer higher resolution over capture rate

‎packages/camera/camera_android_camerax/example/integration_test/integration_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,47 @@ void main() {
243243
expect(duration, greaterThanOrEqualTo(const Duration(seconds: 4).inMilliseconds));
244244
await controller.dispose();
245245
});
246+
247+
testWidgets('video recording state is cleared after camera is disposed', (
248+
WidgetTester tester,
249+
) async {
250+
final List<CameraDescription> cameras = await availableCameras();
251+
if (cameras.isEmpty) {
252+
return;
253+
}
254+
255+
final cameraController = CameraController(
256+
cameras[0],
257+
mediaSettings: const MediaSettings(resolutionPreset: ResolutionPreset.low),
258+
);
259+
await cameraController.initialize();
260+
await cameraController.startVideoRecording();
261+
262+
// Dispose the controller, which simulates what the example app does
263+
// when the AppLifecycleState becomes inactive (e.g. backgrounding).
264+
await cameraController.dispose();
265+
266+
// Create a new controller (simulating app resume)
267+
final newController = CameraController(
268+
cameras[0],
269+
mediaSettings: const MediaSettings(resolutionPreset: ResolutionPreset.low),
270+
);
271+
await newController.initialize();
272+
273+
// Attempt to start a new recording. This should not throw or silently fail.
274+
await newController.startVideoRecording();
275+
276+
await Future<void>.delayed(const Duration(seconds: 4));
277+
278+
// Stop it, ensuring no NPE is thrown by the native side and the file is valid.
279+
final XFile file = await newController.stopVideoRecording();
280+
expect(file, isNotNull);
281+
282+
// Ensure the video was saved correctly
283+
final videoFile = File(file.path);
284+
expect(videoFile.existsSync(), isTrue);
285+
expect(videoFile.lengthSync(), greaterThan(0));
286+
287+
await newController.dispose();
288+
});
246289
}

‎packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ class AndroidCameraCameraX extends CameraPlatform {
518518
await processCameraProvider?.unbindAll();
519519
await imageAnalysis?.clearAnalyzer();
520520
await deviceOrientationManager.stopListeningForDeviceOrientationChange();
521+
522+
// `processCameraProvider.unbindAll()` implicitly finalizes active recordings natively.
523+
// Clear the Dart state here to prevent an exception on resume.
524+
recording = null;
525+
pendingRecording = null;
526+
videoOutputPath = null;
521527
}
522528

523529
/// The camera with ID [cameraId] has been initialized.

‎packages/camera/camera_android_camerax/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_android_camerax
22
description: Android implementation of the camera plugin using the CameraX library.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.7.4+3
5+
version: 0.7.4+4
66

77
environment:
88
sdk: ^3.12.0

‎packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,13 +1959,21 @@ void main() {
19591959
camera.liveCameraState = MockLiveCameraState();
19601960
camera.imageAnalysis = MockImageAnalysis();
19611961

1962+
camera.recording = MockRecording();
1963+
camera.pendingRecording = MockPendingRecording();
1964+
camera.videoOutputPath = 'test/path.mp4';
1965+
19621966
await camera.dispose(3);
19631967

19641968
verify(camera.preview!.releaseSurfaceProvider());
19651969
verify(camera.liveCameraState!.removeObservers());
19661970
verify(camera.processCameraProvider!.unbindAll());
19671971
verify(camera.imageAnalysis!.clearAnalyzer());
19681972
expect(stoppedListeningForDeviceOrientationChange, isTrue);
1973+
1974+
expect(camera.recording, isNull);
1975+
expect(camera.pendingRecording, isNull);
1976+
expect(camera.videoOutputPath, isNull);
19691977
},
19701978
);
19711979

0 commit comments

Comments
 (0)