调用finishWritingWithCompletionHandler时,AVAssetWriter失败

时间:2013-09-19 03:30:03

标签: audio encoding avfoundation ios7

我正在使用AVAssetWriter / AVAssetReader将PCM音频文件转码为AAC。我把它归结为一个在iOS6中工作但在iOS7中失败的简单项目。

一切顺利,直到我到[self.assetWriter finishWritingWithCompletionHandler:]然后,assetWriter进入失败状态,错误设置为-11800 AVFoundation未知错误,内部错误设置为-12733,显然对应于SampleBufferNotReady

dispatch_queue_t queue = dispatch_queue_create("audio.encode", DISPATCH_QUEUE_SERIAL);

success = [self.assetWriter startWriting];
if (!success)
{
  [self showStatus:@"Export: writer failed to startWriting"];
  return;
}
[self.assetWriter startSessionAtSourceTime:kCMTimeZero];

[assetWriterInput requestMediaDataWhenReadyOnQueue:queue
                                        usingBlock:
 ^{
   while([assetWriterInput isReadyForMoreMediaData])
   {
     NSAssert (self.assetWriter.status == AVAssetWriterStatusWriting, nil);
     CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];

     if (sampleBuffer)
     {
       NSAssert (CMSampleBufferIsValid(sampleBuffer), nil);
       NSAssert (CMSampleBufferDataIsReady(sampleBuffer), nil);

       BOOL success = [assetWriterInput appendSampleBuffer:sampleBuffer];

       if (!success)
       {
         [self showError:self.assetWriter.error];
         self.assetWriter = nil;
         CFRelease(sampleBuffer);
         return;
       }

       CFRelease(sampleBuffer);
     }
     else
     {
       if ([assetReader status] == AVAssetReaderStatusCompleted)
       {
         [assetWriterInput markAsFinished];

         [self.assetWriter finishWritingWithCompletionHandler:^{
           BOOL success = self.assetWriter.status == AVAssetWriterStatusCompleted;
           if (success)
           {
             [self showStatus: @"Did it!"];
             self.assetWriter = nil;
           }
           else
           {
             [self showError:self.assetWriter.error];
             self.assetWriter = nil;
           }
         }];
       }
       else
       {
         [self showError:assetReader.error];
         self.assetWriter = nil;
       }
     }
   }
 }

];

注意:我发布了Apple的一个错误,发布到开发论坛并使用了TSI。还没有得到任何答案。我的希望是你们中的一位天才会指出我的解决方法。

1 个答案:

答案 0 :(得分:5)

我和你有同样的问题,但最后我解决了这个问题,我使用这种方法:

CMTime cmTime = CMTimeMake(longDuration, 1);
[assetWriter endSessionAtSourceTime:cmTime];
[assetWriter finishWritingWithCompletionHandler^(){
    NSLog (@"finished writing");
];

如果我们调用finishWritingWithCompletionHandler,通常我们不需要调用它; 我希望这能解决你的问题。