【问题标题】:Sound overlapping with multiple button presses多次按下按钮时声音重叠
【发布时间】:2024-01-16 12:38:01
【问题描述】:

当我按下一个按钮,然后再按下另一个按钮时,声音会重叠。我该如何解决这个问题,以便在按下另一个声音时停止第一个声音?

 - (void)playOnce:(NSString *)aSound {

NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];    
 }

- (void)playLooped:(NSString *)aSound {

NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];


    }

【问题讨论】:

  • 你需要真正提出一个问题。
  • 我的问题是,当我点击一个按钮时,它会播放声音,然后我点击另一个按钮,它们会重叠
  • 您可以执行以下任一操作: 1. 当按钮单击时,检查 AVaudioplayer 是否仍在播放。如果是,你可以忽略它而不做任何事情,或者 2 你可以停止它然后播放新的声音。
  • - (void)playOnce:(NSString *)aSound;
  • - (IBAction) beatButton50 { [self playOnce:@"racecars"]; }

标签: iphone ios4 memory audio


【解决方案1】:

在 viewController 的标题中声明您的 AVAudioPlayer(不要在每次播放声音时分配一个新的)。这样,您将拥有一个可以在 StopAudio 方法中使用的指针。

@interface myViewController : UIViewController <AVAudioPlayerDelegate> { 
    AVAudioPlayer *theAudio;
}
@property (nonatomic, retain) AVAudioPlayer *theAudio;
@end


@implementation myViewController
@synthesize theAudio;
- (void)dealloc {
    [theAudio release];
}
@end



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

- (void)playLooped:(NSString *)aSound {
    NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
    if(!theAudio){
        theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
    }
    [theAudio setDelegate: self];
    // loop indefinitely
    [theAudio setNumberOfLoops:-1];
    [theAudio setVolume:1.0];
    [theAudio play];
}

- (void)stopAudio {
    [theAudio stop];
    [theAudio setCurrentTime:0];
}

也请务必阅读Apple Docs

【讨论】:

  • 我将如何在头文件中创建 AVAudioPlayer 我是新手,不太了解
  • 我添加了那行代码,声音仍然重叠
  • 刚刚编辑了上面的代码。你能更详细地描述你想要达到的目标吗?
  • 我正在制作一个有 20 个按钮的应用程序,每个按钮播放不同的声音,但是当我点击其他按钮时声音会重叠
  • 然后在播放下一个声音之前停止 AVAudioPlayer。
【解决方案2】:

您发布的代码只会播放一个声音,即发送到方法的第一个声音。

如果您只想播放一种可变声音,请在停止播放器后,释放音频,然后设置新声音。

【讨论】:

  • 我如何释放音频并设置新的
【解决方案3】:

在每个方法的开头添加一个检查以查看播放器是否已经在播放:

if (theAudio.playing == YES) {
    [theAudio stop];
}

AVAudioPlayer Class Reference

【讨论】:

    【解决方案4】:

    在您的 playOnce 方法中,“路径”变量未使用 - 删除它以消除您的警告消息。你的 playOnce 没有设置任何要播放的东西,所以我不确定它应该如何工作——除非你先调用 playLooped?您还需要在 initWithContentsOfUrl 之后调用 prepareToPlay。

     - (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];
      }
    
    }
    

    【讨论】:

    • 我删除了 playLoop 行,因为我真的不需要它,我删除了路径行但仍然没有声音播放
    • 添加prepareToPlay,你试过了吗?您确实需要调用 playLoop 中的代码来设置 AVAudioPlayer。
    • 我做到了,现在声音停止重叠,当我尝试按下另一个声音不同的按钮时,它只会播放相同的声音
    • 啊,对,我明白你在说什么。我想你必须检查一下你是否有不同的声音并在这种情况下重新加载播放器。
    • 我有所有按钮的代码,但是我如何查看我是否有不同的声音播放并重新加载播放器我是编程新手,所以我不太了解
    【解决方案5】:

    您需要使用 BOOL 值才能使其正常工作。

    在 @implementation 之前的 .m 文件中输入:

    static BOOL soundIsPlaying = NO;
    

    那么您的 IBAction 需要看起来像这样:

    - (IBAction)play {
        if (soundIsPlaying == YES) {
            [theAudio release];
            soundIsPlaying = NO;
    
        }
    
        else if (soundIsPlaying == NO) {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"SOUNDFILENAME" ofType:@"wav"];
            theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            theAudio.delegate = self;
            theAudio.volume = 1.0;
            theAudio.numberOfLoops = 0;
            [theAudio play];
            soundIsPlaying = YES;
    
        }   
    }
    

    说实话。当按下另一个按钮时它会停止声音。

    【讨论】:

      【解决方案6】:

      您可能可以执行以下操作:

          (void) playLooped: (NSString * ) aSound {
              NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
      
          //stop audio player from playing so the sound don't overlap
          if([theAudio isPlaying])
          {
              [theAudio stop]; //try this instead of stopAudio
      
          }
      
          if (!theAudio) {
              theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
          } 
          [theAudio setDelegate: self];
          // loop indefinitely
          [theAudio setNumberOfLoops: -1];
          [theAudio setVolume: 1.0];
          [theAudio play];
      }
      

      【讨论】:

      • 它告诉我找不到 stopAudio
      • 奇怪的是当我按下多个按钮时音频仍然重叠
      • 您是否交易了代码并确保调用了 [theAudio stop]?
      • 我如何交易代码我必须发布它是新的编程我还在努力学习
      最近更新 更多