0

I am using ReplayKit in an app to record the visible screen with some text and a video playing. The issue I am facing is that ReplayKit is working just fine for the first screen recording, but if I am to record again in the same session (ie without closing the app) it runs into this error:

MyViewController[423:39346] viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}

In this scenario, I am actually trying to screen record on the same ViewController (only with a different video being played and some text content altered). Below is my recording code:

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

        recorder.startRecording{ [unowned self] (error) in
            if let unwrappedError = error {
                print(unwrappedError.localizedDescription)
                print("NOT Recording")
            } else {
                self.video.play()
                print("Recording")
                self.isRecording = true
            }
        }

        recordIcon.isHidden = true
        ring.isHidden = true
    }

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

        recorder.stopRecording( handler: { previewViewController, error in
            if let error = error {
                print("\(error.localizedDescription)")
            }

            // Handling iPads
            if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
                previewViewController?.modalPresentationStyle = UIModalPresentationStyle.popover
                previewViewController?.popoverPresentationController?.sourceRect = CGRect.zero
                previewViewController?.popoverPresentationController?.sourceView = self.view
            }
            if previewViewController != nil {
                self.previewViewController = previewViewController
                previewViewController?.previewControllerDelegate = self
            }
            self.present(previewViewController!, animated: true, completion: nil)
        })
        isRecording = false
        recordIcon.isHidden = false
        ring.isHidden = false
        return
    }

    func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
        dismiss(animated: true)
    }

Any help on this is greatly appreciated. I'd hate to force users to have to reopen the app before recording again.

1 Answers1

0

It might be that your app keeps the screen recording longer than it should. If this is the case, try implementing the discardRecording(handler: @escaping () -> Void) function. Here are more details on the discardRecording.

James Castrejon
  • 567
  • 6
  • 16
  • Thank you for the comment. I attempted your solution but encountered the same error. Looks like I may just have to ask the users to reopen the app for every new recording – Coding Hobbies Jul 10 '19 at 14:33
  • No problem! One last thing you can try is removing/comment out the `self.video.play()` function in your `startRecording()` method. I haven't seen that in tutorials or the apple docs so it might be this. Here is another thread with the same error code but different problem. [link](https://stackoverflow.com/questions/40131751/error-while-running-custom-keyboard-running-on-simulator-swift) Besides that, I hope you figure it out! – James Castrejon Jul 10 '19 at 17:19
  • looks like I had no luck, but will write here if I find a fix. fyi the self.video.play() is my own code that plays the AVPlayer so not relevant to this issue. Thank you – Coding Hobbies Jul 15 '19 at 15:44