AVAssetWriterInput的readyForMoreMediaData是否在后台线程中更新?如果readyForMoreMediaData为NO,我是否可以在主线程中阻塞并等待,直到值变为YES?
我通过向AVAssetWriterInput推送数据来使用它(即不使用requestMediaDataWhenReadyOnQueue),并且我已经设置了expectsMediaDataInRealTime,99.9%的时间我可以在它上面调用appendSampleBuffer (或appendPixelBuffer),就像我的应用程序生成框架一样快。
除非您在AVAssetWriter会话过程中将设备(iPhone 3GS)设置为休眠15分钟左右,否则这种方法工作得很好。在唤醒设备后,appendPixelBuffer有时会收到一个错误消息,“当readyForMoreMediaData为NO时,无法追加像素缓冲区”。因此我的问题是-如何最好地响应readyForMoreMediaData=NO,如果我可以像这样在主线程中等待一小段时间:
while ( ![assetWriterInput readyForMoreMediaData] )
{
Sleep for a few milliseconds
}发布于 2012-07-22 08:11:17
注意不要只是阻塞线程,这是我在没有工作之前所做的事情:
while (adaptor.assetWriterInput.readyForMoreMediaData == FALSE) {
[NSThread sleepForTimeInterval:0.1];
}在我的iPad2上,上述方法有时会失败。这样做反而解决了问题:
while (adaptor.assetWriterInput.readyForMoreMediaData == FALSE) {
NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
[[NSRunLoop currentRunLoop] runUntilDate:maxDate];
}发布于 2018-03-27 17:12:49
找不到类似的东西,所以我把它留在了这里。Swift 4解决方案。最好使用精确的技术来解决这个问题。F.e.使用NSCondition:
func startRecording() {
// start recording code goes here
readyForMediaCondition = NSCondition()
readyForMediaObservation = pixelBufferInput?.assetWriterInput.observe(\.isReadyForMoreMediaData, options: .new, changeHandler: { [weak self](_, change) in
guard let isReady = change.newValue else {
return
}
if isReady {
self?.readyForMediaCondition?.lock()
self?.readyForMediaCondition?.signal()
self?.readyForMediaCondition?.unlock()
}
})
}接下来:
func grabFrame(time: CMTime? = nil) {
readyForMediaCondition?.lock()
while !pixelBufferInput!.assetWriterInput.isReadyForMoreMediaData {
readyForMediaCondition?.wait()
}
readyForMediaCondition?.unlock()
// append your framebuffer here
}最后别忘了让观察者失效
readyForMediaObservation?.invalidate()发布于 2018-09-29 16:44:22
int waitTime = 300;
while (weakSelf.input.readyForMoreMediaData == NO) {
NSLog(@"readyForMoreMediaData is NO");
NSTimeInterval waitIntervale = 0.001 * waitTime;
NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:waitIntervale];
[[NSRunLoop currentRunLoop] runUntilDate:maxDate];
waitTime += 200; // add 200ms every time
}https://stackoverflow.com/questions/5877149
复制相似问题