Chapter 18: Recording Your ARKit Experience with ReplayKit
Last updated
Last updated
import ReplayKit
//////////////////////////////////////
let sharedRecorder = RPScreenRecorder.shared()
private var isRecording = false
//////////////////////////////////////
// MARK: - RPPreviewViewControllerDelegate (ReplayKit)
extension ViewController: RPPreviewViewControllerDelegate {
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
print("previewControllerDidFinish")
dismiss(animated: true, completion: nil)
}
private func startRecording() {
sharedRecorder.isMicrophoneEnabled = true
sharedRecorder.startRecording { error in
guard error == nil else {
print("There was an error starting the recording: \(String(describing: error?.localizedDescription))")
return
}
print("Started Recording Successfully")
self.isRecording = true
DispatchQueue.main.async {
self.recordButton.setTitle("[ STOP RECORDING ]", for: .normal)
self.recordButton.backgroundColor = UIColor.red
}
}
}
func stopRecording() {
sharedRecorder.isMicrophoneEnabled = false
sharedRecorder.stopRecording { previewViewController, error in
guard error == nil else {
print("There was an error starting the recording: \(String(describing: error?.localizedDescription))")
return
}
// 這段preview的地方需要比較注意
if let unwrappedPreview = previewViewController {
unwrappedPreview.previewControllerDelegate = self
self.present(unwrappedPreview, animated: true, completion: {})
}
self.isRecording = false
DispatchQueue.main.async {
self.recordButton.setTitle("[ RECORD ]", for: .normal)
self.recordButton.backgroundColor = UIColor(red: 0.0039,
green: 0.5882, blue: 1, alpha: 1.0)
}
}
}
} sharedRecorder.delegate = self
///////////////////////////////////
// RPScreenRecorderDelegate methods
func screenRecorder(_ screenRecorder: RPScreenRecorder, didStopRecordingWith previewViewController: RPPreviewViewController?, error: Error?) {
guard error == nil else {
print("There was an error recording: \(String(describing:error?.localizedDescription))")
self.isRecording = false
return
}
} guard sharedRecorder.isAvailable else {
print("Recording is not available.")
return
}
//////////////////////////////////////
// RPScreenRecorderDelegate methods
func screenRecorderDidChangeAvailability(_ screenRecorder: RPScreenRecorder) {
recordButton.isEnabled = sharedRecorder.isAvailable
if !recordButton.isEnabled {
self.isRecording = false
}
// 更新button title
if sharedRecorder.isAvailable {
DispatchQueue.main.async {
self.recordButton.setTitle("[ RECORD ]", for: .normal)
self.recordButton.backgroundColor = UIColor(red: 0.0039,
green: 0.5882,
blue: 1,
alpha: 1.0)
}
} else {
DispatchQueue.main.async {
self.recordButton.setTitle("[ RECORDING DISABLED ]", for: .normal)
self.recordButton.backgroundColor = UIColor.red
}
}
}- if let unwrappedPreview = previewViewController {
- unwrappedPreview.previewControllerDelegate = self
- self.present(unwrappedPreview, animated: true, completion: {})
- }
// 原本只是單純呈現preview, 現在在這裡多增加一個alert提示,並多提拱delete跟edit選項
+ let alert = UIAlertController(title: "Recording Complete", message: "Do you want to preview/edit your recording or delete it?", preferredStyle: .alert)
+ let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
+ self.sharedRecorder.discardRecording {
+ print("Recording deleted.")
+ }
+ })
// edit就是原本present的功能
+ let editAction = UIAlertAction(title: "Edit",
+ style: .default,
+ handler: { (action: UIAlertAction) -> Void in
+ if let unwrappedPreview = previewViewController {
+ unwrappedPreview.previewControllerDelegate = self
+ self.present(unwrappedPreview, animated: true, completion: {})
+ }
+ })
+
+ alert.addAction(editAction)
+ alert.addAction(deleteAction)
+ self.present(alert, animated: true, completion: nil)