首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么RPScreenRecorder stopRecordingWithHandler不能工作?

为什么RPScreenRecorder stopRecordingWithHandler不能工作?
EN

Stack Overflow用户
提问于 2016-04-28 13:50:04
回答 1查看 1.9K关注 0票数 6

我正在测试一个网站的可用性,并在一个本地应用程序中使用WKWebView。这样做的原因是,我可以使用COSTouchVisualizer来显示触感,使用RPScreenRecorder来记录交互和与麦克风的“大声说话”。

我有下面的IBAction来开始录音:

代码语言:javascript
复制
@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以停止录制的按钮时,应运行以下代码:

代码语言:javascript
复制
@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。

我正在iPad Pro 9.7“运行iOS 9.3上测试这一点。

我开始怀疑它是否与尝试记录WKWebView有关,但我会想象如果这是问题的话,我会得到一个错误。

如能提供任何帮助,将不胜感激:)

EN

回答 1

Stack Overflow用户

发布于 2016-05-04 00:50:31

我怀疑如果要在guard语句中的任何位置设置断点(在stopRecordingCompletionHandler中),它不会导致程序崩溃或进入调试器,因为您的guard语句的else子句从未被调用。

事实上,这是预期的行为。我们不会期望执行else子句,除非两个控制器都等于nil,因此不能绑定到常量previewControllererror存在,因此不等于nil

对于guard,调用else子句的唯一方法是如果指定的条件是而不是 true。调用startRecordingWithMicrophoneEnabled闭包中的print语句是因为它位于guard语句之外。

因此,您只需要将一些逻辑从else子句中移出。不过,您仍然希望在那里处理错误。

代码语言:javascript
复制
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, animated: true,
  completion: nil)

print("stopped recording!")

守卫

因此,为了确保我们对guard语法很清楚,它是:

代码语言:javascript
复制
guard <condition> else {
    <statements to execute if <condition> is not true>
}

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

  • 如果controller不是nil,它将绑定到一个名为previewController的常量。
  • 如果是nil,那么我们停止计算,永远不要创建常量previewController,然后转义到else子句,该子句必须用关键字(如return )来传递控制。
  • 如果controller不是nil,并且已经创建了常量,那么我们将继续检查where子句。
  • 如果where的计算结果为true (因此如果没有错误),我们将通过测试并使用guard语句完成。else 永远不会被执行.
  • 如果guard的计算结果为false,则在不调用语句之后,我们将执行else块和。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36916715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档