Lim Thye Chean
Lim Thye Chean

Reputation: 9434

ReplayKit stopRecording not entered the first time

I am trying to video capture an ARKIt app using ReplayKit. I have a record button, when pressed turned red and start recording, then pressed again to turn white and stop recording.

But the stopRecording method never worked on the first time.

if recorder.isAvailable { recorder.delegate = self

        if recorder.isRecording {
            print("Recorder is recording...")

            // Stop recording

            recorder.stopRecording { previewController, error in
                print("Stop recording...")
                self.recordImage.color = UIColor.white
                self.recordImage.colorBlendFactor = 1

                if let controller = previewController {
                    controller.previewControllerDelegate = self
                    self.present(controller, animated:true, completion:nil)
                }
            }
        }
        else {

            // Start recording

            recorder.startRecording { error in
                print("Starting to record…")

                if error == nil {
                    print("Start Recording…")
                    self.recordImage.color = UIColor.red
                    self.recordImage.colorBlendFactor = 1
                }
            }
        }

When first pressed, I can see the recording started. Then when I pressed again, I can see that recorder.isRecording is entered, but the block in recorder.stopRecording does not work. I have to press again to start recording, then stop again before the recorder.stopRecording block is entered.

Any idea? Help is appreciated.

Press Record!
Starting to record…
Start Recording…
Press Record!
Recorder is recording...

Upvotes: 1

Views: 821

Answers (2)

flopr
flopr

Reputation: 450

I fixed this issue based on the replies at https://forums.developer.apple.com/thread/62624

This is definitely a bug in iOS; but removing the "Localization native development region" entry from the Info.plist seems to solve this issue.

Upvotes: 1

Chris3643
Chris3643

Reputation: 46

Which iOS version are you using? I've seen cases where the completion handler doesn't get called, often on the first try, but then works thereafter. This happened a lot on iOS 9 and again in 11.0, but seems to be better in 11.0.3.

I'm not sure if you are trying this on an iPad, but your code above won't work on an iPad. You need to set a presentation style.

   if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
                    self.present(controller, animated: true, completion: nil)
                }
                else {
                    controller.popoverPresentationController?.sourceRect = self.recordingButton.bounds
                    controller.popoverPresentationController?.sourceView = self.view
                    controller.modalPresentationStyle = UIModalPresentationStyle.popover
                    controller.preferredContentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
                    self.present(controller, animated: true, completion: nil)                    
                }

Upvotes: 0

Related Questions