AVAudioPlayer lag issue?

Joined
Mar 4, 2010
Messages
51
Reaction score
0
Points
6
I have a sound setup that uses AVAudioPlayer. Unfortunately there is always a slight (half second) pause/lag just before the first sound is played. Any sound can get the lag as long as it is the first sound played since the app was first opened. (And it happens again if you swipe the app closed from multitasking, and then open it again.) On the simulator the lag is much longer than it is when just run on my iPad 2.

Any idea what's causing this and how I can stop it from happening?

Extra info:

The code makes a singleton class called SoundsController that makes a dictionary of strings to correspond to sound file names, and has a method called playSound that creates an AVAudioPlayer object, adds it to an array object (which is a class property), and then implements the audioPlayerDidFinishPlaying method to remove the AVAudioPlayer from the array so it can be released.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
I have not done anything with the AVAudioPlayer class myself for a while, but the problem is the fact that the your playSound: method is creating and initializing the AVAudioPlayer class, then loading the URL or Data from a sound file, then also preparing itself to be able to manipulate the sound file, so thatt's the reason for the lag time your experiencing.

You should try to initialize and pepare the AVAudioPlayer well before you need it to play, perhaops in another method, after the user has selected a sound file in a list, or from a file open dialog box.
That way the play button and corresponding play method is only having to tell the AVAdioPlayer to play only, and that should reduce the time lag a bit, but by how much migth take some experimentation.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top