3

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

J.Treutlein
  • 963
  • 8
  • 23
  • Your "RECORDING" message prints fine for me _once I hit the Allow Screen Recording" in the privacy dialog_. This is on iPhoneX/11.2.5. There was a similar problem here https://stackoverflow.com/a/50936353/22147 which was fixed by a device restart. – Rhythmic Fistman Aug 17 '18 at 21:02
  • Also, as a side note; replay kit does not work in simulator. – D. Pratt Nov 13 '19 at 15:27

2 Answers2

7

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.

Mehmet Fatih Yıldız
  • 1,763
  • 18
  • 25
1

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.
Talha Ahmad Khan
  • 3,416
  • 5
  • 23
  • 38