AudioServicesPlaySystemSound
是异步的,所以你不能阻止它。您想要做的是让音频服务在播放完成时通知您。你可以通过AudioServicesAddSystemSoundCompletion
来做到这一点。
这是一个C级API这样的事情是有点难看,但你可能想要的东西,如:
// somewhere, a C function like...
void audioServicesSystemSoundCompleted(SystemSoundID ssID, void *clientData)
{
[(MyClass *)clientData systemSoundCompleted:ssID];
}
// meanwhile, in your class' init, probably...
AudioServicesAddSystemSoundCompletion(
soundIDAsYoullPassToAudioServicesPlaySystemSound,
NULL, // i.e. [NSRunloop mainRunLoop]
NULL, // i.e. NSDefaultRunLoopMode
audioServicesSystemSoundCompleted,
self);
// in your dealloc, to avoid a dangling pointer:
AudioServicesRemoveSystemSoundCompletion(
soundIDAsYoullPassToAudioServicesPlaySystemSound);
// somewhere in your class:
- (void)systemSoundCompleted:(SystemSoundID)sound
{
if(sound == soundIDAsYoullPassToAudioServicesPlaySystemSound)
{
NSLog(@"time to do the next thing!");
}
}
如果你真的想阻止时播放声音时,并假设UI类是一个视图控制器,你应该在相应的时间段内关闭self.view.userInteractionDisable
。你绝对不想做的是阻止主运行循环;这将阻止重要的系统事件,如低内存警告通过,因此可能导致您的应用程序被强制退出。你也可能还想服从像旋转设备这样的东西。