title: ios: How to resume AVAsset video based writing session
body: <p>I'm working on video editing application for iphone/ipod touch. My app simply asks user to choose  one of already existing videos in the camera roll directory, then frame by frame changes pixel  values and saves again under different name in the camera roll directory. Because video processing might be quite long I really need to implement some kind of functionality to resume previously started session(ie. when video processing reaches 30% of total video length and user presses down the home button(or there is a phone call) when my application is brought back to the foreground video processing should start from 30%, not from beginning).</p>
  
  <p>Most important parts of my code(simplified a bit to be more readable):</p>
  
  <pre><code>AVAssetReader* assetReader = [[AVAssetReader alloc] initWithAsset:sourceAsset error:&amp;error];
  NSArray* videoTracks = [sourceAsset tracksWithMediaType:AVMediaTypeVideo]; 
  AVAssetTrack* videoTrack = [videoTracks objectAtIndex:0];
  AVAssetReaderTrackOutput* assetReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack 
  [assetReader addOutput:assetReaderOutput]);
  [assetReader addOutput:assetReaderOutput];
  [mVideoWriter startWriting]
  [mAssetReader startReading]
  [mVideoWriter startSessionAtSourceTime: kCMTimeZero];
  mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
  [writerInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{ 
      while (mWriterInput.readyForMoreMediaData) {
          CMSampleBufferRef nextBuffer = [mAssetReaderOutput copyNextSampleBuffer];
          if (nextBuffer) {
              // frame processing here
          } // if
      } // while
   }]; // block
   // when done saving my video to camera roll directory
  </code></pre>
  
  <p>I'm listening to my app's delegate callback methods like applicationWillResignActive, applicationWillEnterForeground but I'm not sure how to handle them in proper way. What I've tried already:</p>
  
  <p>1) [AVAssetWriter start/endSessionAtSourceTime], unfortunately stopping and starting in the last frame presentation time did not work for me</p>
  
  <p>2) saving "by hand" every part of movie that was processed and when processing reaches 100% merging all of them using AVMutableComposition, this solution however sometimes crashes in dispatch queue </p>
  
  <p>Would be really great if someone could give me some hints how it should be done correctly...                                        </p>