简体   繁体   English

在iOS中重新获取背景中的麦克风

[英]reacquisition of microphone in background in iOS

I am trying to acquire microphone while the app is in the background. 我正在尝试在应用程序处于后台时获取麦克风。 I am using audio unit technology and able to record audio while in background. 我正在使用音频单元技术,能够在后台录制音频。 But once my AudioSession gets interrupted, I am unable to restart the AudioSession with app in the background. 但是一旦我的AudioSession被中断,我就无法在后台使用app重启AudioSession。 Note: I am able to restart the AudioSession if the app is in foreground. 注意:如果应用程序位于前台,我可以重新启动AudioSession。 Here is the code corresponding to interruption: 这是与中断相对应的代码:

- (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 AudioSession setup 与AudioSession设置对应的代码

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

Code corresponding to AudioUnit 与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 of the popular recording apps does not seem to support it, even iOS native "Voice Memo" gets suspended upon starting the Siri. 大多数流行的录制应用程序似乎都不支持它,甚至iOS本地“语音备忘录”在启动Siri时被暂停。

These are the errors I get in EndInterruption: AUIOClient_StartIO failed (-12985) AURemoteIO::ChangeHardwareFormats: error -10875 这些是我在EndInterruption中遇到的错误:AUIOClient_StartIO失败(-12985)AURemoteIO :: ChangeHardwareFormats:错误-10875

Has anyone been successful in reacquisition of microphone while the app is in background? 当应用程序处于后台时,有没有人成功重新获取麦克风?

I got this working after adding 添加后我就开始工作了

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

in viewDidAppear . viewDidAppear Thanks to the post MPMoviePlayerController / AVAudioSession in background doesn't restart play after incoming call 感谢后台MPMoviePlayerController / AVAudioSession在后台调用后没有重启播放

It is unclear to me how beginReceivingRemoteControlEvents is impacting the AudioSession here. 我不清楚beginReceivingRemoteControlEvents如何影响AudioSession。

Another important thing I observed is independent of answering/declining phone call you do receive an endInterruption event. 我观察到的另一个重要事项是独立于接听/拒绝电话,你会收到endInterruption事件。 Apple documentation makes you think that you may not be getting endInterruption in case of answering a phone call Apple文档会让您认为在接听电话时可能无法获得endInterruption

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM