AVAudioPlayer sometimes calls the application icon

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]; } } 
0
source share
1 answer

Make a very short, quiet sound. Use your timer to play this (with a different sound player) every second or so. -You understand that the idea is here; we try to keep the media server alive, to be ready to go when we want our sound effect. Since you say there is no problem for up to 3 seconds, it makes me think that after 3 seconds the media server fell asleep again. Our goal is to β€œtickle” him so that this does not happen.

In general, however, I got the impression that AVAudioPlayer is not intended for this kind of thing. You should probably use AVAudioEngine, which you can continue to work on and do to play sounds without delay.

0
source

Source: https://habr.com/ru/post/1264750/


All Articles