I am having a problem using AVAudioPlayer. There is a major delay (3-4 seconds) the first time the play method is called. I got around this by calling the play immediately at game load time, while the splash screen displays. A kludge but, at least I can live with that piece.
The problem I continue to have, however, is each and every time I call [audioPlayer play], there is a sub-second delay. I have created a Pong game and am calling one of two sounds each time the ball hits either the sides of the court or the ball hits the face of the paddles. Each time the ball hits one of those there is that very small delay but is noticeable enough to be quite annoying. I only notice it when played on the device. In the Simulator it works like a champ. The audio player is supposed to run asynchronously so I’m not sure why the delay.
Any ideas on how to solve the problem? Code below.
Code:
.h file
#import <AVFoundation/AVFoundation.h>
@interface PongViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
}
.m file
- (void)viewDidLoad
{
NSURL *url = [NSURL fileURLWithPath:[NSString
stringWithFormat:@"%@/ponblipg-5.wav", [[NSBundle mainBundle]
resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play]; // Delay here and within the game loop.
}
- (void)dealloc
{
[audioPlayer release];
[super dealloc];
}