如何在Swift中录制带有avfoundation的视频? [英] How to record a video with avfoundation in Swift?

查看:218
本文介绍了如何在Swift中录制带有avfoundation的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在Swift中使用AVFoundation录制视频。我已经创建了一个自定义相机,但我只知道如何使用它拍摄静态图片,我无法弄清楚如何录制视频。根据我的理解,您必须使用 AVCaptureVideoDataOutput 来从录制中获取数据,但我无法弄清楚如何开始录制并实现委托方法。

I am trying to figure out how to record a video using AVFoundation in Swift. I have got as far as creating a custom camera but I only figured out how to take still pictures with it and I can't figure out how to record video. From what I understand you have to use AVCaptureVideoDataOutput to get the data from the recording but I can't figure out how to start the recording and implement the delegate methods.

整个AVFoundation编程指南/静止和视频媒体捕获都在Objective-C中,我似乎无法解读它。以下是我完成此任务的尝试:

The whole AVFoundation Programing Guide/Still and Video Media Capture is in Objective-C and I can't seem to decipher it out. Here's my attempt to accomplish this task:

首先我设置了相机/捕获会话

First I set up the camera/capture session

override func viewDidLoad() {
    super.viewDidLoad()

    captureSession.sessionPreset = AVCaptureSessionPresetHigh
    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()
                }
            }
        }
    }

}

然后一旦调用了beginSession(),我就设置了实时订阅源

Then once beginSession() is called I set up the live feed

func beginSession() {
    var err : NSError? = nil
    captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
    if err != nil {
        println("error: \(err?.localizedDescription)")
    }
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    self.cameraView.layer.addSublayer(previewLayer)
    self.cameraView.bringSubviewToFront(takePhotoButton)
    self.cameraView.bringSubviewToFront(self.snappedPicture)
    self.cameraView.bringSubviewToFront(self.backButton)
    previewLayer?.frame = self.cameraView.layer.frame
    captureSession.startRunning()
}

这是我在用户按下记录以实际记录和捕获视频的地方卡住的地方:

Here's where I get stuck where the user presses record to actually record and capture a video:

@IBAction func takeVideoAction(sender: AnyObject) {

    var recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self

    var videoFileOutput = AVCaptureMovieFileOutput()
    self.captureSession.addOutput(videoFileOutput)

    let filePath = NSURL(fileURLWithPath: "filePath")

    videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)

}

然后我调用 self.videoFileOutput.stopRecording()

然后是委托方法

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    return
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
    return
}

没有任何事情可以想象,因为我无法弄清楚如何实现它co直接。

And nothing happens as you can imagine because I can't figure out how to implement it correctly.

推荐答案

我已经尝试了你的代码。设置有意义的 filePath 时,一切都很顺利。像这样更改 filePath ,您将获得调用的委托方法:

I have tried your code. When set a meaningful filePath, everything goes fine. Change your filePath like this and you will get the delegate methods invoked:

    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let filePath = documentsURL.URLByAppendingPathComponent("temp")

    // Do recording and save the output to the `filePath`
    videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)

这篇关于如何在Swift中录制带有avfoundation的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