Lee
Lee

Reputation: 39

AVKit used with Swift, yet no file created. Why?

Here is some simplified code that attempts to use AVKit in Swift to record a video to a file. No errors are thrown, yet no file exists when checked. What have I left out?

I've tried several different variations, all with the same result. No file is created.

extension UIViewController:AVCaptureFileOutputRecordingDelegate{
    public func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        if let error = error{
            print(error.localizedDescription)
        }
        print("checking if \(outputFileURL.absoluteString) exists: \(FileManager.default.fileExists(atPath: outputFileURL.absoluteString))")
    }
}

class ViewController: UIViewController {

    var captureSession = AVCaptureSession()
    var movieOutput = AVCaptureMovieFileOutput()
    var previewLayer = AVCaptureVideoPreviewLayer()

    @IBOutlet var cameraView: UIView!

    override func viewWillAppear(_ animated: Bool) {
        self.cameraView = self.view

        guard let camera = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front) else{
                return
            
        }
        do{
            let input = try AVCaptureDeviceInput(device: camera)
            if captureSession.canAddInput(input){

                captureSession.addInput(input)

                if captureSession.canAddOutput(movieOutput){

                    captureSession.addOutput(movieOutput)

                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
                    previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
                    cameraView.layer.addSublayer(previewLayer)

                    previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
                    previewLayer.bounds = cameraView.frame
               

                    captureSession.startRunning()
                    
                    let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("output.mov")
                    try? FileManager.default.removeItem(at: fileUrl)
                    
                    print("recording to: \(fileUrl.absoluteString)")
                    movieOutput.startRecording(to: fileUrl, recordingDelegate: self)
                    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
                        self.movieOutput.stopRecording()
                        
                    }
                }
            }
        }
        catch{
            print("Error")
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Lee
Lee

Reputation: 39

I found it. The problem was in the check I was doing. The line

print("checking if \(outputFileURL.absoluteString) exists: \(FileManager.default.fileExists(atPath: outputFileURL.absoluteString))")

should read

print("checking if \(outputFileURL.absoluteString) exists: \(FileManager.default.fileExists(atPath: outputFileURL.path))")

Upvotes: 0

Related Questions