Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c7a749a
Added maxVideoDuration to startVideoRecording
danielroek Dec 23, 2020
131918d
updated documentation
danielroek Dec 23, 2020
9b3ae14
updated documentation
danielroek Dec 23, 2020
3ca25df
Fixed long line in docs
danielroek Dec 23, 2020
5e626b9
Formatting
danielroek Dec 23, 2020
02811b7
Started implementation for Android
danielroek Dec 24, 2020
b8b07e2
WIP: Started implementation of stream when time limit is reached
danielroek Dec 24, 2020
aff2938
Merge remote-tracking branch 'origin/master' into limit_video_length
danielroek Jan 8, 2021
532cbb0
Merge remote-tracking branch 'origin/master' into limit_video_length
danielroek Jan 11, 2021
50edc53
Initial working implementation
danielroek Jan 11, 2021
6e31d7c
Android implementation works
danielroek Jan 11, 2021
d3bab02
Improved implementation
danielroek Jan 11, 2021
ae7365c
Updated README order
danielroek Jan 11, 2021
ae1b47c
removed debugPrints
danielroek Jan 11, 2021
4037b5e
Fixed url in README.md
danielroek Jan 11, 2021
36acc32
Formatting
danielroek Jan 11, 2021
5a78233
Merge remote-tracking branch 'origin/master' into limit_video_length
danielroek Jan 13, 2021
09dad16
Implemented Java feedback
danielroek Jan 13, 2021
771f116
Implemented Event and Stream to notify about videoRecording
danielroek Jan 13, 2021
16e43fb
stopVideoRecording now listens to VideoRecordedEvent
danielroek Jan 13, 2021
7cb73ef
Fixed future returning xFile
danielroek Jan 13, 2021
759f163
finished iOS implementation
Feb 3, 2021
0b4270d
Fixed formatting
Feb 3, 2021
12231da
fixed formatting
Feb 3, 2021
3b8d6ed
Reverted platform_interface changes
danielroek Feb 3, 2021
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
Prev Previous commit
Next Next commit
Android implementation works
  • Loading branch information
