Interface (AudioSessionManager)

This interface implements audio session management.

Before calling any API in AudioSessionManager, you must use getSessionManager to obtain an AudioSessionManager instance.

NOTE

  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The initial APIs of this interface are supported since API version 12.

Modules to Import

import { audio } from '@kit.AudioKit';

activateAudioSession12+

activateAudioSession(strategy: AudioSessionStrategy): Promise<void>

Activates an audio session. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
strategy AudioSessionStrategy Yes Audio session strategy.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types.
6800101 Parameter verification failed.
6800301 System error. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let strategy: audio.AudioSessionStrategy = {
  concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS
};

audioSessionManager.activateAudioSession(strategy).then(() => {
  console.info('activateAudioSession SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`ERROR: ${err}`);
});

deactivateAudioSession12+

deactivateAudioSession(): Promise<void>

Deactivates this audio session. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 System error. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioSessionManager.deactivateAudioSession().then(() => {
  console.info('deactivateAudioSession SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`ERROR: ${err}`);
});

isAudioSessionActivated12+

isAudioSessionActivated(): boolean

Checks whether this audio session is activated.

System capability: SystemCapability.Multimedia.Audio.Core

Return value

Type Description
boolean Check result for whether the audio session is activated. true if activated, false otherwise.

Example

let isActivated = audioSessionManager.isAudioSessionActivated();

on('audioSessionDeactivated')12+

on(type: 'audioSessionDeactivated', callback: Callback<AudioSessionDeactivatedEvent>): void

Subscribes to the audio session deactivation event, which is triggered when an audio session is deactivated. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'audioSessionDeactivated' is triggered when the audio session is deactivated.
callback Callback<AudioSessionDeactivatedEvent> Yes Callback used to return the reason why the audio session is deactivated.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types.
6800101 Parameter verification failed.

Example

audioSessionManager.on('audioSessionDeactivated',
  (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => {
  console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `);
});

off('audioSessionDeactivated')12+

off(type: 'audioSessionDeactivated', callback?: Callback<AudioSessionDeactivatedEvent>): void

Unsubscribes from the audio session deactivation event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'audioSessionDeactivated' is triggered when the audio session is deactivated.
callback Callback<AudioSessionDeactivatedEvent> No Callback used to return the reason why the audio session is deactivated.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101 Parameter verification failed.

Example

// Cancel all subscriptions to the event.
audioSessionManager.off('audioSessionDeactivated');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let audioSessionDeactivatedCallback = (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => {
  console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `);
};

audioSessionManager.on('audioSessionDeactivated', audioSessionDeactivatedCallback);

audioSessionManager.off('audioSessionDeactivated', audioSessionDeactivatedCallback);

setAudioSessionScene20+

setAudioSessionScene(scene: AudioSessionScene): void

Sets an audio session scene.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
scene AudioSessionScene Yes Audio session scene.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800103 Operation not permit at current state.
6800301 Audio client call audio service error, System error.

Example

audioSessionManager.setAudioSessionScene(audio.AudioSessionScene.AUDIO_SESSION_SCENE_MEDIA);

on('audioSessionStateChanged')20+

on(type: 'audioSessionStateChanged', callback: Callback<AudioSessionStateChangedEvent>): void

Subscribes to the audio session state change event, which is triggered when the audio session focus is changed. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'audioSessionStateChanged' is triggered when the audio session state is changed.
callback Callback<AudioSessionStateChangedEvent> Yes Callback used to return the audio session change information.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800102 Allocate memory failed.
6800301 Audio client call audio service error, System error.

Example

audioSessionManager.on('audioSessionStateChanged', (audioSessionStateChangedEvent: audio.AudioSessionStateChangedEvent) => {
  console.info(`hint of audioSessionStateChanged: ${audioSessionStateChangedEvent.stateChangeHint} `);
});

off('audioSessionStateChanged')20+

off(type: 'audioSessionStateChanged', callback?: Callback<AudioSessionStateChangedEvent>): void

Unsubscribes from the audio session state change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'audioSessionStateChanged' is triggered when the audio session state is changed.
callback Callback<AudioSessionStateChangedEvent> No Callback used to return the audio session change information.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

// Cancel all subscriptions to the event.
audioSessionManager.off('audioSessionStateChanged');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let audioSessionStateChangedCallback = (audioSessionStateChangedEvent: audio.AudioSessionStateChangedEvent) => {
  console.info(`hint of audioSessionStateChanged: ${audioSessionStateChangedEvent.stateChangeHint} `);
};

audioSessionManager.on('audioSessionStateChanged', audioSessionStateChangedCallback);

audioSessionManager.off('audioSessionStateChanged', audioSessionStateChangedCallback);

setDefaultOutputDevice20+

setDefaultOutputDevice(deviceType: DeviceType): Promise<void>

Sets the default audio output device. This API uses a promise to return the result.

NOTE

  • This API applies to the following scenario: When AudioSessionScene is set to VoIP, the setting takes effect immediately after the AudioSession is activated. For non-VoIP scenarios, the setting does not take effect upon AudioSession activation. Instead, the setting applies when StreamUsage for playback is voice message, VoIP voice call, or VoIP video call. Supported devices include the earpiece, speaker, and system default device.
  • This API can be called at any time after an AudioSessionManager instance is created. The system records the device set by the application. However, the setting takes effect only after the AudioSession is activated. When the application starts playing, if an external device like Bluetooth headsets or wired headsets is connected, the system prioritizes audio output through the external device. Otherwise, the system uses the device set by the application.

System capability: SystemCapability.Multimedia.Audio.Device

Device behavior difference: If the default audio output device is set to earpiece on a device without a earpiece, the speaker will still be used for audio output.

Parameters

Name Type Mandatory Description
deviceType DeviceType Yes Device type.
The options are EARPIECE, SPEAKER, and DEFAULT.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed. Return by promise.
6800102 Allocate memory failed. Return by promise.
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioSessionManager.setDefaultOutputDevice(audio.DeviceType.SPEAKER).then(() => {
  console.info('setDefaultOutputDevice Success!');
}).catch((err: BusinessError) => {
  console.error(`setDefaultOutputDevice Fail: ${err}`);
});

getDefaultOutputDevice20+

getDefaultOutputDevice(): DeviceType

Obtains the default audio output device set by calling setDefaultOutputDevice.

System capability: SystemCapability.Multimedia.Audio.Device

Return value

Type Description
DeviceType Device type.
The options are EARPIECE, SPEAKER, and DEFAULT.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800103 Operation not permit at current state. Return by promise.

Example

let deviceType = audioSessionManager.getDefaultOutputDevice();

on('currentOutputDeviceChanged')20+

on(type: 'currentOutputDeviceChanged', callback: Callback<CurrentOutputDeviceChangedEvent>): void

Subscribes to the current output device change event, which is triggered when the current output device is changed. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'currentOutputDeviceChanged' is triggered when the current output device is changed.
callback Callback<CurrentOutputDeviceChangedEvent> Yes Callback used to return the information about the current output device.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800102 Allocate memory failed.
6800301 Audio client call audio service error, System error.

Example

import { audio } from '@kit.AudioKit';

let currentOutputDeviceChangedCallback = (currentOutputDeviceChangedEvent: audio.CurrentOutputDeviceChangedEvent) => {
  console.info(`reason of audioSessionStateChanged: ${currentOutputDeviceChangedEvent.changeReason} `);
};

audioSessionManager.on('currentOutputDeviceChanged', currentOutputDeviceChangedCallback);

off('currentOutputDeviceChanged')20+

off(type: 'currentOutputDeviceChanged', callback?: Callback<CurrentOutputDeviceChangedEvent>): void

Unsubscribes from the current output device change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'currentOutputDeviceChanged' is triggered when the current output device is changed.
callback Callback<CurrentOutputDeviceChangedEvent> No Callback used to return the information about the current output device.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

// Cancel all subscriptions to the event.
audioSessionManager.off('currentOutputDeviceChanged');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let currentOutputDeviceChangedCallback = (currentOutputDeviceChangedEvent: audio.CurrentOutputDeviceChangedEvent) => {
  console.info(`reason of audioSessionStateChanged: ${currentOutputDeviceChangedEvent.changeReason} `);
};

audioSessionManager.on('currentOutputDeviceChanged', currentOutputDeviceChangedCallback);

audioSessionManager.off('currentOutputDeviceChanged', currentOutputDeviceChangedCallback);

getAvailableDevices21+

getAvailableDevices(deviceUsage: DeviceUsage): AudioDeviceDescriptors

Obtains the available audio devices.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
deviceUsage DeviceUsage Yes Audio device type (classified by usage).

Return value

Type Description
AudioDeviceDescriptors Device list.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let data: audio.AudioDeviceDescriptors = audioSessionManager.getAvailableDevices(audio.DeviceUsage.MEDIA_OUTPUT_DEVICES);
  console.info('Succeeded in doing getAvailableDevices.');
} catch (err) {
  let error = err as BusinessError;
   console.error(`Failed to getAvailableDevices. Code: ${error.code}, message: ${error.message}`);
}

on('availableDeviceChange')21+

on(type: 'availableDeviceChange', deviceUsage: DeviceUsage, callback: Callback<DeviceChangeAction>): void

Subscribes to the event indicating that the connection status of an available audio device is changed.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'availableDeviceChange' is triggered when the connection status of available audio devices is changed.
deviceUsage DeviceUsage Yes Audio device type (classified by usage).
callback Callback<DeviceChangeAction> Yes Callback used to return the available device change details.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

audioSessionManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_INPUT_DEVICES, (deviceChanged: audio.DeviceChangeAction) => {
  console.info('device change type : ' + deviceChanged.type);
  console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length);
  console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole);
  console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType);
});

