To prepare for recording using an audio recorder:
Specify a sound file URL.
Set up the audio session.
Configure the audio recorder’s initial state.
Audio unit |
Description |
iPod Equalizer unit |
The iPod EQ unit, of type kAudioUnitSubType_AUiPodEQ, provides a simple, preset-based equalizer you can use in your application. For a demonstration of how to use this audio unit, see the sample code project iPhoneMixerEQGraphTest . |
3D Mixer unit |
The 3D Mixer unit, of type kAudioUnitSubType_AU3DMixerEmbedded, lets you mix multiple audio streams, specify stereo output panning, manipulate playback rate, and more. OpenAL is built on top of this audio unit and provides a higher-level API well suited for game apps. |
Multichannel Mixer unit |
The Multichannel Mixer unit, of type kAudioUnitSubType_- MultiChannelMixer, lets you mix multiple mono or stereo audio streams to a single stereo stream. It also supports left/right panning for each input. For a demonstration of how to use this audio unit, see the sample code project Audio Mixer (MixerHost) . |
Remote I/O unit |
The Remote I/O unit, of type kAudioUnitSubType_RemoteIO, connects to audio input and output hardware and supports realtime I/O. For demonstrations of how to use this audio unit, see the sample code project aurioTouch . |
Voice Processing I/O unit |
The Voice Processing I/O unit, of type kAudioUnitSubType_- VoiceProcessingIO, has the characteristics of the I/O unit and adds echo suppression and other features for two-way communication. |
Generic Output unit |
The Generic Output unit, of type kAudioUnitSubType_- GenericOutput, supports converting to and from linear PCM format; can be used to start and stop a graph. |
Converter unit |
The Converter unit, of type kAudioUnitSubType_AUConverter, lets you convert audio data from one format to another. You typically obtain the features of this audio unit by using the Remote I/O unit, which incorporates a Converter unit. |
Tip |
Action |
Use compressed audio appropriately |
For AAC, MP3, and ALAC (Apple Lossless) audio, decoding can take place using hardware-assisted codecs. While efficient, this is limited to one audio stream at a time. If you need to play multiple sounds simultaneously, store those sounds using the IMA4 (compressed) or linear PCM (uncompressed) format. |
Convert to the data format and file format you need |
The afconvert tool in Mac OS X lets you convert to a wide range of audio data formats and file types. See “Preferred Audio Formats in iOS” (page 28) and the afconvert man page. |
Evaluate audio memory issues |
When playing sound with Audio Queue Services, you write a callback that sends short segments of audio data to audio queue buffers. In some cases, loading an entire sound file to memory for playback, which minimizes disk access, is best. In other cases, loading just enough data at a time to keep the buffers full is best. Test and evaluate which strategy works best for your application. |
Reduce audio file sizes by limiting sample rates, bit depths, and channels |
Sample rate and the number of bits per sample have a direct impact on the size of your audio files. If you need to play many such sounds, or long-duration sounds, consider reducing these values to reduce the memory footprint of the audio data. For example, rather than use 44.1 kHz sampling rate for sound effects, you could use a 32 kHz (or possibly lower) sample rate and still provide reasonable quality. Using monophonic (single-channel) audio instead of stereo (two channel) reduces file size. For each sound asset, consider whether mono could suit your needs. |
Pick the appropriate technology |
Use OpenAL when you want a convenient, high-level interface for positioning sounds in a stereo field or when you need low latency playback. To parse audio packets from a file or a network stream, use Audio File Stream Services. For simple playback of single or multiple sounds, use the AVAudioPlayer class. For recording to a file, use the AVAudioRecorder class. For audio chat, use the Voice Processing I/O unit. To play audio resources synced from a user’s iTunes library, use iPod Library Access. When your sole audio need is to play alerts and user-interface sound effects, use Core Audio’s System Sound Services. For other audio applications, including playback of streamed audio, precise synchronization, and access to packets of incoming audio, use Audio Queue Services. |
Code for low latency
|
For the lowest possible playback latency, use OpenAL or use the I/O unit directly.
|
An audio session comes with some default behavior. Specifically:
To obtain meaningful values for hardware characteristics, ensure that the audio session is initialized and active before you issue queries.
kAudioSessionProperty_AudioInputAvailable
Whether audio input is available on the device. Use this property to determine if audio recording is possible.
If you have configured a movie player to use its own audio session, there’s some cleanup to perform. After a movie finishes, or the user dismisses it, do these two steps, in sequence, to regain the ability to play audio:
Dispose of the movie player—even if you intend to play the same movie again later.
Reactivate your audio session.
#if TARGET_IPHONE_SIMULATOR
#warning *** Simulator mode: audio session code works only on a device
// Execute subset of code that works in the Simulator
#else
// Execute device-only code as well as the other code