Swift将视频捕获到不同的路径

时间:2017-05-06 20:06:02

标签: swift swift3 avfoundation avcapturesession

我尝试使用swift捕获视频。我想每隔x秒在一个新文件中捕获视频。

这是我目前的代码:

var i = 0

override func viewDidLoad() {
    super.viewDidLoad()

    if setupSession() {
        setupPreview()
        startSession()
    }

}

func setupSession() -> Bool {

    ...

    return true
}

func startSession() {

    ...
}

func tempURL() -> URL? {
    let directory = NSTemporaryDirectory() as NSString

    if directory != "" {

        i = i+1
        old_cur_videoID = cur_videoID_system

        cur_videoID_system = "\(cur_videoID)_\(i).mp4"
        let path = directory.appendingPathComponent(cur_videoID_system)
        return URL(fileURLWithPath: path)
    }

    return nil
}


func startRecording() {


    let delayTime = DispatchTime.now() + 2
    DispatchQueue.main.asyncAfter(deadline: delayTime) {
        self.startRecording()
    }

    outputURL = tempURL()
    movieOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: self)
}

两个代表职能:

func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {

}

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
    if (error != nil) {
        print("Error recording movie: \(error!.localizedDescription)")
    } else {        
        if(i > 1){
            saveOnServer()
        }   
    }
    outputURL = nil
}

如果我尝试运行代码,我会收到以下错误消息:

Error recording movie: Movie recording cannot be started

有人可以帮我解决问题吗?如果需要更多代码或某些部分不清楚,请随时询问。

非常感谢!

0 个答案:

没有答案