-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera]Fix CameraPreview freezes during startVideoRecording on iOS #1023
Changes from all commits
56e9eec
a140c07
6dd5e2e
8f28873
b33f2af
98b5e39
6e7e3bc
9c346d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,6 +256,24 @@ class CameraController extends ValueNotifier<CameraValue> { | |
return _creatingCompleter.future; | ||
} | ||
|
||
/// Prepare the capture session for video recording. | ||
/// | ||
/// Use of this method is optional, but it may be called for performance | ||
/// reasons on iOS. | ||
/// | ||
/// Preparing audio can cause a minor delay in the CameraPreview view on iOS. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: leave an empty line after the first line in dart-doc comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
/// If video recording is intended, calling this early eliminates this delay | ||
/// that would otherwise be experienced when video recording is started. | ||
/// This operation is a no-op on Android. | ||
/// | ||
/// Throws a [CameraException] if the prepare fails. | ||
Future<void> prepareForVideoRecording() async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some way we can avoid exposing this on the dart side? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are three use cases to consider:
Today in flutter, use cases #2 and #3 cause a delay in the preview when startVideoRecording() is started. Use case #3 without this delay would look like this (iPhone native camera app): This demo doesn't show a delay when starting video capture, presumably because audio capture is initialized by the native app when the user taps the 'Video' button while fuzzing the preview view. This isn't possible in flutter without a prepareForVideoRecording(), or initializing audio early. Initializing audio early is an option, but isn't ideal:
prepareForVideoRecording() is an optional optimization, giving the developer the option of choosing when to take on the delay in the preview view. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any more thoughts on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not have both? Can we manage some initialization state (presumably on iOS side?) so that if the user has not called prepareForVideoRecording(), it still works? My guess is that majority of our users won't care about that delay so forcing them to call an additional API is harsh. For the remaining users, having this fine tuned control is great. We should also document this behavior (that this is an optional call). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prepareForVideoRecording() is intended to be an optional call, it would only be required for users that care about performance in the above way. I've added a comment to make this more clear. |
||
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter. | ||
// https://github.com/flutter/flutter/issues/26431 | ||
// ignore: strong_mode_implicit_dynamic_method | ||
await _channel.invokeMethod('prepareForVideoRecording'); | ||
cvolzke4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Listen to events from the native plugins. | ||
/// | ||
/// 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ name: camera | |
description: A Flutter plugin for getting information about and controlling the | ||
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video, | ||
and streaming image buffers to dart. | ||
version: 0.4.2 | ||
version: 0.4.3 | ||
authors: | ||
- Flutter Team <[email protected]> | ||
- Luigi Agosti <[email protected]> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stopRunning() / startRunning() pair interferes with the CameraPreview view, causing it to freeze momentarily. Are they needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are right that they are not needed.
I no longer have an iOS setup to test it. Does recording work without them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked out their branch and it everything still works when it's removed on my iphone 6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Maurice for checking that. It also seems to work on my iPhone 7.