Friday 31 May 2019

How to record a long video using AVFoundation (>1 min)?

When I record video using the following view controller:

class AVCameraViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    initializeMotionManager()
    sessionQueue.async {
        let movieFileOutput = AVCaptureMovieFileOutput()
        if self.session.canAddOutput(movieFileOutput) {
            self.session.beginConfiguration()
            self.session.addOutput(movieFileOutput)
            self.session.sessionPreset = .high
            if let connection = movieFileOutput.connection(with: .video) {
                if connection.isVideoStabilizationSupported {
                    connection.preferredVideoStabilizationMode = .auto
                }
            }
            self.session.commitConfiguration()
            movieFileOutput.maxRecordedDuration = CMTime(seconds: 120, preferredTimescale: 60)

            self.movieFileOutput = movieFileOutput

            DispatchQueue.main.async {
                self.recordButton.isEnabled = true
                }
            }
        }
    }

    func fileOutput(_ output: AVCaptureFileOutput,
                didFinishRecordingTo outputFileURL: URL,
                from connections: [AVCaptureConnection],
                error: Error?) {
    // Note: Since we use a unique file path for each recording, a new recording won't overwrite a recording mid-

save.
        UIApplication.shared.isIdleTimerDisabled = false
        func cleanup() {
            let path = outputFileURL.path
            if FileManager.default.fileExists(atPath: path) {
                do {
                    try FileManager.default.removeItem(atPath: path)
                } catch {
                    print("Could not remove file at url: \(outputFileURL)")
                }
            }

            if let currentBackgroundRecordingID = backgroundRecordingID {
                backgroundRecordingID = UIBackgroundTaskIdentifier.invalid

                if currentBackgroundRecordingID != UIBackgroundTaskIdentifier.invalid {
                    UIApplication.shared.endBackgroundTask(currentBackgroundRecordingID)
                }
            }
        }

        var success = true

        if error != nil {
            print("Movie file finishing error: \(String(describing: error))")
            success = (((error! as NSError).userInfo[AVErrorRecordingSuccessfullyFinishedKey] as AnyObject).boolValue)!
        }

        if success {
            // Check authorization status.
            UIView.animate(withDuration: 0.5){
                self.overlay.alpha = 0.9
                self.navigationController?.navigationBar.isTranslucent = false
            }
            footageURL = outputFileURL
            performSegue(withIdentifier: "TrimFootage", sender: nil)
        } else {
            cleanup()
        }

        // Enable the Camera and Record buttons to let the user switch camera and start another recording.
        DispatchQueue.main.async {
            // Only enable the ability to change camera if the device has more than one camera.
            self.recordButton.isEnabled = true
//            self.recordButton.setImage(#imageLiteral(resourceName: "CaptureVideo"), for: [])
        }
    }
}

As you can see I am setting the maxRecordedDuration to 2 minutes. When its done recording successfully, it eventually segues to another view controller.

The problem is right now it only records for a minute and then stops recording and segues. Im not sure if Im not setting the maxRecordedDuration correctly or if I have to be doing something else instead.



from How to record a long video using AVFoundation (>1 min)?

No comments:

Post a Comment