SCNRecorder allows you to record videos and to capture images from ARSCNView and SCNView without sacrificing performance. It gives you an incredible opportunity to share the media content of your augmented reality app or SceneKit based game.
SCNRecorder supports Metal and OpenGL.
- iOS 11.0+
- Xcode 11.5+
- Swift 4.2+
pod 'SCNRecorder', '~> 1.1'
github "gorastudio/SCNRecorder"
Import the SCNRecorder module.
import SCNRecorder
At viewDidLoad
it is recomended to prepare a sceneView
for recording.
@IBOutlet var sceneView: SCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.prepareForRecording()
}
And now you can use new functions to capture videos.
try sceneView.startVideoRecording()
sceneView.finishVideoRecording { (videoRecording) in
/* Process the captured video. Main thread. */
let controller = AVPlayerViewController()
controller.player = AVPlayer(url: recording.url)
self.navigationController?.pushViewController(controller, animated: true)
}
To capture an image it is enough to call:
try sceneView.takePhoto { (photo) in
/* Your photo is now here. Main thread. */
}
Look at the Example project for more details.
To capture video with audio from ARSCNView
enable audio in the ARConfiguration
.
let configuration = ARWorldTrackingConfiguration()
configuration.providesAudioData = true
sceneView.session.run(configuration)
To capture audio from SCNView
you have to implement it by yourself.
var captureSession: AVCaptureSession?
override func viewDidLoad() {
super.viewDidLoad()
sceneView.prepareForRecording()
guard let recorder = sceneView.recorder else { return }
let captureSession = AVCaptureSession()
guard let captureDevice = AVCaptureDevice.default(for: .audio) else { return }
do {
let captureInput = try AVCaptureDeviceInput(device: captureDevice)
guard captureSession.canAddInput(captureInput) else { return }
captureSession.addInput(captureInput)
}
catch { print("Can't create AVCaptureDeviceInput: \(error)")}
guard captureSession.canAddRecorder(recorder) else { return }
captureSession.addRecorder(recorder)
captureSession.startRunning()
self.captureSession = captureSession
}
Look at the Example project for more details.
Thanks to Fedor Prokhorov and Dmitry Yurlov for testing, reviewing and inspiration.
This project is licensed under the MIT License - see the LICENSE file for details