我已经基于苹果的aurio Touch 示例应用程序编写了呼吸检测器。但我不知道如何设置音频单元或音频会话不播放来自音频输入的声音。现在,当对着麦克风吹气时,我可以听到来自 iPhone 扬声器的呼吸声。如何防止这种情况?
这是苹果的音频会话初始化代码:
XThrowIfError(AudioSessionInitialize(NULL, NULL, rioInterruptionListener, self), "couldn't initialize audio session");
XThrowIfError(AudioSessionSetActive(true), "couldn't set audio session active\n");
UInt32 audioCategory = kAudioSessionCategory_RecordAudio;
XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "couldn't set audio category");
XThrowIfError(AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self), "couldn't set property listener");
Float32 preferredBufferSize = .005;
XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize), "couldn't set i/o buffer duration");
UInt32 size = sizeof(hwSampleRate);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &size, &hwSampleRate), "couldn't get hw sample rate");
XThrowIfError(SetupRemoteIO(rioUnit, inputProc, thruFormat), "couldn't setup remote i/o unit");
dcFilter = new DCRejectionFilter[thruFormat.NumberChannels()];
UInt32 maxFPS;
size = sizeof(maxFPS);
XThrowIfError(AudioUnitGetProperty(rioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFPS, &size), "couldn't get the remote I/O unit's max frames per slice");
fftBufferManager = new FFTBufferManager(maxFPS);
l_fftData = new int32_t[maxFPS/2];
XThrowIfError(AudioOutputUnitStart(rioUnit), "couldn't start remote i/o unit");
size = sizeof(thruFormat);
XThrowIfError(AudioUnitGetProperty(rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &thruFormat, &size), "couldn't get the remote I/O unit's output client format");