danielroek committed Jan 11, 2021
commit 6e31d7c0ba0ae7be9d65b2f4ca97d5d819f6a2d3
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,16 @@ public void startVideoRecording(Result result, Integer maxVideoDuration) {
if(maxVideoDuration != null) {
mediaRecorder.setOnInfoListener((mr, what, extra) -> {
if (what == MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
dartMessenger.sendTimeLimitReachedEvent(videoRecordingFile.getPath());
try {
dartMessenger.sendTimeLimitReachedEvent(videoRecordingFile.getAbsolutePath());
cameraCaptureSession.abortCaptures();
mediaRecorder.stop();
recordingVideo = false;
videoRecordingFile = null;
startPreview();
} catch (CameraAccessException e) {
// Ignore exceptions and try to continue (changes are camera session already aborted capture)
}
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,13 @@ class _CameraExampleHomeState extends State<CameraExampleHome>

try {
await controller.startVideoRecording(
maxVideoDuration: null //const Duration(milliseconds: 5000),
maxVideoDuration: const Duration(milliseconds: 5000),
);
debugPrint('recording started');
controller.onCameraTimeLimitReachedEvent(onCameraTimeLimitReached: (file) {
debugPrint('onCameraTimeLimitReached ${file.path}');
});
debugPrint('listening');
} on CameraException catch (e) {
_showCameraException(e);
return;
Expand Down
45 changes: 29 additions & 16 deletions packages/camera/camera/lib/src/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ class CameraController extends ValueNotifier<CameraValue> {
///
/// The video is returned as a [XFile] after calling [stopVideoRecording].
/// Throws a [CameraException] if the capture fails.
Future<XFile> startVideoRecording({Duration maxVideoDuration}) async {
///
/// TODO: Documentation: when maxVideoDuration listen to Stream with CameraTimeLimitReachedEvent
Copy link
Contributor

@BeMacized BeMacized Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining TODO (PR is in draft state, but marking these just in case 🙂 )

Future<void> startVideoRecording({Duration maxVideoDuration}) async {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController',
Expand All @@ -402,24 +404,9 @@ class CameraController extends ValueNotifier<CameraValue> {
}

try {
Completer<XFile> completer = Completer();
await CameraPlatform.instance
.startVideoRecording(_cameraId, maxVideoDuration: maxVideoDuration);
value = value.copyWith(isRecordingVideo: true, isRecordingPaused: false);

if (maxVideoDuration != null) {
await CameraPlatform.instance
.onCameraTimeLimitReached(_cameraId)
.listen((event) {
debugPrint('Video recorded to: ${event.path}');
completer.complete(XFile(event.path));
value =
value.copyWith(isRecordingVideo: false, isRecordingPaused: false);
});
return completer.future;
} else {
return null;
}
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
Expand Down Expand Up @@ -498,6 +485,32 @@ class CameraController extends ValueNotifier<CameraValue> {
}
}

/// TODO: Documentation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining TODO

void onCameraTimeLimitReachedEvent({onCameraTimeLimitReached}) {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController',
'cameraTimeLimitReachedEventStream was called on uninitialized CameraController',
);
}
if (!value.isRecordingVideo) {
throw CameraException(
'No video is recording',
'cameraTimeLimitReachedEventStream was called when no video is recording.',
);
}
debugPrint('ping');

CameraPlatform.instance.onCameraTimeLimitReached(_cameraId).listen((event) {
debugPrint('onCameraTimeLimitReached');
value = value.copyWith(isRecordingVideo: false);
onCameraTimeLimitReached(event.path);
});

debugPrint('pong');
return;
}

/// Returns a widget showing a live camera preview.
Widget buildPreview() {
if (!value.isInitialized || _isDisposed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ class CameraErrorEvent extends CameraEvent {
}

class CameraTimeLimitReachedEvent extends CameraEvent {
final String path;
final XFile path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Rename to file instead of path:

Suggested change
final XFile path;
final XFile file;


CameraTimeLimitReachedEvent(int cameraId, this.path) : super(cameraId);

/// Converts the supplied [Map] to an instance of the [CameraTimeLimitReachedEvent]
/// class.
CameraTimeLimitReachedEvent.fromJson(Map<String, dynamic> json)
: path = json['path'],
: path = XFile(json['path']),
super(json['cameraId']);

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

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ class MethodChannelCamera extends CameraPlatform {
.whereType<DeviceOrientationChangedEvent>();
}

@override
Stream<CameraTimeLimitReachedEvent> onCameraTimeLimitReached(int cameraId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to rename this to onVideoRecordingTimeLimitReached, so it is clear this is directly related to the video recording feature. Of course if you accept my earlier comment about also emitting the recorded video on this stream when the user calls stopVideoRecording we should rename it to something more generic like onVideoRecorded.

return _cameraEvents(cameraId).whereType<CameraTimeLimitReachedEvent>();
}

@override
Future<void> lockCaptureOrientation(
int cameraId, DeviceOrientation orientation) async {
Expand All @@ -182,11 +187,6 @@ class MethodChannelCamera extends CameraPlatform {
);
}

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

@override
Future<XFile> takePicture(int cameraId) async {
String path = await _channel.invokeMethod<String>(
Expand All @@ -210,12 +210,6 @@ class MethodChannelCamera extends CameraPlatform {
'maxVideoDuration': maxVideoDuration?.inMilliseconds,
},
);

if (maxVideoDuration != null) {
await onCameraTimeLimitReached(cameraId).first.then((value) {
debugPrint('received event');
});
}
}

@override
Expand Down Expand Up @@ -435,7 +429,7 @@ class MethodChannelCamera extends CameraPlatform {
case 'max_time_limit_reached':
cameraEventStreamController.add(CameraTimeLimitReachedEvent(
cameraId,
call.arguments['path'],
XFile(call.arguments['path']),
));
break;
case 'resolution_changed':
Expand Down