Reputation: 6777
I'm programming an application that plays background music, and to do so I am using AVAudioSession like so:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithData:data error:NULL];
player.delegate = self;
[player setVolume:1.0];
...
[player prepareToPlay];
[player play];
Then, later, the user has the option to disable music. If they choose to do so, the following code is run:
[[AVAudioSession sharedInstance] setActive:NO error:nil];
[player stop];
Which successfully stops the music from playing. However, if music from the Music app (or another, like Spotify) is started, and then the app is switched to, the users music will fade out and pause. My app's music will not play, since it is still disabled, so the user is then left in silence.
How can I keep my app from pausing the user's music?
Upvotes: 3
Views: 6139
Reputation: 9944
You have to do the following when the user chooses to remove audio:
[player stop];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Upvotes: 10
Reputation: 6405
You need to set your audio session category
NSError *categoryError = nil;
[audioSession setCategory: AVAudioSessionCategoryAmbient error: &categoryError];
If you set it to AVAudioSessionCategorySoloAmbient (which is default), it will kill the background music and let your app take over, even if you're not playing music. It may be best to set it to SoloAmbient when you do intend to play music, and set it to Ambient when the user disables the music.
Upvotes: 3