iOS Swift 2录制videoAVCaptureSession

我创建了一个AVCaptureSession并将前置摄像头连接到它上面

do { try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice)) }catch{print("err")} 

现在我想开始并停止在touche事件上录制。 我该怎么做呢?

 override func touchesBegan(touches: Set, withEvent event: UIEvent?) { print("touch") //Start Recording } override func touchesEnded(touches: Set, withEvent event: UIEvent?) { print("release"); //End Recording and Save } 

您没有提及是否使用AVCaptureMovieFileOutputAVCaptureVideoDataOutput作为会话的输出。 前者非常适合快速录制video而无需进一步编码,后者通过在录制会话期间获取CMSampleBuffer块来用于更高级的录制。

对于这个答案的范围,我将使用AVCaptureMovieFileOutput ,这里是一些极简主义的起始代码:

 import UIKit import AVFoundation import AssetsLibrary class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate { var captureSession = AVCaptureSession() lazy var frontCameraDevice: AVCaptureDevice? = { let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice] return devices.filter{$0.position == .Front}.first }() lazy var micDevice: AVCaptureDevice? = { return AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) }() var movieOutput = AVCaptureMovieFileOutput() private var tempFilePath: NSURL = { let tempPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("tempMovie").URLByAppendingPathExtension("mp4").absoluteString if NSFileManager.defaultManager().fileExistsAtPath(tempPath) { do { try NSFileManager.defaultManager().removeItemAtPath(tempPath) } catch { } } return NSURL(string: tempPath)! }() private var library = ALAssetsLibrary() override func viewDidLoad() { super.viewDidLoad() //start session configuration captureSession.beginConfiguration() captureSession.sessionPreset = AVCaptureSessionPresetHigh // add device inputs (front camera and mic) captureSession.addInput(deviceInputFromDevice(frontCameraDevice)) captureSession.addInput(deviceInputFromDevice(micDevice)) // add output movieFileOutput movieOutput.movieFragmentInterval = kCMTimeInvalid captureSession.addOutput(movieOutput) // start session captureSession.commitConfiguration() captureSession.startRunning() } override func touchesBegan(touches: Set, withEvent event: UIEvent?) { print("touch") // start capture movieOutput.startRecordingToOutputFileURL(tempFilePath, recordingDelegate: self) } override func touchesEnded(touches: Set, withEvent event: UIEvent?) { print("release") //stop capture movieOutput.stopRecording() } private func deviceInputFromDevice(device: AVCaptureDevice?) -> AVCaptureDeviceInput? { guard let validDevice = device else { return nil } do { return try AVCaptureDeviceInput(device: validDevice) } catch let outError { print("Device setup error occured \(outError)") return nil } } func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) { } func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { if (error != nil) { print("Unable to save video to the iPhone \(error.localizedDescription)") } else { // save video to photo album library.writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: { (assetURL: NSURL?, error: NSError?) -> Void in if (error != nil) { print("Unable to save video to the iPhone \(error!.localizedDescription)") } }) } } } 

有关Camera Capture的更多信息,请参阅WWDC 2014 – 会话508