2012-06-14 124 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

這對我來說很好,對於我來說很少有大驚小怪。 –

相關問題