title: MPMoviePlayerController, playing in background with audio and video (detach video layer?)
body: <p>I'm having some issues getting a MPMoviePlayerController playing both <strong>audio and video</strong> to continue once the screen locks/ app is backgrounded. </p>
  
  <p>Here's what I've tried:</p>
  
  <p>"Required background modes" is an array with the first entry: [Item 0:  App Plays Audio]. </p>
  
  <p>Also, my view controller has:</p>
  
  <pre><code>-(void)viewDidLoad {
      [super viewDidLoad];
  
  
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  
      NSError *setCategoryError = nil;
      BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&amp;setCategoryError];
      if (!success) {
          NSLog(@"DOH Could not set Category!");
      }
  
      NSError *activationError = nil;
      success = [audioSession setActive:YES error:&amp;activationError];
      if (!success) {
          NSLog(@"DOH Could not set Active!");
      }
  
      … 
  
  }
  
  
  - (void)viewWillAppear:(BOOL)animated {
  
          [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
          [self becomeFirstResponder];
  }
  
  - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
      switch (event.subtype) {
          case UIEventSubtypeRemoteControlPlay:
              NSLog(@"Play");
              break;
          case UIEventSubtypeRemoteControlPause:
              NSLog(@"Pause");
              break;
          default:
              break;
      }
  }
  
  - (BOOL)canBecomeFirstResponder{
      return YES;
  }
  </code></pre>
  
  <p>The app that I'm trying to modify is (to all intents and purposes) the Sample app for LBYouTubeView, which allows you to play YouTube videos by determining the stream URL and just playing it using a MPMoviePlayerController. The full code from above is <a href="https://github.com/iandundas/LBYouTubeView" rel="nofollow">on github</a> - it's a modified LBYouTubeView project.</p>
  
  <p>The audio/video plays fine, but when the app is minimised then (no matter what I try) the MPMoviePlayerController gets paused. </p>
  
  <p>I've pored over the docs &amp; SO for a solution, but to no avail. </p>
  
  <p>NB: Apple's Technote "[Playing media in the background](Playing media in the background)" states that:</p>
  
  <blockquote>
    <p>Applications that play video cannot continue playing that video while in the background.</p>
  </blockquote>
  
  <p>but goes on to say: </p>
  
  <blockquote>
    <p>Note: If you disable the video tracks in the movie, or detach the AVPlayerLayer from its associated AVPlayer it will allow the movie audio to continue playing in the background.</p>
  </blockquote>
  
  <p>So - does anyone have any ideas on how this might be possible? I know I can feed the extracted YouTube URL into something like <a href="https://github.com/DigitalDJ/AudioStreamer" rel="nofollow">Matt Gallagher's Audio Streamer</a> and get complete <strong>audio only</strong> support for play/pause/phone lock etc, but unfortunately I do still need the video too. Thanks</p>
  
  <p>p.s. Just to repeat: the code I'm trying to get working is <a href="https://github.com/iandundas/LBYouTubeView" rel="nofollow">on github</a> with my changes - it's just a modified LBYouTubeView project, very little changed except what I've detailed above.</p>
  
  <p><strong>edit</strong> Matt Gallagher has <a href="http://www.cocoawithlove.com/2011/04/background-audio-through-ios-movie.html" rel="nofollow">written a post on CocoaWithLove</a> about this problem - seems he settles on switching to an audio-only stream when the app is minimised, though he admits this is a bit unreliable as it causes a few seconds of silence (when minimising and restoring the app) whilst it buffers the new stream. Not ideal :(</p>