加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 为什么RPScreenRecorder不会stopRecordingWithHandler工

发布时间:2020-12-14 19:37:56 所属栏目:百科 来源:网络整理
导读:我正在测试网站的可用性,并在本机应用程序中使用WKWebView.这样做的原因是我可以使用COSTouchVisualizer来显示触摸,而RPScreenRecorder则可以使用麦克风记录交互和“大声说话”. 我有以下IBAction开始录音: @IBAction func startRecordSession(sender: AnyO
我正在测试网站的可用性,并在本机应用程序中使用WKWebView.这样做的原因是我可以使用COSTouchVisualizer来显示触摸,而RPScreenRecorder则可以使用麦克风记录交互和“大声说话”.

我有以下IBAction开始录音:

@IBAction func startRecordSession(sender: AnyObject) {

    let recorder = RPScreenRecorder.sharedRecorder()

    guard recorder.available else{
        print("Cannot record the screen")
        return
    }

    recorder.delegate = self

    recorder.startRecordingWithMicrophoneEnabled(true) { (err) in

        guard err == nil else{
            if err!.code ==
                RPRecordingErrorCode.UserDeclined.rawValue{
                print("User declined app recording")
            }
            else if err!.code ==
                RPRecordingErrorCode.InsufficientStorage.rawValue{
                print("Not enough storage to start recording")
            }
            else{
                print("Error happened = (err!)")
            }
            return
        }

        print("Successfully started recording")
        self.recordBtn.enabled = false
        self.stopRecordBtn.enabled = true
    }

}

这似乎适用于打印成功开始录制.

但是,当按下连接到IBAction以停止录制的按钮时,应运行以下代码:

@IBAction func stop() {

  let recorder = RPScreenRecorder.sharedRecorder()

  print("1. before the recorder function")// This prints

  recorder.stopRecordingWithHandler{controller,err in     

    guard let previewController = controller where err == nil else {
      self.recordBtn.enabled = true
      self.stopRecordBtn.enabled = false

      print("2. Failed to stop recording")// This does not prints

      return
    }

    previewController.previewControllerDelegate = self

    self.presentViewController(previewController,animated: true,completion: nil)

  }

}

但是没有任何事情发生,除了打印出第一个日志(“1.记录器功能之前”).我没有得到其他日志语句,也没有按钮切换其启用状态.

我知道IBAction是由于命中声明而连接的,但不知道为什么我无法触发stopRecordingWithHandler.

我在运行iOS 9.3的iPad Pro 9.7上进行测试.

我开始怀疑它是否与尝试录制WKWebView有关,但是如果这是问题,我会想到会出错.

任何帮助将不胜感激 :)

解决方法

我怀疑如果你要在guard语句中的任何地方设置一个断点(在stopRecordingCompletionHandler中),它不会使程序崩溃或进入调试器,因为你的guard语句的else子句永远不会被调用.

实际上,这是预期的行为.我们不希望执行else子句,除非任一控制器等于nil,因此不能绑定到常量previewController或错误存在,因此不等于nil.

使用guard,如果指定的条件不为真,则调用else子句的唯一方法.正在调用startRecordingWithMicrophoneEnabled闭包中的print语句,因为它位于guard语句之外.

所以你只需要从else子句中移出一些逻辑.你仍然想要处理那里的错误.

recorder.stopRecordingWithHandler{controller,err in     

      guard let previewController = controller where err == nil else {

          print("2. Failed to stop recording with error (err!)") // This prints if there was an error

          return
      }
}

self.recordBtn.enabled = true
self.stopRecordBtn.enabled = false

previewController.previewControllerDelegate = self

self.presentViewController(previewController,completion: nil)

print("stopped recording!")

守护

所以,为了确保我们清楚保护语法,它是:

guard <condition> else {
    <statements to execute if <condition> is not true>
}

在您的示例中,您将guard与可选绑定和where子句组合在一起,以创建以下情况:

>如果controller不是nil,它将被绑定到一个名为previewController的常量.>如果它是nil则我们停止计算,永远不会创建常量previewController,并转义到else子句,该子句必须使用诸如return之类的关键字来转移控制.>如果控制器不是nil并且我们的常量已经创建,那么我们将继续检查where where子句.>如果where评估为true(所以如果没有错误),我们将通过测试并使用guard语句完成.否则永远不会被执行.>如果where的计算结果为false,我们将执行else块,并且不会调用guard语句之后的任何内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读