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

[camera_platform_interface] Added VideoRecordedEvent #3504

Closed
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
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 @@ -209,11 +215,15 @@ class MethodChannelCamera extends CameraPlatform {

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

@override
Expand Down Expand Up @@ -421,6 +431,15 @@ class MethodChannelCamera extends CameraPlatform {
call.arguments['focusPointSupported'],
));
break;
case 'video_recorded':
cameraEventStreamController.add(VideoRecordedEvent(
cameraId,
XFile(call.arguments['path']),
call.arguments['maxVideoDuration'] != null
? Duration(milliseconds: call.arguments['maxVideoDuration'])
: null,
));
break;
case 'resolution_changed':
cameraEventStreamController.add(CameraResolutionChangedEvent(
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
2 changes: 1 addition & 1 deletion 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 Down