我正在尝试在后台录制。当我使用 audiounit 技术时,我在背景中看到一个红色条。但是,当我使用 AV Foundation 框架 (AVAudioRecorder) 时,我看不到红条。我在所需的背景模式下有“应用程序播放音频”。这是 AudioSession 的代码片段
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
AVAudioRecorder对应的代码
// ... url, recordSettings initialized properly
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if([recorder record] == YES)
NSLog(@"Started recording");
Audio Unit对应的代码
// 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 6、iPhone 5 上运行它。我不清楚为什么使用 AVAudioRecorder 时看不到录制状态栏。关于如何获得它的任何建议?