- Record and save assets(video, audio, images) with AVFoundation
- Export recorded assets
Coordinates the flow of data between inputs and outputs. A single session of AVCaptureSession can handle multiple inputs and outputs (you do not need to create multiple sessions)
var captureSession = AVCaptureSession()
Starting an AVCaptureSessions allows data to flow between the input and output connections
// Starting a session
captureSession.startRunning()
// Stopping a session
captureSession.stopRunning()
You can use a AVCaptureVideoPreviewLayer to display the output video from the AVCaptureSession
AVCaptureConnection
It represents a connection between an input and an output to an AVCaptureSession
This represents the physical hardware devices (camera, microphone) that you can use as input to an AVCaptureSession.
You can list the available capture devices(eg. front vs rear camera), their abilities (wide angle format camera, slo motion)
Note: When capturing video, the camera and microphone are separate hardware, you will need to set both up as inputs to AVCaptureSession
To capture audio/visual data, we need to add inputs from the camera and the microphone.
(1) Start with creating a session.
let session = AVCaptureSession()
(2) Then create your device for capture
let frontCamera = AVCaptureDevice.defaultDevice(
withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera,
mediaType: AVMediaTypeVideo,
position: AVCaptureDevicePosition.front
)
(3) Create an input from the capture device
let cameraInput = try AVCaptureDeviceInput(device: frontCamera)
(4) Try adding the camera input to the capture session
if captureSession.canAddInput(input) {
captureSession.addInput(input)
}
Outputing to a playable file format (eg .mov)
let movieFileOutput = AVCaptureMovieFileOutput()
if captureSession.canAddOutput(movieFileOutput) {
captureSession.addOutput(movieFileOutput)
}
To start recording to a file format we can use (eg .mov), we first need a place to store our video files
let tempVideoURL = FileManager.default.temporaryDirectory
let outputFileName = NSUUID().uuidString
let tempFileURL = tempVideoURL.appendingPathComponent(outputFileName).appendingPathExtension("mov")
The AVCaptureMovieFileOutput provides some handy methods to start and stop recording
movieFileOutput.startRecording(toOutputFileURL: tempFileURL, recordingDelegate: self)
Note: This also sets the delegate for the recording to self
The AVCaptureFileOutputRecordingDelegate tells you when a recording session is finished, with a url link to the location on disk where the video file is stored
All of the recording can be done on a background queue
After we are done using our temporary resources don't forget to cleanup else our app will take up a ton of space!
func cleanUpTempResources() {
do {
let tempURL = FileManager.default.temporaryDirectory
try FileManager.default.removeItem(at: tempURL)
try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: false, attributes: nil)
}catch let error {
print(error.localizedDescription)
}
}
deinit {
cleanUpTempResources()
}
- What are the inputs and outputs for capturing and saving audio & video?
- How will we achieve recording video segments?
- How can we show a thumbnail preview of each video segment?
Before you can record audio/video, you need to set the permissions in your info.plist file and request for permission to use the camera and the microphone.