Ray Fix
Ray Fix

Reputation: 5645

AVAssetWriter fails when calling finishWritingWithCompletionHandler

I am using AVAssetWriter/AVAssetReader to transcode a PCM audio file to AAC. I have boiled it down to a simple project that works in iOS6 and fails in iOS7.

Every thing is going well until I get to [self.assetWriter finishWritingWithCompletionHandler:] Then the assetWriter goes into the failed state with the error set to -11800 AVFoundation unknown error with an internal error set to -12733 which apparently corresponds to 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;
       }
     }
   }
 }

];

Note: I have posted a bug with Apple, posted to the dev forum and used a TSI. Haven't got any answers yet. My hope is one of you geniuses will point me to a workaround.

Upvotes: 3

Views: 4172

Answers (1)

datuk_ahmad
datuk_ahmad

Reputation: 51

i have same problem with you, but finally i solve that problem, i use this method :

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

usually we don't need call this if we call finishWritingWithCompletionHandler; i hope this solve your problem.

Upvotes: 5

Related Questions