iPhone Audio: System Sound vs AVAudioPlayer

You want to play a short sound on certain actions (view swipe, data saved, data deleted) and the first question is: what library should I use, System Sound or AVAudioPlayer? Both libraries are fine for playing short sounds, but Apple recommends using AVAudioPlayer.

System Sound Services (AudioToolbox.framework) is a collection of C functions and it can be used to play short sounds of 30 seconds or less (it can also be used to vibrate the iPhone).

AVAudioPlayer class (AVFoundation.framework) provides a simple Objective-C interface for playing sounds (it was introduced in version 2.2). It can play audio data of any duration, from a file or memory. AVAudioPlayerDelegate Protocol allows you to handle interruptions (such as an incoming phone call) and to be notified when a sound has finished playing.

Example of using AVAudioPlayer:

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@”audio_file” ofType:@”wav”];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil] autorelease];
[audioPlayer prepareToPlay];
[audioPlayer play];

Another approach to adding sound effects to iPhone apps can be found on Surgeworks European blog.