On an iPad, why does AVAudioSessionCategoryAmbient stop playing music on an iPod?

The following code should play sound and not stop playing music using iPad Music / iPod. (On iOS 5.1, the iPod app has become a music app).

Used AVAudioSessionCategoryAmbientinstead AVAudioSessionCategorySoloAmbient. But it actually stopped the music anyway. Is there something wrong with using AVAudioSessionCategoryAmbient?

NSURL *sound0URL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"0" ofType:@"aiff"]];

audioPlayer0 = [[AVAudioPlayer alloc] initWithContentsOfURL:sound0URL error:nil];

audioPlayer0.delegate = self;
[audioPlayer0 prepareToPlay];

NSError *setCategoryErr = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryErr];

NSLog(@"%@", setCategoryErr);

audioPlayer0.currentTime = 0;
[audioPlayer0 play];

setCategoryErrprinted as (null), so there should be no errors.

+3
source share
1 answer

As I said in my comment, the solution seems to be to set the AVAudioSession category BEFORE calling prepareToPlayAVAudioPlayer.

+5
source

All Articles