androidnotgenius
androidnotgenius

Reputation: 417

How to play sounds in iPhone app

I am a beginner in this very confusing Objective-C game.

I would like to play a very short sound effect every time a certain button is pushed.

How can I do this?

Can I use an mp3 file? Or will I have to convert to wav?

Upvotes: 3

Views: 2588

Answers (2)

dstnbrkr
dstnbrkr

Reputation: 4335

You can use System Sound Services to play short sounds:

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainBundle, CFSTR("filename"), CFSTR("aiff"), NULL);
SystemSoundID soundId;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundId);
AudioServicesPlaySystemSound(soundId);
CFRelease(soundFileURLRef)

.caf, .aiff and .wav formats are supported.

Upvotes: 4

ryanrhee
ryanrhee

Reputation: 2571

Not enough reputation to comment on the solution by @dstnbrkr so I'll post it as an answer instead.

In the above example, the CFURLRef soundFileURLRef isn't being released.

Call CFRelease(soundFileURLRef) after the last line (AudioServicesPlaySystemSound(soundId);) to release the Core Foundation object.

Upvotes: 1

Related Questions