2

I am new to sound in XCode, I am using sound effects. I want a sound effect and I know that for sound effects you should use audio toolbox framework, what is the code? How do I code it, what are the variables and properties?

user842059
  • 73
  • 3
  • 12

1 Answers1

1

First, convert the sound to lei16 caf. Here's a good write up that explains why you should use that format.

afconvert -d LEI16 -f caff /path/to/mp3-or-wav-or-whatever

Here's the code to play it.

// I usually store "mySound" in an instance variable in the view
// controller init
CFURLRef mySoundUrl = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("my_sound"), CFSTR("caf"), NULL);
SystemSoundID mySound;
AudioServicesCreateSystemSoundID(mySoundUrl, &mySound);
CFRelease(mySoundUrl);

// Plays the sound.
AudioServicesPlaySystemSound(mySound);
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
  • 1
    I would also suggest using AVFoundation for longer sounds. Although it is slightly less efficient, AVFoundation allows you to play sounds in a larger variety of formats with longer durations. – Alex Nichol Jul 22 '11 at 02:33