2012-08-15 35 views
2

在iOS应用程序中执行其他操作之前,我需要播放3秒左右的短暂声音(如倒计数嘟嘟声)。iOS在执行其他动作之前播放声音

使用情况如下:

用户点击一个按钮...哔声播放(使用AudioServicesPlaySystemSound简单的哔哔声......那么该方法的其余部分运行

我似乎无法。找到一个方法来阻止我的方法,而音播放

我已尝试以下步骤:

[self performSelector:@selector(playConfirmationBeep) onThread:[NSThread currentThread] withObject:nil waitUntilDone:YES]; 

在音色播放同步WH执行其余的方法。

上述呼叫我错过了什么?

回答

2

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。你绝对不想做的是阻止主运行循环;这将阻止重要的系统事件,如低内存警告通过,因此可能导致您的应用程序被强制退出。你也可能还想服从像旋转设备这样的东西。