Replay Kit 使用

Replay Kit 感觉是玩游戏时需要的,可以录制视频,可以语言教学。但是要求在iOS9以上,那么我们来看看如何简单的集成这个框架进行录制自己的小视频吧。
首先需要包含框架

import ReplayKit

其次就是创建一个按钮,来供我们进行录制功能,然后方法名为startRecording

func startRecording(sender :UIButton) {
    if RPScreenRecorder.sharedRecorder().available {
        // true :第一个参数代表是否开启麦克风
        RPScreenRecorder.sharedRecorder().startRecordingWithMicrophoneEnabled(true, handler: { (error :NSError?) -> Void in
            if error == nil {
                sender.removeTarget(self, action: "startRecording:", forControlEvents: .TouchUpInside)
                sender.addTarget(self, action: "stopRecording:", forControlEvents: .TouchUpInside)
                sender.setTitle("停止", forState: .Normal)
            }else{
                print("录像失败")
            }
        })
    }else{
         print("不能录像")
    }
}

接下来我们要实现停止录像的方法

func stopRecording(sender :UIButton){
    RPScreenRecorder.sharedRecorder().stopRecordingWithHandler { (previewController : RPPreviewViewController?, error :NSError?) -> Void in
        if previewController != nil {
            previewController?.previewControllerDelegate = self
            let alertController = UIAlertController(title: "Recording", message: "Do you wish to discard or view your gameplay recording?", preferredStyle: .Alert)
            // 取消按钮
            let discardAction = UIAlertAction(title: "Discard", style: .Default) { (action: UIAlertAction) in
                RPScreenRecorder.sharedRecorder().discardRecordingWithHandler({ () -> Void in
                    // Executed once recording has successfully been discarded
                })
            }
            // 确定按钮
            let viewAction = UIAlertAction(title: "View", style: .Default, handler: { (action: UIAlertAction) -> Void in
                self.presentViewController(previewController!, animated: true, completion: nil)
            })
                
            alertController.addAction(discardAction)
            alertController.addAction(viewAction)
                
            self.presentViewController(alertController, animated: true, completion: nil)
                
            sender.removeTarget(self, action: "stopRecording:", forControlEvents: .TouchUpInside)
            sender.addTarget(self, action: "startRecording:", forControlEvents: .TouchUpInside)
            sender.setTitle("Start Recording", forState: .Normal)
            sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
        }else{
                
        }
    }
}

上面的代码我们还需要实现了replaykit框架的一个delegate。RPPreviewViewControllerDelegate
以下是他的两个delegate方法
// 此方式是点击view之后 弹出视频预览页面,可以直接返回 可以保存 也可以分享 通过activityTypes的返回字段可以拿到相应的状态

func previewController(previewController: RPPreviewViewController, didFinishWithActivityTypes activityTypes: Set) {
    print("\(activityTypes)")
        
    if activityTypes.contains("com.apple.UIKit.activity.SaveToCameraRoll")  {
        print("存储视频")
    }else{
        print("取消")
    }
}
// 这是点击视频预览页面顶部两个按钮之后执行的操作
func previewControllerDidFinish(previewController: RPPreviewViewController) {
    previewController.dismissViewControllerAnimated(true, completion: nil)
}