off('availableDeviceChange')21+

off(type: 'availableDeviceChange', callback?: Callback<DeviceChangeAction>): void

Unsubscribes from the event indicating that the connection status of an available audio device is changed.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'availableDeviceChange' is triggered when the connection status of available audio devices is changed.
callback Callback<DeviceChangeAction> No Callback used to return the available device change details.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 Audio client call audio service error, System error.

Example

// Cancel all subscriptions to the event.
audioSessionManager.off('availableDeviceChange');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let availableDeviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => {
  console.info('device change type : ' + deviceChanged.type);
  console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length);
  console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole);
  console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType);
};

audioSessionManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_INPUT_DEVICES, availableDeviceChangeCallback);

audioSessionManager.off('availableDeviceChange', availableDeviceChangeCallback);

selectMediaInputDevice21+

selectMediaInputDevice(inputAudioDevice: AudioDeviceDescriptor): Promise<void>

Selects a media input device. This API uses a promise to return the result.

NOTE

  • This API is not suitable for VoIP call recording; that is, it does not apply to scenarios where SourceType is SOURCE_TYPE_VOICE_COMMUNICATION.
  • Before calling this API, call getAvailableDevices to query the list of available input devices and select an input device from the list.
  • If there are recording streams of other applications with higher priorities in the system, the actual input device used will follow the input device selected by these applications.
  • Applications can listen for the currentInputDeviceChanged event to find out the actual input device being used.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
