Reply To: Muting audio from background apps

Home Forums OpenEars Muting audio from background apps Reply To: Muting audio from background apps

#1026632
wfilleman
Participant

Hi Halle,

It’s Wes again :) It’s been awhile, but my users are still getting great value out of OE.

Upgraded to 2.041 to address the memory growth issue some of my users were seeing and I got bit by something I “fixed” in previous releases. Not sure if I ever posted about this, but in relation to this post about how the AVAudioSession works on init, there’s an undesirable side effect in OEContinuousAudioUnit.m when the app resumes from background.

Namely what happens is on resume from background, OE sees an audioSession interruption Type Ended which could be due to something in my code (not sure, but it doesn’t matter). The problem is if the user never has turned on OE (voice recognition) OE just goes ahead and set the active audio session in the handleInterruption call of OEContiniuousAudioUnit.m

What happens is that the background music the user was playing is killed and they are confused because they never turned on voice recognition.

How I fixed it for myself, and I’m presenting it here because I think this should be merged into the main release, is to see if the self.audioUnitState is kAudioUnitIsStarted before setting the AVAudioSession to Active. This prevents OE from stepping on the AudioSession if it’s not actually turned on.

Here’s the code. Note that I surrounded the additions with /* MOBILINC ADDITIONS

– (void)handleInterruption:(NSNotification *)notification {

NSInteger interruptionPhase = [[[notification userInfo] valueForKey:AVAudioSessionInterruptionTypeKey] intValue];

if (interruptionPhase == AVAudioSessionInterruptionTypeBegan) {
if(openears_logging == 1) NSLog(@”The Audio Session was interrupted.”);

NSDictionary *userInfoDictionary = @{@”OpenEarsNotificationType”: @”AudioSessionInterruptionDidBegin”}; // Send notification to OEEventsObserver.
NSNotification *notification = [NSNotification notificationWithName:@”OpenEarsNotification” object:nil userInfo:userInfoDictionary];
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];

}

if (interruptionPhase == AVAudioSessionInterruptionTypeEnded) {

if(openears_logging == 1) NSLog(@”The Audio Session interruption is over.”);

NSDictionary *userInfoDictionary = @{@”OpenEarsNotificationType”: @”AudioSessionInterruptionDidEnd”}; // Send notification to OEEventsObserver.
NSNotification *notification = [NSNotification notificationWithName:@”OpenEarsNotification” object:nil userInfo:userInfoDictionary];
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];

/*
* MOBILINC ADDITIONS
*/
if (self.audioUnitState == kAudioUnitIsStarted) {
/*
* MOBILINC ADDITIONS
*/

NSError *error = nil;

error = [self setAllAudioSessionSettings];

[[AVAudioSession sharedInstance] setActive:YES error:&error];

if (error != nil) NSLog(@”AVAudioSession set active failed with error: %@”, error);

/*
* MOBILINC ADDITIONS
*/
}
/*
* MOBILINC ADDITIONS
*/

}
}