Keater
Keater

Reputation: 123

how to record screen video in swift?

I am doing a guess song game app , I need to record the screen when the user is guessing and capture the device's audio output as well. I want my app to support ios8 , so "ReplayKit" is off the table, then which SDK should I use? I am a beginner, if there's any example code would be much more help,thanks.

Upvotes: 4

Views: 7999

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 1058

Using Apple's ReplayKit, you can let your user record gameplay, or in your case, whatever the user is doing.

A link to the WWDC 2015 presentation is included here

Use these functions to start and stop recording:

func startRecording() {
        let recorder = RPScreenRecorder.shared()

        recorder.startRecordingWithMicrophoneEnabled(true) { [unowned self] (error) in
            if let unwrappedError = error {
                print(unwrappedError.localizedDescription)
            } else {
                self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Stop", style: .Plain, target: self, action: "stopRecording")
            }
        }
    }

    func stopRecording() {
        let recorder = RPScreenRecorder.shared()

        recorder.stopRecordingWithHandler { [unowned self] (preview, error) in
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .Plain, target: self, action: "startRecording")

            if let unwrappedPreview = preview {
                unwrappedPreview.previewControllerDelegate = self
                self.presentViewController(unwrappedPreview, animated: true, completion: nil)
            }
        }
    }

Upvotes: 4

Mikita Manko
Mikita Manko

Reputation: 1113

RPScreenRecorder is kind of cool, although you could not access the video, or even access it. The only way how to do video screen capturing that I've came up with is to make a screenshots, store them in the array in Images and than to convert it to video. It's not cool from performance standpoint and it's hard to achieve 30+fps, but might work for some use cases, here's the full manual how to do it.

Upvotes: 0

Related Questions