自定义摄像录制 AVFoundation
AVCaptureSession 会话,需要添加输入两个 声音(captureVideoInput:AVCaptureDeviceInput) 和 摄像头(captureAudioInput:AVCaptureDeviceInput)
captureVideoPreviewLayer:AVCaptureVideoPreviewLayer! 展示给用户看,captureMovieFileOutput:AVCaptureMovieFileOutput 控制录制的路径,和开始录制 和结束录制
自定义摄像录制 AVFoundation
import AVKit
import AVFoundation
import UIKit
class ViewController: UIViewController {
var isBack:AVCaptureDevice.Position = .back
lazy var captureSession:AVCaptureSession = AVCaptureSession.init()
var captureVideoInput:AVCaptureDeviceInput!
var captureAudioInput:AVCaptureDeviceInput!
// 视频输出流
var captureMovieFileOutput:AVCaptureMovieFileOutput!
var captureVideoPreviewLayer:AVCaptureVideoPreviewLayer!
var viewContainer:UIView!
var focusCursor:UIImageView?
var timeLabel:UILabel!
/// 切换摄像头
var switchCameraBtn:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if self.captureSession.canSetSessionPreset(AVCaptureSession.Preset.hd1280x720) {
self.captureSession.sessionPreset = .hd1280x720
}
if let device = self.getVideoDevice() {
do {
try? self.captureVideoInput = AVCaptureDeviceInput.init(device: device)
} catch {
}
}
if let device = self.getAudioDevice() {
do {
try? self.captureAudioInput = AVCaptureDeviceInput.init(device: device)
} catch {
}
}
// 添加到 会话中
self.captureSession.addInput(self.captureVideoInput)
self.captureSession.addInput(self.captureAudioInput)
self.captureMovieFileOutput = AVCaptureMovieFileOutput.init()
// 添加到会话中
self.captureSession.addOutput(self.captureMovieFileOutput)
//
// AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
self.captureVideoPreviewLayer = AVCaptureVideoPreviewLayer.init(session: self.captureSession)
self.captureVideoPreviewLayer.frame = self.view.bounds
// self.view.layer.addSublayer(self.captureVideoPreviewLayer)
self.view.layer.insertSublayer(self.captureVideoPreviewLayer, at: 0)
self.captureSession.startRunning()
}
@IBAction func changeQH(_ sender: UIButton) {
if self.isBack == .back {
self.isBack = .front
}else{
self.isBack = .back
}
// self.isBack = !self.isBack
self.captureSession.removeInput(self.captureVideoInput);
let input = try! AVCaptureDeviceInput.init(device: self.getVideoDevice()!)
self.captureSession.addInput(input)
self.captureVideoInput = input
}
@IBAction func start(_ sender: Any) {
if self.captureMovieFileOutput.isRecording {
self.captureMovieFileOutput.stopRecording()
}else{
var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first
path = "\(path!)/aaaa.mp4"
let pathUrl = URL.init(fileURLWithPath: path!)
self.captureMovieFileOutput.startRecording(to: pathUrl, recordingDelegate: self)
}
}
func getVideoDevice()->AVCaptureDevice?{
var devices:[AVCaptureDevice]!
if #available(iOS 10, *) {
let ios10Device = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: self.isBack)
devices = ios10Device.devices
}else{
devices = AVCaptureDevice.devices(for: AVMediaType.video)
}
for item in devices {
if item.hasMediaType(AVMediaType.video) {
if item.position == self.isBack {
// 背面后摄像头
return item
}
}
}
return nil
}
func getAudioDevice()->AVCaptureDevice?{
var devices:[AVCaptureDevice]!
if #available(iOS 10, *) {
let ios10Device = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInMicrophone], mediaType: AVMediaType.audio, position: .unspecified)
devices = ios10Device.devices
}else{
devices = AVCaptureDevice.devices(for: AVMediaType.audio)
}
for item in devices {
if item.hasMediaType(AVMediaType.audio) {
return item
}
}
return nil
}
@IBAction func playerClick(_ sender: Any) {
var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first
path = "\(path!)/aaaa.mp4"
let pathUrl = URL.init(fileURLWithPath: path!)
let vc = AVPlayerViewController.init()
vc.player = AVPlayer.init(url: pathUrl)
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension ViewController:AVCaptureFileOutputRecordingDelegate
{
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
print("eee \(error ) outputFileURL \(outputFileURL)")
}
}
网友评论