Game Development Reference
In-Depth Information
How to do it...
Now we will look at some of the ways to implement sound services in our app.
System Sound Services
This is an easy way of playing audio files. For playing an audio sound using System Sound
Services and to see how it works, the following are the steps involved:
1. Add an audio file to the project, and using mainBundle get the path of the audio
file:
NSString *samplePath = [[NSBundle mainBundle]
pathForResource:@"sample-sound" ofType:@"caf"];
2. Here we have used the .caf format for the audio files. This is the recommended
Apple format.
3. Using the path, form a NSURL , which will be used to create a SystemSoundID :
NSURL *sampleURL = [NSURL fileURLWithPath:samplePath];
4. Then create a systemSoundID using the NSURL formed earlier and a property
declared as SystemSoundID named sampleSound in the code:
AudioServicesCreateSystemSoundID((__bridge
CFURLRef)sampleURL, &self.sampleSound);
5. Finally, using the systemSoundID that is, self.sampleSound , play the au-
dio file.
AudioServicesPlaySystemSound(self.sampleSound);
6. For playing an audio sound using AVAudioPlayer AVFoundation framework is
needed so it has to be imported and then add an audio file to the project and using
mainBundle get the path of the audio file. To import the framework add the fol-
lowing line of code:
#import <AVFoundation/AVFoundation.h>
7. Now create an instance of AVAudioPlayer with content as the NSURL of the file to
be played with error.
Search WWH ::




Custom Search