iOS offers a rich set of tools for working with sound in your application.
These tools are arranged into frameworks according to the features they provide, as follows:
■ Use theMedia Player framework to play songs, audio books, or audio podcasts froma user’s iPod library.
For details, seeMedia Player Framework Reference, iPod LibraryAccess ProgrammingGuide, and the AddMusic sample code project.
■ Use the AV Foundation framework to play and record audio using a simple Objective-C interface.
For details, see AV Foundation Framework Reference and the avTouch sample code project.
■ Use the Audio Toolbox framework to play audio with synchronization capabilities, access packets of incoming audio, parse audio streams,
convert audio formats, and record audio with access to individual packets.
For details, see Audio Toolbox Framework Reference and the SpeakHere sample code project.
■ Use the Audio Unit framework to connect to and use audio processing plug-ins. For details, see Audio Unit Hosting Guide for iOS.
■ Use the OpenAL framework to provide positional audio playback in games and other applications.
iOS supports OpenAL 1.1. For information on OpenAL, see the OpenAL website, OpenAL FAQ for iPhone OS,and the oalTouch sample code project.
1 Playing Media Items with iPod Library Access
2 Playing UI Sound Effects or Invoking Vibration Using System Sound Services (require Audio Toolbox framework)
To play user-interface sound effects (such as button clicks), or to invoke vibration on devices that support it,
use System Sound Services. This compact interface is described in System Sound Services Reference.
You can find sample code in the Audio UI Sounds (SysSound) sample in the iOS Dev Center.
Note: Sounds played with System Sound Services are not subject to configuration using your audio session.
As a result, you cannot keep the behavior of System Sound Services audio in line with other audio behavior
in your application. This is the most important reason to avoid using System Sound Services for any audio
apart from its intended uses.
The AudioServicesPlaySystemSound function lets you very simply play short sound files. The simplicity
carries with it a few restrictions. Your sound files must be:
■ No longer than 30 seconds in duration
■ In linear PCM or IMA4 (IMA/ADPCM) format
■ Packaged in a .caf, .aif, or .wav file
In addition, when you use the AudioServicesPlaySystemSound function:
■ Sounds play at the current system audio volume, with no programmatic volume control available
■ Sounds play immediately
■ Looping and stereo positioning are unavailable
■ Simultaneous playback is unavailable: You can play only one sound at a time
The similar AudioServicesPlayAlertSound function plays a short sound as an alert. If a user has configured
their device to vibrate in Ring Settings, calling this function invokes vibration in addition to playing the sound
3 Playing Sounds Easily with the AVAudioPlayer Class
The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application
does not require stereo positioning or precise synchronization, and if you are not playing audio captured
from a network stream, Apple recommends that you use this class for playback.
Using an audio player you can:
■ Play sounds of any duration
■ Play sounds from files or memory buffers
■ Loop sounds
■ Play multiple sounds simultaneously (although not with precise synchronization)
■ Control relative playback level for each sound you are playing
■ Seek to a particular point in a sound file, which supports application features such as fast forward and rewind
■ Obtain audio power data that you can use for audio level metering
4 Playing Sounds with Control Using Audio Queue Services Audio Queue Services adds playback capabilities beyond those available with the AVAudioPlayer class.
Using Audio Queue Services for playback lets you:
■ Precisely schedule when a sound plays, allowing synchronization
■ Precisely control volume on a buffer-by-buffer basis
■ Play audio that you have captured from a stream using Audio File Stream Services
5 Playing Sounds with Positioning Using OpenAL
The open-sourced OpenAL audio API, available in iOS in the OpenAL framework, provides an interface
optimized for positioning sounds in a stereo field during playback. Playing, positioning, and moving sounds
works just as it does on other platforms. OpenAL also lets you mix sounds. OpenAL uses the I/O unit for
playback, resulting in the lowest latency.
For all of these reasons, OpenAL is your best choice for playing sounds in game applications on iOS-based
devices. However, OpenAL is also a good choice for general iOS application audio playback needs.