2012-05-08 26 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