User guide

Important: To obtain meaningful values for hardware characteristics, ensure that the audio session is
initialized and active before you issue queries.
Two of the most useful audio session hardware properties are sampleRate and outputLatency. The
sampleRate property contains the hardware sample rate of the device. The outputLatency property contains
the playback latency of the device.
Specifying Preferred Hardware I/O Buffer Duration
Use the AVAudioSession class to specify preferred hardware sample rates and preferred hardware I/O buffer
durations, as shown in Listing 4-2. To set preferred sample rate you’d use similar code.
Listing 4-2 Specifying preferred I/O buffer duration using the AVAudioSession class
NSError *setPreferenceError = nil;
NSTimeInterval preferredBufferDuration = 0.005;
[[AVAudiosession sharedInstance]
setPreferredIOBufferDuration: preferredBufferDuration
error: &setPreferenceError];
After establishing a hardware preference, always ask the hardware for the actual value, because the system
may not be able to provide what you ask for.
Obtaining and Using the Hardware Sample Rate
As part of setting up for recording audio, you obtain the current audio hardware sample rate and apply it to
your audio data format. Listing 4-3 shows how. You typically place this code in the implementation file for a
recording class. Use similar code to obtain other hardware properties, including the input and output number
of channels.
Before you query the audio session for current hardware characteristics, ensure that the session is active.
Listing 4-3 Obtaining the current audio hardware sample rate using the AVAudioSession class
double sampleRate;
sampleRate = [[AVAudioSession sharedInstance] currentHardwareSampleRate];
Optimizing Your App for Device Hardware
Querying Hardware Characteristics
2014-09-17 | Copyright © 2014 Apple Inc. All Rights Reserved.
35