AVAudioPlayer delay

Member
Posts: 281
Joined: 2006.05
Post: #1
I'm trying to use AVFoundation's AVAudioPlayer to add sound to my game. It's a very short sound that'll need to be played many times per second (but not looped). Every time I play the sound the game lags for about 0.2 seconds, causing a noticeable delay.

The prepareToPlay method seems to be responsible. The docs say "Invoking the stop method, or allowing a sound to finish playing, undoes this setup." So I did a quick test, adding lots of silence to the end of my sound. Instead of -play every time I'd check if it's playing and if it is just set currentTime to 0. It worked nicely; no delay.

So now I'm thinking about putting all my sounds into one big sound file separated by silence. Then I can pause it once a sound has finished playing and set currentTime to the start of another sound when it's time to play another. Before I do that, am I missing something easier? It seems like a pretty crazy thing to do.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
Placing all the sounds in one large file would be memory friendly, plus i/o read time may increase if you have a large number of sound files.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Member
Posts: 281
Joined: 2006.05
Post: #3
Thanks kodex.

So now it plays sounds with no delay nicely. AVAudioPlayer also seems to 'unload' when it's paused for more than a few seconds, so instead of pausing I make it loop over a silent section of the file.

It works, but I still feel like I must be doing something wrong.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  AVAudioPlayer returning -43 FlamingHairball 4 6,198 Aug 31, 2011 10:29 AM
Last Post: FlamingHairball
  AVAudioPlayer kae_ 8 7,301 Aug 10, 2010 04:13 AM
Last Post: kae_
  performance bug in AVAudioPlayer captainfreedom 2 3,489 Aug 4, 2010 12:03 PM
Last Post: captainfreedom
  AVAudioPlayer stutters the game when changing song AdrianM 0 3,290 May 13, 2010 03:59 PM
Last Post: AdrianM
  OpenAL and AVAudioPlayer in the Simulator AkumaStreak 4 5,287 Jul 12, 2009 11:05 AM
Last Post: Madrayken