Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing an AVAudioPlayer for a different sound

I have an instance of AVAudioPlayer that will play several sounds. Is there a way to provide an AVAudioPlayer instance with a new sound, or do I have to create a new instance with initWithData:?

like image 932
cfischer Avatar asked Jun 23 '11 09:06

cfischer


1 Answers

There's no API to pass a new file to an existing AVAudioPlayer instance. However, AVAudioPlayer creation isn't expensive, so you shouldn't be concerned about performance, as long as you release instances once you're done with them.

If you have a fixed set of sounds that play over each other, or are likely to play repeatedly, you can create one AVAudioPlayer instance for each sound and re-use those instances by calling play on the appropriate instance.

If only one sound is playing at a time, and you're generating new sounds (for instance with text-to-speech based on user input), I generally have a single player as a property. When a new sound plays, I call [player stop], release the old player, and instantiate a new one with the new sound file.

like image 157
Christopher Pickslay Avatar answered Nov 23 '22 03:11

Christopher Pickslay