title: Audio playing while app in background iOS
body: <p>I have an app mostly based around Core Bluetooth.
  When something specific happens, the app is woken up using Core Bluetooth background modes and it fires off an alarm, however I can't get the alarm working when the app is not in the foreground.</p>
  
  <p>I have an Alarm Singleton class which initialises <code>AVAudioPlayer</code> like this:</p>
  
  <pre><code>NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                      pathForResource:soundName
                               ofType:@"caf"]];
  
  self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error: nil];
  [self.player prepareToPlay];
  self.player.numberOfLoops = -1;
  [self.player setVolume:1.0];
  
  NSLog(@"%@", self.player);
  </code></pre>
  
  <p>This is the method that is called when my alarm code is called:</p>
  
  <pre><code>-(void)startAlert
  {
      NSLog(@"%s", __FUNCTION__);
  
      playing = YES;
      [self.player play];
      NSLog(@"%i", self.player.playing);
  
      if (vibrate) {
          [self vibratePattern];
      }
  }
  </code></pre>
  
  <p>Now when the app is in the foreground, <code>self.player.playing</code> returns <code>1</code> however when the app is in the background <code>self.player.playing</code> returns <code>0</code>. Why would this be?
  All the code is being called, so the app is awake and functioning.
  The vibrate works perfectly which uses <code>AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);</code></p>
  
  <p>Any idea why this sound won't play?</p>
  
  <p>Thanks</p>