title: avaudioplayer play music in background
body: <p>I have a problem with AVAudioPlayer in background, everything works but when I have the controls in the lock screen and I click on the pause button the control receive the event but the lock screen with controls disappears. I want that lock screen remain with controls like Music app.</p>
  
  <p>I put these lines in <code>viewDidLoad</code>: </p>
  
  <pre><code>[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error: nil];
  </code></pre>
  
  <p>and then :</p>
  
  <pre><code>-(void)viewDidAppear:(BOOL)animated{
  
  
     [[UIApplication sharedApplication]beginReceivingRemoteControlEvents];
     [self becomeFirstResponder];
  }
  
  -(BOOL)canBecomeFirstResponder{ return YES; }
  
  - (void)viewWillDisappear:(BOOL)animated {
  
  
      //End recieving events
      [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
      [self resignFirstResponder];
  
       [super viewWillDisappear:animated];
  }
  
  - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
      //if it is a remote control event handle it correctly
  
      if (event.type == UIEventTypeRemoteControl) {
          if (event.subtype == UIEventSubtypeRemoteControlPause) {
  
              [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_icon.png"]
                                         forState:UIControlStateNormal];
              [self.audioPlayer pauseAudio];
              self.isPaused = FALSE;
          }
          else if (event.subtype == UIEventSubtypeRemoteControlPlay){
  
              if (!self.isPaused) {
                  [self.playButton setBackgroundImage:[UIImage imageNamed:@"pause.png"]
                                             forState:UIControlStateNormal];
  
                  //start a timer to update the time label display
                  self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                target:self
                                                              selector:@selector(updateTime:)
                                                              userInfo:nil
                                                               repeats:YES];
  
                  [self.audioPlayer playAudio];
                  self.isPaused = TRUE;
  
              }
          }
      }
  }
  </code></pre>