iPhone OS Reference Library Apple Developer Connection spyglass button

AVAudioSession Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AVFoundation.framework/
Availability
Available in iPhone OS 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 application. Use this class to:

Starting with iPhone OS 3.0, this class provides an Objective-C alternative to Audio Session Services, described in Audio Session Services Reference. For more information on audio sessions, see Audio Session Programming Guide.

Tasks

Instantiating an Audio Session

Specifying a Delegate

Managing an Audio Session

Working with Audio Hardware

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

category

The audio session category.

@property (readonly) NSString* category;
Discussion

An audio session has one of the categories listed in “Audio Session Categories.” The default category is AVAudioSessionCategorySoloAmbient.

Availability
See Also
Declared In
AVAudioSession.h

currentHardwareInputNumberOfChannels

The number of audio hardware input channels.

@property (readonly) NSInteger currentHardwareInputNumberOfChannels;
Discussion

To respond to a change in the number of audio hardware input channels, implement the currentHardwareInputNumberOfChannelsChanged: method.

Availability
Declared In
AVAudioSession.h

currentHardwareOutputNumberOfChannels

The number of audio hardware output channels.

@property (readonly) NSInteger currentHardwareOutputNumberOfChannels;
Discussion

To respond to a change in the number of audio hardware output channels, implement the currentHardwareOutputNumberOfChannelsChanged: method.

Availability
Declared In
AVAudioSession.h

currentHardwareSampleRate

The audio hardware sample rate, in hertz.

@property (readonly) double currentHardwareSampleRate;
Discussion

To respond to a change in the audio hardware sample rate, implement the currentHardwareSampleRateChanged: delegate method.

Availability
See Also
Declared In
AVAudioSession.h

delegate

Specifies the delegate object for the audio session.

@property (assign) id<AVAudioSessionDelegate> delegate;
Discussion

The delegate object must implement the protocol described in AVAudioSessionDelegate Protocol Reference.

Availability
Declared In
AVAudioSession.h

inputIsAvailable

A Boolean value that indicates whether audio input is available (YES), or not (NO).

@property (readonly) BOOL inputIsAvailable;
Discussion

Use this method on launch to determine whether the device your application is running on supports audio input. To respond to a change in the availability of audio input, implement the inputIsAvailableChanged: delegate method.

Availability
Declared In
AVAudioSession.h

preferredHardwareSampleRate

The preferred hardware sample rate, in hertz.

@property (readonly) double preferredHardwareSampleRate;
Availability
See Also
Declared In
AVAudioSession.h

preferredIOBufferDuration

The preferred I/O buffer duration, in seconds.

@property (readonly) NSTimeInterval preferredIOBufferDuration;
Availability
See Also
Declared In
AVAudioSession.h

Class Methods

sharedInstance

Returns the singleton audio session.

+ (id)sharedInstance

Availability
Related Sample Code
Declared In
AVAudioSession.h

Instance Methods

setActive:error:

Activates or deactivates the audio session.

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

Parameters
beActive

Use YES to activate the audio session or NO to deactivate it.

outError

Contains nil if the activation/deactivation call was successful; otherwise, contains an error code.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iPhone OS 3.0 and later.
Related Sample Code
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

Contains nil if the category was set; otherwise, contains an error code.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Related Sample Code
Declared In
AVAudioSession.h

setPreferredHardwareSampleRate:error:

Sets the preferred hardware sample rate for recording.

- (BOOL)setPreferredHardwareSampleRate:(double)sampleRate error:(NSError**)outError

Parameters
sampleRate

The hardware sample rate you want to use.

outError

On success, contains nil. On failure, contains an error code.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Declared In
AVAudioSession.h

setPreferredIOBufferDuration:error:

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

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

Parameters
duration

The I/O buffer duration, in seconds, that you want to use.

outError

On success, contains nil. On failure, contains an error code.

Return Value

Returns YES on success or NO on failure.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
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;
Constants
AVAudioSessionCategoryAmbient

For an application in which sound playback is nonprimary—your application can be used successfully with the sound turned off. This category is also appropriate for “play along” style applications, such as a virtual piano that a user plays over iPod audio. When you use this category, other audio, such as from the iPod application, mixes with your audio. Your audio is silenced by screen locking and by the Ring/Silent switch.

Available in iPhone OS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategorySoloAmbient

The default category; used unless you set a category with the setCategory:error: method. This category silences audio from other applications, such as the iPod. Your audio is silenced by screen locking and by the Ring/Silent switch.

Available in iPhone OS 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 application. This category silences audio from other applications, such as the iPod. You can, however, modify this category to allow mixing by using the kAudioSessionProperty_OverrideCategoryMixWithOthers property. Your audio continues with the Ring/Silent switch set to silent and with the screen locked.

Available in iPhone OS 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 iPhone OS 3.0 and later.

Declared in AVAudioSession.h.

AVAudioSessionCategoryPlayAndRecord

For recording and playback of audio—simultaneous or not—such as for a VOIP (voice over IP) application. This category silences audio from other applications, such as the iPod. You can, however, modify this category to allow mixing by using the kAudioSessionProperty_OverrideCategoryMixWithOthers property. Your audio continues with the Ring/Silent switch set to silent and with the screen locked.

Available in iPhone OS 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.

Available in iPhone OS 3.1 and later.

Declared in AVAudioSession.h.

Discussion

Each application running in iPhone OS has a single audio session, which can be active or inactive. You can change your audio session’s category while your program is running.

Use the AVAudioSessionCategoryAmbient category when you want your sounds to mix with other audio (such as from the iPod application). Use one of the other playback categories when you want iPod audio to be silenced when your session is active. For more information on audio session categories, see Audio Session Programming Guide.



Last updated: 2009-10-19

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