How to wait for NSSound to Finish playing...Any tips?
So I have developed a counting app that basically updates a int instance variable adding or subtracting to it. I add several other features, one of which is when the count equals a multiple the user sets a chosen sound plays. This is where my problem lies. I would like that when you hold down the button for adding or subtracting the sound plays for each count if you have it set that way and the app pauses for the sound to finish before it adds another. Maybe my code and app files would help more than me trying to explain it
.
Heres the setCount Code:
- (void)setCount:(int)newCount{
if(count == newCount || newCount < 0 || ([style isEqualToString:@"Down From:"] && newCount > countDown)){
}else{
if(remainder(count, beepOn) == 0 && count != 0 && noBeep != YES){
[beepSound play];
while ([beepSound isPlaying]) { //i used this to sort of pause the app but it didnt work too well, you can see if you check out the app i upploaded
}
}else if(countDownSelected && count == 0 && noBeep != YES)
{
[finishedSound play];
}else
{
noBeep = NO;
}
count = newCount;
}
}
Thanks for any help
Attachment: Counter.m (3.0KB)
Attachment: Counter.h (0.0KB)
Attachment: MainMenu.xib (108.0KB)
Attachment: CounterController.m (4.0KB)
Attachment: CounterController.h (0.0KB)
Looping in UI code to wait for data to arrive usually doesn't work. You have to re-enter the event loop to enable the work to progress. So, it's more common to use a delegate. By attaching a delegate to beep sound, you can avoid the while and the sound will tell you when he has completed.
There's an example in Aaron Hillegass' wonderful book pp86-93 where he does exactly this on the NSSpeechSynthesizer call to "speak" the words from a dialog box. He doesn't show progress, however he does disable/enable parts of the UI while the speech is in progress. He also enables the speech to be stopped in mid-sentence. The code's available from http://www.bignerdranch.com/products.shtml (click 'Get the solutions') and it's Solutions/05_HelperObjects/SpeakLine_D
On the other hand, perhaps I've misunderstood the issue. If I've misunderstood, please explain a little more and I'll try again. It's probably a good idea to clean your project and zip the whole project so that I can build and run it.