Sound overlapping with multiple button presses

前端 未结 6 1451
野的像风
野的像风 2020-11-29 13:56

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?

 - (void)playOnce         


        
6条回答
  •  有刺的猬
    2020-11-29 14:42

    In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.

     - (void)playOnce:(NSString *)aSound {
    
      NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
      if (theAudio && [theAudio isPlaying]) {
        [theAudio stop]
      } else {
             theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
             [theAudio prepareToPlay];
    
             [theAudio setDelegate: self];
        [theAudio setNumberOfLoops: 1];
        [theAudio setVolume: 1.0];
        [theAudio play];
      }
    
    }
    

提交回复
热议问题