J.Treutlein
J.Treutlein

Reputation: 983

ReplayKit: RPScreenRecorder.shared().startCapture() NOT WORKING

ReplayKit has really been frustrating me recently. For some reason

RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in

does not actually work when I call it because I have a print() statement inside it and it is never called.

My code in the ViewController is:

import UIKit
import AVFoundation
import SpriteKit
import ReplayKit
import AVKit

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, RPPreviewViewControllerDelegate {

    var assetWriter:AVAssetWriter!
    var videoInput:AVAssetWriterInput!

func startRecording(withFileName fileName: String) {
        if #available(iOS 11.0, *)
        {

            assetWriter = try! AVAssetWriter(outputURL: fileURL, fileType:
                AVFileType.mp4)
            let videoOutputSettings: Dictionary<String, Any> = [
                AVVideoCodecKey : AVVideoCodecType.h264,
                AVVideoWidthKey : UIScreen.main.bounds.size.width,
                AVVideoHeightKey : UIScreen.main.bounds.size.height
            ];

            videoInput  = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
            videoInput.expectsMediaDataInRealTime = true
            assetWriter.add(videoInput)
            print("HERE")
            RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
                print("RECORDING")
            }
      }
}

func stopRecording(handler: @escaping (Error?) -> Void)
    {
        if #available(iOS 11.0, *)
        {
            RPScreenRecorder.shared().stopCapture
                {    (error) in
                    handler(error)
                    self.assetWriter.finishWriting
                        {
                            print("STOPPED")
                    }
            }
        }
    }

"HERE" is printed, but not "RECORDING"

[p.s. sorry for bad formatting in code, I'm sure you'll understand :)]


I have also tried a different method:

let recorder = RPScreenRecorder.shared()
recorder.startRecording{ [unowned self] (error) in
    guard error == nil else {
        print("There was an error starting the recording.")
        return
    }
    print("Started Recording Successfully")
 }

and to stop the recording...

    recorder.stopRecording { [unowned self] (preview, error) in
        print("Stopped recording")
        guard preview != nil else {
            print("Preview controller is not available.")
            return
        }
        onGoingScene = true
        preview?.previewControllerDelegate = self
        self.present(preview!, animated: true, completion: nil)
    }

This method does not stop when I call the recorder.stopRecording() function, "Stopped recording" is never called.


Can someone please help me because this is really frustrating me, how can you PROPERLY use ReplayKit to record your screen in iOS 11? I have searched all over the internet and none of the methods work for me, I don't why. P.S. I have the necessary permission keys in my Info.plist.

Thanks

Upvotes: 3

Views: 3876

Answers (2)

Mehmet Fatih Yıldız
Mehmet Fatih Yıldız

Reputation: 1827

A huge reminder that ReplayKit doesn't work in simulator. I wasted hours on the exact same issue until realized that ReplayKit will never trigger startCapture handler because it never records in simulator.

Upvotes: 8

Talha Ahmad Khan
Talha Ahmad Khan

Reputation: 3476

Well there are quite few possible causes for this issue. Some of them are here:

  1. Your Replay kit Shared Recorder might be crashed, For that you can restart your device and check again.
  2. There might be printable issue in your replay kit. For that kindly conform to the RPScreenRecorderDelegateProtocol and add Recording Changes screenRecorder:didStopRecordingWithPreviewViewController:error: method to your class and check if any error shows up in this method.

Upvotes: 1

Related Questions