PDA

View Full Version : NSSound repeating in a loop?




Amigalander
Jan 31, 2009, 01:40 AM
I've just begun learning about Sound, and am having a little trouble in loops.
I'd like to play a system sound every time I go thru a loop. However, the code below plays only once and won't repeat in the loop. What have I done wrong?

Thank you


tinker = [[NSSound alloc] initWithContentsOfFile: @"/System/Library/Sounds/Tink.aiff" byReference:YES];
for(i = 0; i < 20; i++)
{
[tinker play];
usleep(90000);
}



kainjow
Jan 31, 2009, 11:33 AM
Use [tinker setCurrentTime:0.0]; right before you call play.

Amigalander
Jan 31, 2009, 05:20 PM
Thank you.

So this means an NSSound doesn't "rewind" after playing.

May I ask how you knew this? I mean, is there some part of the Apple docs I am not seeing?

kainjow
Feb 1, 2009, 12:34 PM
I looked at the docs, made a guess and tested it out :)

There's probably another way to do it though, since setCurrentTime: is a 10.5 method. I rarely use NSSound, only for playing simple sounds. QTMovie provides much more control.

Amigalander
Feb 1, 2009, 01:10 PM
Bah! A guess? Hehe, thanks :)

This gets me wondering, am I approaching this totally wrong?

What's the standard way to just play system sounds which are in /System/Library/Sounds? Or is there no "standard" way?

kainjow
Feb 1, 2009, 01:30 PM
I think what you're doing is fine, but it'd be better to use [NSSound soundNamed:@"Tink"] instead of using the file path.

est
May 15, 2010, 10:49 PM
Use [tinker setCurrentTime:0.0]; right before you call play.

kain,
Thank you for that, solved my problem too. I should probably look into QTMovie as well.