【问题标题】:Latency between different sounds in iPhone applicationiPhone应用程序中不同声音之间的延迟
【发布时间】:2011-06-01 02:23:01
【问题描述】:

我正在尝试制作一个带有一些按钮的小型 iPhone 应用程序来播放 WAV 声音。 我的按钮可以工作,但延迟时间很短(~ 0.5 秒)。

这是我的 .m 文件:

#import "buttonSoundViewController.h"

@implementation buttonSoundViewController
//@synthesize player;

-(IBAction) playSoundA:(id)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

-(IBAction) playSoundB:(id)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"b" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
}

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
}

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [audioPlayer release];
    [super dealloc];
}   

@end

如何避免播放不同声音之间的延迟?

【问题讨论】:

  • WAV 文件大吗?尝试使用较小的文件类型,看看它是否会缩短加载时间
  • @Mark No 我的 WAV 文件很轻(1 秒,~ 500 Kb)。

标签: iphone objective-c cocoa-touch audio latency


【解决方案1】:

对于小声音(少于 30 秒),使用 AudioServices 确实要快得多。所需的代码也不是很长(但它需要一些好的旧 C)。

#import <AudioToolbox/AudioServices.h>

SystemSoundID soundID = 0;
NSString* str =  [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
CFURLRef soundFileURL = (CFURLRef)[NSURL URLWithString:str ];
OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);
if (errorCode != 0) {
    // Handle failure here
}
else
    AudioServicesPlaySystemSound(soundID);

您还可以使用以下终端命令优化您的声音(减小它们的大小):

afconvert mysound.caf mysoundcompressed.caf -d ima4 -f caff

【讨论】:

  • 您好,感谢您的回复。我对 Ob-C 的理解不够好,无法理解如何实现音频服务……我是个菜鸟。你认为 Hotpaw2 的把戏对我有用吗?我不知道如何申请。
  • 您只需将 AudioServices.framework 添加到您的 XCode 项目中(在 XCode 4 的项目设置中)。那么上面的代码就可以正常工作了。
  • 谢谢,但我还没有找到 AudioServices.Framework。如果它在 AudioToolbox 中,我添加了这个。或者也许我很愚蠢......我什至不知道如何处理上面的代码......放在哪里,如何链接我的 WAV 文件等。Objective-C 是一场噩梦。我经常使用 .NET 和 Delphi 进行开发,它很直观。不是Objective-C。我浪费了难以置信的时间(整整两天)来寻找基本的东西......我筋疲力尽。我更愿意花钱请人让我成为一个现成的 XCode 项目,通过它我可以理解它是如何安静地工作的。如果有兴趣...
  • 糟糕。抱歉,音频服务的框架名称是 audiotoolbox。上面的代码不适合你吗?
  • 这对我不起作用,直到我将其改为 [NSURL fileURLWithPath:]
【解决方案2】:

使用系统声音可以大大简化事情。在文档中查找:AudioServicesCreateSystemSoundID。还有一个“系统声音服务参考文档”讨论了该功能和其他相关功能。这是播放短声音的一种简单而有效的方法。不确定它是否会解决您的延迟问题,但它是一个好的开始。您也可以尝试使用一些不同的声音文件类型。可能存在压缩或未压缩的问题。

【讨论】:

    【解决方案3】:

    也许你可以看看AppSoundEngine。它解决了延迟问题并大大简化了 System Sound Services 的使用,因为它是 SystemSoundID 和相关 C 函数的 Objective-C 包装器。

    您无法从 AVAudioPlayer 获得可接受的延迟(

    【讨论】:

    • 感谢您的想法。我会看的:)
    【解决方案4】:

    从 Apple Docs 中获取项目 BubbleLevel 中的 SoundEffect 类

    SoundEffect 是一个简单的 Objective-C 音频服务包装器 允许加载和播放声音文件的函数。

    类/SoundEffect.m

    这将使播放文件变得像

    一样简单
      SoundEffect *soundEffect = [SoundEffect soundEffectWithContentsOfFile:@""]
      [soundEffect play];
    

    它还将处理内存释放。 AudioServicesDisposeSystemSoundID(soundID);

    【讨论】:

    • 谢谢约翰。我试试看;)
    【解决方案5】:

    一个简单的解决方法就是对 buttonSoundViewController 的 init 方法中的 2 种声音执行 AVAudioPlayer alloc init。那么这 2 个音频播放器就已经准备好在您的按钮代理中播放了。

    播放声音最快的方法是使用 RemoteIO 音频单元,但这是一个更高级和复杂的 API。

    【讨论】:

    • 谢谢大家,我会看看所有这些回复!我会与这个帖子保持联系。
    • 嗨 Hotpaw2,我找不到放置 AVAudioPlayer alloc init 的位置和方法...我有点迷路了:s
    • @Beny:您是否在视图控制器中创建了实例变量以保存您的 2 个 av 播放器以供以后使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多