tomidelucca
tomidelucca

Reputation: 2543

Playing a sound that repeats throughout the program in iOS

In my app, every time a user switches to a new view a sound will be played. Also, every time a button is tapped, a sound will be played.

The sounds to be played are the same for each case, so for example, sound.caf will be used many times and will be the same for every view.

My problem is that I cant think of a clever place to alloc these sounds, thats visible to all views. Is this possible? I dont want to alloc the sound everytime a view is created because I think it's a waste of time.

Is there someplace where I can alloc these sounds, and play them from any view in my program? Thanks!

Upvotes: 0

Views: 164

Answers (1)

Ben Baron
Ben Baron

Reputation: 14815

The simplest solution would probably be to use the app delegate for that purpose because it's a singleton class (there is only one instance of it per application) and it can be accessed by any class easily: [UIApplication sharedApplication].delegate

You could set up the sound(s) in the applicationDidFinishLaunching: method so they're ready to go when your app is.

Upvotes: 1

Related Questions