1

I am attempting to use ReplayKit's native screen recording functionality along with my Unity app so that I can enjoy the benefits of native screen recording and sharing UI.

I have created a Unity plugin which calls my native code written in Objective-C(++) and everything seems to work fine up until it comes to dismissing the RPPreviewViewController.

So my record button in the Unity UI triggers the ReplayKit startRecordingWithMicrophoneEnabled:handler function and the stop button correctly calls the stopRecording function which returns the RPPreviewViewController instance to my handler.

In the RPPreviewViewController, the "Save" button correctly saves to the camera roll and the share button works as expected. But Tapping "Cancel" (or "Done" depending on the context) does nothing.

Here is the code in my stopRecordingWithHandler: function which deals with the RPPreviewViewController:

 - (void) stopRecording {

     [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewController,NSError *error){
     if (previewController != nil) {

         previewController.previewControllerDelegate = self;

         [previewController willMoveToParentViewController:rootView];

         [rootView addChildViewController:previewController];

         previewController.view.frame = CGRectMake(0.0, 0.0, rootView.view.bounds.size.width, rootView.view.bounds.size.height);

         [rootView.view addSubview:previewController.view];

         self.presentedController = previewController;

     } else {
         NSString *msg = [NSString stringWithFormat:@"The screen recorder is unavailable:\n%@", [error localizedDescription]];
         [self displayMessageDialog:msg];
     }
 }];
}

// Implementation of PreviewControllerDelegate protocol
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {

    [previewController willMoveToParentViewController:nil];

    [previewController.view removeFromSuperview];

    [previewController removeFromParentViewController];

    self.presentedController = nil;

}

As you can see, I have implemented the protocol for RPPreviewViewControllerDelegate and attempt to dismiss the view on its is closed event but the delegate function is never being called.

I can't figure out what is wrong, is it something I've coded wrong or is there an intricacy when using with Unity?

I'm stuck so any pointers would be much appreciated!

UPDATE: rootView is used in the above code so here is how I retrieved that reference in the the init function:

static UIViewController *rootView;

- (ReplayKitController*) init {
    self = [super init];

    if(self && rootView == nil) {
        rootView = [[UIApplication sharedApplication] windows][0].rootViewController;
    }

    return self;
}
Tim Mutlow
  • 11
  • 4

1 Answers1

0

I finally sorted it after trying a few things but I believe the problem was how I was setting the previewControllerDelegate with

previewController.previewControllerDelegate = self;

And have since changed it to use

[previewController setPreviewControllerDelegate:self];

Which seems to work!

Thanks to all who checked out my question and I hope this helps someone out there!

Tim Mutlow
  • 11
  • 4