Luke
Luke

Reputation: 9690

One AVAudioPlayer to play multiple sounds without re-initialising?

I'm wanting to use a single instance of an AVAudioPlayer to handle a few different quick sound effects, that could be triggered in any order, etc.

As far as I can see, the only way to change the audio file to be played is to re-initialise the player with initWithContentsOfURL:.

So, two questions: since that's what I'm doing now, is that alright? More overhead in re-allocating, etc. Should I care? And, is there a way to put all four files in there and tell it to just load them one at a time, but NOT sequentially (i.e. by using the AVQueuePlayer)?

Upvotes: 1

Views: 395

Answers (1)

ruoho ruotsi
ruoho ruotsi

Reputation: 1313

If the files are short (which it sounds like), I recommend loading them into an AVAudioBuffer and scheduling these buffers to play on the AVAudioPlayer at the correct time.

See the WWDC talk on AVAudioEngine in Practice (transcript).

Upvotes: 1

Related Questions