Skip to content

Commit

Permalink
Merge pull request capacitor-community#141 from rudyZanotti/start-rec…
Browse files Browse the repository at this point in the history
…ording-ios
  • Loading branch information
arielhernandezmusa authored Jun 6, 2021
2 parents 55d11c4 + da8f45d commit 941a766
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ios/Plugin/CameraController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class CameraController: NSObject {
var sampleBufferCaptureCompletionBlock: ((UIImage?, Error?) -> Void)?

var highResolutionOutput: Bool = false

var audioDevice: AVCaptureDevice?
var audioInput: AVCaptureDeviceInput?
}

extension CameraController {
Expand Down Expand Up @@ -59,6 +62,7 @@ extension CameraController {
camera.unlockForConfiguration()
}
}
self.audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
}

func configureDeviceInputs() throws {
Expand All @@ -82,6 +86,16 @@ extension CameraController {
self.currentCameraPosition = .front
}
} else { throw CameraControllerError.noCamerasAvailable }

// Add audio input
if let audioDevice = self.audioDevice {
self.audioInput = try AVCaptureDeviceInput(device: audioDevice)
if captureSession.canAddInput(self.audioInput!) {
captureSession.addInput(self.audioInput!)
} else {
throw CameraControllerError.inputsAreInvalid
}
}
}

func configurePhotoOutput() throws {
Expand Down Expand Up @@ -119,6 +133,7 @@ extension CameraController {
try configureDeviceInputs()
try configurePhotoOutput()
try configureDataOutput()
try configureVideoOutput()
}

catch {
Expand Down Expand Up @@ -374,6 +389,31 @@ extension CameraController {
}

}

func captureVideo(completion: @escaping (URL?, Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(nil, CameraControllerError.captureSessionIsMissing)
return
}
let path = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let identifier = UUID()
let randomIdentifier = identifier.uuidString.replacingOccurrences(of: "-", with: "")
let finalIdentifier = String(randomIdentifier.prefix(8))
let fileName="cpcp_video_"+finalIdentifier+".mp4"

let fileUrl = path.appendingPathComponent(fileName)
try? FileManager.default.removeItem(at: fileUrl)
videoOutput!.startRecording(to: fileUrl, recordingDelegate: self)
self.videoRecordCompletionBlock = completion
}

func stopRecording(completion: @escaping (Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(CameraControllerError.captureSessionIsMissing)
return
}
self.videoOutput?.stopRecording()
}
}

extension CameraController: AVCapturePhotoCaptureDelegate {
Expand Down Expand Up @@ -536,3 +576,13 @@ extension UIImage {
return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
}
}

extension CameraController: AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error == nil {
self.videoRecordCompletionBlock?(outputFileURL, nil)
} else {
self.videoRecordCompletionBlock?(nil, error)
}
}
}
2 changes: 2 additions & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
CAP_PLUGIN_METHOD(flip, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getSupportedFlashModes, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setFlashMode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(startRecordVideo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(stopRecordVideo, CAPPluginReturnPromise);
)
32 changes: 32 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,37 @@ public class CameraPreview: CAPPlugin {
call.reject("failed to set flash mode")
}
}

@objc func startRecordVideo(_ call: CAPPluginCall) {
DispatchQueue.main.async {

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)
return
}

self.videoUrl = image

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


@objc func stopRecordVideo(_ call: CAPPluginCall) {

self.cameraController.stopRecording { (error) in

}
}

}

0 comments on commit 941a766

Please sign in to comment.