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

[camera] Pause/resume video recording for Android(update of PR 1370) #1909

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ public void onMethodCall(MethodCall call, final Result result) {
camera.stopVideoRecording(result);
break;
}
case "pauseVideoRecording":
{
camera.pauseVideoRecording(result);
break;
}
case "resumeVideoRecording":
{
camera.resumeVideoRecording(result);
break;
}
case "startImageStream":
{
try {
Expand Down Expand Up @@ -702,6 +712,34 @@ private void stopVideoRecording(@NonNull final Result result) {
}
}

private void pauseVideoRecording(@NonNull final Result result) {
if (!recordingVideo) {
result.success(null);
return;
}

try {
mediaRecorder.pause();
result.success(null);
} catch (IllegalStateException e) {
result.error("videoRecordingFailed", e.getMessage(), null);
}
}

private void resumeVideoRecording(@NonNull final Result result) {
if (!recordingVideo) {
result.success(null);
return;
}

try {
mediaRecorder.resume();
result.success(null);
} catch (IllegalStateException e) {
result.error("videoRecordingFailed", e.getMessage(), null);
}
}

private void startPreview() throws CameraAccessException {
closeCaptureSession();

Expand Down
56 changes: 56 additions & 0 deletions packages/camera/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,62 @@ class CameraController extends ValueNotifier<CameraValue> {
}
}

/// Pause recording.
Future<void> pauseVideoRecording() async {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController',
'pauseVideoRecording was called on uninitialized CameraController',
);
}
if (!value.isRecordingVideo) {
throw CameraException(
'No video is recording',
'pauseVideoRecording was called when no video is recording.',
);
}
try {
// value = value.copyWith(isRecordingVideo: false);
// 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(
'pauseVideoRecording',
<String, dynamic>{'textureId': _textureId},
);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
}

/// Resume recording.
Future<void> resumeVideoRecording() async {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController',
'resumeVideoRecording was called on uninitialized CameraController',
);
}
if (!value.isRecordingVideo) {
throw CameraException(
'No video is recording',
'resumeVideoRecording was called when no video is recording.',
);
}
try {
// value = value.copyWith(isRecordingVideo: false);
// 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(
'resumeVideoRecording',
<String, dynamic>{'textureId': _textureId},
);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
}

/// Releases the resources of this camera.
@override
Future<void> dispose() async {
Expand Down