Skip to content

Instantly share code, notes, and snippets.

@Ilesh
Created November 20, 2019 09:55
Show Gist options
  • Save Ilesh/952e7f1e28566358b58d05281a758d68 to your computer and use it in GitHub Desktop.
Save Ilesh/952e7f1e28566358b58d05281a758d68 to your computer and use it in GitHub Desktop.
Video recorder with audio controller.
//
// VideoRecorder.swift
// VideoRecorder
//
// Created by Ilesh's 2018 on 20/11/19.
// Copyright © 2019 Ilesh's. All rights reserved.
//
import UIKit
import AVFoundation
class VideoRecorder: UIViewController {
@IBOutlet weak var videoView: UIView!
var captureSession = AVCaptureSession()
var previewLayer = AVCaptureVideoPreviewLayer()
var movieOutput = AVCaptureMovieFileOutput()
var videoCaptureDevice : AVCaptureDevice?
var objCaptureConnection : AVCaptureConnection?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.avCaptureVideoSetUp()
}
func avCaptureVideoSetUp(){
if let devices = AVCaptureDevice.devices(for: AVMediaType.video) as? [AVCaptureDevice] {
for device in devices {
if device.hasMediaType(AVMediaType.video) {
if device .position == AVCaptureDevice.Position.back{
videoCaptureDevice = device
}
}
}
if videoCaptureDevice != nil {
do {
// Add Video Input
try self.captureSession.addInput(AVCaptureDeviceInput(device: videoCaptureDevice!))
// Get Audio Device
//let audioInput = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
let audioInput = AVCaptureDevice.default(for: .audio)
//Add Audio Input
try self.captureSession.addInput(AVCaptureDeviceInput(device: audioInput!))
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection!.videoOrientation = AVCaptureVideoOrientation.portrait
self.videoView.layer.addSublayer(self.previewLayer)
//Add File Output
self.captureSession.addOutput(self.movieOutput)
captureSession.startRunning()
//create object and store it value
if let audioConnection :AVCaptureConnection = movieOutput.connection(with:.audio){
self.objCaptureConnection = audioConnection
}
}catch {
print(error)
}
}
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let bounds: CGRect = videoView.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.bounds = bounds
previewLayer.position = CGPoint(x: bounds.midX, y: bounds.midY)
}
@IBAction func recordVideoAction(_ sender: UIButton) {
if movieOutput.isRecording {
movieOutput.stopRecording()
} else {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let fileUrl = paths.first!.appendingPathComponent("output.mov")
try? FileManager.default.removeItem(at: fileUrl)
// movieOutput.startRecording(toOutputFileURL: fileUrl, recordingDelegate: self as AVCaptureFileOutputRecordingDelegate)
movieOutput.startRecording(to: fileUrl, recordingDelegate: self)
}
}
@IBAction func audioClick(_ sender: UIButton) {
if let connection = self.objCaptureConnection{
if connection.isEnabled {
connection.isEnabled = false;
}else{
connection.isEnabled = true;
}
}
}
}
extension VideoRecorder : AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error == nil {
UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, nil, nil, nil)
let alert = UIAlertController(title: "Success", message: "Saved in Photo Library", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment