AMH
AMH

Reputation: 479

how can we record and save video in Swift 4+ and IOS 11+?

I am trying to record video and then save it on an IOS device, I am able to record it but I am wondering how to save it on the device?

import UIKit
import AVKit
import MobileCoreServices

class ViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {
    @IBOutlet weak var RecordButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func RecordAction(_ sender: UIButton) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print("Camera Available")

            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            imagePicker.allowsEditing = false

            self.present(imagePicker, animated: true, completion: nil)
        } else {
            print("Camera UnAvaialable")
        }
    }
}

ViewController Screen Shot

Upvotes: 3

Views: 6594

Answers (2)

Wahab Khan Jadon
Wahab Khan Jadon

Reputation: 1196

I am just posting an updated and more detailed version of @HHH answer ... with Xcode 14.3.1 and swift5

extension MyViewController :  UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  func openCamera() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.sourceType = .camera
            imagePicker.cameraDevice = .front
            imagePicker.cameraFlashMode = .off
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            imagePicker.videoMaximumDuration = 30
            imagePicker.videoQuality = .typeIFrame1280x720
            imagePicker.cameraCaptureMode = .video
            imagePicker.allowsEditing = true
            imagePicker.showsCameraControls = true
            imagePicker.delegate = self
            present(imagePicker, animated: true, completion: nil)
        } else {
            // Camera not available
            print("Camera not avaiable")
        }
    }


// UIImagePickerControllerDelegate method
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let mediaType = info[.mediaType] as? String,
           mediaType == kUTTypeMovie as String,
           let videoURL = info[.mediaURL] as? URL {
            self.videoURL = videoURL
            
            // Process the captured video
            dismiss(animated: true, completion: {
//                self.requestForUploadVideo()
            })
            
        }
    }
    
    // UIImagePickerControllerDelegate method
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

}

Upvotes: 1

HHH
HHH

Reputation: 66

First make sure to add below Privacies to info.plist :

Privacy - Photo Library Additions Usage Description
Privacy - Camera Usage Description
Privacy - Microphone Usage Description 

and add below functions under ViewDidLoad

    func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : Any]) {
    dismiss(animated: true, completion: nil)

    guard
        let mediaType = info[UIImagePickerControllerMediaType] as? String,
        mediaType == (kUTTypeMovie as String),
        let url = info[UIImagePickerControllerMediaURL] as? URL,
        UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
        else {
            return
    }

    // Handle a movie capture
    UISaveVideoAtPathToSavedPhotosAlbum(
        url.path,
        self,
        #selector(video(_:didFinishSavingWithError:contextInfo:)),
        nil)
}

@objc func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo info: AnyObject) {
    let title = (error == nil) ? "Success" : "Error"
    let message = (error == nil) ? "Video was saved" : "Video failed to save"

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))
    present(alert, animated: true, completion: nil)
}

Upvotes: 5

Related Questions