I am trying to figure out something –
Every time I use AVAudioPlayer I need to initialize a new AVAudioPlayer object.
I have 10 Sentence objects on a view and I wish to add a “PlaySentence” method to each Sentence object so when the user taps the Sentence the app will play a sound file.
I need this behavior on many views, so I thought adding the method to the object class so I can simply call –
[Sentence playSound];
Since AVAudioPlayer is any way initialized every time I wish to use it I can not see why this should be more expensive operation.
Am I right / is it a good approch for this need and why?
Thanks
Shani
So if I understand you right, you want your
Sentenceobject have aplaySoundmethod which sets up anAVAudioPlayerand plays the sound.You can definitely do it like that, but be aware that if you have a lot of
Sentenceobjects then you will end up creating a lot ofAVAudioPlayerobjects. But you could stop the high water line being too high by releasing it at after the file has finished playing.Another way you could do this is to have a method on
Sentenceto return the URL of the file to play and then just have a singleAVAudioPlayerinstance in your view controller where you want to play the sounds and set it up each time for the correct file. This would be my personal suggested way of doing it.