AVAudioSession Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AVFoundation.framework
Availability
Available in iOS 3.0 and later.
Companion guide
Declared in
AVAudioSession.h
Related sample code

Overview

An instance of the AVAudioSession class, called an audio session, is a singleton object that you employ to set the audio context for your app. Use this class to:

This class provides an Objective-C alternative to many features from the C-based Audio Session Services, described in Audio Session Services Reference. Certain audio session features, such as handling audio route changes and adjusting audio input gain, can be accessed only using Audio Session Services.

Starting in iOS 5.0, you can refine the device audio configuration provided by certain audio session categories by using an audio session mode. See “Audio Session Modes.”

Also starting in iOS 5.0, audio route information is dictionary based, giving you access to 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, and kAudioSessionProperty_OutputDestinations.

For more information on audio sessions, see Audio Session Programming Guide.

Tasks

Getting the Shared Audio Session

Managing an Audio Session

Working with Audio Channels

Accessing Latency and Gain Settings

Deprecated Properties and Methods

Properties

category

The audio session category. (read-only)

@property(readonly) NSString *category
Discussion

An audio session category is a key that identifies a set of audio behaviors for your app. An audio session has one of the categories listed in “Audio Session Categories.” The default category is AVAudioSessionCategorySoloAmbient. For more information, see Audio Session Programming Guide.

Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

categoryOptions

The options associated with the current category. (read-only)

@property(readonly) AVAudioSessionCategoryOptions categoryOptions
Discussion

Category options allow you to tweak your app’s audio behavior in small ways. For a list of options that may be specified in this property, see “AVAudioSessionCategoryOptions”.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

currentRoute

The current audio routing information. (read-only)

@property(readonly) AVAudioSessionRouteDescription *currentRoute
Discussion

The current audio route contains information about the input and output ports involved in the session. You can use the object in this property to retrieve those ports and get information about them.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputAvailable

A Boolean value indicating whether an audio input path is available. (read-only)

@property(readonly, getter=isInputAvailable) BOOL inputAvailable
Discussion

Use this property at launch time to determine whether the current device supports audio input. To respond to a change in the availability of audio input, register for the AVAudioSessionInputDidBecomeAvailableNotification and AVAudioSessionInputDidBecomeUnavailableNotification notifications.

This property contains the value YES if a path is available or NO if one is not.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputDataSource

The currently selected input data source. (read-only)

@property(readonly) AVAudioSessionDataSourceDescription *inputDataSource
Discussion

If there are no input data sources available, the value of this property will be nil. The value of this property must be an AVAudioSessionDataSourceDescription object from the inputDataSources property.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputDataSources

An array of AVAudioSessionDataSourceDescription objects represeting the available input data sources for the audio session. (read-only)

@property(readonly) NSArray *inputDataSources
Discussion

When there is an audio accessory that supports input data source selection, this property is populated with all of the available input data sources. This currently only applies to certain USB devices.

This property is also key-value observable so you can be informed when any changes are made to the array of input data sources.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputGain

The gain applied to inputs associated with the session. (read-only)

@property(readonly) float inputGain
Discussion

The value in this property is in the range 0.0 to 1.0, where 0.0 represents no additional gain and 1.0 represents the maximum possible gain.

You can observe changes to the value of this property using key-value observing.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputGainSettable

A Boolean value indicating whether the input gain can be set. (read-only)

@property(readonly, getter=isInputGainSettable) BOOL inputGainSettable
Discussion

Some inputs may not provide the ability to set the input gain, so it is important to check this property before attempting to set the input gain.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputLatency

The latency for audio input, measured in seconds. (read-only)

@property(readonly) NSTimeInterval inputLatency
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

inputNumberOfChannels

The number of audio input channels associated with the session. (read-only)

@property(readonly) NSInteger inputNumberOfChannels
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

IOBufferDuration

The I/O buffer duration (in seconds) that is currently in effect. (read-only)

@property(readonly) NSTimeInterval IOBufferDuration
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

mode

The audio session mode. (read-only)

@property(readonly) NSString *mode
Discussion

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. An audio session has one of the modes listed in “Audio Session Modes.” The default mode is AVAudioSessionModeDefault.

Availability
  • Available in iOS 5.0 and later.
Declared In
AVAudioSession.h

otherAudioPlaying

A Boolean value indicating whether another app is currently playing audio. (read-only)

@property(readonly, getter=isOtherAudioPlaying) BOOL otherAudioPlaying
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

outputDataSource

The currently selected output data source. (read-only)

@property(readonly) AVAudioSessionDataSourceDescription *outputDataSource
Discussion

If there are no output data sources available, the value of this property will be nil. The value of this property must be an AVAudioSessionDataSourceDescription object from the outputDataSources property.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

outputDataSources

An array of AVAudioSessionDataSourceDescription objects represeting the available output data sources for the audio session. (read-only)

@property(readonly) NSArray *outputDataSources
Discussion

When there is an audio accessory that supports output data source selection, this property is populated with all of the available output data sources. This currently only applies to certain USB devices.

This property is also key-value observable so you can be informed when any changes are made to the array of output data sources.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

outputLatency

The latency for audio output, measured in seconds. (read-only)

@property(readonly) NSTimeInterval outputLatency
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

outputNumberOfChannels

The number of audio output channels. (read-only)

@property(readonly) NSInteger outputNumberOfChannels
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

outputVolume

The current output volume for your app’s audio content. (read-only)

@property(readonly) float outputVolume
Discussion

This value is in the range 0.0 to 1.0, with 0.0 representing no sound output and 1.0 representing the maximum volume.

You can observe changes to the value of this property using key-value observing.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

preferredIOBufferDuration

The preferred I/O buffer duration, in seconds. (read-only)

@property(readonly) NSTimeInterval preferredIOBufferDuration
Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

preferredSampleRate

The preferred sample rate, in hertz (read-only)

@property(readonly) double preferredSampleRate
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

sampleRate

The audio sample rate (in hertz) that is currently in effect. (read-only)

@property(readonly) double sampleRate
Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

Class Methods

sharedInstance

Returns the singleton audio session.

+ (id)sharedInstance
Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

Instance Methods

overrideOutputAudioPort:error:

Changes the current output route for audio.

- (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride error:(NSError **)outError
Parameters
portOverride

The override option for output. For a list of constants, see “AVAudioSessionPortOverride”.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

YES if the new routing option was set successfully or NO if it was not.

Discussion

You can use this method to route audio temporarily away from the current outputs and to a different destination. Similarly, you can use this method to remove any overrides and return to the original outputs. Currently, you can use this method to reroute audio to the device’s built-in speaker.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setActive:error:

Activates or deactivates your app’s audio session.

- (BOOL)setActive:(BOOL)beActive error:(NSError **)outError
Parameters
beActive

Use YES to activate your app’s audio session or NO to deactivate it.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

Returns YES on success or NO on failure.

Discussion

If another app’s active audio session has higher priority than your app, and that other audio session does not allow mixing with other apps, attempting to activate your audio session may fail.

Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

setActive:withOptions:error:

Activates or deactivates your app’s audio session using the specified options.

- (BOOL)setActive:(BOOL)active withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError **)outError
Parameters
active

Specify YES to activate your app’s audio session or NO to deactivate it.

options

A bitmapped value containing one or more constants from the “AVAudioSessionSetActiveOptions” enumeration.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

YES if the session’s active state was changed successfully or NO if it was not.

Discussion

If another app’s active audio session has higher priority than your app, and that other audio session does not allow mixing with other apps, attempting to activate your audio session may fail.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setCategory:error:

Sets the audio session category.

- (BOOL)setCategory:(NSString *)theCategory error:(NSError **)outError
Parameters
theCategory

The audio session category you want to apply to the audio session. See “Audio Session Categories.”

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

setCategory:withOptions:error:

Sets the audio session category with the specified options.

- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError
Parameters
category

The audio session category you want to apply to the audio session. For a list of values, see “Audio Session Categories.”

options

The additional options for handling audio. For a list of constants, see “AVAudioSessionCategoryOptions”

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

YES if the category and options were set successfully or NO if they were not.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setInputDataSource:error:

Sets the input data source for an audio session.

- (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource error:(NSError **)outError
Parameters
dataSource

The data source for the audio session’s input.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in NULL.

Return Value

YES if the input data source for the audio session was successfully assigned; otherwise NO.

Discussion

The AVAudioSessionDataSourceDescription object that is passed in as the dataSource will be assigned to the inputDataSource property if this method was successful. Only objects from the inputDataSources property may be passed in for the dataSource parameter.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setInputGain:error:

Changes the input gain to the specified value.

- (BOOL)setInputGain:(float)gain error:(NSError **)outError
Parameters
gain

The new gain value, which must be in the range 0.0 to 1.0, where 0.0 represents no additional gain and 1.0 represents the maximum possible gain.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

YES if the new gain value was set successfully or NO if it was not.

Discussion

Before calling this method, you should check the value in the inputGainSettable property to make sure the input gain level is settable for the current inputs.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setMode:error:

Sets the audio session mode.

- (BOOL)setMode:(NSString *)theMode error:(NSError **)outError
Parameters
theMode

The audio session mode you want to apply to the audio session. See “Audio Session Modes.”

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iOS 5.0 and later.
Declared In
AVAudioSession.h

setOutputDataSource:error:

Sets the output data source for an audio session.

- (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource error:(NSError **)outError
Parameters
dataSource

The data source for the audio session’s output.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in NULL.

Return Value

YES if the output data source for the audio session was successfully assigned; otherwise NO.

Discussion

The AVAudioSessionDataSourceDescription object that is passed in as the dataSource will be assigned to the outputDataSource property if this method was successful. Only objects from the outputDataSources property may be passed in for the dataSource parameter.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

setPreferredIOBufferDuration:error:

Sets the preferred audio I/O buffer duration, in seconds.

- (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration error:(NSError **)outError
Parameters
duration

The audio I/O buffer duration, in seconds, that you want to use. The available range for I/O buffer duration is 0.005 through 0.093 s, corresponding to a range of 256 through 4,096 sample frames at 44.1 kHz.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

Returns YES on success or NO on failure.

Discussion

The audio I/O buffer duration is the number of seconds for a single audio input/output cycle. For example, with an I/O buffer duration of 0.005 s, on each audio I/O cycle:

  • You receive 0.005 s of audio if obtaining input.

  • You must provide 0.005 s of audio if providing output.

To obtain the actual I/O buffer duration, call the AudioSessionGetProperty function using the kAudioSessionProperty_CurrentHardwareIOBufferDuration key as its inID parameter.

For more information see “Querying and Using Audio Hardware Characteristics” in Audio Session Programming Guide.

Availability
  • Available in iOS 3.0 and later.
Declared In
AVAudioSession.h

setPreferredSampleRate:error:

Sets the preferred sample rate for input and output.

- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError
Parameters
sampleRate

The hardware sample rate you want to use. The available range for hardware sample rate is device dependent. It typically ranges from 8,000 through 48,000 hertz.

outError

On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value

YES if the preferred sample rate was accepted and set or NO if it was not.

Discussion

The audio hardware input and output sample rates are always equal to each other when input and output are both running. For more information, see “Querying and Using Audio Hardware Characteristics” in Audio Session Programming Guide.

Availability
  • Available in iOS 6.0 and later.
Declared In
AVAudioSession.h

Constants

Audio Session Categories

Category identifiers for audio sessions, used as values for the setCategory:error: method.

NSString *const AVAudioSessionCategoryAmbient;
NSString *const AVAudioSessionCategorySoloAmbient;
NSString *const AVAudioSessionCategoryPlayback;
NSString *const AVAudioSessionCategoryRecord;
NSString *const AVAudioSessionCategoryPlayAndRecord;
NSString *const AVAudioSessionCategoryAudioProcessing;
NSString *const AVAudioSessionCategoryMultiRoute;
Constants
AVAudioSessionCategoryAmbient

For an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.

This category is also 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).

Available in iOS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategorySoloAmbient

The default category; used unless you set a category with the setCategory:error: method.

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).

Available in iOS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryPlayback

For 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.) To continue playing audio when your app transitions to the background (for example, when the screen locks), you need to add the audio value to the “UIBackgroundModes” in Information Property List Key Reference key in your information property list file.

This category normally prevents audio from other apps from mixing with your app's audio. To allow mixing for this category, use the kAudioSessionProperty_OverrideCategoryMixWithOthers property.

Available in iOS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryRecord

For recording audio; this category silences playback audio. Recording continues with the screen locked.

Available in iOS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryPlayAndRecord

Allows 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.) To continue playing audio when your app transitions to the background (for example, when the screen locks), you need to add the audio value to the “UIBackgroundModes” in Information Property List Key Reference key in your information property list file.

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 AVAudioSessionCategoryRecord category 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_OverrideCategoryMixWithOthers property.

Available in iOS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryAudioProcessing

For 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 iOS App Programming Guide.

Available in iOS 3.1 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryMultiRoute

Allows you to output distinct streams of audio data to different output devices at the same time. For example, you would use this category to route audio to both a USB device and a set of headphones. Use of this category requires a more detailed knowledge of, and interaction with, the capabilities of the available audio routes.

This category may be used for input, output, or both.

Available in iOS 6.0 and later.

Declared in AVAudioSession.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 AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord categories by using an audio session mode, as described in “Audio Session Modes.”

Use the AVAudioSessionCategoryAmbient 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 AVAudioSessionCategoryPlayback and AVAudioSessionCategoryPlayAndRecord categories by using the kAudioSessionProperty_OverrideCategoryMixWithOthers property. For more information on audio session categories, see Audio Session Programming Guide.

AVAudioSessionCategoryOptions

Constants that specify optional audio behaviors.

typedef NS_OPTIONS(NSUInteger,
   AVAudioSessionCategoryOptions)
{
   AVAudioSessionCategoryOptionMixWithOthers = 1,
   AVAudioSessionCategoryOptionDuckOthers = 2,
   AVAudioSessionCategoryOptionAllowBluetooth = 4,
   AVAudioSessionCategoryOptionDefaultToSpeaker = 8
};
Constants
AVAudioSessionCategoryOptionMixWithOthers

Allow audio in this session to be mixed with audio from other active sessions. This option is only valid if the session category is AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryPlayback.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryOptionDuckOthers

Allow audio in this session to duck under the audio for other active sessions. This option is only valid if the session category is AVAudioSessionCategoryPlayAndRecord.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryOptionAllowBluetooth

Allow audio in this session to be received from or sent to a Bluetooth device. This option is only valid if the session category is AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryOptionDefaultToSpeaker

Allow audio in this session to route to the built-in speaker by default. This option is only valid if the session category is AVAudioSessionCategoryPlayAndRecord.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

Audio Session Modes

Mode identifiers for audio sessions, used as values for the setMode:error: method.

NSString *const AVAudioSessionModeDefault;
NSString *const AVAudioSessionModeVoiceChat;
NSString *const AVAudioSessionModeGameChat
NSString *const AVAudioSessionModeVideoRecording;
NSString *const AVAudioSessionModeMeasurement;
NSString *const AVAudioSessionModeMoviePlayback;
Constants
AVAudioSessionModeDefault

The default mode; used unless you set a mode with the setMode:error: method.

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 more than one built-in microphone, the primary microphone is used.

Available in iOS 5.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionModeVoiceChat

Specify 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 AVAudioSessionCategoryPlayAndRecord audio 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_OverrideCategoryEnableBluetoothInput category override to TRUE.

Available in iOS 5.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionModeGameChat

Do not set this mode directly. This mode is set by Game Kit on behalf of an application that uses a GKVoiceChat object. This mode is valid only with the AVAudioSessionCategoryPlayAndRecord category.

If you need similar behavior and are not using a GKVoiceChat object, use AVAudioSessionModeVoiceChat instead.

Available in iOS 5.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionModeVideoRecording

Specify this mode if your app is recording a movie.

For use with the AVAudioSessionCategoryRecord audio session category. Also works with the AVAudioSessionCategoryPlayAndRecord category. 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.

Available in iOS 5.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionModeMeasurement

Specify 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 AVAudioSessionCategoryRecord or AVAudioSessionCategoryPlayAndRecord audio session categories. On devices with more than one built-in microphone, the primary microphone is used.

Available in iOS 5.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionModeMoviePlayback

Specify this mode if your app is playing back movie content.

When this mode is in use, the device engages any appropriate signal processing hardware to improve audio content played over the built-in speaker.

Available in iOS 6.0 and later.

Declared in AVAudioSession.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.

AVAudioSessionInterruptionOptions

Constant that indicates the state of the audio session following an interruption

typedef NS_OPTIONS(NSUInteger,
   AVAudioSessionInterruptionOptions)
{
   AVAudioSessionInterruptionOptionShouldResume = 1
};
Constants
AVAudioSessionInterruptionOptionShouldResume

Indicates that the audio session is active and immediately ready to be used. Your app can resume the audio operation that was interrupted.

Look for this flag in the flags parameter when your audio session delegate’s endInterruptionWithFlags: method is invoked.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionSetActiveOptions

A flag that provides additional information about your app’s audio intentions upon session activation or deactivation.

typedef NS_OPTIONS(NSUInteger,
   AVAudioSessionSetActiveOptions)
{
   AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation = 1
};
Constants
AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation

When passed in the flags parameter of the setActive:withFlags:error: instance method, indicates that when your audio session deactivates, other audio sessions that had been interrupted by your session can return to their active state.

This flag is used only when deactivating your audio session; that is, when you pass a value of NO in the beActive parameter of the setActive:withFlags:error: instance method.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionPortOverride

Constants to use when overriding the output audio port.

typedef NS_ENUM(NSUInteger,
   AVAudioSessionPortOverride)
{
   AVAudioSessionPortOverrideNone    = 0,
   AVAudioSessionPortOverrideSpeaker = 'spkr'
};
Constants
AVAudioSessionPortOverrideNone

Do not override the output audio port. Use this option to route audio to the intended outputs.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionPortOverrideSpeaker

Override the current outputs and route audio to the built-in speaker. Use this override only in conjunction with the AVAudioSessionCategoryPlayAndRecord category.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReason

Constants indicating the reason for an audio route change.

typedef NS_ENUM(NSUInteger,
   AVAudioSessionRouteChangeReason)
{
   AVAudioSessionRouteChangeReasonUnknown = 0,
   AVAudioSessionRouteChangeReasonNewDeviceAvailable = 1,
   AVAudioSessionRouteChangeReasonOldDeviceUnavailable = 2,
   AVAudioSessionRouteChangeReasonCategoryChange = 3,
   AVAudioSessionRouteChangeReasonOverride = 4,
   AVAudioSessionRouteChangeReasonWakeFromSleep = 6,
   AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory = 7
};
Constants
AVAudioSessionRouteChangeReasonUnknown

The reason for the change is unknown.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonNewDeviceAvailable

A preferred new audio output path is now available.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonOldDeviceUnavailable

The previous audio output path is no longer available.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonCategoryChange

The category of the session object changed.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonOverride

The output route was overridden by the app.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonWakeFromSleep

The route changed when the device woke up from sleep.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory

The route changed because no suitable route is now available for the specified category.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionInterruptionType

Constants that describe the state of the audio interruption.

typedef NS_ENUM(NSUInteger,
   AVAudioSessionInterruptionType)
{
   AVAudioSessionInterruptionTypeBegan = 1,
   AVAudioSessionInterruptionTypeEnded = 0,
};
Constants
AVAudioSessionInterruptionTypeBegan

The system interrupted the audio session.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionInterruptionTypeEnded

The interruption ended.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

Notification User Info Keys

Keys available in the user info dictionary of an interruption notification.

NSString *const AVAudioSessionInterruptionTypeKey;
NSString *const AVAudioSessionInterruptionOptionKey;
   
NSString *const AVAudioSessionRouteChangeReasonKey;
NSString *const AVAudioSessionRouteChangePreviousRouteKey;
Constants
AVAudioSessionInterruptionTypeKey

The value for this key is an NSNumber object containing an unsigned integer that identifies the type of interruption. For a list of possible values, see “AVAudioSessionInterruptionType”.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionInterruptionOptionKey

The value for this key is an NSNumber object containing an unsigned integer that identifies any options associated with the interruption. For a list of possible flags, see “AVAudioSessionInterruptionOptions”.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangeReasonKey

The value for this key is an NSNumber object containing an unsigned integer that identifies the reason why the route changed. For a list of possible values, see “AVAudioSessionRouteChangeReason”.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionRouteChangePreviousRouteKey

The value for this key is an AVAudioSessionRouteDescription object with the previous route information.

Available in iOS 6.0 and later.

Declared in AVAudioSession.h.

Interruption Flags

Constants that indicate the state of the audio session following an interruption. (Deprecated. Use the constants in “AVAudioSessionInterruptionOptions” instead.)

enum {
   AVAudioSessionInterruptionFlags_ShouldResume = 1
};
Constants
AVAudioSessionInterruptionFlags_ShouldResume

Indicates that your audio session is active and immediately ready to be used. Your app can resume the audio operation that was interrupted. (Deprecated. Use AVAudioSessionInterruptionOptionShouldResume instead.)

Look for this flag in the flags parameter when your audio session delegate’s endInterruptionWithFlags: method is invoked.

Available in iOS 4.0 and later.

Declared in AVAudioSession.h.

Activation Flags

Flags that provide additional information about your app’s audio intentions upon session activation or deactivation. (Deprecated. Use the constants in “AVAudioSessionSetActiveOptions” instead.)

enum {
   AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation = 1
};
Constants
AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation

When passed in the flags parameter of the setActive:withFlags:error: instance method, indicates that when your audio session deactivates, other audio sessions that had been interrupted by your session can return to their active state. (Deprecated. Use AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation instead.)

This flag is used only when deactivating your audio session; that is, when you pass a value of NO in the beActive parameter of the setActive:withFlags:error: instance method.

Available in iOS 4.0 and later.

Declared in AVAudioSession.h.

Notifications

AVAudioSessionDidBeginInterruptionNotification

Posted after an interruption in your audio session occurs.

This notification is posted on the main thread of your app. There is no userInfo dictionary.

AVAudioSessionDidEndInterruptionNotification

Posted after an interruption in your audio session ends.

This notification is posted on the main thread of your app. There is no userInfo dictionary.

AVAudioSessionInputDidBecomeAvailableNotification

Posted when an input to the audio session becomes available.

This notification is posted on the main thread of your app. There is no userInfo dictionary.

AVAudioSessionInputDidBecomeUnavailableNotification

Posted when an input to the audio session becomes unavailable.

This notification is posted on the main thread of your app. There is no userInfo dictionary.

AVAudioSessionInterruptionNotification

Posted when an audio interruption occurs.

The userInfo dictionary of this notification contains the AVAudioSessionInterruptionTypeKey. If the interruption type is AVAudioSessionInterruptionTypeEnded, this dictionary also contains the AVAudioSessionInterruptionOptionKey key. This notification is posted on the main thread of your app.

Availability
Declared In
AVAudioSession.h

AVAudioSessionRouteChangeNotification

Posted when the audio route for the session changes.

The userInfo dictionary of this notification contains the AVAudioSessionRouteChangeReasonKey and AVAudioSessionRouteChangePreviousRouteKey keys, which provide information about the route change.

Availability
Declared In
AVAudioSession.h

AVAudioSessionMediaServicesWereResetNotification

Posted when the media server restarts.

Use this notification as a cue to reinitialize any audio players and recorders used by your app, as well as reappply your AVAudioSession state. This notification is posted on the main thread of your app. There is no userInfo dictionary.

Availability
Declared In
AVAudioSession.h

Did this document help you? Yes It's good, but... Not helpful...