inputAudioDevice AudioDeviceDescriptor Yes Media input device.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed, for example, the selected device does not exist.
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let data: audio.AudioDeviceDescriptors = audioSessionManager.getAvailableDevices(audio.DeviceUsage.MEDIA_OUTPUT_DEVICES);
  console.info('Succeeded in doing getAvailableDevices.');

  if (data[0]) {
    audioSessionManager.selectMediaInputDevice(data[0]).then(() => {
      console.info('Succeeded in doing selectMediaInputDevice.');
    }).catch((selectErr: BusinessError) => {
      console.error(`Failed to selectMediaInputDevice. Code: ${selectErr.code}, message: ${selectErr.message}`);
    });
  }
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to getAvailableDevices. Code: ${error.code}, message: ${error.message}`);
}

getSelectedMediaInputDevice21+

getSelectedMediaInputDevice(): AudioDeviceDescriptor

Obtains the media input device set by calling selectMediaInputDevice. If no device has been specified, the device with deviceType set to INVALID is returned.

System capability: SystemCapability.Multimedia.Audio.Device

Return value

Type Description
AudioDeviceDescriptor Media input device.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let device: audio.AudioDeviceDescriptor = audioSessionManager.getSelectedMediaInputDevice();
  console.info('Succeeded in doing getSelectedMediaInputDevice.');
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to getSelectedMediaInputDevice. Code: ${error.code}, message: ${error.message}`);
}

