• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSSound needs a run loop cycle to repeat-play a sound?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: NSSound needs a run loop cycle to repeat-play a sound?
  • From: Jerry Krinock <email@hidden>
  • Date: Fri, 27 Mar 2009 23:11:57 -0700

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSSound needs a run loop cycle to repeat-play a sound?
      • From: Michael Ash <email@hidden>
  • Prev by Date: re: Processing data maintained with Core Data
  • Next by Date: re: Core Data: is fetching safe during validateForInsert:/validateForUpdate?
  • Previous by thread: Re: NSSlider with mouseUp
  • Next by thread: Re: NSSound needs a run loop cycle to repeat-play a sound?
  • Index(es):
    • Date
    • Thread