2012-06-14 3 views
2

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

이 작업은 상당히 번거롭지 만 잘 처리되었습니다. –