clearSelectedMediaInputDevice21+

clearSelectedMediaInputDevice(): Promise<void>

Clears the media input device set by calling selectMediaInputDevice. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Device

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioSessionManager.clearSelectedMediaInputDevice().then(() => {
  console.info('Succeeded in doing clearSelectedMediaInputDevice.');
}).catch((err: BusinessError) => {
  console.error(`Failed to clearSelectedMediaInputDevice. Code: ${err.code}, message: ${err.message}`);
});

setBluetoothAndNearlinkPreferredRecordCategory21+

setBluetoothAndNearlinkPreferredRecordCategory(category: BluetoothAndNearlinkPreferredRecordCategory): Promise<void>

Sets the preferred device category for recording with Bluetooth or NearLink. This API uses a promise to return the result.

NOTE

  • Applications can set this category before connecting to Bluetooth or NearLink devices, and the system prioritizes using the device for recording when the device is connected.
  • If there are recording streams of other applications with higher priorities in the system, the actual input device used will follow the input device selected by these applications.
  • Applications can listen for the currentInputDeviceChanged event to find out the actual input device being used.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
category BluetoothAndNearlinkPreferredRecordCategory Yes Preferred device category for recording with Bluetooth or NearLink.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

const category = audio.BluetoothAndNearlinkPreferredRecordCategory.PREFERRED_LOW_LATENCY;
audioSessionManager.setBluetoothAndNearlinkPreferredRecordCategory(category).then(() => {
  console.info('Succeeded in doing setBluetoothAndNearlinkPreferredRecordCategory.');
}).catch((err: BusinessError) => {
  console.error(`Failed to setBluetoothAndNearlinkPreferredRecordCategory. Code: ${err.code}, message: ${err.message}`);
});

getBluetoothAndNearlinkPreferredRecordCategory21+

getBluetoothAndNearlinkPreferredRecordCategory(): BluetoothAndNearlinkPreferredRecordCategory

Obtains the preferred device category for recording with Bluetooth or NearLink, which is set by calling setBluetoothAndNearlinkPreferredRecordCategory.

System capability: SystemCapability.Multimedia.Audio.Device

Return value

