title: IOS: Play sound while app in background
body: <p>I working on a little app where i want the phone to play a sound file on an event from my location controller.
  The problem is when the App is in Background mode the AVAudioPlayer not start the Audio file but the code is scheduled, getting NSLog output.
  Another thing is it works in the simulator but not on the phone.</p>
  
  <p>Whys not?</p>
  
  <p>This is my code to play the file:</p>
  
  <pre><code>- (void)tryPlayTaleSound:(NSString*)fileName {
  
      NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
      NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
      NSError *error;
  
      backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&amp;error];
  
      // Check to see if iPod music is already playing
      //UInt32 propertySize = sizeof(otherMusicIsPlaying);
      //AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &amp;propertySize, &amp;otherMusicIsPlaying);
  
      // Play the music if no other music is playing and we aren't playing already
      //if (otherMusicIsPlaying != 1 &amp;&amp; !backgroundMusicPlaying) {
      //[backgroundMusicPlayer prepareToPlay];
      [backgroundMusicPlayer play];
      backgroundMusicPlaying = YES;
      //}
      NSLog(@"%s - ", __FUNCTION__);
  }
  </code></pre>
  
  <p>In the log Im getting this.</p>
  
  <pre><code>Apr 26 13:57:48 iPhone CommCenter[58] &lt;Notice&gt;: Client [com.apple.persistentconnection[dataaccessd,85]] is telling PDP context 0 to go active.
  Apr 26 13:57:50 iPhone Herning100[1467] &lt;Warning&gt;: Playing audiofile - filename is Fil03
  Apr 26 13:57:50 iPhone mediaserverd[39] &lt;Warning&gt;: 13:57:50.653 &lt;AudioControl&gt; WARNING translating CMSession error: -12985 
  Apr 26 13:57:50 iPhone mediaserverd[39] &lt;Warning&gt;: 13:57:50.656 &lt;AudioControl&gt; WARNING translating CMSession error: -12985 
  Apr 26 13:57:50 IPhone mediaserverd[39] &lt;Error&gt;: 13:57:50.734 &lt;AudioControl&gt; AudioQueue: Error -12985 from AudioSessionSetClientPlayState(1467)
  Apr 26 13:57:50 iPhone Herning100[1467] &lt;Warning&gt;: -[AppDelegate tryPlayTaleSound:] - 
  Apr 26 13:57:50 iPhone com.apple.debugserver-199[1465] &lt;Warning&gt;: 9 +28.832083 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address =&gt; 0xfffffffe, size =&gt; 0, nesting_depth =&gt; 1024, info =&gt; 0x2fd7fe48, infoCnt =&gt; 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)
  Apr 26 13:57:51 iPhone com.apple.debugserver-199[1465] &lt;Warning&gt;: 10 +0.187961 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address =&gt; 0xfffffffe, size =&gt; 0, nesting_depth =&gt; 1024, info =&gt; 0x2fd7fe48, infoCnt =&gt; 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)
  </code></pre>
  
  <p>Is there a difference if I use the AVAudioPlayer or the AVAudioSession.?</p>
  
  <p>Hope someone can help.</p>
  
  <p>/Lasse</p>