2012-05-08 63 views
0

電話通話結束後如何恢復音頻。電話通話結束後沒有音頻

這裏是我的代碼,但它不工作,不知道爲什麼

@interface MainViewController : UIViewController <InfoDelegate, AVAudioPlayerDelegate> 

在M檔

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer; 
{ 

} 
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer; 

{ 
    [self.audioPlayer play]; 
    } 

任何想法是什麼我在做錯誤或遺漏碼。

請幫助。

回答

2

根據您的音頻停止方式(您是否撥打[self.audioPlayer stop]?),您可能需要在致電play之前致電[self.audioPlayer prepareToPlay]

我相信你應該做的是以下幾點:

 
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer; 
{ 
    [self.audioPlayer pause]; 
} 

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer; 
{ 
    [self.audioPlayer play]; 
} 

以我的經驗,如果你打電話stop,那麼你必須調用prepareToPlay可以再次調用play之前。

編輯:

或者,你可能需要通過AudioSession直接處理中斷。

您的應用程序應該初始化AudioSession,像這樣:

AudioSessionInitialize(NULL, NULL, AudioInterruptionListener, NULL); 

然後,實施AudioInterruptionListener@implementation/@end塊之外,這樣的事情:

 
#define kAudioEndInterruption @"AudioEndInterruptionNotification" 
#define kAudioBeginInterruption @"AudioBeginInterruptionNotification" 

void AudioInterruptionListener (
          void  *inClientData, 
          UInt32 inInterruptionState 
          ) 
{ 
    NSString *notificationName = nil; 
    switch (inInterruptionState) { 
     case kAudioSessionEndInterruption: 
      notificationName = kAudioEndInterruption; 
      break; 

     case kAudioSessionBeginInterruption: 
      notificationName = kAudioBeginInterruption; 
      break; 

     default: 
      break; 
    } 

    if (notificationName) { 
     NSNotification *notice = [NSNotification notificationWithName:notificationName object:nil]; 
     [[NSNotificationCenter defaultCenter] postNotification:notice]; 
    } 
} 

回到你的Objective-C ,您需要收聽此代碼可能發佈的通知,例如:

 
// Listen for audio interruption begin/end 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(beginAudioInterruption:) 
              name:kAudioBeginInterruption object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(endAudioInterruption:) 
              name:kAudioEndInterruption object:nil]; 

和:

 
-(void)beginAudioInterruption:(id)context 
{ 
    [self.audioPlayer pause]; 
} 

-(void)endAudioInterruption:(id)context 
{ 
    [self.audioPlayer play]; 
} 

給一個漩渦。 :-)

+0

由於電話音頻自己暫停並且電話通話結束後沒有音頻。 – user1120133

+0

對。所以我認爲_you_需要暫停'audioPlayerBeginInterruption'中的音頻。電話正在導致您的音頻停止_stop_。您希望自己暫停使用,以便在中斷結束後重新啓動。 –

+0

我也嘗試過,但仍然沒有音頻 – user1120133