1

I like to get the url of the ReplayKit screen recording instead of saving the video to my camera roll or forwarding it. From a WWDC 2017 video, it was mentioned that to get the url, one can use the following function:

func stopRecording(withOutput url: URL, completionHandler: ((Error?) -> Void)? = nil){}

But I am having a hard time figuring out how to call/implement this line of code. I have a start recording @IBAction button and a stop recording @IBAction button. The screen recording is working fine. However, can someone show me how or/and where to add this stopRecording function so I can get the url for the screen recording? Appreciate any help pointing me to the right direction. I am still learning Xcode. Thank-you!

@IBAction func StartScreenRec( sender: Any) {
    screenrecorder.startRecording { (error) in
        if let error = error {
            print(error)
        }
        self.ScreenStartRecordBtn.isHidden = true
        self.StopScreenRecBtn.isHidden = false
    }
}



@IBAction func StopScreenRec( sender: Any) {
    screenrecorder.stopRecording { (previewVC, error) in
        if let previewVC = previewVC {
            previewVC.modalPresentationStyle = .fullScreen
            previewVC.previewControllerDelegate = self
            self.present(previewVC, animated: true, completion: nil)
        }

        if let error = error {
            print(error)
        }

        self.ScreenStartRecordBtn.isHidden = false
        self.StopScreenRecBtn.isHidden = true
    }
}
Athena
  • 31
  • 2

1 Answers1

0

I hope this helps - To get the URL of the Video that you've just recorded, you need to specify the URL first, then assign the withOutput Function:

outputURL = tempURL()
    recorder.stopRecording(withOutput: outputURL) { (error) in
        guard error == nil else{
            print("Failed to save ")
            return
        }
        print(self.outputURL)
    }

And this is for the tempURL() Function:

func tempURL() -> URL? {
    let directory = NSTemporaryDirectory() as NSString
        
    if directory != "" {
        let path = directory.appendingPathComponent(NSUUID().uuidString + ".mp4")
        return URL(fileURLWithPath: path)
    } 
    return nil
}
elarcoiris
  • 1,914
  • 4
  • 28
  • 30
  • Oh cool! Thank-you so much for that. I will try this out when I come back around to it. Can't wait to test that out. – Athena Jun 17 '21 at 07:34