iPhone应用程序中不同声音之间的延迟
我正在尝试制作一个带有一些按钮来播放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于小声音(不到 30 秒),使用 AudioServices 确实要快得多。所需的代码也不是很长(但它需要一些好的旧 C 语言)。
您还可以使用以下终端命令优化声音(减小其大小):
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).
Also you can optimize your sounds (reduce their size) with the following terminal command:
通过使用系统声音,您可以大大简化事情。在文档中查找:
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.也许你可以看看 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.
从 Apple 文档中获取 BubbleLevel 项目中的 SoundEffect 类
SoundEffect 是音频服务的简单 Objective-C 包装器
允许加载和播放声音文件的函数。
Classes/SoundEffect.m
这将使播放文件变得很容易,因为
它还可以处理内存释放。 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
it will also handle memory deallocation. AudioServicesDisposeSystemSoundID(soundID);
一个简单的修复方法就是在您的 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.