I'm trying to play a sound when my App is in the background. I use AVAudioPlayer
, and it works correctly when the app is active, but in the background AVAudioPlayer
doesn't play anything and I don't know how can I do.
I have thought that I could use UILocalNotification
, but it shows and alert in the lockscreen and I don't want it.
Thanks for your answers. I have another doubt. If I create this audiosession, and I am playing music the system will mix both at the same time?.
How can I do to decrease ipod music volume will my app sound is playing (3 seconds) and increase again when it had finished like most of the running apps in the AppStore. (In the background too)
This is my code in the AppDelegate
//Creamos la sesión de audio
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
And in my view when I want to play the sound:
-(void)avisosound{
// AudioSessionSetActive(YES);
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil){
}
else{
[audioPlayer play];
}
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
But when I play the sound, the ipod music stops.