本文将为Kcamera增加录制视频的功能.Kcamera的代码可以在https://github.com/changjianfeishui/Kcamera上找到.
实现步骤
初始化视频输出对象
继续之前的Kcamera项目,在CaptureModel类中以下属性:
//用户捕捉输出视频
var movieOutput:AVCaptureMovieFileOutput!
//记录当前的捕捉输入对象
var activeVideoInput:AVCaptureDeviceInput!
//视频的输出地址
var outputURL:NSURL!
在setupSession()方法返回前增加如下代码:
//8. 创建视频输出对象并添加到捕捉会话中
self.movieOutput = AVCaptureMovieFileOutput()
if self.captureSession.canAddOutput(self.movieOutput){
self.captureSession.addOutput(self.movieOutput)
}
并在方法中获取到设备输入对象之后添加下面一句代码,
//在注释的第4步之后添加
self.activeVideoInput = videoInput
设置AVCaptureFileOutputRecordingDelegate代理
在开始将捕获到的视频写入本地文件时,需要先遵循AVCaptureFileOutputRecordingDelegate代理.在CaptureModel类声明后遵循该代理,参考如下:
class CaptureModel: NSObject, AVCaptureFileOutputRecordingDelegate{
//...
}
并实现代理方法:
//MARK : AVCaptureFileOutputRecordingDelegate
//捕获并写入到本地完成
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
if (error == nil) {
self.writeVideoToAssetsLibrary(self.outputURL)
}
self.outputURL = nil
}
其中writeVideoToAssetsLibrary()方法如下:
//写入视频到本地文件系统
func writeVideoToAssetsLibrary(videoURL:NSURL) -> Void {
//1. 创建资源库
let library = ALAssetsLibrary()
//2. 检查视频是否可被写入
if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(videoURL) {
library.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (assetURL, error) in
})
}
}
开始捕捉视频
在CaptureModel类中增加startRecording()方法:
//开始捕捉视频
func startRecording() -> Void {
//1. 判断是否正在录制
if !self.movieOutput.recording {
//2. 设置视频输入和输出的连接
let videoConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo)
//3. 设置视频方向
if videoConnection.supportsVideoOrientation {
videoConnection.videoOrientation = self.currentVideoOrientation()
}
//4. 设置视频稳定性
if videoConnection.supportsVideoStabilization {
videoConnection.enablesVideoStabilizationWhenAvailable = true
}
//5. 平滑对焦模式
let device = self.activeVideoInput.device
if device.smoothAutoFocusSupported {
if ((try? device.lockForConfiguration()) != nil) {
device.smoothAutoFocusEnabled = true
device.unlockForConfiguration()
}
}
//6. 获取要写入到的本地URL地址
self.outputURL = self.uniqueURL()
//7. 写入视频
self.movieOutput.startRecordingToOutputFileURL(self.outputURL, recordingDelegate: self)
}
}
其中,uniqueURL()方法如下
//生成本地文件地址
func uniqueURL() -> NSURL {
let dirPath = NSTemporaryDirectory() as NSString
let date = NSDate()
let dateformattre = NSDateFormatter()
dateformattre.dateFormat = "yyyyMMddHHMMSS"
var dateString = dateformattre.stringFromDate(date)
dateString = dateString.stringByAppendingString(".mov")
let filePath = dirPath.stringByAppendingPathComponent(dateString)
return NSURL.fileURLWithPath(filePath)
}
停止捕获视频
停止捕捉的方法如下:
//停止捕捉视频
func stopRecording() -> Void {
if self.movieOutput.recording {
self.movieOutput.stopRecording()
}
}
测试
修改ViewController的captureBtnDidClicked()方法如下:
//点击拍摄按钮
@IBAction func captureBtnDidClicked(sender: CaptureButton) {
if self.cameraModel == CameraModel.Video{
sender.selected = !sender.selected
//录制视频
if sender.selected {
self.captureModel.startRecording()
}else{
self.captureModel.stopRecording()
}
}else{
//拍摄图片
self.captureModel.captureStillImage()
}
}
编译运行程序,拍摄视频,然后在相册中查看录制的视频.