Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to play sounds without stopping iPod music?

Is it possible to play sounds within an app without stopping iPod music?

Right now I'm using the following, but it stops iPod music

soundPath =[[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"mp3"];
soundURL = [NSURL fileURLWithPath:soundPath];   
mySound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[mySound prepareToPlay];

and then

[mySound play];
like image 444
Abramodj Avatar asked Jan 26 '26 20:01

Abramodj


2 Answers

Yes you can, you could use the following code

// setup session correctly
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryPlayback error:nil];
OSStatus propertySetError = 0;

UInt32 mixingAllow = true;
propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (mixingAllow),&mixingAllow);

NSError *error = nil;
[audiosession setActive:YES error:&error];

// play sound
NSURL *url = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *audioplayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&;error]autorelease];
like image 53
Omar Abdelhafith Avatar answered Jan 28 '26 10:01

Omar Abdelhafith


Yes, or just

AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
like image 45
Tertium Avatar answered Jan 28 '26 11:01

Tertium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!