我有一个 iOS 应用程序,它记录指定时间段的音频片段,对该音频执行分析,返回结果,然后基本上处于非活动状态,直到用户想要再次录制。
该应用程序的音频配置类似于aurioTouch2,并利用 RemoteI/O 音频单元和音频会话服务。该应用程序当前AudioSessionSetActive(true)
在初始启动期间设置,然后启动远程 I/O。音频会话和音频单元在应用程序的整个生命周期内保持运行。
所以这是我的问题 - 由于该应用程序实际上只在相对较短(15-60 秒)的时间内使用麦克风,我是否应该AudioSessionSetActive(false)
在未主动使用音频会话时停用它?
我试过只在录制过程中同时运行音频单元和音频会话——在录制生物时开始,在录制完成后停止——虽然它在模拟器上按预期工作,但在物理上运行它时会崩溃设备。这是错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
*** First throw call stack:
(0x2e4baf4b 0x3884a6af 0x2e4bae8d 0x2e4054b3 0xb2e67 0xb420d 0x2ddd5ee5 0x2ddd5b1f 0x2ddd6283 0x2ddd6163 0x2ddd6045 0x2ddd5fc7 0x2ddd57a5 0x2e4859df 0x2e48597b 0x2e48414f 0x2e3eec27 0x2e3eea0b 0x330cd283 0x30c92049 0xa1d6f 0x38d52ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
以下是我开始/停止录制的方式:
- (void)startRecording
{
CheckError(AudioSessionSetActive(true), "Couldn't set audio session active\n");
if (![self unitIsRunning]) {
CheckError(AudioOutputUnitStart([self audioUnit]), "Couldn't start unit");
[self setUnitIsRunning:YES];
}
}
- (void)stopRecording
{
if ([self unitIsRunning]) {
CheckError(AudioOutputUnitStop([self audioUnit]), "Couldn't stop unit");
[self setUnitIsRunning:NO];
}
// if I comment out the following line, and keep the audio session running
// throughout the lifetime of the app, everything works fine
CheckError(AudioSessionSetActive(false), "Couldn't set audio session inactive\n");
}
显然,为了简洁起见,我省略了在单元运行时发生的音频渲染回调以及在初始启动时完成的音频会话/音频单元的配置,但同样,这非常接近于 aurioTouch2 中所做的。该错误似乎是由于将音频会话设置为非活动状态,但我似乎无法弄清楚原因。
我很难确定 Apple 是否建议运行相同的音频会话,即使应用程序实际上并没有做任何事情。会话应该在应用程序的生命周期内保留还是在需要使用的基础上保留?有什么想法吗?