Type Description
BluetoothAndNearlinkPreferredRecordCategory Preferred device category for recording with Bluetooth or NearLink.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 Audio client call audio service error, System error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let category: audio.BluetoothAndNearlinkPreferredRecordCategory = audioSessionManager.getBluetoothAndNearlinkPreferredRecordCategory();
  console.info('Succeeded in doing getBluetoothAndNearlinkPreferredRecordCategory.');
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to getBluetoothAndNearlinkPreferredRecordCategory. Code: ${error.code}, message: ${error.message}`);
}

on('currentInputDeviceChanged')21+

on(type: 'currentInputDeviceChanged', callback: Callback<CurrentInputDeviceChangedEvent>): void

Subscribes to the current input device change event, which is triggered when the current input device is changed.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'currentInputDeviceChanged' is triggered when the current input device is changed.
callback Callback<CurrentInputDeviceChangedEvent> Yes Callback used to return the information about the current input device.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800301 Audio client call audio service error, System error.

Example

import { audio } from '@kit.AudioKit';

let currentInputDeviceChangedCallback = (currentInputDeviceChangedEvent: audio.CurrentInputDeviceChangedEvent) => {
  console.info(`reason of currentInputDeviceChanged: ${currentInputDeviceChangedEvent.changeReason} `);
};

audioSessionManager.on('currentInputDeviceChanged', currentInputDeviceChangedCallback);

off('currentInputDeviceChanged')21+

off(type: 'currentInputDeviceChanged', callback?: Callback<CurrentInputDeviceChangedEvent>): void

Unsubscribes from the current input device change event.

System capability: SystemCapability.Multimedia.Audio.Device

Parameters

Name Type Mandatory Description
type string Yes Event type. The event 'currentInputDeviceChanged' is triggered when the current input device is changed.
callback Callback<CurrentInputDeviceChangedEvent> No Callback used to return the information about the current input device.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800301 Audio client call audio service error, System error.

Example

// Cancel all subscriptions to the event.
audioSessionManager.off('currentInputDeviceChanged');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let currentInputDeviceChangedCallback = (currentInputDeviceChangedEvent: audio.CurrentInputDeviceChangedEvent) => {
  console.info(`reason of currentInputDeviceChanged: ${currentInputDeviceChangedEvent.changeReason} `);
};

audioSessionManager.on('currentInputDeviceChanged', currentInputDeviceChangedCallback);

audioSessionManager.off('currentInputDeviceChanged', currentInputDeviceChangedCallback);

enableMuteSuggestionWhenMixWithOthers23+

enableMuteSuggestionWhenMixWithOthers(enable: boolean): void

Enables mute suggestion notifications for mixed playback.

Typically, when the audio mixing mode is used, if two applications plays audio at the same time, their audio streams are mixed. In certain scenarios (such as games or broadcasts), applications can mute their own audio to provide a better user experience.

If this feature is enabled, mute and unmute suggestions will be sent through the AudioSessionStateChangedEvent callback after the audio session state change event is subscribed to. Receiving the muted suggestion indicates that another application starts to play audio, and the played audio and the audio of this application cannot be mixed.

This feature can be used only by audio sessions for which AudioSessionScene has been set and the CONCURRENCY_MIX_WITH_OTHERS mode has been activated. This feature takes effect only once when the audio session is activated. You need to enable it again before each activation of the audio session.

For details, see Enabling Mute Suggestion Notifications for Mixed Playback.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
enable boolean Yes Whether to enable mute suggestion notifications for mixed playback. true to enable, false otherwise.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800103 Function is called without setting AudioSessionScene or called after audio session activation.
6800301 Audio client call audio service error, system internal error.

Example

audio.getAudioManager().getSessionManager().enableMuteSuggestionWhenMixWithOthers(true);

isOtherMediaPlaying23+

isOtherMediaPlaying(): boolean

Check whether any other application is currently playing audio of the four media types: MUSIC, MOVIE, AUDIOBOOK, and GAME. Audio sessions that have activated these media types will also be checked.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Multimedia.Audio.Core

Return value

Type Description
boolean Whether another application is playing audio of certain media types. true means yes; false otherwise.

Example

let isExistence = audioSessionManager.isOtherMediaPlaying();

setAudioSessionBehavior24+

setAudioSessionBehavior(behavior: number): void

Sets audio session behavior parameters. (Multiple flags can be combined.)

NOTE

If this API is called while an audio session is active, you must call the activateAudioSession API again for the settings to take effect.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Multimedia.Audio.Core

Parameters

Name Type Mandatory Description
behavior number Yes Specifies the audio session behavior.
This can be a single flag or a bitwise OR combination of multiple flags.
For details about the supported audio session behaviors, see AudioSessionBehaviorFlags.

Error codes

For details about the error codes, see Audio Error Codes.

ID Error Message
6800101 Parameter verification failed.
6800103 Operation not permitted in the current state.

Example

let behavior: number = audio.AudioSessionBehaviorFlags.MUTE_WHEN_INTERRUPTED;
audioSessionManager.setAudioSessionBehavior(behavior);