NSSound repeating in a loop?

Discussion in 'Mac Programming' started by Amigalander, Jan 30, 2009.

  1. macrumors regular

    Joined:
    Jan 13, 2008
    #1
    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

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

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Use [tinker setCurrentTime:0.0]; right before you call play.
     
  3. thread starter macrumors regular

    Joined:
    Jan 13, 2008
    #3
    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?
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. thread starter macrumors regular

    Joined:
    Jan 13, 2008
    #5
    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?
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    I think what you're doing is fine, but it'd be better to use [NSSound soundNamed:mad:"Tink"] instead of using the file path.
     
  7. est
    macrumors newbie

    Joined:
    May 15, 2010
    #7
    kain,
    Thank you for that, solved my problem too. I should probably look into QTMovie as well.
     

Share This Page