title: iOS background audio not playing
body: <p>I have an app that uses <code>CoreBluetooth</code> background modes. When a certain event happens I need to play an alarm sound. Everything works fine in the foreground and all bluetooth functionality works fine in the background. I also have it working where it schedules <code>UILocalNotification</code>'s in the background to sound the alarm, however I don't like the lack of volume control with these so want to play the alarm sound using AVAudioPlayer.</p>
  
  <p>I've added the background mode <code>audio</code> to my <code>.plist</code> file but can't get the sound to play properly.</p>
  
  <p>I am using a singleton class for the alarm and initialise the sound like this:</p>
  
  <pre><code>NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                      pathForResource:soundName
                                      ofType:@"caf"]];
  
  player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
  player.numberOfLoops = -1;
  [player setVolume:1.0];
  </code></pre>
  
  <p>I start the sound like this:</p>
  
  <pre><code>-(void)startAlert
  {
      playing = YES;
      [player play];
      if (vibrate)
          [self vibratePattern];
  }
  </code></pre>
  
  <p>and use this for the vibration:</p>
  
  <pre><code>-(void)vibratePattern
  {
      if (vibrate &amp;&amp; playing) {
          AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
          [self performSelector:@selector(vibratePattern) withObject:nil afterDelay:0.75];
      }
  }
  </code></pre>
  
  <p>The vibration works fine in the background, but no sound.
  If I use <code>Systemsound</code> to play the sound like this, it plays fine (But no volume control):</p>
  
  <pre><code>AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &amp;_sound);
  AudioServicesPlaySystemSound(_sound);
  </code></pre>
  
  <p>So what could be the reason why the AVAudioPlayer is not playing the sound file?</p>
  
  <p>Thanks</p>
  
  <p>EDIT -------</p>
  
  <p>The sound will play if it's already playing when the app is backgrounded. However making it start to play whilst backgrounded is not working.</p>