views:

212

answers:

1

Hello, 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?

Thankya

A: 

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);

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

dbarker
Thanks, it compiles great. I think I read that sound doesn't work on the simulator... is this true?
pureman
Don't think so - simulator should be fine.
dbarker
Okay, I've loaded this onto my iPhone and sound still does not work. I've used both .wav and .aif file formats. I've included "AudioToolbox/AudioToolbox.h." Any ideas to make this work?
pureman
I'd need to see your code + any error messages...
dbarker