Is it possible to play sounds without stopping iPod music?

此生再无相见时 提交于 2019-12-20 01:39:40

问题


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];

回答1:


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];



回答2:


Yes, or just

AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];


来源:https://stackoverflow.com/questions/11036748/is-it-possible-to-play-sounds-without-stopping-ipod-music

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!