2012-05-08 2 views
0

Как возобновить звук после окончания телефонного разговора.После окончания телефонного разговора нет звука

Вот мой код, но он не работает, не знаю, почему

@interface MainViewController : UIViewController <InfoDelegate, AVAudioPlayerDelegate> 

В файле м

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

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

{ 
    [self.audioPlayer play]; 
    } 

Любые идеи, что это я делаю неправильно или отсутствует код.

Пожалуйста, помогите.

ответ

2

В зависимости от того, как ваш звук был остановлен (вы звонили [self.audioPlayer stop]?), Вам, возможно, придется позвонить [self.audioPlayer prepareToPlay], прежде чем звонить play.

Я считаю, что вы должны сделать, это следующее:

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

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

По моему опыту, если вы звоните stop вы тогда должны вызвать prepareToPlay перед вызовом play снова.

EDIT:

С другой стороны, вам может понадобиться для обработки прерываний через 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

Правильно. Поэтому я думаю, что вам нужно приостановить звук в 'audioPlayerBeginInterruption'. Телефонный вызов вызывает ваш звук _stop_. Вы хотите, чтобы он сам _pause_, поэтому вы можете перезапустить его, когда прерывание завершено. –

+0

Я тоже пробовал это, но все равно нет звука – user1120133

Смежные вопросы