当在调用broadcastPaused()函数后异步写入音视频时,可能会遇到此问题。解决方法是使用AVAssetWriter的waitForAsynchronousWrites()函数来等待异步写入完成,然后再调用finishWriting(completionHandler:)函数结束视频录制。
示例代码如下:
// 在调用broadcastPaused()后异步写入音视频
writer?.startSession(atSourceTime: CMClockGetTime(CMClockGetHostTimeClock()))
if writer?.status == .writing {
let audioInput = audioWriterInput
let videoInput = videoWriterInput
// 异步写入音频和视频
writer?.startWriting()
writer?.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
if audioInput.isReadyForMoreMediaData && videoInput.isReadyForMoreMediaData {
audioInput.append(sampleBuffer)
if let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
let timestamps = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
videoInput.append(imageBuffer, withPresentationTime: timestamps)
}
}
// 等待异步写入完成
writer?.waitForAsynchronousWrites {
writer?.finishWriting(completionHandler: { [weak self] in
// 结束录制
})
}
}