我试图在应用程序后台时获取麦克风。我正在使用音频单元技术并能够在后台录制音频。但是一旦我的AudioSession被中断,我就无法在应用程序在后台时重新启动AudioSession。 注意:如果应用程序在前台,我可以重新启动AudioSession。 以下是与中断对应的代码:
- (void) beginInterruption {
[[AVAudioSession sharedInstance] setActive:NO error:&error];
AudioOutputUnitStop(m_audioUnit);
}
- (void) endInterruptionWithFlags:(NSUInteger) flags{
[[AVAudioSession sharedInstance] setActive:YES error:&error];
AudioOutputUnitStart(m_audioUnit);
}
与音频会话设置相对应的代码
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
与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);
大多数流行的录音应用似乎不支持此功能,即使是iOS自带的“语音备忘录”在启动Siri时也会被中断。
以下是我在EndInterruption中遇到的错误: AUIOClient_StartIO失败(-12985) AURemoteIO :: ChangeHardwareFormats:错误-10875
有人成功从后台重新获取麦克风吗?