如何在 Swift 中使用 avfoundation 录制视频?
How to record a video with avfoundation in Swift?
我想知道如何在 Swift 中使用 AVFoundation 录制视频。我已经创建了一个自定义相机,但我只知道如何用它拍摄静态照片,但我不知道如何录制视频。据我了解,您必须使用 AVCaptureVideoDataOutput
从记录中获取数据,但我不知道如何开始记录和实现委托方法。
整个 AVFoundation 编程 Guide/Still 和视频媒体捕获都在 Objective-C 中,我似乎无法破译它。这是我完成此任务的尝试:
首先我设置了 camera/capture 会话
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() 被调用,我就会设置实时提要
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()
}
这是我卡住的地方,用户按下记录以实际录制和捕获视频:
@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
}
并没有像你想象的那样发生,因为我不知道如何正确地实现它。
我试过你的代码。当设置一个有意义的 filePath
时,一切正常。像这样更改您的 filePath
,您将调用委托方法:
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 录制视频。我已经创建了一个自定义相机,但我只知道如何用它拍摄静态照片,但我不知道如何录制视频。据我了解,您必须使用 AVCaptureVideoDataOutput
从记录中获取数据,但我不知道如何开始记录和实现委托方法。
整个 AVFoundation 编程 Guide/Still 和视频媒体捕获都在 Objective-C 中,我似乎无法破译它。这是我完成此任务的尝试:
首先我设置了 camera/capture 会话
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() 被调用,我就会设置实时提要
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()
}
这是我卡住的地方,用户按下记录以实际录制和捕获视频:
@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
}
并没有像你想象的那样发生,因为我不知道如何正确地实现它。
我试过你的代码。当设置一个有意义的 filePath
时,一切正常。像这样更改您的 filePath
,您将调用委托方法:
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)