I am working on a simple game, and after each touch of the screen the animation happens on a small UIImageView, and this happens very well, and the application works smoothly with CADisplayLink as a timer.
I added an mp3 file to play after every touch with 1 second, as AVAudioPlayer will imagine such a sound as: Bip
So, the first time I touch the screen, the first hiccup occurs when the application freezes for less than a second, and I can say that this is normal, because the first time the sound allocates memory.
The problem arises later, when you touch the screen again, if I touch it earlier than 3 seconds, the application does not hiccup, but if I wait 4 seconds or more, the application will start hiccuping after each touch.
Every time I touch again and again earlier than 3 seconds between touches, the application does not hiccup, but after 4 seconds between touches the application hiccups.
Any idea to solve hiccups?
This is the code if necessary.
@property (nonatomic, strong) AVAudioPlayer *mySound; - (void)viewDidLoad { NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"bip" ofType:@"mp3"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath]; AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; self.mySound = newPlayer; [mySound prepareToPlay]; [mySound setDelegate:self]; }
and after clicking will happen
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; if (location.x < viewWidth/2) { [mySound play]; } else { [mySound play]; } }