I have a need to play a brief sound 3 seconds or so (like a count down beep) before I perform some other action in an iOS application.
The use case is as follows:
User clicks a button… the beeps play (simple beeps using AudioServicesPlaySystemSound… then the rest of the method is run.
I cannot seem to find out a way to block my method while the tones are playing.
I’ve tried the following:
[self performSelector:@selector(playConfirmationBeep) onThread:[NSThread currentThread] withObject:nil waitUntilDone:YES];
But the tones play synchronously while the rest of the method is performed.
What am I missing with the above call?
AudioServicesPlaySystemSoundis asynchronous so you can’t block on it. What you want to do is get the audio services to notify you when playback is finished. You can do that viaAudioServicesAddSystemSoundCompletion.It’s a C-level API so things are a bit ugly, but you probably want something like:
If you actually want to block the UI while the sound is playing, and assuming your class is a view controller, you should probably just disable
self.view.userInteractionDisablefor the relevant period. What you definitely don’t want to do is block the main run loop; that’ll stop important system events like low memory warnings getting through and hence potentially cause your app to be force quit. You also probably still want to obey things like device rotations.