我正在开发应用程序,我在其中引用来自苹果的 aurioTouch 应用程序来显示波形。我必须在每个视图控制器的顶部显示波形。所以,我在 firstViewController 的 viewDidLoad 方法中设置了 AudioUnit。
XThrowIfError(AudioSessionInitialize(NULL, NULL, rioInterruptionListener, self), "couldn't initialize audio session");
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
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(AudioSessionSetActive(true), "couldn't set audio session active\n");
XThrowIfError(SetupRemoteIO(rioUnit, inputProc, thruFormat), "couldn't setup remote i/o unit");
在这里我可以显示输入声音的波形,现在当我点击 secondTab 时,在 FirstviewController 的 viewWillDisappear 方法中我正在停止 AudioUnit。
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
AudioOutputUnitStop(rioUnit);
AudioUnitUninitialize(rioUnit);
AudioSessionSetActive(false);
//[self performSelectorOnMainThread:@selector(disposeCoreAudio) withObject:nil waitUntilDone:NO];
//[eaglView stopAnimation];
}
在 secondViewController 我再次尝试初始化 AudioUnit
XThrowIfError(AudioSessionInitialize(NULL, NULL, rioInterruptionListener, self), "couldn't initialize audio session");
但它显示异常: 即将抛出'init':无法初始化音频会话错误:无法初始化音频会话('init')
在第一个 viewController 中,我能够停止 AudioUnit 的实例,但无法在 secondViewController 中再次对其进行初始化。谁能帮我摆脱这种情况?