Class YSKAudioSessionHandler

Configures the app's audio session for recording and playing audio.

An audio session is a singleton object. The audio session configures the app's audio content and allows it to interact with other apps that use audio. The app uses a single object of the audio session, so the audio session is configured and activated outside the realm of the SpeechKit library components. The audio session's object can be obtained using the sharedInstance method. The YSKAudioSessionHandler class makes it easier to configure the audio session:
  • If your app uses an audio session only when working with the SpeechKit library, we recommend using this class to configure the audio session.

  • If your app uses the audio session outside the library (for audio and video playback, audio recording, and so on), you can also use this class or configure the audio session independently.

To configure the necessary settings and activate the audio session, call the activateAudioSession: or activateAudioSessionWithSettings:error: method before working with an audio (that is, before creating an audio source, if it uses the standard device input, or before any other operations with the audio that will be performed in your app). This method will specify the settings and call the -[AVAudioSession setActive:error:] system class method. Activation of an audio session takes quite a long time and is performed synchronously. If an error occurs during configuration and activation, it will be returned in the error parameter. When operations with the audio are completed, the audio session should be deactivated using the deactivateAudioSession: method.
Note.

If you are configuring an audio session yourself, to enable the SpeechKit library, set in the -[AVAudioSession setCategory:withOptions:error:] system class method one of the following categories: AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryRecord, or AVAudioSessionCategoryMultiRoute.

Instance Methods

-activateAudioSession:

Configures and activates an audio session.

-activateAudioSessionWithSettings:error:

Sets the passed parameters, configures and activates an audio session.

-deactivateAudioSession:

Stops the audio session.

Class Methods

+sharedInstance

Returns a shared object of the YSKAudioSessionHandler class.

Properties

settings

Audio session external settings.

Method Detail

-activateAudioSession:

- (BOOL)activateAudioSession:(NSError *_Nullable *)error 

Configures and activates an audio session.

This method should be called before starting operations with an audio (recognition, voice activation, or playback of synthesized speech). It is executed synchronously and may take quite a long time.
Parameters:
error

An error that occurred during configuration and activation of an audio session.

Returns:

YES if the audio session's configuration and activation were successful, NO otherwise.

-activateAudioSessionWithSettings:error:

- (BOOL)activateAudioSessionWithSettings:(YSKAudioSessionSettings *)settings
                                   error:(NSError *_Nullable *)error 

Sets the passed parameters, configures and activates an audio session.

This method should be called before starting operations with an audio (recognition, voice activation, or playback of synthesized speech). It is executed synchronously and may take quite a long time.
Parameters:
settings

Audio session external parameters.

error

An error that occurred during configuration and activation of an audio session.

Returns:

YES if the audio session's configuration and activation were successful, NO otherwise.

-deactivateAudioSession:

- (BOOL)deactivateAudioSession:(NSError *_Nullable *)error 

Stops the audio session.

Parameters:
error

An error that occurred during deactivation of an audio session.

Returns:

YES if the audio session deactivated successfully, NO otherwise.

+sharedInstance

+ (instancetype)sharedInstance

Returns a shared object of the YSKAudioSessionHandler class.

Returns:

Shared object of the YSKAudioSessionHandler class.