Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 236885b

Browse files
cvolzke4bparrishMines
authored andcommitted
[camera]Fix CameraPreview freezes during startVideoRecording on iOS (#1023)
1 parent 432f775 commit 236885b

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

packages/camera/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.3
2+
3+
* Add capability to prepare the capture session for video recording on iOS.
4+
15
## 0.4.2
26

37
* Add sensor orientation value to `CameraDescription`.

packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ public void onMethodCall(MethodCall call, final Result result) {
205205
camera.takePicture((String) call.argument("path"), result);
206206
break;
207207
}
208+
case "prepareForVideoRecording":
209+
{
210+
// This optimization is not required for Android.
211+
result.success(null);
212+
break;
213+
}
208214
case "startVideoRecording":
209215
{
210216
final String filePath = call.argument("filePath");

packages/camera/ios/Classes/CameraPlugin.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,7 @@ - (void)startVideoRecordingAtPath:(NSString *)path result:(FlutterResult)result
467467
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Setup Writer Failed"});
468468
return;
469469
}
470-
[_captureSession stopRunning];
471470
_isRecording = YES;
472-
[_captureSession startRunning];
473471
result(nil);
474472
} else {
475473
_eventSink(@{@"event" : @"error", @"errorDescription" : @"Video is already recording!"});
@@ -726,6 +724,9 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)re
726724
[_camera close];
727725
_dispatchQueue = nil;
728726
result(nil);
727+
} else if ([@"prepareForVideoRecording" isEqualToString:call.method]) {
728+
[_camera setUpCaptureSessionForAudio];
729+
result(nil);
729730
} else if ([@"startVideoRecording" isEqualToString:call.method]) {
730731
[_camera startVideoRecordingAtPath:call.arguments[@"filePath"] result:result];
731732
} else if ([@"stopVideoRecording" isEqualToString:call.method]) {

packages/camera/lib/camera.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ class CameraController extends ValueNotifier<CameraValue> {
256256
return _creatingCompleter.future;
257257
}
258258

259+
/// Prepare the capture session for video recording.
260+
///
261+
/// Use of this method is optional, but it may be called for performance
262+
/// reasons on iOS.
263+
///
264+
/// Preparing audio can cause a minor delay in the CameraPreview view on iOS.
265+
/// If video recording is intended, calling this early eliminates this delay
266+
/// that would otherwise be experienced when video recording is started.
267+
/// This operation is a no-op on Android.
268+
///
269+
/// Throws a [CameraException] if the prepare fails.
270+
Future<void> prepareForVideoRecording() async {
271+
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
272+
// https://github.com/flutter/flutter/issues/26431
273+
// ignore: strong_mode_implicit_dynamic_method
274+
await _channel.invokeMethod('prepareForVideoRecording');
275+
}
276+
259277
/// Listen to events from the native plugins.
260278
///
261279
/// A "cameraClosing" event is sent when the camera is closed automatically by the system (for example when the app go to background). The plugin will try to reopen the camera automatically but any ongoing recording will end.

packages/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera
22
description: A Flutter plugin for getting information about and controlling the
33
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
44
and streaming image buffers to dart.
5-
version: 0.4.2
5+
version: 0.4.3
66
authors:
77
- Flutter Team <[email protected]>
88
- Luigi Agosti <[email protected]>

0 commit comments

Comments
 (0)