2013-11-21 64 views
0

我有兩段代碼:問題與手機在播放音樂文件調用的iOS SDK

代碼1 - 這是通過入耳式揚聲器播放音樂文件:

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"8Ocean.mp3"]; 
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory]; 
NSError *error=nil; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; 
if(!error) 
{ 
    [self.audioPlayer prepareToPlay]; 
    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; 
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); 
    [self.audioPlayer play]; 
} 
else 
{ 
    NSLog(@"%@",error.localizedDescription); 
} 

碼2 - 這是一個電話呼叫(路由到耳內或免提電話在通話過程中根據用戶設定)

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"8Ocean.mp3"]; 
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory]; 
NSError *error=nil; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; 
if(!error) 
{ 
    [self.audioPlayer prepareToPlay]; 
    self.audioPlayer.volume=10.0; 
    UInt32 sessionCategory = kAudioSessionOverrideAudioRoute_Speaker; 
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 
    [self.audioPlayer play]; 
} 
else 
{ 
    NSLog(@"%@",error.localizedDescription); 
} 

我的問題在播放音樂文件是:

代碼1和2獨立運行。然而,在使用代碼1之後,代碼2根本無法工作(除非應用程序被終止並重新啓動)

我試過了所有我能想到但無法解決問題的地方。

+1

請安排您的代碼與適當的甲酸酯,並請澄清你想達到什麼 – Retro

+0

我想提供用戶通過入耳式揚聲器在任何時間播放音樂的能力。即使在通話期間,用戶也可以通過入耳式揚聲器播放音樂(如果用戶決定在揚聲器電話上進行電話交談,那麼音樂也將在揚聲器電話上播放)。「 – Sarthak

回答

0

您的應用程序需要處理來自電話的音頻會話中斷,以便音頻處理不會因您的應用程序而中止。

+0

hotpaw2:請您詳細說明一下更多的是在通話過程中造成這種情況的音頻中斷,如果你在這裏通過一些代碼,這將是一個很大的幫助。 – Sarthak