views:

225

answers:

2

How hard is it to do that? Someone told me that it's incredible hard playing audio on the iPhone. Can't really believe it. But he said playing one is hard, but playing two or three at same time is incredible hard. Sounded scary. What can you guys say about this?

What I want to do: I have about 10 CAF files with little blips and beeps and button press sounds. Nothing really special. Now I want to play these files every time something happens. And several things could happen at same time. So it could happen that playbacks overlap.

Maybe there's a framework I can use for doing this?

+1  A: 

Playing small blips, beeps, etc (sounds less than 30 seconds) is actually pretty easy - this is exactly what AudioServices is meant to do. You need to first obtain an ID for each sound, using something like this:

SystemSoundID soundID = 0;
CFURLRef soundFileURL = (CFURLRef)[NSURL URLWithString:somePathString];
OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);
if (errorCode != 0) {
  // Handle failure here
}

You'll then hold onto that ID until you need to use it, and then you can play the sound simply using this line:

AudioServicesPlaySystemSound(soundID);

There's also AudioServicesPlayAlertSound() that will behave slightly differently depending on the device's capability, such as vibrating an iPhone if the device is configured to by the user. You should read Apple's notes in the docs for a more complete explanation: http://is.gd/57dtl.

Note that you have minimal control over the playback of sounds using AudioServices - you can't stop them, control the volume, etc. AudioServices is only meant for alert sounds and other such short beeps and blips. There is, of course, AVAudioPlayer but its a bit more heavyweight than what you need in this case. You're probably better off sticking with AudioServices unless one of its constraints makes it unusable.

You need to link AudioToolbox.framework in order to use these functions.

P.S. Welcome to Stack Overflow! Don't forget to read the FAQ and mark accepted answers for your questions (if they're good answers worth accepting, of course).

Sbrocket
Thanks. Xcode can't find any symbols for any AudioServices____ calls. What do I have to import?
openfrog
@openfrog: I added a line to the answer. Don't forget to click the green checkmark next to accepted answers for your questions.
Sbrocket
+1  A: 

I have only been working with IPhone for a few weeks, and I've gotten lots of help on this forum, so it gives me great pleasure to actually be able to answer a question.

The gadget you need is the AVAudioPlayer, which is in the Apple documentation. I have just finished a little recorder/playback app, primarily using a good bit of help from Chapter 16 of the book "IPhone SDK Development" (Dudney and Adamson).

Of the four IPhone programming books I've invested in, it has the most realistic apps to work through.

But at any rate, check out the AVAudioPlayer class reference. It's quite easy to use, and that being said by a novice.

John Doner

John R Doner
Thanks! Going to have a look at AVAudioPlayer too.
openfrog