From 2971c85de588cc7ce75e6ae87f4ffb617c0f58f4 Mon Sep 17 00:00:00 2001 From: huulbaek Date: Thu, 21 Mar 2019 10:09:59 +0100 Subject: [PATCH] Pause/resume video recording for Android --- .../flutter/plugins/camera/CameraPlugin.java | 38 +++++++++++++ packages/camera/lib/camera.dart | 56 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java b/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java index a204c1749264..a6209db2d2c1 100644 --- a/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java +++ b/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java @@ -222,6 +222,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 { @@ -722,6 +732,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(); diff --git a/packages/camera/lib/camera.dart b/packages/camera/lib/camera.dart index 8edbb8c59658..e3733fb5eabd 100644 --- a/packages/camera/lib/camera.dart +++ b/packages/camera/lib/camera.dart @@ -493,6 +493,62 @@ class CameraController extends ValueNotifier { } } + /// Pause recording. + Future 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', + {'textureId': _textureId}, + ); + } on PlatformException catch (e) { + throw CameraException(e.code, e.message); + } + } + + /// Resume recording. + Future 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', + {'textureId': _textureId}, + ); + } on PlatformException catch (e) { + throw CameraException(e.code, e.message); + } + } + /// Releases the resources of this camera. @override Future dispose() async {