iPhone应用程序中不同声音之间的延迟

发布于 2024-11-11 07:05:21 字数 1347 浏览 0 评论 0原文

我正在尝试制作一个带有一些按钮来播放 WAV 声音的小型 iPhone 应用程序。 我的按钮可以工作,但延迟很小(~ 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

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

I’m trying to make a small iPhone application with some buttons to play WAV sounds.
My buttons works, but I have a small latency (~ 0,5 sec).

This is my .m file :

#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

How can I avoid this latency between playing different sounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守不住的情 2024-11-18 07:05:21

对于小声音(不到 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

Using AudioServices is indeed much quicker for small sounds (less than 30 seconds). The required code isn't very long either (but it requires some good old 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);

Also you can optimize your sounds (reduce their size) with the following terminal command:

afconvert mysound.caf mysoundcompressed.caf -d ima4 -f caff
苍景流年 2024-11-18 07:05:21

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

You could simplify things a lot by using system sounds. Look up: AudioServicesCreateSystemSoundID in the documentation. There is also a "System Sound Services Reference Document" that talks about that and other related functions. This is a simple and efficient way to play short sounds. Not sure if it will solve your latency issues but its a good start. You may also try using some different sound file types. Perhaps there is an issue with how it was or was not compressed.

来日方长 2024-11-18 07:05:21

也许你可以看看 AppSoundEngine。它解决了延迟问题并极大地简化了系统声音服务的使用,因为它是 SystemSoundID 和相关 C 函数的 Objective-C 包装器。

您无法从 AVAudioPlayer 获得可接受的延迟(<10 毫秒)。系统声音服务是最佳选择。

Maybe you could look at AppSoundEngine. It addresses latency and greatly simplifies using of System Sound Services, because it is objective-c wrapper for SystemSoundID and associated C functions.

You can not get acceptable latency (<10 ms) from AVAudioPlayer. System Sound Services is the way to go.

可爱咩 2024-11-18 07:05:21

从 Apple 文档中获取 BubbleLevel 项目中的 SoundEffect 类

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

Classes/SoundEffect.m

这将使播放文件变得很容易,因为

  SoundEffect *soundEffect = [SoundEffect soundEffectWithContentsOfFile:@""]
  [soundEffect play];

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

From the Apple Docs grab the SoundEffect class in project BubbleLevel

SoundEffect is a simple Objective-C wrapper around Audio Services
functions that allow the loading and playing of sound files.

Classes/SoundEffect.m

this will make playing files as easy as

  SoundEffect *soundEffect = [SoundEffect soundEffectWithContentsOfFile:@""]
  [soundEffect play];

it will also handle memory deallocation. AudioServicesDisposeSystemSoundID(soundID);

灼痛 2024-11-18 07:05:21

一个简单的修复方法就是在您的 ButtonSoundViewController 的 init 方法中为 2 个声音执行 AVAudioPlayer alloc init 。然后这 2 个音频播放器就已经准备好在您的按钮代表中播放了。

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

One simple fix would just be to do the AVAudioPlayer alloc init for the 2 sounds in your buttonSoundViewController's init method. Then those 2 audio players will already be ready to play in your button delegates.

The fastest way to play sounds is to use the RemoteIO Audio Unit, but that's a far more advanced and complicated looking API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文