Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Next Next commit
Added stopRecordingVideo
  • Loading branch information
danielroek committed Feb 5, 2021
commit 737b2034ff582a2aa9fdc8151f3e296117b5305a
4 changes: 4 additions & 0 deletions packages/camera/camera_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.5.1

- Added VideoRecordedEvent to support ending a video recording in the native implementation.

## 1.5.0

- Introduces interface methods for locking and unlocking the capture orientation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,48 @@ class CameraErrorEvent extends CameraEvent {
@override
int get hashCode => super.hashCode ^ description.hashCode;
}

/// An event fired when a video has finished recording.
class VideoRecordedEvent extends CameraEvent {
/// XFile of the recorded video.
final XFile file;
/// Maximum duration of the recorded video.
final Duration maxVideoDuration;

/// Build a VideoRecordedEvent triggered from the camera with the `cameraId`.
///
/// The `file` represents the file of the video.
/// The `maxVideoDuration` shows if a maxVideoDuration shows if a maximum
/// video duration was set.
VideoRecordedEvent(int cameraId, this.file, this.maxVideoDuration)
: super(cameraId);

/// Converts the supplied [Map] to an instance of the [VideoRecordedEvent]
/// class.
VideoRecordedEvent.fromJson(Map<String, dynamic> json)
: file = XFile(json['path']),
maxVideoDuration = json['maxVideoDuration'] != null
? Duration(milliseconds: json['maxVideoDuration'] as int)
: null,
super(json['cameraId']);

/// Converts the [VideoRecordedEvent] instance into a [Map] instance that can be
/// serialized to JSON.
Map<String, dynamic> toJson() => {
'cameraId': cameraId,
'path': file.path,
'maxVideoDuration': maxVideoDuration?.inMilliseconds
};

@override
bool operator ==(Object other) =>
identical(this, other) ||
super == other &&
other is VideoRecordedEvent &&
runtimeType == other.runtimeType &&
maxVideoDuration == other.maxVideoDuration;

@override
int get hashCode =>
super.hashCode ^ file.hashCode ^ maxVideoDuration.hashCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:cross_file/cross_file.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:pedantic/pedantic.dart';
import 'package:stream_transform/stream_transform.dart';

const MethodChannel _channel = MethodChannel('plugins.flutter.io/camera');
Expand Down Expand Up @@ -156,6 +157,11 @@ class MethodChannelCamera extends CameraPlatform {
return _cameraEvents(cameraId).whereType<CameraErrorEvent>();
}

@override
Stream<VideoRecordedEvent> onVideoRecordedEvent(int cameraId) {
return _cameraEvents(cameraId).whereType<VideoRecordedEvent>();
}

@override
Stream<DeviceOrientationChangedEvent> onDeviceOrientationChanged() {
return deviceEventStreamController.stream
Expand Down Expand Up @@ -216,6 +222,19 @@ class MethodChannelCamera extends CameraPlatform {
return XFile(path);
}

@override
Future<XFile> stopRecordingVideo(int cameraId) async {
Completer<XFile> completer = Completer();
unawaited(onVideoRecordedEvent(cameraId)
.first
.then((event) => completer.complete(event.file)));
unawaited(_channel.invokeMethod<void>(
'stopVideoRecording',
<String, dynamic>{'cameraId': cameraId},
));
return completer.future;
}

@override
Future<void> pauseVideoRecording(int cameraId) => _channel.invokeMethod<void>(
'pauseVideoRecording',
Expand Down Expand Up @@ -433,6 +452,15 @@ class MethodChannelCamera extends CameraPlatform {
cameraId,
));
break;
case 'video_recorded':
cameraEventStreamController.add(VideoRecordedEvent(
cameraId,
XFile(call.arguments['path']),
call.arguments['maxVideoDuration'] != null
? Duration(milliseconds: call.arguments['maxVideoDuration'])
: null,
));
break;
case 'error':
cameraEventStreamController.add(CameraErrorEvent(
cameraId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ abstract class CameraPlatform extends PlatformInterface {
throw UnimplementedError('onCameraError() is not implemented.');
}

/// The camera finished recording a video
Stream<VideoRecordedEvent> onVideoRecordedEvent(int cameraId) {
throw UnimplementedError('onCameraTimeLimitReached() is not implemented.');
}

/// The device orientation changed.
///
/// Implementations for this:
Expand Down Expand Up @@ -123,16 +128,24 @@ abstract class CameraPlatform extends PlatformInterface {
/// The length of the recording can be limited by specifying the [maxVideoDuration].
/// By default no maximum duration is specified,
/// meaning the recording will continue until manually stopped.
/// The video is returned as a [XFile] after calling [stopVideoRecording].
/// The video is returned in a [VideoRecordedEvent] in the [onVideoRecordedEvent] stream.
Future<void> startVideoRecording(int cameraId, {Duration maxVideoDuration}) {
throw UnimplementedError('startVideoRecording() is not implemented.');
}

/// Stops the video recording and returns the file where it was saved.
/// Function is deprecated and is replaced by [stopRecordingVideo].
/// This function will be removed in 2.0.0
@deprecated
Future<XFile> stopVideoRecording(int cameraId) {
throw UnimplementedError('stopVideoRecording() is not implemented.');
}

/// Stops the video recording and returns the file where it was saved.
Future<XFile> stopRecordingVideo(int cameraId) {
throw UnimplementedError('stopVideoRecording() is not implemented.');
}

/// Pause video recording.
Future<void> pauseVideoRecording(int cameraId) {
throw UnimplementedError('pauseVideoRecording() is not implemented.');
Expand Down
4 changes: 2 additions & 2 deletions packages/camera/camera_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the camera plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.5.0
version: 1.5.1

dependencies:
flutter:
Expand All @@ -12,13 +12,13 @@ dependencies:
plugin_platform_interface: ^1.0.1
cross_file: ^0.1.0
stream_transform: ^1.2.0
pedantic: ^1.8.0

dev_dependencies:
flutter_test:
sdk: flutter
async: ^2.4.2
mockito: ^4.1.1
pedantic: ^1.8.0

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down