gpt4 book ai didi

swift - Swift,AVFoundation startRecording 当 Recording 为 true 时

转载 作者:行者123 更新时间:2023-11-30 10:34:50 29 4
gpt4 key购买 nike

我想录制一个长视频,在录制过程中将其分成几个部分,在不同的路径中持续几秒钟(例如 Instagram 故事)。

我再次尝试 stopRecording 和 startRecording,但出现错误或丢失部分视频(再次在 stopRecording 和 startRecording 之间) 开发人员文档显示

You can call this method when they want to stop recording new samples to the current file, and do not want to continue recording to another file. If you want to switch from one file to another, you should not call this method. Instead you should simply call startRecording(to:recordingDelegate:) with the new file URL.

但是,当我第二次调用 startRecording() 时(isRecording == true)委托(delegate)被调用

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {}

错误是

Error Domain=AVFoundationErrorDomain Code=-11859 "Movie recording cannot be started" UserInfo={NSLocalizedDescription=Movie recording cannot be started, NSUnderlyingError=0x281efcc00 {Error Domain=NSOSStatusErrorDomain Code=-16419 "(null)"}, NSLocalizedRecoverySuggestion=Stop the movie recording in progress and try again., NSLocalizedFailureReason=A movie recording is already in progress.}

最佳答案

我这样解决了我的问题

我做了一面旗帜

var isRecordong: Bool = false

在我的开始操作中,开始录制后,我将标志更改为 true 并启动计时器

@IBAction func startRecording() {

movieOutput.startRecording(to: outputURL, recordingDelegate: self)
isRecording = true

timer = Timer.scheduledTimer(withTimeInterval: duration, repeats: true, block: { [weak self] ( _ ) in

guard let weakSelf = self else { return }

if weakSelf.movieOutput.isRecording == true {
weakSelf.movieOutput.stopRecording()
}
})
}

接下来我实现委托(delegate)方法 didFinishRecordingTo 来进行循环录制

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {

if self.isRecording {
movieOutput.startRecording(to: newOutputURL, recordingDelegate: self)
} else {
//perform action after recording
}

}

在我的停止操作中,我停止录制,将标志更改为 false 并删除计时器

@IBAction func stopRecording() {
isRecording = false
if movieOutput.isRecording == true {
movieOutput.stopRecording()
} else {
//perform action after recording, if action and timer will work simultaneously
}
timer.invalidate()
}

一些要点

  • 开始和停止记录之间有轻微的延迟(即这个间隔不记录),但大约是0.1秒,这适合我。
  • 第一个文件比其余文件和文件持续时间小 0.1–0.3 秒,这是由于计时器的原因。此外,每个文件的持续时间将与计时器相差零点几秒

关于swift - Swift,AVFoundation startRecording 当 Recording 为 true 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266668/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com