首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS Swift 2录制视频AVCaptureSession

iOS Swift 2录制视频AVCaptureSession
EN

Stack Overflow用户
提问于 2015-09-25 02:09:45
回答 1查看 4.3K关注 0票数 5

我创建了一个AVCaptureSession,并在其上附加了前置摄像头

代码语言:javascript
运行
复制
do {
   try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
   }catch{print("err")}

现在我想开始和停止记录触摸事件。我该怎么做呢?

代码语言:javascript
运行
复制
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("touch")
        //Start Recording
    }

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("release");
        //End Recording and Save
    }
EN

回答 1

Stack Overflow用户

发布于 2016-03-15 21:56:30

您没有提到是使用AVCaptureMovieFileOutput还是AVCaptureVideoDataOutput作为会话的输出。前者非常适合于快速记录视频,而后者则通过在记录会话期间获得CMSampleBuffer块来用于更高级的记录。

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

代码语言:javascript
运行
复制
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<UITouch>, withEvent event: UIEvent?) {
    print("touch")
    // start capture
    movieOutput.startRecordingToOutputFileURL(tempFilePath, recordingDelegate: self)

}

override func touchesEnded(touches: Set<UITouch>, 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)")
            }
            })

        }
    }
}

有关摄像头捕获的更多信息,请参阅WWDC 2014 - Session 508

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32768137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档