I am trying to acquire a microphone when the application is in the background. I use the technology of audio devices and can record audio in the background. But as soon as my AudioSession is interrupted, I cannot restart the AudioSession with the application in the background. Note. I can restart AudioSession if the application is in the foreground. Here is the interrupt code:
- (void) beginInterruption { [[AVAudioSession sharedInstance] setActive:NO error:&error]; AudioOutputUnitStop(m_audioUnit); } - (void) endInterruptionWithFlags:(NSUInteger) flags{ [[AVAudioSession sharedInstance] setActive:YES error:&error]; AudioOutputUnitStart(m_audioUnit); }
Code corresponding to setting AudioSession
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error]; [[AVAudioSession sharedInstance] setActive:YES error:&error];
Code matching AudioUnit
// Describe audio component AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentSubType = kAudioUnitSubType_RemoteIO; desc.componentFlags = 0; desc.componentFlagsMask = 0; desc.componentManufacturer = kAudioUnitManufacturer_Apple; // Get component AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc); // Get audio units oserr = AudioComponentInstanceNew(inputComponent, &m_audioUnit); checkStatus(oserr); // Enable IO for recording UInt32 flag = 1; oserr = AudioUnitSetProperty(m_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &flag, sizeof(flag)); checkStatus(oserr); UInt32 enableOutput = 0; // to disable output AudioUnitElement outputBus = 0; // Disable output oserr = AudioUnitSetProperty ( m_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, outputBus, &enableOutput, sizeof (enableOutput) ); checkStatus(oserr); oserr = AudioUnitInitialize(m_audioUnit); oserr = AudioOutputUnitStart(m_audioUnit);
Most popular recording apps don't seem to support it, even iOS native βVoice Memoβ pauses after starting Siri.
These are the errors I get in EndInterruption: Error AUIOClient_StartIO (-12985) AURemoteIO :: ChangeHardwareFormats: error -10875
Has anyone been successful in restoring a microphone when the application is in the background?
source share