title: NSSound not working properly
body: <p>I am using Cocoa on Mac 10.6.  I am trying to have a metronome click out the time but when I run the code only the first two sounds are played then it is quiet till the end when a sound plays again.  If I replace the [player play] statements with NSBeep() it works fine.  Any suggestions?</p>
  
  <pre><code>#import &lt;Cocoa/Cocoa.h&gt;
  
  
  @interface Metronome : NSObject {
  
  NSInteger timeSig;
  NSInteger noteValue;
  NSInteger bpm;
  NSUInteger beatNumber;
  CGFloat duration;
  NSSound *tickPlayer;
  NSSound *tockPlayer;
  
  }
  @property(readwrite) NSInteger timeSig;
  @property(readwrite) NSInteger noteValue;
  @property(readwrite) NSInteger bpm;
  @property(readwrite) float duration;
  @property(readwrite) NSUInteger beatNumber;
  
  -(id)init;
  -(id)initWithTimeSig:(NSInteger)ts  andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp;
  -(void)BeginMetronome;
   -(void)PlaySound;
  
  @end
  
  #import "Metronome.h"
  
   #define defaultBpm 80
   #define defaultTimeSig 4
   #define defaultNoteValue 4
  
   @implementation Metronome
  
   @synthesize timeSig, noteValue, duration, bpm, beatNumber;
   -(id)init{
  return [self initWithTimeSig:defaultTimeSig andNoteValue:defaultNoteValue       andBpm:defaultBpm];
   }
   -(id)initWithTimeSig:(NSInteger)ts andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp {
  [super init];
  if(self){
      self.timeSig = ts;
      self.noteValue = nv;
      self.bpm = bp;
      self.duration = 60.0/bpm;
      tickPlayer = [NSSound soundNamed:@"Hat1"];
      tockPlayer = [NSSound soundNamed:@"Hat2"];
      self.beatNumber = 1;
  
  
  }
  return self;
   }
   -(void)BeginMetronome{
  BOOL continuePlaying = YES;
  NSInteger iter=0;
  
  while (continuePlaying){
      if(iter &gt; 12){
          continuePlaying = FALSE;
      }
      iter++;
      [self PlaySound];
      NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self.duration];
      NSDate *currentTime = [[NSDate alloc] init];
  
      while (continuePlaying &amp;&amp; ([currentTime compare:curtainTime] != NSOrderedDescending)) { 
          // if ([soundPlayerThread isCancelled] == YES) {
          //   continuePlaying = NO;
          //}
          //[NSThread sleepForTimeInterval:0.01];
          sleep(.01);
          //[tickPlayer stop];
          //[tockPlayer stop];
          [currentTime release];
          currentTime = [[NSDate alloc] init];
      }
      [curtainTime release];      
      [currentTime release];      
  
  }
  }
    - (void)PlaySound {          //***********Problem in this loop
  if ([tickPlayer isPlaying])
      [tickPlayer stop];
  if ([tockPlayer isPlaying])
      [tockPlayer stop];
  
  if (beatNumber == 1) {
      [tockPlayer play];
  }
  else if (beatNumber == 2){
      [tickPlayer play];
  }
  else if (beatNumber == self.timeSig) {
      beatNumber = 0;
      [tickPlayer play];
  }
  else{
      //[tickPlayer stop];
      [tickPlayer play];
      NSBeep();
  }
  
  
  beatNumber++;
  //NSLog(@" %d ", beatNumber);
   // NSBeep();
   }
  
  
  
   @end
  </code></pre>