From 8a2310c4a5debd4b6dbeaa9cf0562bfcc1eb653b Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Thu, 6 Feb 2014 10:50:49 +0100 Subject: [PATCH] Use iOS6 AudioSession notifications instead of the deprecated Delegate method --- Classes/LinphoneManager.h | 2 +- Classes/LinphoneManager.m | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index bbab62112..3abc17c08 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -87,7 +87,7 @@ typedef struct _LinphoneManagerSounds { SystemSoundID message; } LinphoneManagerSounds; -@interface LinphoneManager : NSObject { +@interface LinphoneManager : NSObject { @protected SCNetworkReachabilityRef proxyReachability; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 23571eff6..ec055ff1f 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -833,6 +833,17 @@ static LinphoneCoreVTable linphonec_vtable = { linphone_core_iterate(theLinphoneCore); } + +- (void)audioSessionInterrupted:(NSNotification *)notification +{ + int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue]; + if (interruptionType == AVAudioSessionInterruptionTypeBegan) { + [self beginInterruption]; + } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) { + [self endInterruption]; + } +} + - (void)startLibLinphone { if (theLinphoneCore != nil) { [LinphoneLogger logc:LinphoneLoggerLog format:"linphonecore is already created"]; @@ -904,16 +915,23 @@ static LinphoneCoreVTable linphonec_vtable = { repeats:YES]; //init audio session AVAudioSession *audioSession = [AVAudioSession sharedInstance]; - BOOL bAudioInputAvailable= [audioSession inputIsAvailable]; - [audioSession setDelegate:self]; - + BOOL bAudioInputAvailable= audioSession.inputAvailable; NSError* err; - [audioSession setActive:NO error: &err]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(audioSessionInterrupted:) + name:AVAudioSessionInterruptionNotification + object:nil]; + + + if( ![audioSession setActive:NO error: &err] && err ){ + NSLog(@"audioSession setActive failed: %@", [err description]); + } if(!bAudioInputAvailable){ - UIAlertView* error = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No microphone",nil) - message:NSLocalizedString(@"You need to plug a microphone to your device to use this application.",nil) - delegate:nil - cancelButtonTitle:NSLocalizedString(@"Ok",nil) + UIAlertView* error = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No microphone",nil) + message:NSLocalizedString(@"You need to plug a microphone to your device to use this application.",nil) + delegate:nil + cancelButtonTitle:NSLocalizedString(@"Ok",nil) otherButtonTitles:nil ,nil]; [error show]; [error release]; @@ -974,8 +992,7 @@ static LinphoneCoreVTable linphonec_vtable = { //just in case [self removeCTCallCenterCb]; - AVAudioSession *audioSession = [AVAudioSession sharedInstance]; - [audioSession setDelegate:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; if (theLinphoneCore != nil) { //just in case application terminate before linphone core initialization [LinphoneLogger logc:LinphoneLoggerLog format:"Destroy linphonecore"]; @@ -1146,7 +1163,7 @@ static int comp_call_state_paused (const LinphoneCall* call, const void* param) - (void)beginInterruption { LinphoneCall* c = linphone_core_get_current_call(theLinphoneCore); [LinphoneLogger logc:LinphoneLoggerLog format:"Sound interruption detected!"]; - if (c) { + if (c && linphone_call_get_state(c) == LinphoneCallStreamsRunning) { linphone_core_pause_call(theLinphoneCore, c); } }