title: MPMoviePlayerController audio only after application became active
body: <p>I use <code>MPMoviePlayerController</code> in my application and when I tap the home button and then open application again, the player continues playback, but only the audio. Instead of the video, the user sees a black screen.</p>
  
  <p>This bug is only on the device (iOS 5.0, maybe earlier versions too). On the simulator it's OK.</p>
  
  <p>Any ideas?</p>
  
  <p>Code that creates the <code>MPMoviePlayerController</code> instance and adds it to view:</p>
  
  <pre><code>if (!self.moviePlayerController) {
      MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
      if (player) 
      {
          /* Save the movie object. */
          [self setMoviePlayerController:player];
  
          /* Register the current object as an observer for the movie
           notifications. */
          [self installMovieNotificationObservers];
  
          player.shouldAutoplay = YES;
          player.controlStyle = MPMovieControlStyleNone;
          player.scalingMode = MPMovieScalingModeAspectFit;
          player.useApplicationAudioSession = NO;
          [player setContentURL:movieURL];
          [player prepareToPlay];
          if ([player respondsToSelector:@selector(setAllowsAirPlay:)]) {
              [player setAllowsAirPlay:YES];
          }
          [player setMovieSourceType:sourceType];
          [[player view] setFrame:[self.view bounds]];
  
          [self.view addSubview: [player view]];
          [self.view sendSubviewToBack:[player view]];
          [player release];
      }
  }
  </code></pre>
  
  <p>movieURL - is link to mp4 of this form: <a href="http://host.domain/filename.mp4" rel="nofollow">http://host.domain/filename.mp4</a>
  maybe <code>MPMoviePlayerController</code> not correctly works with mp4 source type over http?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>In this case, all is well:</p>
  
  <pre><code>- (void)applicationWillResignActiveNotification:(NSNotification*)notification {    
      if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
          self.playerState = PlayerStatePlaying;
      } else if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) {
          self.playerState = PlayerStatePaused;
      self.playbackTimeBeforeBackground = self.moviePlayerController.currentPlaybackTime;
      [self.moviePlayerController pause];
  }
  
  - (void)applicationDidBecomeActiveNotification:(NSNotification*)notification {
  self.moviePlayerController.currentPlaybackTime = -1;    
      if (self.playerState &amp; PlayerStatePlaying) {
          [self performSelector:@selector(startPlayerAfterDelayPause:) withObject:nil afterDelay:0.2];
      } else if (self.playerState &amp; PlayerStatePaused) {
          [self performSelector:@selector(startPlayerAfterDelayPause:) withObject:[NSNumber numberWithBool:YES] afterDelay:0.2];
      }
  }
  
  - (void)startPlayerAfterDelayPause:(NSNumber *)isPause{
      self.moviePlayerController.currentPlaybackTime = self.playbackTimeBeforeBackground;
      if ([isPause boolValue] == YES) {
          [self.moviePlayerController pause];
      } else {
          [self.moviePlayerController play];
      }
  }
  </code></pre>