AVKit录像与播放

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

import UIKit
import AVFoundation
import AVKit

class SimpleVideoCamController: UIViewController {

    @IBOutlet var cameraButton:UIButton!
    let captureSession = AVCaptureSession()
    var currentDevice:AVCaptureDevice!
    var videoFileOutput:AVCaptureMovieFileOutput!
    var cameraPreviewLayer:AVCaptureVideoPreviewLayer?
    var isRecording = false
    override func viewDidLoad() {
        super.viewDidLoad()
        configure()

    }

    private func configure(){
        // 以高解析度来预设session(高品质)
        // 如果想占用内存少的话可以用.low
        captureSession.sessionPreset = AVCaptureSession.Preset.high

        // 取得后置相机获取影片
        let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.back)
        // 当discovery搜寻到session实例化后,可用装置会被存在device的属性中
        guard let device = deviceDiscoverySession.devices.first else {return}
        currentDevice = device
        // 获取输入资料源
        guard let captureDeviceInput = try? AVCaptureDeviceInput(device: currentDevice) else{return}
        // 设置影片获取后输出的session
        videoFileOutput = AVCaptureMovieFileOutput()
        // 设置输入与输出装置的session
        captureSession.addInput(captureDeviceInput)
        captureSession.addOutput(videoFileOutput)
        // 提供相机预览
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        view.layer.addSublayer(cameraPreviewLayer!)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        cameraPreviewLayer?.frame = view.layer.frame

        // 将相机按钮带到前面
        view.bringSubview(toFront: cameraButton)
        captureSession.startRunning()
    }



    // MARK: - Action methods

    @IBAction func unwindToCamera(segue:UIStoryboardSegue) {

    }

    @IBAction func capture(sender: AnyObject) {
        if !isRecording {
            isRecording = true
            UIView.animate(withDuration: 0.5, delay: 0.0, options: [.repeat,.autoreverse,.allowUserInteraction], animations: {
                self.cameraButton.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
            }, completion: nil)
            let outputPath = NSTemporaryDirectory() + "output.mov"
            let outputFileURL = URL(fileURLWithPath: outputPath)
            videoFileOutput?.startRecording(to: outputFileURL, recordingDelegate: self)
        }else{
            isRecording = false
            UIView.animate(withDuration: 0.5, delay: 1.0, options: [], animations: {
                self.cameraButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }, completion: nil)
            cameraButton.layer.removeAllAnimations()
            videoFileOutput.stopRecording()
        }


    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "playVideo"{
            let videoPlayerViewController = segue.destination as! AVPlayerViewController
            let videoFileURL = sender as! URL
            videoPlayerViewController.player = AVPlayer(url: videoFileURL)
        }
    }
}

extension SimpleVideoCamController:AVCaptureFileOutputRecordingDelegate {
    func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        guard error == nil else {print(error ?? ""); return}
        performSegue(withIdentifier: "playVideo", sender: outputFileURL)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值