title: Stream video while downloading iOS
body: <p>I am using iOS 7 and I have a .mp4 video that I need to download in my app. The video is large (~ 1 GB) which is why it is not included as part of the app. I want the user to be able to start watching the video as soon as is starts downloading. I also want the video to be able to be cached on the iOS device so the user doesn't need to download it again later. Both the normal methods of playing videos (progressive download and live streaming) don't seem to let you cache the video, so I have made my own web service that chunks up my video file and streams the bytes down to the client. I start the streaming HTTP call using NSURLConnection:</p>
  
  <pre><code>self.request = [[NSMutableURLRequest alloc] initWithURL:self.url];
  [self.request setTimeoutInterval:10]; // Expect data at least every 10 seconds
  [self.request setHTTPMethod:@"GET"];
  self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
  </code></pre>
  
  <p>When I receive a data chunk, I append it to the end of the local copy of the file:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
       NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:[self videoFilePath]];
       [handle truncateFileAtOffset:[handle seekToEndOfFile]];
       [handle writeData:data];
  }
  </code></pre>
  
  <p>If I let the device run, the file is downloaded successfully and I can play it using MPMoviePlayerViewController:</p>
  
  <pre><code>NSURL *url=[NSURL fileURLWithPath:self.videoFilePath];
  MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
  controller.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  [self presentMoviePlayerViewControllerAnimated:controller];
  </code></pre>
  
  <p>However, if I start the player before the file is completely downloaded, the video starts playing just fine. It even has the correct video length displayed at the top scrubber bar. But when the user gets to the position in the video that I had completed downloading before the video started, the video just hangs. If I close and reopen the MPMoviePlayerViewController, then the video plays until it gets to whatever location I was then at when I launched the MPMoviePlayerViewController again. If I wait until the entire video is downloaded, then the video plays without a problem.</p>
  
  <p>I am not getting any events fired, or error messages printed to the console when this happens (MPMoviePlayerPlaybackStateDidChangeNotification and MPMoviePlayerPlaybackDidFinishNotification are never sent after the video starts). It seems like there is something else that is telling the controller what the length of the video is other than what the scrubber is using...</p>
  
  <p>Does anyone know what could be causing this issue? I am not bound to using MPMoviePlayerViewController, so if a different video playback method would work in this situation I am all for it.</p>
  
  <p><strong>Related Unresolved Questions:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/14843546/avplayer-and-progressive-video-downloads-with-avurlassets">AVPlayer and Progressive Video Downloads with AVURLAssets</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11294990/progressive-video-download-on-ios?rq=1">Progressive Video Download on iOS</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12399699/how-to-play-an-in-downloading-progress-video-file-in-ios?rq=1">How to play an in downloading progress video file in IOS</a></p>
  
  <p><strong>UPDATE 1</strong>
  I have found that the video stall is indeed because of the file size when the video starts playing. I can get around this issue by creating a zero-ed out file before I start the download and over overwrite it as I go. Since I have control over the video streaming server, I added a custom header so I know the size of the file being streamed (default file size header for a streaming file is -1). I am creating the file in my didReceiveResponse method as follows:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {        
      // Retrieve the size of the file being streamed.
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
      NSDictionary *headers = httpResponse.allHeaderFields;
  
      NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
      [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
      self.streamingFileSize = [formatter numberFromString:[headers objectForKey:@"StreamingFileSize"]];
  
      // Check if we need to initialize the download file
      if (![[NSFileManager defaultManager] fileExistsAtPath:self.path])
      {            
          // Create the file being downloaded
          [[NSData data] writeToFile:self.path atomically:YES];
  
          // Allocate the size of the file we are going to download.
          const char *cString = [self.path cStringUsingEncoding:NSASCIIStringEncoding];
          int success = truncate(cString, self.streamingFileSize.longLongValue);
          if (success != 0)
          {
              /* TODO: handle errors here. Probably not enough space... See 'man truncate' */
          }
      }
  }
  </code></pre>
  
  <p>This works great, except that truncate causes the app to hang for about 10 seconds while it creates the ~1GB file on disk (on the simulator it is instant, only a real device has this problem). This is where I am stuck now - does anyone know of a way to allocate a file more efficiently, or a different way to get the video player to recognize the size of the file without needing to actually allocate it? I know some filesystems support "file size" and "size on disk" as two different properties... not sure if iOS has something like that?</p>