I'm writing an iPhone app that plays sound at regular intervals via a timer. The first version of this app played audio just fine when it was minimized, or when the screen went blank, however, after adding some UI, it looks like this has stopped. I'm not sure why, or even how to diagnose this (removing the UI code doesn't solve the issue).
I've set the background mode in the info.plist file: ("App plays audio or streams audio/video via Airplay") and I've also got the following in my app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback
error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES
error:&activationErr];
return YES;
}
And in my ViewDidLayoutSubViews method, I initialize AVPLayer thusly:
self.fAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.fAudioPlayer.numberOfLoops = 0;
[self.fAudioPlayer setVolume:volume];
[self.fVolumeLevelSlider setValue:volume];
Where fAudioPlayer is of type AVAudioPlayer. When my timer is hit, I play the audio like this:
self.fBGTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[self.fAudioPlayer play];
I'm still going through my changes to see what, if anything, I had put in that could cause the sound to stop playing in the background from one app version to the next. Does anyone see anything here that could be amiss?