NSSound needs a run loop cycle to repeat-play a
sound?



If I invoke NSSound to play the same sound more than once, without cycling through a run loop, it only plays the first time, and other times, fails "silently". [1]

If I cycle through a run loop, then it works fine. [2]

The documentation states that NSSounds play in a separate thread and I don't see any explanation for this in "Sound Programming Topics for Cocoa". Is this a bug or is it too late for me to be trying to read?

Thanks,

Jerry

[1] Code that Doesn't Work -- "Tink" only plays once

int main(int argc, const char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;

NSSound* tink = [NSSound soundNamed:@"Tink"] ;

NSInteger j = 0 ;
while (j < 3) {
NSLog(@"tink") ;
[tink stop] ; // This line has no effect
[tink play] ;

usleep(2000000) ; // sleep 2 seconds

// Creating a new sound doesn't help either
[[NSSound soundNamed:@"Tink"] play] ;

usleep(2000000) ; // sleep 2 seconds
j++ ;
}

return 0 ;
}


[2] Code that works -- "Tink" plays repeatedly

@interface Player : NSObject {}

@end

@implementation Player

+ (void)playit {
[[NSSound soundNamed:@"Tink"] play] ;
}

@end


int main(int argc, const char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;

[NSTimer scheduledTimerWithTimeInterval:2.0
target:[Player class]
selector:@selector(playit)
userInfo:nil
repeats:YES] ;
[[NSRunLoop currentRunLoop] run] ;


return 0 ;
}
_______________________________________________

...

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy