【问题标题】:Play Short Audio Effect播放短音频效果
【发布时间】:2012-01-31 15:57:04
【问题描述】:

为了避免我可能有任何发泄,我很想知道这里的人们一直在使用什么(代码 sn-p 明智)在他们的应用程序中播放短小的音频效果?现在我正在处理一个严格的 iOS 5 ARC 项目,我以前的方法被禁止并引发错误。

那么,如何使用 iOS 5 和 Objective-C 在他们的应用中简单地播放一个 1 秒的小音频文件?

感谢任何积极贡献的人!

编辑:下面是使用 ARC 和 iOS 5 发送错误的代码

NSString *soundPath=[[NSBundle mainBundle] pathForResource:@"swoosh" ofType:@"caf"];
    SystemSoundID sound;
    AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)[NSURL fileURLWithPath:soundPath],&sound);
    AudioServicesPlaySystemSound(sound);

【问题讨论】:

  • 请为您之前的方法提供代码!
  • NSString *soundPath=[[NSBundle mainBundle] pathForResource:@"swoosh" ofType:@"caf"]; SystemSoundID 声音; AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)[NSURL fileURLWithPath:soundPath],&sound); AudioServicesPlaySystemSound(声音);

标签: iphone objective-c ios5 avfoundation


【解决方案1】:

尝试将__bridge_retained 更改为__bridge,ARC 消息应该会消失。

我相信 AVAudioPlayer 级别更高,内存占用更大,因此播放短声音时它可能会滞后,并且使用的内存超出必要。见Multimedia Programming Guide: using Audio

【讨论】:

    【解决方案2】:

    这是我正在使用的代码 sn-p:

    //alarm sound
    self.alarmSoundPlayer = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:[[NSBundle mainBundle] 
                                  URLForResource:@"alarm" 
                                   withExtension:@"caf"]
                        error:0];
    
    ...
    
    [alarmSoundPlayer play];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2012-06-21
      相关资源
      最近更新 更多