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
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
Fixed formatting and iOS implementation
  • Loading branch information
Daniel Roek committed Feb 5, 2021
commit 63a95341adfe8245aa47f2faa345056b868249ab
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private void initFps(CameraCharacteristics cameraCharacteristics) {
Log.i("Camera", "[FPS Range] is:" + fpsRange);
}

private void prepareMediaRecorder(String outputFilePath, Integer maxVideoDuration) throws IOException {
private void prepareMediaRecorder(String outputFilePath, Integer maxVideoDuration)
throws IOException {
if (mediaRecorder != null) {
mediaRecorder.release();
}
Expand Down Expand Up @@ -632,20 +633,20 @@ public void startVideoRecording(Result result, Integer maxVideoDuration) {
CameraDevice.TEMPLATE_RECORD, () -> mediaRecorder.start(), mediaRecorder.getSurface());
if (maxVideoDuration != null) {
mediaRecorder.setOnInfoListener(
(mr, what, extra) -> {
if (what == MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
try {
dartMessenger.sendVideoRecordedEvent(
videoRecordingFile.getAbsolutePath(), maxVideoDuration);
recordingVideo = false;
videoRecordingFile = null;
maxDurationLimit = null;
resetCaptureSession();
} catch (CameraAccessException e) {
result.error("videoRecordingFailed", e.getMessage(), null);
}
}
});
(mr, what, extra) -> {
if (what == MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
try {
dartMessenger.sendVideoRecordedEvent(
videoRecordingFile.getAbsolutePath(), maxVideoDuration);
recordingVideo = false;
videoRecordingFile = null;
maxDurationLimit = null;
resetCaptureSession();
} catch (CameraAccessException e) {
result.error("videoRecordingFailed", e.getMessage(), null);
}
}
});
}
result.success(null);
} catch (CameraAccessException | IOException e) {
Expand All @@ -667,7 +668,6 @@ public void resetCaptureSession() throws CameraAccessException {
startPreview();
}


public void stopVideoRecording(@NonNull final Result result) {
if (!recordingVideo) {
result.success(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ void sendCameraErrorEvent(@Nullable String description) {

void sendVideoRecordedEvent(String path, Integer maxVideoDuration) {
this.send(
CameraEventType.VIDEO_RECORDED,
new HashMap<String, Object>() {
{
if (path != null) put("path", path);
if (maxVideoDuration != null) put("maxVideoDuration", maxVideoDuration);
}
});
CameraEventType.VIDEO_RECORDED,
new HashMap<String, Object>() {
{
if (path != null) put("path", path);
if (maxVideoDuration != null) put("maxVideoDuration", maxVideoDuration);
}
});
}

void send(CameraEventType eventType) {
Expand Down
61 changes: 31 additions & 30 deletions packages/camera/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ - (CVPixelBufferRef)copyPixelBuffer {
}

- (void)startVideoRecordingWithResult:(FlutterResult)result
maxVideoDuration:(int64_t)maxVideoDuration {
maxVideoDuration:(int64_t)maxVideoDuration {
if (!_isRecording) {
NSError *error;
_videoRecordingPath = [self getTemporaryFilePathWithExtension:@"mp4"
Expand All @@ -798,14 +798,14 @@ - (void)startVideoRecordingWithResult:(FlutterResult)result
result([FlutterError errorWithCode:@"IOError" message:@"Setup Writer Failed" details:nil]);
return;
}
if (maxVideoDuration != 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(maxVideoDuration * NSEC_PER_MSEC)),
dispatch_get_main_queue(), ^{
if (self->_isRecording) {
[self stopVideoRecordingWithResult:nil maxVideoRecording:maxVideoDuration];
}
});
}
if (maxVideoDuration != 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(maxVideoDuration * NSEC_PER_MSEC)),
dispatch_get_main_queue(), ^{
if (self->_isRecording) {
[self stopVideoRecordingWithResult:nil maxVideoDuration:maxVideoDuration];
}
});
}
_isRecording = YES;
_isRecordingPaused = NO;
_videoTimeOffset = CMTimeMake(0, 1);
Expand All @@ -819,26 +819,26 @@ - (void)startVideoRecordingWithResult:(FlutterResult)result
}

- (void)stopVideoRecordingWithResult:(FlutterResult)result
maxVideoDuration:(int64_t)maxVideoDuration {
maxVideoDuration:(int64_t)maxVideoDuration {
if (_isRecording) {
_isRecording = NO;
if (_videoWriter.status != AVAssetWriterStatusUnknown) {
[_videoWriter finishWritingWithCompletionHandler:^{
if (self->_videoWriter.status == AVAssetWriterStatusCompleted) {
if(result != nil) {
if (result != nil) {
result(self->_videoRecordingPath);
}
[self->_methodChannel invokeMethod:@"video_recorded"
arguments:@{
@"path" : self->_videoRecordingPath,
@"maxVideoDuration" : @(maxVideoDuration),
}];
[self->_methodChannel invokeMethod:@"video_recorded"
arguments:@{
@"path" : self->_videoRecordingPath,
@"maxVideoDuration" : @(maxVideoDuration),
}];
self->_videoRecordingPath = nil;
} else {
if(result != nil) {
if (result != nil) {
result([FlutterError errorWithCode:@"IOError"
message:@"AVAssetWriter could not finish writing!"
details:nil]);
message:@"AVAssetWriter could not finish writing!"
details:nil]);
}
[self->_methodChannel invokeMethod:errorMethod
arguments:@"AVAssetWriter could not finish writing!"];
Expand All @@ -850,9 +850,10 @@ - (void)stopVideoRecordingWithResult:(FlutterResult)result
[NSError errorWithDomain:NSCocoaErrorDomain
code:NSURLErrorResourceUnavailable
userInfo:@{NSLocalizedDescriptionKey : @"Video is not recording!"}];
if(result != nil){ result(getFlutterError(error));
} [self->_methodChannel invokeMethod:errorMethod arguments:@"Video is not recording!"];

if (result != nil) {
result(getFlutterError(error));
}
[self->_methodChannel invokeMethod:errorMethod arguments:@"Video is not recording!"];
}
}

Expand Down Expand Up @@ -1408,15 +1409,15 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)re
[_camera setUpCaptureSessionForAudio];
result(nil);
} else if ([@"startVideoRecording" isEqualToString:call.method]) {
if ([call.arguments[@"maxVideoDuration"] class] != [NSNull class]) {
[_camera startVideoRecordingWithResult:result
maxVideoDuration:((NSNumber *)call.arguments[@"maxVideoDuration"])
.intValue];
} else {
[_camera startVideoRecordingWithResult:result maxVideoDuration:0];
}
if ([call.arguments[@"maxVideoDuration"] class] != [NSNull class]) {
[_camera startVideoRecordingWithResult:result
maxVideoDuration:((NSNumber *)call.arguments[@"maxVideoDuration"])
.intValue];
} else {
[_camera startVideoRecordingWithResult:result maxVideoDuration:0];
}
} else if ([@"stopVideoRecording" isEqualToString:call.method]) {
[_camera stopVideoRecordingWithResult:result];
[_camera stopVideoRecordingWithResult:result maxVideoDuration:0];
} else if ([@"pauseVideoRecording" isEqualToString:call.method]) {
[_camera pauseVideoRecordingWithResult:result];
} else if ([@"resumeVideoRecording" isEqualToString:call.method]) {
Expand Down