openfrog
openfrog

Reputation: 40755

Easiest way to play a CAF sound on the iPhone?

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?

Upvotes: 3

Views: 5737

Answers (4)

Giridharan Kannan
Giridharan Kannan

Reputation: 39

Following code is worked for me to play .caf files

SystemSoundID SoundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &SoundID);
AudioServicesPlaySystemSound(SoundID);

Upvotes: 1

scurioni
scurioni

Reputation: 2344

`#include < AudioToolbox/AudioToolbox.h>

SystemSoundID soundID = 0;

CFURLRef soundFileURL = (CFURLRef)[NSURL URLWithString:@"myfile.caf"];

OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);

AudioServicesPlaySystemSound(soundID);

Upvotes: 0

Bryan Henry
Bryan Henry

Reputation: 8408

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.

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

Upvotes: 7

John R Doner
John R Doner

Reputation: 2282

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

Upvotes: 2

Related Questions