我通过做两件事解决了这个问题:
1-我做的第一件事就是改变
videoURL
s文件来源路径:
// Old Way that was causing some sort of path error
videoURL = URL(fileURLWithPath: documentsPath.appending(UUID().uuidString + ".mp4"))
// This is what the Old Path looked like. Look at the series of numbers beginning with 506... directly after Documents
///var/mobile/Containers/Data/Application/AAEF38A2-7AF1-4A32-A612-296B1584A764/Documents506D36BA-0C27-466A-A0BA-C197481F471A.mp4
到
// New Way that got the path to work
let dirPath = "\(documentsPath)/Videos_\(UUID().uuidString).mp4"
videoURL = URL(fileURLWithPath: dirPath)
// This is what the new path looks like. After Documents there is now a forward slash, the word Videos with an underscore, and then the series of numbers beginning with 506...
///var/mobile/Containers/Data/Application/AAEF38A2-7AF1-4A32-A612-296B1584A764/Documents/Videos_506D36BA-0C27-466A-A0BA-C197481F471A.mp4
2-我做的第二件事是更改内部的代码
recorder.startCapture(handler: { (cmSampleBuffer, rpSampleBufferType, err)
:
recorder.startCapture(handler: { (cmSampleBuffer, rpSampleBufferType, err) in
if let err = err { return }
if CMSampleBufferDataIsReady(cmSampleBuffer) {
DispatchQueue.main.async {
switch rpSampleBufferType {
case .video:
print("writing sample....")
if self.assetWriter?.status == AVAssetWriter.Status.unknown {
print("Started writing")
self.assetWriter?.startWriting()
self.assetWriter?.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(cmSampleBuffer))
}
if self.assetWriter.status == AVAssetWriter.Status.failed {
print("StartCapture Error Occurred, Status = \(self.assetWriter.status.rawValue), \(self.assetWriter.error!.localizedDescription) \(self.assetWriter.error.debugDescription)")
return
}
if self.assetWriter.status == AVAssetWriter.Status.writing {
if self.videoInput.isReadyForMoreMediaData {
print("Writing a sample")
if self.videoInput.append(cmSampleBuffer) == false {
print("problem writing video")
}
}
}
case .audioMic:
if self.audioMicInput.isReadyForMoreMediaData {
print("audioMic data added")
self.audioMicInput.append(cmSampleBuffer)
}
default:
print("not a video sample")
}
}
}, completionHandler: { (error) in
if let error = error { return }
})
这与我遇到的实际问题无关,但如果音频不同步,则必须在下面添加此代码
viewDidLoad
我从
comments section here
.
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch {
#if DEBUG
print("Setting category to AVAudioSessionCategoryPlayback failed.")
#endif
}
如果你需要找到错误代码的含义,你可以在这里查看
https://www.osstatus.com
它帮我找到了
11800
对于这个问题,但不是
17508
.