Class: AVAudioSession

Inherits:
NSObject show all

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:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Instance Attribute Details

- (String) category (readonly)

The audio session category. (read-only) 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.

Returns:

- (AVAudioSessionCategoryOptions) categoryOptions (readonly)

The options associated with the current category. (read-only) 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”.

Returns:

  • (AVAudioSessionCategoryOptions)

- (AVAudioSessionRouteDescription) currentRoute (readonly)

The current audio routing information. (read-only) 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.

- (Boolean) inputAvailable (readonly)

A Boolean value indicating whether an audio input path is available. (read-only) 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.

Returns:

  • (Boolean)

- (AVAudioSessionDataSourceDescription) inputDataSource (readonly)

The currently selected output data source. (read-only) 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.

- (Array) inputDataSources (readonly)

An array of AVAudioSessionDataSourceDescription objects represeting the available input data sources for the audio session. (read-only) This property is populated with all of the available input data sources by the system. This property is also key-value observable so you can be informed when any changes are made to the array of input data sources.

Returns:

- (Float) inputGain (readonly)

The gain applied to inputs associated with the session. (read-only) 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.

Returns:

- (Boolean) inputGainSettable (readonly)

A Boolean value indicating whether the input gain can be set. (read-only) 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.

Returns:

  • (Boolean)

- (NSTimeInterval) inputLatency (readonly)

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

Returns:

  • (NSTimeInterval)

- (Integer) inputNumberOfChannels (readonly)

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

Returns:

- (NSTimeInterval) IOBufferDuration (readonly)

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

Returns:

  • (NSTimeInterval)

- (String) mode (readonly)

The audio session mode. (read-only) 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.

Returns:

- (Boolean) otherAudioPlaying (readonly)

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

Returns:

  • (Boolean)

- (Array) outputDataSources (readonly)

An array of AVAudioSessionDataSourceDescription objects represeting the available output data sources for the audio session. (read-only) This property is populated with all of the available output data sources by the system. This property is also key-value observable so you can be informed when any changes are made to the array of output data sources.

Returns:

- (NSTimeInterval) outputLatency (readonly)

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

Returns:

  • (NSTimeInterval)

- (Integer) outputNumberOfChannels (readonly)

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

Returns:

- (Float) outputVolume (readonly)

The current output volume for your app’s audio content. (read-only) 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.

Returns:

- (NSTimeInterval) preferredIOBufferDuration (readonly)

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

Returns:

  • (NSTimeInterval)

- (Float) preferredSampleRate (readonly)

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

Returns:

- (Float) sampleRate (readonly)

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

Returns:

Class Method Details

+ (Object) sharedInstance

Returns the singleton audio session.

Returns:

Instance Method Details

- (Boolean) overrideOutputAudioPort(portOverride, error:outError)

Changes the current output route for audio. 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.

Parameters:

  • portOverride (Object)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setActive(beActive, error:outError)

Activates or deactivates your app’s audio session. 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.

Parameters:

  • beActive (Boolean)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

    Returns YES on success or NO on failure.

- (Boolean) setActive(active, withOptions:options, error:outError)

Activates or deactivates your app’s audio session using the specified options. 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.

Parameters:

  • active (Boolean)

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

  • options (AVAudioSessionSetActiveOptions)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setCategory(theCategory, error:outError)

Sets the audio session category.

Parameters:

  • theCategory (String)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

    Returns YES on success or NO on failure.

- (Boolean) setCategory(category, withOptions:options, error:outError)

Sets the audio session category with the specified options.

Parameters:

  • category (String)

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

  • options (AVAudioSessionCategoryOptions)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setInputDataSource(dataSource, error:outError)

Sets the input data source for an audio session. 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.

Parameters:

  • dataSource (AVAudioSessionDataSourceDescription)

    The data source for the audio session’s input.

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setInputGain(gain, error:outError)

Changes the input gain to the specified value. 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.

Parameters:

  • gain (Float)

    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 (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setMode(theMode, error:outError)

Sets the audio session mode.

Parameters:

  • theMode (String)

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

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

    Returns YES on success or NO on failure.

- (Boolean) setOutputDataSource(dataSource, error:outError)

Sets the output data source for an audio session. 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.

Parameters:

  • dataSource (AVAudioSessionDataSourceDescription)

    The data source for the audio session’s output.

  • outError (Pointer)

    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.

Returns:

  • (Boolean)

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

- (Boolean) setPreferredIOBufferDuration(duration, error:outError)

Sets the preferred audio I/O buffer duration, in seconds. 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.

Parameters:

  • duration (NSTimeInterval)

    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 (Pointer)

    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.

Returns:

  • (Boolean)

    Returns YES on success or NO on failure.

- (Boolean) setPreferredSampleRate(sampleRate, error:outError)

Sets the preferred sample rate for input and output. 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.

Parameters:

  • sampleRate (Float)

    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 (Pointer)

    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.

Returns:

  • (Boolean)

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