How to Implement Video Recording Using Multi-Camera in Your iOS App (Part 2)
Let users access both front and back cameras simultaneously!
Join the DZone community and get the full member experience.
Join For FreeIn the first part of this tutorial, we have seen how to configure multi-camera for recording a video.
As we know that in the previous iOS version, Apple did not permit to record videos using the front and back cameras at the same time. This time, with the release of iOS 13, they have introduced this feature that allows users to capture video recording using the back and front camera.
So, let's see how it works.
What you will learn in this part of the tutorial:
- How to implement multi-camera recording using Replaykit
You may also like: Learn Swift With This Hands-On Tutorial.
With the launch of iOS 13 and some new features, Apple has also provided AVCapture (Sample Output Delegate) to fetch front and back video, but here, we will be using ReplayKit Framework and a function called screenRecorder
.
Let’s perform the task of video recording to generate output video here.
Step 1: Import Replaykit framework as follows:
import ReplayKit
Step 2: Create one shared object of RPScreenRecorder, to start and stop screen recording.
let screenRecorder = RPScreenRecorder.shared()
Step 3: We will track recording status using the isRecording
variable.
var isRecording = false
Step 4: When the user taps on the screen once, it starts screen recording, and when the user double taps on screen it stops screen recording and saves output video to the user's photo library.
We have used one custom class for appending video and audio output to one video output using AssetWriter.swift
.
import UIKit
import Foundation
import AVFoundation
import ReplayKit
import Photos
extension UIApplication {
classfuncgetTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)
} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)
} else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}
}
class AssetWriter {
privatevarassetWriter: AVAssetWriter?
privatevarvideoInput: AVAssetWriterInput?
privatevaraudioInput: AVAssetWriterInput?
privateletfileName: String
letwriteQueue = DispatchQueue(label: "writeQueue")
init(fileName: String) {
self.fileName = fileName
}
privatevar videoDirectoryPath: String {
let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
return dir +"/Videos"
}
privatevarfilePath: String {
return videoDirectoryPath +"/\(fileName)"
}
privatefuncsetupWriter(buffer: CMSampleBuffer) {
if FileManager.default.fileExists(atPath: videoDirectoryPath) {
do {
tryFileManager.default.removeItem(atPath: videoDirectoryPath)
} catch {
print("fail to removeItem")
}
}
do {
tryFileManager.default.createDirectory(atPath: videoDirectoryPath, withIntermediateDirectories: true, attributes: nil)
} catch {
print("fail to createDirectory")
}
self.assetWriter = try? AVAssetWriter(outputURL: URL(fileURLWithPath: filePath), fileType: AVFileType.mov)
let writerOutputSettings = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: UIScreen.main.bounds.width,
AVVideoHeightKey: UIScreen.main.bounds.height,
] as [String : Any]
self.videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: writerOutputSettings)
self.videoInput?.expectsMediaDataInRealTime = true
guardlet format = CMSampleBufferGetFormatDescription(buffer),
let stream = CMAudioFormatDescriptionGetStreamBasicDescription(format) else {
print("fail to setup audioInput")
return
}
let audioOutputSettings = [
AVFormatIDKey : kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey : stream.pointee.mChannelsPerFrame,
AVSampleRateKey : stream.pointee.mSampleRate,
AVEncoderBitRateKey : 64000
] as [String : Any]
self.audioInput = AVAssetWriterInput(mediaType: AVMediaType.audio, outputSettings: audioOutputSettings)
self.audioInput?.expectsMediaDataInRealTime = true
if let videoInput = self.videoInput, (self.assetWriter?.canAdd(videoInput))! {
self.assetWriter?.add(videoInput)
}
if let audioInput = self.audioInput, (self.assetWriter?.canAdd(audioInput))! {
self.assetWriter?.add(audioInput)
}
}
publicfuncwrite(buffer: CMSampleBuffer, bufferType: RPSampleBufferType) {
writeQueue.sync {
ifassetWriter==nil {
if bufferType == .audioApp {
setupWriter(buffer: buffer)
}
}
ifassetWriter==nil {
return
}
if self.assetWriter?.status== .unknown {
print("Start writing")
let startTime = CMSampleBufferGetPresentationTimeStamp(buffer)
self.assetWriter?.startWriting()
self.assetWriter?.startSession(atSourceTime: startTime)
}
if self.assetWriter?.status== .failed {
print("assetWriter status: failed error: \(String(describing: self.assetWriter?.error))")
return
}
if CMSampleBufferDataIsReady(buffer) == true {
if bufferType == .video {
if let videoInput = self.videoInput, videoInput.isReadyForMoreMediaData {
videoInput.append(buffer)
}
} else if bufferType == .audioApp {
if let audioInput = self.audioInput, audioInput.isReadyForMoreMediaData {
audioInput.append(buffer)
}
}
}
}
}
public func finishWriting() {
writeQueue.sync {
self.assetWriter?.finishWriting(completionHandler: {
print("finishWriting")
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: self.filePath))
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
if let topVC = UIApplication.getTopViewController() {
topVC.present(alertController, animated: true, completion: nil)
}
}
}
})
}
}
}
Step 5: Now, we can go to the final stage of our demo.
When the user taps on screen we need to start the screen recording.
Create Start Capture function as following:
func startCapture() {
screenRecorder.startCapture(handler: { (buffer, bufferType, err) in
self.isRecording = true
self.assetWriter!.write(buffer: buffer, bufferType: bufferType)
}, completionHandler: {
if let error = $0 {
print(error)
}
})
}
The screen recorder method will return a buffer and buffer type, which we need to pass to the assetwriter
. AssetWriter
will write this buffer into the URL, which we need to pass to the assetwriter
initially.
Step 6: We have created object of AssetWriter assetWrite
and initialized as follows:
let outputFileName = NSUUID().uuidString+".mp4"
assetWriter = AssetWriter(fileName: outputFileName)
Step 7: Now, when the user double-taps on the screen, screen recording stops. So we will use stopCapture
of screen recorder method as following:
func stopCapture() {
screenRecorder.stopCapture {
self.isRecording = false
if let err = $0 {
print(err)
}
self.assetWriter?.finishWriting()
}
}
Step 8: As we are not writing any buffer into the assetWriter
, we need to tell assetWriter
to finish writing and generate output video URL.
Now, AssetWriter
will finish writing, and it will ask the user for permission to store video in the user photo library; if the user approves permission, it will be saved there.
AssetWriter
has a finishWritng
function for this.
public func finishWriting() {
writeQueue.sync {
self.assetWriter?.finishWriting(completionHandler: {
print("finishWriting")
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: self.filePath))
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
if let topVC = UIApplication.getTopViewController() {
topVC.present(alertController, animated: true, completion: nil)
}
}
}
})
}
}
Note: Some developers might get confused that RPscreenRecorder
also provides startRecording
and stopRecording
, but we have used startCapture
and stopCapture
.
Why is it so?
Here’s an image to answer it
startRecording
will start recording the app display. When we use the startRecording
method to end recording, we need to call the stopRecroding
method of RPscreenRecorder
.
When we are running an AVSession
and want to record screens, we need to use the startCapture
method.
startCapture
will start screen and audio capture. The recording initiated with startCapture
must end with stopCapture
.
Summing Up
We hope that you found the iPhone dual camera recording tutorial useful and your concepts about this feature are clear. You can find the source code of this dual-camera recording tutorial on Github.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments