Audio Session Services Reference
| Framework | /System/Library/Frameworks/AudioToolbox.framework |
| Companion guide | |
| Declared in | AudioSession.h |
Overview
This document describes Audio Session Services, a C interface for managing an application’s audio behavior in the context of other applications.
Audio Session Services lets you specify the intended audio behavior for your iOS app. For example, you can specify whether you intend for your app’s audio to silence other apps or to mix with their audio. You also use this API to specify your app’s behavior when it is interrupted, such as by a phone call. When the system knows your intentions, it configures the audio hardware in the device to satisfy those intentions, as possible.
Starting in iOS 5.0, Audio Session Services offers the following three features:
Audio session modes, each of which refines the configuration provided by your audio session category setting according to the specific purpose of your app. See “Audio Session Modes.”
Input gain adjustment, providing you with the ability to improve the recorded signal level when the sound you are recording is unusually loud or soft. See
kAudioSessionProperty_InputGainScalar.Dictionary-based audio route information, providing specific information on the input and output audio route, on audio route changes, and on audio routes available in a USB-attached audio accessory. See
kAudioSessionProperty_AudioRouteDescription,kAudioSessionProperty_AudioRouteChange,kAudioSessionProperty_InputSources, andkAudioSessionProperty_OutputDestinations.
Many of the features of this C interface are available through the Objective-C interface described in AVAudioSession Class Reference.
Functions by Task
These functions apply only to iOS. They do not apply to Mac OS X.
Initializing and Activating an Audio Session
Using Audio Session Properties
Functions
AudioSessionAddPropertyListener
Adds a property listener callback function to your application’s audio session object.
OSStatus AudioSessionAddPropertyListener ( AudioSessionPropertyID inID, AudioSessionPropertyListener inProc, void *inClientData );
Parameters
- inID
The identifier for the audio session property whose value changes you want to listen for.
- inProc
The name of your property listener callback function.
- inClientData
Data that you would like to be passed to your property listener callback.
Return Value
A result code. See “Result Codes.”
Discussion
When an audio session property value changes, and you have added a listener callback for that property, the audio session object invokes the callback.
You can add exactly one listener callback for a given inID-inClientData pair. In other words, you can add more than one property listener callback function for a given audio session property, provided you pass a unique inClientData parameter value each time you add a property listener.
Audio session properties are listed and described in “Audio Session Property Identifiers.”
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
AudioSession.hAudioSessionGetProperty
Gets the value of a specified audio session property.
OSStatus AudioSessionGetProperty ( AudioSessionPropertyID inID, UInt32 *ioDataSize, void *outData );
Parameters
- inID
The identifier for the audio session property that you want to get the value of.
- ioDataSize
On input, the memory size for the outData parameter. On output, the actual size of the property value.
- outData
On output, the value of the specified audio session property.
Return Value
A result code. See “Result Codes.”
Discussion
Audio session properties are listed and described in “Audio Session Property Identifiers.”
Special Considerations
Some Core Audio property values are C types and others are Core Foundation objects.
If you call this function to retrieve a value that is a Core Foundation object, then this function—despite the use of “Get” in its name—duplicates the object. You are responsible for releasing the object, as described in “The Create Rule” in Memory Management Programming Guide for Core Foundation.
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hAudioSessionGetPropertySize
Gets the size of the value for a specified audio session property.
OSStatus AudioSessionGetPropertySize ( AudioSessionPropertyID inID, UInt32 *outDataSize );
Parameters
- inID
The identifier for the audio session property whose value you want to get the size of.
- outDataSize
On output, the size of the property value.
Return Value
A result code. See “Result Codes.”
Discussion
Audio session properties are listed and described in “Audio Session Property Identifiers.”
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hAudioSessionInitialize
Initializes an iOS application’s audio session object.
OSStatus AudioSessionInitialize ( CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioSessionInterruptionListener inInterruptionListener, void *inClientData );
Parameters
- inRunLoop
The run loop that the interruption listener callback should be run on. Pass
NULLto use the main run loop.- inRunLoopMode
The mode for the run loop that the interruption listener function will run on. Passing
NULLis equivalent to passingkCFRunLoopDefaultMode.- inInterruptionListener
The interruption listener callback function. The application’s audio session object invokes the callback when the session is interrupted and (if the application is still running) when the interruption ends. Can be
NULL. SeeAudioSessionInterruptionListener.- inClientData
Data that you would like to be passed to your interruption listener callback.
Return Value
A result code. See “Result Codes.”
Discussion
Your application must call this function before making any other Audio Session Services calls. You may activate and deactivate your audio session as needed (see AudioSessionSetActive), but should initialize it only once.
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hAudioSessionRemovePropertyListener
Removes an audio session property listener callback function. (Deprecated. Use AudioSessionRemovePropertyListenerWithUserData instead.)
OSStatus AudioSessionRemovePropertyListener ( AudioSessionPropertyID inID );
Parameters
- inID
The identifier for the audio session property whose value changes you no longer want to listen for.
Return Value
A result code. See “Result Codes.”
Availability
- Available in iOS 2.0 and later.
- Deprecated in iOS 2.0.
Declared In
AudioSession.hAudioSessionRemovePropertyListenerWithUserData
Removes a property listener callback function from your application’s audio session object.
OSStatus AudioSessionRemovePropertyListenerWithUserData ( AudioSessionPropertyID inID, AudioSessionPropertyListener inProc, void *inClientData );
Parameters
- inID
The identifier for the audio session property whose value changes you no longer want to listen for.
- inProc
The name of your property listener callback function.
- inClientData
The same custom data for the property listener callback that you passed when calling the
AudioSessionAddPropertyListenerfunction.
Return Value
A result code. See “Result Codes.”
Discussion
Audio session properties are listed and described in “Audio Session Property Identifiers.”
Availability
- Available in iOS 2.1 and later.
Declared In
AudioSession.hAudioSessionSetActive
Actives or deactivates your application’s audio session.
OSStatus AudioSessionSetActive ( Boolean active );
Parameters
- active
Pass
trueto activate your application’s audio session, orfalseto deactivate it.
Return Value
A result code. See “Result Codes.”
Discussion
Activating your audio session may interrupt audio sessions belonging to other applications running in the background, depending on categories and priorities. Deactivating your audio session allows other, interrupted audio sessions to resume.
When another active audio session does not allow mixing, attempting to activate your audio session may fail.
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hAudioSessionSetActiveWithFlags
Activates or deactivates your application’s audio session; provides flags for use by other audio sessions.
OSStatus AudioSessionSetActiveWithFlags ( Boolean active, UInt32 inFlags );
Parameters
- active
Pass
trueto activate your application’s audio session, orfalseto deactivate it.- inFlags
A bitmapped value containing one or more flags from the “Audio Session Activation Flags” enumeration.
Return Value
A result code. See “Result Codes.”
Discussion
Activating your audio session may interrupt audio sessions belonging to other applications running in the background, depending on categories and priorities. Deactivating your audio session allows other, interrupted audio sessions to resume.
When another active audio session does not allow mixing, attempting to activate your audio session may fail.
Availability
- Available in iOS 4.0 and later.
Declared In
AudioSession.hAudioSessionSetProperty
Sets the value of a specified audio session property.
OSStatus AudioSessionSetProperty ( AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData );
Parameters
- inID
The identifier for the audio session property that you want to set the value of.
- inDataSize
The size, in bytes, of the value in the
inDataparameter.- inData
The value that you are applying to the specified audio session property.
Return Value
A result code. See “Result Codes.”
Discussion
Audio session properties are listed and described in “Audio Session Property Identifiers.”
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hCallbacks
AudioSessionInterruptionListener
Invoked when an audio interruption in iOS begins or ends.
typedef void (*AudioSessionInterruptionListener)( void *inClientData, UInt32 inInterruptionState );
If you named your function MyInterruptionListener, you would declare it like this:
void MyInterruptionListener ( void *inClientData, UInt32 inInterruptionState );
Parameters
- inClientData
Data that you specified in the inClientData parameter of the
AudioSessionInitializefunction. Can beNULL.- inInterruptionState
A constant that indicates whether the interruption has just started or just ended. See “Audio Session Interruption States.”
Discussion
To register your interruption listener callback with your application’s audio session object, specify it in the AudioSessionInitialize function.
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hAudioSessionPropertyListener
Invoked when an audio session property changes in iOS.
typedef void (*AudioSessionPropertyListener)( void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData );
If you named your function MyPropertyListener, you would declare it like this:
void MyPropertyListener ( void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData );
Parameters
- inClientData
Data that you specified in the inClientData parameter of the
AudioSessionAddPropertyListenerfunction. Can beNULL.- inID
The identifier for the audio session property whose value just changed. See “Audio Session Property Identifiers.”
- inDataSize
The size, in bytes, of the value of the changed property.
- inData
The new value of the changed property.
Discussion
You can register one or more property listener callbacks with your application’s audio session object by calling the AudioSessionAddPropertyListener function.
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hData Types
AudioSessionPropertyID
The data type for an audio session property identifier.
typedef UInt32 AudioSessionPropertyID;
Discussion
Each Audio Session property has a four-character identifier. The properties are listed in “Audio Session Property Identifiers.”
Availability
- Available in iOS 2.0 and later.
Declared In
AudioSession.hConstants
Audio Session Property Identifiers
Property identifiers used with Audio Session Services in iOS.
enum {
kAudioSessionProperty_PreferredHardwareSampleRate = 'hwsr',
// read/write
kAudioSessionProperty_PreferredHardwareIOBufferDuration = 'iobd',
// read/write
kAudioSessionProperty_AudioCategory = 'acat',
// read/write
kAudioSessionProperty_AudioRouteChange = 'roch',
// callback function
kAudioSessionProperty_CurrentHardwareSampleRate = 'chsr',
// read-only
kAudioSessionProperty_CurrentHardwareInputNumberChannels = 'chic',
// read-only
kAudioSessionProperty_CurrentHardwareOutputNumberChannels = 'choc',
// read-only
kAudioSessionProperty_CurrentHardwareOutputVolume = 'chov',
// read-only + callback function
kAudioSessionProperty_CurrentHardwareInputLatency = 'cilt',
// read-only
kAudioSessionProperty_CurrentHardwareOutputLatency = 'colt',
// read-only
kAudioSessionProperty_CurrentHardwareIOBufferDuration = 'chbd',
// read-only
kAudioSessionProperty_OtherAudioIsPlaying = 'othr',
// read-only
kAudioSessionProperty_OverrideAudioRoute = 'ovrd',
// write-only
kAudioSessionProperty_AudioInputAvailable = 'aiav',
// read-only + callback function
kAudioSessionProperty_ServerDied = 'died',
// callback function
kAudioSessionProperty_OtherMixableAudioShouldDuck = 'duck',
// read/write
kAudioSessionProperty_OverrideCategoryMixWithOthers = 'cmix',
// read/write
kAudioSessionProperty_OverrideCategoryDefaultToSpeaker = 'cspk',
// read/write
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput = 'cblu'
// read/write
kAudioSessionProperty_InterruptionType = 'type'
// read-only
kAudioSessionProperty_Mode = 'mode',
// read/write
kAudioSessionProperty_InputSources = 'srcs',
// read-only + callback function
kAudioSessionProperty_OutputDestinations = 'dsts',
// read-only + callback function
kAudioSessionProperty_InputSource = 'isrc',
// read/write
kAudioSessionProperty_OutputDestination = 'odst',
// read/write
kAudioSessionProperty_InputGainAvailable = 'igav',
// read-only + callback function
kAudioSessionProperty_InputGainScalar = 'igsc',
// read/write + callback function
kAudioSessionProperty_AudioRouteDescription = 'crar',
// read-only
};
Constants
kAudioSessionProperty_PreferredHardwareSampleRateYour preferred hardware sample rate for the audio session. A read/write
Float64value. The actual sample rate may be different and can be obtained using thekAudioSessionProperty_CurrentHardwareSampleRateproperty.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_PreferredHardwareIOBufferDurationYour preferred hardware I/O buffer duration in seconds. Do not set this property unless you require lower I/O latency than is provided by default.
A read/write
Float32value.The actual I/O buffer duration may be different from the value that you request, and can be obtained from the
kAudioSessionProperty_CurrentHardwareIOBufferDurationproperty.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_AudioCategoryThe category for the audio session. A read/write
UInt32value. See “Audio Session Categories.”Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_AudioRouteChangeA
CFDictionaryRefobject containing the reason the audio route changed along with details on the previous and current audio route.The dictionary contains the keys and corresponding values described in “Audio Route Change Dictionary Keys.”
The
kAudioSessionProperty_AudioRouteChangedictionary is available to your app only by way of theAudioSessionPropertyListenercallback function.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareSampleRateIndicates the current hardware sample rate. A read-only
Float64value.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareInputNumberChannelsIndicates the current number of audio hardware input channels. A read-only
UInt32value.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareOutputNumberChannelsIndicates the current number of audio hardware output channels. A read-only
UInt32value.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareOutputVolumeIndicates the current audio output volume as
Float32value between 0.0 and 1.0. Read-only. This value is available to your app by way of a property listener callback function. SeeAudioSessionAddPropertyListener.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareInputLatencyIndicates the current hardware input latency, in seconds, as a read-only
Float32value.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareOutputLatencyIndicates the current hardware output latency, in seconds, as a read-only
Float32value.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_CurrentHardwareIOBufferDurationIndicates the current hardware IO buffer duration, in seconds, as a read-only
Float32value.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OtherAudioIsPlayingIndicates whether or not another app (typically, the iPod app) is currently playing audio. Read-only. A non-zero
UInt32value indicates that other audio is playing.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OverrideAudioRouteSpecifies whether or not to override the audio session category’s normal audio route.
A write-only
UInt32value. Can be set with one of two values:kAudioSessionOverrideAudioRoute_None, which specifies that you want to use the normal audio route; andkAudioSessionOverrideAudioRoute_Speaker, when sends output audio to the built-in speaker. This property can be used only with thekAudioSessionCategory_PlayAndRecord(or the equivalentAVAudioSessionCategoryPlayAndRecord) category.If a headset is plugged in at the time you set this property’s value to
kAudioSessionOverrideAudioRoute_Speaker, the system changes the audio routing for input as well as for output: input comes from the built-in microphone; output goes to the built-in speaker.Upon an audio route change (such as by plugging in or unplugging a headset), or upon interruption, this property reverts to its default value.
See also
kAudioSessionProperty_OverrideCategoryDefaultToSpeaker.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_AudioInputAvailableIndicates if audio input is available (a nonzero value) or not (a value of 0).
A read-only
UInt32value, interpreted as a Boolean value. Use this property, rather than the device model, to determine if audio input is available.You can listen for changes in the value of this property using a callback function. For instance, if a user plugs a headset into an iPod touch (2nd generation), audio input becomes available via the wired microphone and your callback is invoked. See
AudioSessionAddPropertyListener.Available in iOS 2.2 and later.
Declared in
AudioSession.h.kAudioSessionProperty_ServerDiedIndicates if the audio server has died (indicated by a nonzero
UInt32value) or is still running (a value of 0).This value is available to your app only by way of a property listener callback function. See
AudioSessionAddPropertyListener.Available in iOS 3.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OtherMixableAudioShouldDuckFor audio session categories that allow audio mixing with other apps, specifies whether other audio should be reduced in level when your app produces sound. This property has a value of
FALSE(0) by default. Set it to a nonzero value to turn on ducking.When your app is finished playing sound, be sure to set this property back to
FALSEto remove ducking.Available in iOS 3.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OverrideCategoryMixWithOthersChanges the mixing behavior of the
kAudioSessionCategory_MediaPlaybackandkAudioSessionCategory_PlayAndRecordaudio session categories.A read/write
UInt32value. By default, the value of this property isFALSE(0).Setting this property to
TRUE(any nonzero value) allows audio mixing with other apps. Other aspects of these categories, such as their Silent switch behavior, are not affected. (The switch is called the Ring/Silent switch on iPhone.)When the audio session category changes, such as during an interruption, the value of this property reverts to
FALSE. To regain mixing behavior you must then re-set this property.Always check to see if setting this property succeeds or fails, and react appropriately; behavior may change in future releases of iOS.
Available in iOS 3.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OverrideCategoryDefaultToSpeakerSpecifies whether or not to route audio to the speaker (instead of to the receiver) when no other audio route, such as a headset, is connected.
A read/write
UInt32value. By default, the value of this property isFALSE(0).This property retains its value through an audio route change (such as when plugging in or unplugging a headset), and upon interruption; it reverts to its default value only upon an audio session category change. This property can be used only with the
kAudioSessionCategory_PlayAndRecord(or the equivalentAVAudioSessionCategoryPlayAndRecord) category.See also
kAudioSessionProperty_OverrideAudioRoute.Available in iOS 3.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OverrideCategoryEnableBluetoothInputAllows a paired Bluetooth device to appear as an available audio input route.
A read/write
UInt32value. By default, the value of this property isFALSE(0).This property can be used to modify the
kAudioSessionCategory_RecordAudioorkAudioSessionCategory_PlayAndRecordcategories. Attempting to set this property toTRUEwill fail for all other categories.This property affects the
kAudioSessionCategory_PlayAndRecordcategory as follows: If the audio input to the device is coming from a Bluetooth headset, setting this property toTRUEresults in audio output also going to the Bluetooth headset.Available in iOS 3.1 and later.
Declared in
AudioSession.h.kAudioSessionProperty_InterruptionTypeIndicates the type of an end-interruption event.
A read-only
UInt32value that is one of the constants in the “Audio Session Interruption Types” enumeration.Query this property within your interruption callback to find out whether or not it is appropriate to immediately resume the audio operation that was interrupted. Media playback apps (typically, those that have a “play” button) can use this property’s value as an indication for whether or not to resume playing after an interruption ends. Other app types (such as games) should normally resume audio playback whenever an interruption ends.
This property’s value is available within the scope of your app’s interruption listener callback function (see
AudioSessionInterruptionListener), and valid only when your callback receives thekAudioSessionEndInterruptionstate identifier. At all other times, this property’s value is invalid.Available in iOS 4.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_ModeA read/write
UIInt32value that specifies the audio session mode.An audio session mode is a key that identifies a set of device audio configuration details, such as whether or not the device performs automatic gain adjustment on incoming audio. A mode refines the configuration provided by a category (
kAudioSessionProperty_AudioCategory).The available modes are described in “Audio Session Modes.” The default mode is
kAudioSessionMode_Default.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_InputSourcesA
CFArrayRefobject containing details on the available audio input sources in a USB audio accessory attached through the iPad camera connection kit.Each element of the array contains a
CFDictionaryRefobject with the keys and corresponding values described in “USB Accessory Audio Source Dictionary Keys.”If there is no audio input source available from the attached accessory, this property’s value is an empty array.
This property is read-only. You can employ an
AudioSessionPropertyListenercallback function to listen for changes in this property’s value.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OutputDestinationsA
CFArrayRefobject containing details on the available audio output destinations in a USB audio accessory attached through the iPad camera connection kit.Each element of the array contains a
CFDictionaryRefobject with the keys and corresponding values described in “USB Accessory Audio Destination Dictionary Keys.”If there is no audio output destination available from the attached accessory, this property’s value is an empty array.
This property is read-only. You can employ an
AudioSessionPropertyListenercallback function to listen for changes in this property’s value.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_InputSourceA read/write
CFNumberRefobject that indicates the audio input source, from a USB audio accessory attached through the iPad camera connection kit, that you want to use.The value must be one of the identifiers provided as a
kAudioSession_InputSourceKey_IDkey as part of thekAudioSessionProperty_InputSourcesarray.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_OutputDestinationA read/write
CFNumberRefobject that indicates the audio output destination, from a USB audio accessory attached through the iPad camera connection kit, that you want to use.The value must be one of the identifiers provided as a
kAudioSession_OutputDestinationKey_IDkey as part of thekAudioSessionProperty_OutputDestinationsarray.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_InputGainAvailableA read-only
UInt32value that indicates whether or not audio input gain adjustment is available, where a nonzero value means adjustment is available.Note: Some audio inputs on some devices do not support gain adjustment. You must check this property’s value before attempting to set audio input gain.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_InputGainScalarA read/write
Float32value that indicates the audio input gain setting for the active input source.The range for this value is
[0.0, 1.0], as follows:0indicates the lowest audio input gain setting1indicates the highest audio input gain setting
Attempting to set a value outside this range results in the value being clamped to this range. This property’s value is valid only if audio input gain is available (see
kAudioSessionProperty_InputGainAvailable).If no app with an active audio session is using this property for a given input source, the system restores the default input gain setting for the input source.
You can employ an
AudioSessionPropertyListenercallback function to listen for changes in this property’s value.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionProperty_AudioRouteDescriptionA read-only
CFDictionaryRefobject containing information about an audio route.The dictionary contains the keys and corresponding values described in “Audio Route Description Dictionary Keys.”
Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Discussion
Use these property identifiers in concert with the AudioSessionGetProperty, AudioSessionSetProperty, and AudioSessionAddPropertyListener functions.
Audio Session Categories
Category identifiers for audio sessions, used as values for the kAudioSessionProperty_AudioCategory property.
enum {
kAudioSessionCategory_AmbientSound = 'ambi',
kAudioSessionCategory_SoloAmbientSound = 'solo',
kAudioSessionCategory_MediaPlayback = 'medi',
kAudioSessionCategory_RecordAudio = 'reca',
kAudioSessionCategory_PlayAndRecord = 'plar',
kAudioSessionCategory_AudioProcessing = 'proc'
};
Constants
kAudioSessionCategory_AmbientSoundFor an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.
This category is appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
This category is equivalent to the
AVAudioSessionCategoryAmbientcategory provided in the AV Foundation framework.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionCategory_SoloAmbientSoundThe default category, used unless you set a category with the
AudioSessionSetPropertyfunction.When you use this category, audio from other apps is silenced. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
This category is equivalent to the
AVAudioSessionCategorySoloAmbientcategory provided in the AV Foundation framework.Available in iOS 2.2 and later.
Declared in
AudioSession.h.kAudioSessionCategory_MediaPlaybackFor playing recorded music or other sounds that are central to the successful use of your app.
When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.)
This category normally prevents audio from other apps from mixing with your app's audio. To allow mixing for this category, use the
kAudioSessionProperty_OverrideCategoryMixWithOthersproperty.This category is equivalent to the
AVAudioSessionCategoryPlaybackcategory provided in the AV Foundation framework.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionCategory_RecordAudioFor recording audio; this category silences playback audio. Recording continues with the screen locked.
This category is equivalent to the
AVAudioSessionCategoryRecordcategory provided in the AV Foundation framework.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionCategory_PlayAndRecordAllows recording (input) and playback (output) of audio, such as for a VOIP (voice over IP) app.
Your audio continues with the Silent switch set to silent and with the screen locked. (The switch is called the Ring/Silent switch on iPhone.)
This category is appropriate for simultaneous recording and playback, and also for apps that record and play back but not simultaneously. If you want to ensure that sounds such as Messages alerts do not play while your app is recording, use the
kAudioSessionCategory_RecordAudiocategory instead.This category normally prevents audio from other apps from mixing with your app's audio. To allow mixing when using this category, use the
kAudioSessionProperty_OverrideCategoryMixWithOthersproperty.This category is equivalent to the
AVAudioSessionCategoryPlayAndRecordcategory provided in the AV Foundation framework.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionCategory_AudioProcessingFor using an audio hardware codec or signal processor while not playing or recording audio. Use this category, for example, when performing offline audio format conversion.
This category disables playback (audio output) and disables recording (audio input).
Audio processing does not normally continue when your app is in the background. However, when your app moves to the background, you can request additional time to complete processing. for more information, see “Understanding an Application’s States and Transitions” in iOS App Programming Guide.
This category is equivalent to the
AVAudioSessionCategoryAudioProcessingcategory provided in the AV Foundation framework.Available in iOS 3.1 and later.
Declared in
AudioSession.h.
Discussion
Each app running in iOS has a single audio session, which in turn has a single category. You can change your audio session’s category while your app is running.
You can refine the configuration provided by the kAudioSessionCategory_RecordAudio and kAudioSessionCategory_PlayAndRecord categories by using an audio session mode, as described in “Audio Session Modes.”
Use the kAudioSessionCategory_AmbientSound category when you want your sounds to mix with other audio (such as from the iPod app). Use one of the other playback categories when you want audio from other apps to be silenced when your session is active. However, you can enable mixing for the kAudioSessionCategory_MediaPlayback and kAudioSessionCategory_PlayAndRecord categories by using the kAudioSessionProperty_OverrideCategoryMixWithOthers property. For more information on audio session categories, see Audio Session Programming Guide.
Audio Session Modes
Mode identifiers for audio sessions, used as values for the kAudioSessionProperty_Mode property.
enum {
kAudioSessionMode_Default = 'dflt',
kAudioSessionMode_VoiceChat = 'vcct',
kAudioSessionMode_VideoRecording = 'vrcd',
kAudioSessionMode_Measurement = 'msmt'
};
Constants
kAudioSessionMode_DefaultThe default mode; used unless you set a mode with the
AudioSessionSetPropertyfunction.When this mode is in use, audio session behavior matches that of iOS versions prior to iOS 5.0. You can use this mode with every audio session category. On devices with more than one built-in microphone, the primary microphone is used.
This mode is equivalent to the
AVAudioSessionModeDefaultmode provided in the AV Foundation framework.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionMode_VoiceChatSpecify this mode if your app is performing two-way voice communication, such as using Voice over Internet Protocol (VoIP).
When this mode is in use, the device’s tonal equalization is optimized for voice. For use with the
kAudioSessionCategory_PlayAndRecordaudio session category. On devices with more than one built-in microphone, the primary microphone is used.Using this mode has the side effect of setting the
kAudioSessionProperty_OverrideCategoryEnableBluetoothInputcategory override toTRUE.This mode is equivalent to the
AVAudioSessionModeVoiceChatmode provided in the AV Foundation framework.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionMode_VideoRecordingSpecify this mode if your app is recording a movie.
For use with the
kAudioSessionCategory_RecordAudioaudio session category. Also works with thekAudioSessionCategory_PlayAndRecordcategory. On devices with more than one built-in microphone, the microphone closest to the video camera is used.Using this mode may result in the system providing appropriate audio signal processing.
This mode is equivalent to the
AVAudioSessionModeVideoRecordingmode provided in the AV Foundation framework.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionMode_MeasurementSpecify this mode if your app is performing measurement of incoming audio.
When this mode is in use, the device does not perform automatic gain adjustment on incoming audio. For use with the
kAudioSessionCategory_RecordAudioorkAudioSessionCategory_PlayAndRecordaudio session categories. On devices with more than one built-in microphone, the primary microphone is used.This mode is equivalent to the
AVAudioSessionModeMeasurementmode provided in the AV Foundation framework.Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Discussion
Each app running in iOS has a single audio session, which in turn has a single mode. A mode refines the device’s audio configuration according to the purpose of the mode. You can change your audio session’s mode only when your audio session is inactive, and only if your audio session category is configured to disallow mixing with other apps.
Audio Route Change Reasons
Identifiers for the various reasons that an audio route can change while your app is running.
enum {
kAudioSessionRouteChangeReason_Unknown = 0,
kAudioSessionRouteChangeReason_NewDeviceAvailable = 1,
kAudioSessionRouteChangeReason_OldDeviceUnavailable = 2,
kAudioSessionRouteChangeReason_CategoryChange = 3,
kAudioSessionRouteChangeReason_Override = 4,
// this enum has no constant with a value of 5
kAudioSessionRouteChangeReason_WakeFromSleep = 6,
kAudioSessionRouteChangeReason_NoSuitableRouteForCategory = 7
};
Constants
kAudioSessionRouteChangeReason_UnknownThe audio route changed but the reason is not known.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_NewDeviceAvailableA new audio hardware device became available; for example, a headset was plugged in.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_OldDeviceUnavailableThe previously-used audio hardware device is now unavailable; for example, a headset was unplugged.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_CategoryChangeThe audio session category has changed.
Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_OverrideThe audio route has been overridden. For example, while using the
kAudioSessionCategory_PlayAndRecordcategory, output audio has been redirected to the speaker using thekAudioSessionProperty_OverrideAudioRouteproperty.Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_WakeFromSleepThe device woke from sleep.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionRouteChangeReason_NoSuitableRouteForCategoryThere is no audio hardware route for the audio session category; for instance, the
kAudioSessionCategory_RecordAudiois set but there is no audio input device.Available in iOS 3.1 and later.
Declared in
AudioSession.h.
Discussion
You encounter these identifiers as values in the CFDictionaryRef object passed to your property listener callback function when it is listening for audio route changes. See the description for kAudioSessionProperty_AudioRouteChange.
Audio Session Category Route Overrides
Specifies whether the default audio route for the PlayAndRecord category should be overridden.
enum {
kAudioSessionOverrideAudioRoute_None = 0,
kAudioSessionOverrideAudioRoute_Speaker = 'spkr'
};
Constants
kAudioSessionOverrideAudioRoute_NoneSpecifies, for the
kAudioSessionCategory_PlayAndRecordcategory, that output audio should go to the receiver. This is the default output audio route for this category.Available in iOS 2.1 and later.
Declared in
AudioSession.h.kAudioSessionOverrideAudioRoute_SpeakerSpecifies, for the
kAudioSessionCategory_PlayAndRecordcategory, that output audio should go to the speaker, not the receiver.Available in iOS 2.1 and later.
Declared in
AudioSession.h.
Discussion
The kAudioSessionCategory_PlayAndRecord category supports simultaneous input and output. You could use this category, for example, to add an effect to audio coming into the device’s microphone. By default, output audio for this category goes to the receiver—the speaker you hold to your ear when on a phone call. The kAudioSessionOverrideAudioRoute_Speaker constant lets you direct the output audio to the speaker situated at the bottom of the phone.
Audio Session Activation Flags
Flags that provide additional information about your app’s audio intentions upon session activation or deactivation.
enum {
kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation = (1 << 0) // 0x01
};
Constants
kAudioSessionSetActiveFlag_NotifyOthersOnDeactivationIndicates that when your audio session deactivates, other audio sessions that had been interrupted by your session can return to their active state.
Used only when deactivating your audio session.
Available in iOS 4.0 and later.
Declared in
AudioSession.h.
Audio Session Interruption Types
Identifiers that serve as values for the kAudioSessionProperty_InterruptionType property to indicate the nature of an interruption that has just ended.
enum {
kAudioSessionInterruptionType_ShouldResume = 'irsm',
kAudioSessionInterruptionType_ShouldNotResume = '!rsm'
};
typedef UInt32 AudioSessionInterruptionType;
Constants
kAudioSessionInterruptionType_ShouldResumeIndicates that the interruption that has just ended was one for which it is appropriate to immediately resume playback; for example, an incoming phone call was rejected by the user.
Available in iOS 4.0 and later.
Declared in
AudioSession.h.kAudioSessionInterruptionType_ShouldNotResumeIndicates that the interruption that has just ended was one for which it is not appropriate to resume playback; for example, your app had been interrupted by iPod playback.
Available in iOS 4.0 and later.
Declared in
AudioSession.h.
Audio Session Interruption States
Identifiers used with the AudioSessionInterruptionListener callback function in iOS to indicate that an audio interruption has started or stopped.
enum {
kAudioSessionBeginInterruption = 1,
kAudioSessionEndInterruption = 0
};
Constants
kAudioSessionBeginInterruptionYour app’s audio session has just been interrupted, such as by a phone call.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionEndInterruptionThe interruption to your app’s audio session has just ended. In the case where a user confirms the interruption, such as answering a phone call, your app will not receive this constant.
Available in iOS 2.0 and later.
Declared in
AudioSession.h.
Audio Route Description Dictionary Keys
Keys for the kAudioSessionProperty_AudioRouteDescription dictionary.
const CFStringRef kAudioSession_AudioRouteKey_Inputs; const CFStringRef kAudioSession_AudioRouteKey_Outputs;
Constants
kAudioSession_AudioRouteKey_InputsA
CFArrayRefobject containing details about audio input used in the current audio route.If there is an audio input available, the array contains a
CFDictionaryRefobject with a single key, namelykAudioSession_AudioRouteKey_Type, whose value is one of the constants in “Audio Input Routes.”If no audio input is available, the array is empty.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSession_AudioRouteKey_OutputsA
CFArrayRefobject containing details about the audio output used in the current audio route.If there is an audio output available, the array usually contains one
CFDictionaryRefobject with a single key, namelykAudioSession_AudioRouteKey_Type, whose value is one of the constants in “Audio Output Routes.”In certain circumstances, such as when a ringtone is being sent to the device speaker and to a connected headset, the array contains more than one dictionary.
If no audio output is available, the array is empty.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.
USB Accessory Audio Source Dictionary Keys
Keys for the dictionaries in the kAudioSessionProperty_InputSources array.
const CFStringRef kAudioSession_InputSourceKey_ID; const CFStringRef kAudioSession_InputSourceKey_Description;
Constants
kAudioSession_InputSourceKey_IDA
CFNumberRefobject, defined by a USB audio accessory attached to the device through the iPad camera connection kit, that identifies an audio input source. When setting a source on the accessory, use this identifier.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSession_InputSourceKey_DescriptionA
CFStringRefobject, defined by the accessory, that describes an audio input source and that is suitable for displaying in a user interface.Available in iOS 5.0 and later.
Declared in
AudioSession.h.
USB Accessory Audio Destination Dictionary Keys
Keys for the dictionaries in the kAudioSessionProperty_OutputDestinations array.
const CFStringRef kAudioSession_OutputDestinationKey_ID; const CFStringRef kAudioSession_OutputDestinationKey_Description;
Constants
kAudioSession_OutputDestinationKey_IDA
CFNumberRefobject, defined by a USB audio accessory attached to the device through the iPad camera connection kit, that identifies the output destination. When setting an audio output destination on the accessory, use this identifier. For possible values, see “Audio Output Routes.”Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSession_OutputDestinationKey_DescriptionA
CFStringRefobject, defined by the accessory, that describes the audio output destination and that is suitable for displaying in a user interface.Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Audio Route Type Key
The one key for an audio route input or output dictionary.
const CFStringRef kAudioSession_AudioRouteKey_Type;
Constants
kAudioSession_AudioRouteKey_TypeA
CFStringRefobject that serves as the one key for an audio routes input or output dictionary, whose value specifies an input source or output destination.Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Discussion
This key is used with the dictionaries associated with the kAudioSession_AudioRouteKey_Inputs and kAudioSession_AudioRouteKey_Outputs arrays.
Audio Input Routes
Strings that identify the various audio input sources for a device.
const CFStringRef kAudioSessionInputRoute_LineIn; const CFStringRef kAudioSessionInputRoute_BuiltInMic; const CFStringRef kAudioSessionInputRoute_HeadsetMic; const CFStringRef kAudioSessionInputRoute_BluetoothHFP; const CFStringRef kAudioSessionInputRoute_USBAudio;
Constants
kAudioSessionInputRoute_LineInA line in input
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionInputRoute_BuiltInMicA built-in microphone input.
Some early iOS devices do not have this input.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionInputRoute_HeadsetMicA microphone that is part of a headset.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionInputRoute_BluetoothHFPA microphone that is part of a Bluetooth Hands-Free Profile (HFP) device.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionInputRoute_USBAudioA Universal Serial Bus (USB) input, accessed through the device 30-pin connector.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Discussion
These strings are used as values for the kAudioSession_AudioRouteKey_Type key for the dictionary associated with the kAudioSession_AudioRouteKey_Inputs array.
Audio Output Routes
The various audio output destinations available for an iOS device.
const CFStringRef kAudioSessionOutputRoute_LineOut; const CFStringRef kAudioSessionOutputRoute_Headphones; const CFStringRef kAudioSessionOutputRoute_BluetoothHFP; const CFStringRef kAudioSessionOutputRoute_BluetoothA2DP; const CFStringRef kAudioSessionOutputRoute_BuiltInReceiver; const CFStringRef kAudioSessionOutputRoute_BuiltInSpeaker; const CFStringRef kAudioSessionOutputRoute_USBAudio; const CFStringRef kAudioSessionOutputRoute_HDMI; const CFStringRef kAudioSessionOutputRoute_AirPlay;
Constants
kAudioSessionOutputRoute_LineOutAnalog line-level output.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_HeadphonesSpeakers in headphones or in a headset.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_BluetoothHFPSpeakers that are part of a Bluetooth Hands-Free Profile (HFP) accessory.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_BluetoothA2DPSpeakers in a Bluetooth A2DP device.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_BuiltInReceiverThe built-in speaker you hold to your ear when on a phone call.
Some iOS devices do not have this output.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_BuiltInSpeakerThe primary built-in speaker.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_USBAudioSpeaker(s) in a Universal Serial Bus (USB) accessory, accessed through the device 30-pin connector.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_HDMIAn output available through the HDMI interface.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSessionOutputRoute_AirPlayAn output on an AirPlay device.
Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Discussion
These strings are used as values for the kAudioSession_AudioRouteKey_Type key for the dictionary associated with the kAudioSession_AudioRouteKey_Outputs array.
Audio Route Change Dictionary Keys
Keys for obtaining information about an audio hardware route change.
const CFStringRef kAudioSession_RouteChangeKey_Reason; const CFStringRef kAudioSession_AudioRouteChangeKey_PreviousRouteDescription; const CFStringRef kAudioSession_AudioRouteChangeKey_CurrentRouteDescription;
Constants
kAudioSession_RouteChangeKey_ReasonA
CFNumberRefobject that identifies the reason for the audio route change. See “Audio Route Change Reasons.”Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSession_AudioRouteChangeKey_PreviousRouteDescriptionA
CFDictionaryRefobject that describes the previous audio route. For specifics on the contents of this dictionary, seekAudioSession_AudioRouteKey_InputsandkAudioSession_AudioRouteKey_Outputs.Available in iOS 5.0 and later.
Declared in
AudioSession.h.kAudioSession_AudioRouteChangeKey_CurrentRouteDescriptionA
CFDictionaryRefobject that describes the current audio route. For specifics on the contents of this dictionary, seekAudioSession_AudioRouteKey_InputsandkAudioSession_AudioRouteKey_Outputs.Available in iOS 5.0 and later.
Declared in
AudioSession.h.
Alternative Audio Route Change Reason Dictionary Key
An alternate key for obtaining information about the reason for an audio route change.
#define kAudioSession_AudioRouteChangeKey_Reason "OutputDeviceDidChange_Reason"
Constants
kAudioSession_AudioRouteChangeKey_ReasonValue is a
CFNumberRefobject that identifies the reason for the audio route change. See “Audio Route Change Reasons.”Note: It is typically more convenient to instead use the
CFStringRefversion of this constant,kAudioSession_RouteChangeKey_Reason.Available in iOS 2.0 and later.
Declared in
AudioSession.h.
Discussion
Use these dictionary keys to obtain information, from the kAudioSessionProperty_AudioRouteChange property, about an audio hardware route change event.
Deprecated Audio Session Categories
Deprecated category identifiers for audio sessions. Do not use for new development.
enum {
kAudioSessionCategory_UserInterfaceSoundEffects = 'uifx',
kAudioSessionCategory_LiveAudio = 'live'
};
Constants
kAudioSessionCategory_UserInterfaceSoundEffectsFor sound effects such as touch feedback, explosions, and so on. (Deprecated. Equivalent to the
kAudioSessionCategory_AmbientSoundcategory, which you should use instead. ThekAudioSessionCategory_UserInterfaceSoundEffectscategory was deprecated in iOS 3.0.)Available in iOS 2.0 and later.
Declared in
AudioSession.h.kAudioSessionCategory_LiveAudioFor live performance of music, such as for an app that simulates a piano. (Deprecated. Equivalent to the
kAudioSessionCategory_MediaPlaybackcategory, which you should use instead. ThekAudioSessionCategory_LiveAudiocategory was deprecated in iOS 3.0.)Available in iOS 2.0 and later.
Declared in
AudioSession.h.
Deprecated Audio Route Change Dictionary Keys
Do not use these keys for new development.
#define kAudioSession_AudioRouteChangeKey_OldRoute "OutputDeviceDidChange_OldRoute"
Constants
OutputDeviceDidChange_OldRouteUsed in versions of iOS prior to iOS 5.0 to indicate the previous audio route. The value for this key is a
CFStringRefobject that names the previous audio hardware route (such as “Headphone” or “Speaker”). (Deprecated. Instead, use thekAudioSession_AudioRouteChangeKey_PreviousRouteDescriptiondictionary key.)
Deprecated Audio Session Properties
Do not use these properties for new development
enum {
kAudioSessionProperty_AudioRoute = 'rout'
};
Constants
ConstantThe name of the current audio route (such as “Headphone,” “Speaker,” and so on). A read-only
CFStringRefvalue. (Deprecated. Instead, Use thekAudioSessionProperty_AudioRouteDescriptionproperty.)
Result Codes
This table lists the result codes defined for Audio Session Services.
| Result Code | Value | Description |
|---|---|---|
kAudioSessionNoError | 0 | No error has occurred. Available in iOS 2.0 and later. |
kAudioSessionNotInitialized | '!ini' | An Audio Session function was called without first initializing the session. To avoid this error, call the Available in iOS 2.0 and later. |
kAudioSessionAlreadyInitialized | 'init' | The Available in iOS 2.0 and later. |
kAudioSessionInitializationError | ''ini?' | There was an error during audio session initialization. Available in iOS 2.0 and later. |
kAudioSessionUnsupportedPropertyError | 'pty?' | The audio session property is not supported. Available in iOS 2.0 and later. |
kAudioSessionBadPropertySizeError | '!siz' | The size of the audio session property data was not correct. Available in iOS 2.0 and later. |
kAudioSessionNotActiveError | '!act' | The audio operation failed because your application’s audio session was not active. Available in iOS 2.0 and later. |
kAudioServicesNoHardwareError | 'nohw' | The audio operation failed because the device has no audio input available. Available in iOS 3.0 and later. |
kAudioSessionNoCategorySet | '?cat' | The audio operation failed because it requires the audio session to have an explicitly-set category, but none was set. To use a hardware codec you must explicitly initialize the audio session and explicitly set an audio session category. Available in iOS 3.1 and later. |
kAudioSessionIncompatibleCategory | '!cat' | The specified audio session category cannot be used for the attempted audio operation. For example, you attempted to play or record audio with the audio session category set to Available in iOS 3.1 and later. |
kAudioSessionUnspecifiedError | 'what' | An unspecified audio session error has occurred. This typically results from the audio system being in an inconsistent state. Available in iOS 4.0 and later. |
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-10-25)