Rich
Rich

Reputation: 3762

Playing sound samples on the iPhone

I've reached the point where i want to play some samples in my game/app. My instinct says use openAL ... I will have the situation where I will need to play multiple samples at once (however no more than 2 or 3) and the samples will be short (2 or 3 seconds).

My question is what is the best way of playing samples on the iPhone given that criteria?

Upvotes: 1

Views: 4086

Answers (1)

Wim Haanstra
Wim Haanstra

Reputation: 5998

This is a piece of sample code I am using:

1st option:

I defined this in my .h file

SystemSoundID topClick;

And in my .m file I firest load the sound (aiff file):

NSBundle* bundle = [NSBundle mainBundle];
NSString *topClickFile = [bundle pathForResource:@"top_click" ofType:@"aiff"];

Then, when I want to play the sound, I use:

AudioServicesPlaySystemSound(topClick);

2nd option:

You could also use the AVAudioPlayer (available since firmware 2.2 I think):

NSString *graffitiSprayFile = [bundle pathForResource:@"sound_effect" ofType:@"aiff"];
AVAudioPlayer* sprayAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:graffitiSprayFile] error:NULL];
sprayAudio.delegate = self;
[sprayAudio prepareToPlay];
[sprayAudio setNumberOfLoops:999];

The first option is very usable for relatively short sound effect.

Upvotes: 2

Related Questions