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?
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);
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.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.
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:
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.