美文网首页swift
AVFoundation 自定义相机录制Demo

AVFoundation 自定义相机录制Demo

作者: 秋叶红90 | 来源:发表于2019-12-24 16:38 被阅读0次
自定义摄像录制 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)")
    }
    
    
}

相关文章

  • AVFoundation 自定义相机录制Demo

    自定义摄像录制 AVFoundation AVCaptureSession 会话,需要添加输入两个 声音(capt...

  • CIFilter滤镜使用

    本Demo是以前写的仿Prisma,通过AVFoundation自定义相机UI、CIFilter实现滤镜的、二维码...

  • Swift 4.2 自定义相机

    自定义相机使用 AVFoundation 框架,简单的做了个demo,后面再更新,效果是这样的: 我们需要用到的几...

  • swift:拍照+录制+相册

    用swift写的一个demo,自定义相机,可以拍照+录制+相册,包含闪光灯,单点缩放,焦距,并且可以自定义:视频码...

  • iOS视频录制(AVFoundation自定义相机)

    前言 最近刚做完一个直播类的项目,在视频这块也有一定研究,下面总结下。 过程 首先视频项目一般要求定制化,用UII...

  • 技术集合

    相机开发 iOS开发--AVFoundation自定义相机 iOS视频播放AVPlayer的视频内容拉伸设置 状态...

  • IOS 人脸识别微笑眨眼

    iOS-AVFoundation自定义相机详解 需要翻墙 Face Detection in iOS Using ...

  • AVFoundation 自定义相机

    最近想研究一下AVFoundation,所以产生了这篇博客,欢迎大家指正参考大神博客链接使用 AVFoundati...

  • AVFoundation自定义相机

    git地址点我 代码整体排版重构 依赖Masonry

  • iOS 扫描条形码和二维码

    使用 AVFoundation类 可以使相机识别二维码和条形码 Demo地址: https://github.co...

网友评论

    本文标题:AVFoundation 自定义相机录制Demo

    本文链接:https://www.haomeiwen.com/subject/fbjfoctx.html