【问题标题】:Reliable Way to Add Sound to UIButton向 UIButton 添加声音的可靠方法
【发布时间】:2012-12-19 10:44:39
【问题描述】:

我正在尝试向 UIButton 添加点击声音以在按下时播放。到目前为止,我已经尝试了两种方法,但都没有达到我希望的效果。

  1. 我尝试过使用简单的 SystemSounds,但它们是一个丑陋的解决方案,因为用户无法(或者说非常有限)控制音量。

  2. 我还尝试了以下形式的 AVAudioPlayer(在 ViewController 的 init 方法中调用):

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient 
                                           error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[AVAudioSession sharedInstance] setDelegate:self];
    
    NSString *filePath = [[NSBundle mainBundle] 
                         pathForResource: @"button_pushed"
                                  ofType: @"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filePath];
    self.buttonPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                               error: nil];
    [buttonPlayer prepareToPlay];
    [buttonPlayer setDelegate: self];
    

第二种方法似乎比第一种方法好很多;特别是当我用以下语句调用声音时:

    if (self.clickPlayer.playing) {
        [self.clickPlayer stop];
        self.clickPlayer.currentTime = 0;
        [self.clickPlayer play];
    }
    else {
        [self.clickPlayer play];
    }

以上确保我在反复按下按钮时得到非常快速的响应。顺便说一下,快速响应时间在我的应用中至关重要,不幸的是,这就是第二种方法失败的地方。

如果我让应用程序空闲大约 10 秒,加载第一个声音会出现明显的延迟(然后,它会再次变得快速响应)。不仅如此,它还会延迟按钮被按下。虽然这个延迟非常小(可能只有几分之一秒),但不幸的是它确实影响了我的应用程序的可用性。就好像声音正在自动释放,每次我有一段时间不使用它时都需要初始化或放入内存。

是否有解决方法来始终保持声音可用并避免延迟或其他可能简单的方法来实现按钮声音?

更多信息: 1.我用ARC 2.我不使用IB 3. 目前没有实现音频委托方法 4. 在后台播放来自其他应用程序的音频的能力很重要 5. 延迟发生在 iPhone 5 上;所以,我只能想象在功能较弱的设备上需要多长时间......

【问题讨论】:

    标签: iphone ios audio avaudioplayer system-sounds


    【解决方案1】:

    不完全确定,但如果你将路径的 NSData 更改为 [[AVAudioPlayer alloc] initWithContentsOfURL 并更改为 [[AVAudioPlayer alloc] initWithData,它会不会更快?

    【讨论】:

    • 很遗憾,这没有帮助。
    【解决方案2】:

    我设法通过创建一个“空白”播放器来解决这个问题,该播放器会不断地发出一个没有声音的微小 .wav 文件,从而使 AVPlayer 保持活动状态。这可能不是最优雅的解决方案,但它确实有效,而且似乎不会影响程序的性能。以下是您需要做的:

    1. 创建一个 0.1 秒长的无声 .wav 文件(越小越好)。

    2. 为其初始化一个播放器:

      NSString *filePath = [[NSBundle mainBundle] pathForResource: @"silence"
                                                           ofType: @"wav"];
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filePath];
      silencePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                             error: nil];
      [silencePlayer prepareToPlay];
      [silencePlayer setDelegate: self];
      
    3. 然后让它每隔一秒左右触发一次:

      timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                               target:self
                                             selector:@selector(repeatSilence)
                                             userInfo:nil
                                              repeats:YES];
      
    4. 最后,创建被调用的方法(- (void) repeatSilence { } )来播放声音:

      if (self.silencePlayer.playing) {
          [self.silencePlayer stop];
          self.silencePlayer.currentTime = 0;
          [self.silencePlayer play];
      }
      else {
          [self.silencePlayer play];
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      相关资源
      最近更新 更多