2012-06-14 130 views
2

是否可以在不停止iPod音乐的情况下播放应用程序中的声音?是否可以在不停止iPod音乐的情况下播放声音?

现在,我使用以下,但它停止的iPod音乐

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

然后

[mySound play]; 

回答

6

是的,你可以,你可以使用下面的代码

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

是,或者只是

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

这对我来说很好,对于我来说很少有大惊小怪。 –

相关问题