Reputation: 4950
Hi I have a Pedometer app that uses location services to stay in the background, now I need to play a beep sound every 1000 steps.
I'm trying to play like this:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"beep" withExtension:@"mp3"];
success = [audioSession setActive:YES error:nil];
AVPlayerItem * item = [[AVPlayerItem alloc] initWithURL:audioFileLocationURL];
queuePlayer = [[AVQueuePlayer alloc] initWithItems:@[item]];
[queuePlayer play];
But it doesn't play, it does play if I run this code while the app is in the foreground, but it I run it while it's in background it does nothing.
Any idea?
Thanks!
Upvotes: 1
Views: 261
Reputation: 4950
I still don't get why, but I manage it to work.
What I had to do was to play the music(silent sound) for a little way while the app is active and then pause it.
After doing that, everything works if I resume the playback while being in the background.
Upvotes: 1