Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support video recording on iOS #308

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Moved video return to stopRecordVideo, as per convention.
mario-deaconescu committed Jul 26, 2023
commit c3b5cf3e8ce525c14953a7534e08d10b90489276
12 changes: 6 additions & 6 deletions ios/Plugin/CameraController.swift
Original file line number Diff line number Diff line change
@@ -394,9 +394,9 @@ extension CameraController {

}

func captureVideo(completion: @escaping (URL?, Error?) -> Void) {
func captureVideo(completion: @escaping (Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(nil, CameraControllerError.captureSessionIsMissing)
completion(CameraControllerError.captureSessionIsMissing)
return
}
let path = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
@@ -408,16 +408,16 @@ extension CameraController {
let fileUrl = path.appendingPathComponent(fileName)
try? FileManager.default.removeItem(at: fileUrl)

self.videoCaptureCompletionBlock = completion

videoOutput!.startRecording(to: fileUrl, recordingDelegate: self)
completion(nil)
}

func stopRecording(completion: @escaping (Error?) -> Void) {
func stopRecording(completion: @escaping (URL?, Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(CameraControllerError.captureSessionIsMissing)
completion(nil, CameraControllerError.captureSessionIsMissing)
return
}
self.videoCaptureCompletionBlock = completion
self.videoOutput?.stopRecording()
}
}
32 changes: 18 additions & 14 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
@@ -262,29 +262,33 @@ public class CameraPreview: CAPPlugin {

let quality: Int? = call.getInt("quality", 85)

self.cameraController.captureVideo { (image, error) in

guard let image = image else {
print(error ?? "Image capture error")
guard let error = error else {
call.reject("Image capture error")
return
}
call.reject(error.localizedDescription)
self.cameraController.captureVideo { (error) in
guard let error = error else {
call.resolve()
return
}

// self.videoUrl = image

call.resolve(["value": image.absoluteString])
call.reject(error.localizedDescription)
return
}
}
}

@objc func stopRecordVideo(_ call: CAPPluginCall) {

self.cameraController.stopRecording { (_) in
self.cameraController.stopRecording { (video, error) in
guard let video = video else {
print(error ?? "Video capture error")
guard let error = error else {
call.reject("Video capture error")
return
}
call.reject(error.localizedDescription)
return
}

// self.videoUrl = image

call.resolve(["value": video.absoluteString])
}
}

4 changes: 2 additions & 2 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -60,9 +60,9 @@ export interface CameraOpacityOptions {

export interface CameraPreviewPlugin {
start(options: CameraPreviewOptions): Promise<{}>;
startRecordVideo(options: CameraPreviewOptions): Promise<{ value: string } | never>;
startRecordVideo(options: CameraPreviewOptions): Promise<{}>;
stop(): Promise<{}>;
stopRecordVideo(): Promise<{}>;
stopRecordVideo(): Promise<{ value: string } | never>;
capture(options: CameraPreviewPictureOptions): Promise<{ value: string }>;
captureSample(options: CameraSampleOptions): Promise<{ value: string }>;
getSupportedFlashModes(): Promise<{