如何在swift中通过AVCaptureVideoDataOutput()和AVCaptureSession()录制视频

时间:2015-08-06 10:29:57

标签: ios swift

这是我的代码,我想要记录视频点击startVideoRecording按钮方法,当点击stopVideoRecording按钮时,它应该保存在文件目录中请帮忙。

我已导入UIKit导入MediaPlayer和MobileCoreServices导入

AVFoundation class VideoDelegate : NSObject, AVCaptureFileOutputRecordingDelegate {
    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
        println("capture output : finish recording to \(outputFileURL)")
    }
    func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
        println("capture output: started recording to \(fileURL)")

    }
     } class ViewController: UIViewController ,AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureFileOutputRecordingDelegate{

    var previewLayer : AVCaptureVideoPreviewLayer?
    var captureDevice : AVCaptureDevice?
    var videoCaptureOutput = AVCaptureVideoDataOutput()
    let captureSession = AVCaptureSession()    // var videoCaptureOutputF = AVCaptureFileOutput()

    override func viewDidLoad() {
        super.viewDidLoad()
        captureSession.sessionPreset = AVCaptureSessionPreset640x480
        let devices = AVCaptureDevice.devices()
        for device in devices {

            if (device.hasMediaType(AVMediaTypeVideo)) {
                if device.position == AVCaptureDevicePosition.Back {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        beginSession()
                    }

                }

            }

        }

    }

    func beginSession() {
        var err : NSError? = nil
        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
        if err != nil {
            println("Error: \(err?.localizedDescription)")
        }
        videoCaptureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
        //videoCaptureOutput.sampleBufferDelegate=self
        videoCaptureOutput.alwaysDiscardsLateVideoFrames = true
        captureSession.addOutput(videoCaptureOutput)
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.view.layer.addSublayer(previewLayer)
        previewLayer?.frame = CGRectMake(0, 20, self.view.bounds.width,  self.view.bounds.height)
        var startVideoBtn = UIButton(frame: CGRectMake(10,40, 40, 40))
        startVideoBtn.backgroundColor=UIColor.greenColor()
        startVideoBtn.addTarget(self, action: "startVideoRecording", forControlEvents:
            UIControlEvents.TouchUpInside)
        self.view.addSubview(startVideoBtn)
        var stopVideoBtn = UIButton(frame: CGRectMake(200, 40, 40, 40))
        stopVideoBtn.backgroundColor=UIColor.redColor()
        stopVideoBtn.addTarget(self, action: "stopVideoRecording", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(stopVideoBtn)
    }
    func startVideoRecording(){
        captureSession.startRunning()
        //        AVCaptureMovieFileOutput *aMovieFileOutput = <#Get a movie file output#>;

        //        NSURL *fileURL = <#A file URL that identifies the output location#>;

        //        [aMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:<#The delegate#>];
       }
    func stopVideoRecording(){
        captureSession.stopRunning()
        print(videoCaptureOutput)

        // here i am getting problems that how to save recorded   video
        **let videoDelegate = VideoDelegate()
        let fileOutput = AVCaptureMovieFileOutput()
       // captureSession.addOutput(videoCaptureOutput)
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
        let outputPath = "\(documentsPath)/output.mp4"
        let outputFileUrl = NSURL(fileURLWithPath: outputPath)
     fileOutput.startRecordingToOutputFileURL(outputFileUrl, recordingDelegate: videoDelegate)**

1 个答案:

答案 0 :(得分:0)

快捷键-4

func startVideoRecording(){
    captureSession.startRunning()
    let fileOutput = AVCaptureMovieFileOutput()
    captureSession.addOutput(fileOutput)
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
    let outputPath = "\(documentsPath)/output.mp4"
    let outputFileUrl = NSURL(fileURLWithPath: outputPath)
    fileOutput.startRecording(to: outputFileUrl, recordingDelegate: self)
   }
func stopVideoRecording(){
    fileOutput.stopRecording()
 }

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    println("capture output : finish recording to \(outputFileURL)”)
    UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, nil, nil, nil)
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
    println("capture output: started recording to \(fileURL)")
}