How to start playing music when app is already in background?

I am writing an app that needs to play a sound as a reaction to a change in the users location.

when the app is running in the main focus (not in the background) everything works well and the sound is played correctly using AVAudioPlayer.

if i make the app go to the background (by pressing the home button) the sound keeps playing (i have changed the info.plist file and used [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; to make this possible).

my problem is that if the GPS event occurs when the app is already in the background the sound is never played (not even when the app is returned to the main focus) all though i can see in the console that the GPS event was captured, and when i debug using breakpoints i can see that the code that is supposed to play the music is preformed, but still the music is not played.

my question is: how can I make the app start playing a sound when it is not in the main focus (its in the background)?


I think you cannot do this, as the only way to activate you app is via a notification, and those are sheduled by time, not triggered by action. Maybe, there's a way using a background-thread, but that would be rejected by Apple anyway, so there's no sense in persuing. HTH, Marcus


I think you just need to declare your application as audio application. You can do this by adding UIBackgroundMode key with value audio in your info.plist file.

链接地址: http://www.djcxy.com/p/57446.html

上一篇: 如何在后台中断后恢复AVAudioPlayer

下一篇: 如何在应用程序已在后台时开始播放音乐?