Sound in Xcode (short sound)

by

Sound seems to be easy, just copy from the examples:
Create a soundID
AudioServicesCreateSystemSoundID (soundFileURLRef, soundID);
and play the sound:
AudioServicesPlaySystemSound (soundID);
and wav and caf and aif is allowed, what could be more easy?

Until you hear your sound played! If it played at all. What you forget (at least I did) is this line:
The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.
What can be that stuff????
It took me a while to find out how to do that:
In the terminal window, find your soundfile and do:
macbook: contrechoc$ afconvert -f caff -d ima4 trill.wav
only this reformatting will make your soun play well.
But there is more:
The above lines of code don’t show this little detail:
what is a soundID? Well:
SystemSoundID soundID;
You make it like this:
mainBundle = CFBundleGetMainBundle ();
soundFileURLRef = CFBundleCopyResourceURL (mainBundle, CFSTR ("mySound"), CFSTR ("caf"),NULL );

But wait a minute: I know of NSString, but what is all this: CFSTR ???
And why is there no @ in front??
Ok, now you are in the apple jungle: CFSTR comes from other earlier Xcode environments, the MACOS etc….
Later on came NSString….. :-(
You have to cast this CFSTR parameter if you are carrying an NSString, which is what you want because you want to make a nice object:
(CFStringRef) soundName is of type CFSTR if soundName is of type NSString:

Knowing this you can make a nice drumpad app, with buttons carrying images of snares and base drums and sounds attached on the “action” method of these views.

Example with sounds attached to a button. The buttons are generated in a grid, and stored in an NSArray.

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.