Interface (AudioManager)
This interface implements audio volume and device management.
Before calling any API in AudioManager, you must use getAudioManager to obtain an AudioManager 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.
Modules to Import
import { audio } from '@kit.AudioKit';
getAudioScene8+
getAudioScene(callback: AsyncCallback<AudioScene>): void
Obtains the audio scene. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| callback | AsyncCallback<AudioScene> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the audio scene obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getAudioScene((err: BusinessError, value: audio.AudioScene) => {
if (err) {
console.error(`Failed to obtain the audio scene mode. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the audio scene mode is obtained ${value}.`);
});
getAudioScene8+
getAudioScene(): Promise<AudioScene>
Obtains the audio scene. This API uses a promise to return the result.
System capability: SystemCapability.Multimedia.Audio.Communication
Return value
| Type | Description |
|---|---|
| Promise<AudioScene> | Promise used to return the audio scene. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getAudioScene().then((value: audio.AudioScene) => {
console.info(`Promise returned to indicate that the audio scene mode is obtained ${value}.`);
}).catch ((err: BusinessError) => {
console.error(`Failed to obtain the audio scene mode ${err}`);
});
getAudioSceneSync10+
getAudioSceneSync(): AudioScene
Obtains the audio scene. This API returns the result synchronously.
System capability: SystemCapability.Multimedia.Audio.Communication
Return value
| Type | Description |
|---|---|
| AudioScene | Audio scene. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
try {
let value: audio.AudioScene = audioManager.getAudioSceneSync();
console.info(`indicate that the audio scene mode is obtained ${value}.`);
} catch (err) {
let error = err as BusinessError;
console.error(`Failed to obtain the audio scene mode ${error}`);
}
on('audioSceneChange')20+
on(type: 'audioSceneChange', callback: Callback<AudioScene>): void
Subscribes to the audio scene change event. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'audioSceneChange' is triggered when the audio scene is changed. |
| callback | Callback<AudioScene> | Yes | Callback used to return the current audio scene. |
Example
audioManager.on('audioSceneChange', (audioScene: audio.AudioScene) => {
console.info(`audio scene : ${audioScene}.`);
});
off('audioSceneChange')20+
off(type: 'audioSceneChange', callback?: Callback<AudioScene>): void
Unsubscribes from the audio scene change event. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'audioSceneChange' is triggered when the audio scene is changed. |
| callback | Callback<AudioScene> | No | Callback used to return the current audio scene. |
Example
// Cancel all subscriptions to the event.
audioManager.off('audioSceneChange');
// 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 audioSceneChangeCallback = (audioScene: audio.AudioScene) => {
console.info(`audio scene : ${audioScene}.`);
};
audioManager.on('audioSceneChange', audioSceneChangeCallback);
audioManager.off('audioSceneChange', audioSceneChangeCallback);
getVolumeManager9+
getVolumeManager(): AudioVolumeManager
Obtains an AudioVolumeManager instance.
Atomic service API: This API can be used in atomic services since API version 23.
System capability: SystemCapability.Multimedia.Audio.Volume
Return value
| Type | Description |
|---|---|
| AudioVolumeManager | AudioVolumeManager instance. |
Example
import { audio } from '@kit.AudioKit';
let audioVolumeManager: audio.AudioVolumeManager = audioManager.getVolumeManager();
getStreamManager9+
getStreamManager(): AudioStreamManager
Obtains an AudioStreamManager instance.
System capability: SystemCapability.Multimedia.Audio.Core
Return value
| Type | Description |
|---|---|
| AudioStreamManager | AudioStreamManager instance. |
Example
import { audio } from '@kit.AudioKit';
let audioStreamManager: audio.AudioStreamManager = audioManager.getStreamManager();
getRoutingManager9+
getRoutingManager(): AudioRoutingManager
Obtains an AudioRoutingManager instance.
System capability: SystemCapability.Multimedia.Audio.Device
Return value
| Type | Description |
|---|---|
| AudioRoutingManager | AudioRoutingManager instance. |
Example
import { audio } from '@kit.AudioKit';
let audioRoutingManager: audio.AudioRoutingManager = audioManager.getRoutingManager();
getSessionManager12+
getSessionManager(): AudioSessionManager
Obtains an AudioSessionManager instance.
System capability: SystemCapability.Multimedia.Audio.Core
Return value
| Type | Description |
|---|---|
| AudioSessionManager | AudioSessionManager instance. |
Example
import { audio } from '@kit.AudioKit';
let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager();
getSpatializationManager18+
getSpatializationManager(): AudioSpatializationManager
Obtains an AudioSpatializationManager instance.
System capability: SystemCapability.Multimedia.Audio.Spatialization
Return value
| Type | Description |
|---|---|
| AudioSpatializationManager | AudioSpatializationManager instance. |
Example
import { audio } from '@kit.AudioKit';
let audioSpatializationManager: audio.AudioSpatializationManager = audioManager.getSpatializationManager();
setAudioParameter(deprecated)
setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void
Sets an audio parameter. This API uses an asynchronous callback to return the result.
This API is used to extend the audio configuration based on the hardware capability. The supported audio parameters vary according to the device and are not common parameters. Sample parameters are used in the sample code below.
NOTE
This API is supported since API version 7 and deprecated since API version 11. Its substitute is available only to system applications.
Required permissions: ohos.permission.MODIFY_AUDIO_SETTINGS
System capability: SystemCapability.Multimedia.Audio.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | string | Yes | Key of the audio parameter to set. |
| value | string | Yes | Value of the audio parameter to set. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.setAudioParameter('key_example', 'value_example', (err: BusinessError) => {
if (err) {
console.error(`Failed to set the audio parameter. ${err}`);
return;
}
console.info('Callback invoked to indicate a successful setting of the audio parameter.');
});
setAudioParameter(deprecated)
setAudioParameter(key: string, value: string): Promise<void>
Sets an audio parameter. This API uses a promise to return the result.
This API is used to extend the audio configuration based on the hardware capability. The supported audio parameters vary according to the device and are not common parameters. Sample parameters are used in the sample code below.
NOTE
This API is supported since API version 7 and deprecated since API version 11. Its substitute is available only to system applications.
Required permissions: ohos.permission.MODIFY_AUDIO_SETTINGS
System capability: SystemCapability.Multimedia.Audio.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | string | Yes | Key of the audio parameter to set. |
| value | string | Yes | Value of the audio parameter to set. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.setAudioParameter('key_example', 'value_example').then(() => {
console.info('Promise returned to indicate a successful setting of the audio parameter.');
});
getAudioParameter(deprecated)
getAudioParameter(key: string, callback: AsyncCallback<string>): void
Obtains the value of an audio parameter. This API uses an asynchronous callback to return the result.
This API is used to extend the audio configuration based on the hardware capability. The supported audio parameters vary according to the device and can be obtained from the device manual. The example below is for reference only.
NOTE
This API is supported since API version 7 and deprecated since API version 11. Its substitute is available only to system applications.
System capability: SystemCapability.Multimedia.Audio.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | string | Yes | Key of the audio parameter whose value is to be obtained. |
| callback | AsyncCallback<string> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the audio parameter value obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getAudioParameter('key_example', (err: BusinessError, value: string) => {
if (err) {
console.error(`Failed to obtain the value of the audio parameter. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the value of the audio parameter is obtained ${value}.`);
});
getAudioParameter(deprecated)
getAudioParameter(key: string): Promise<string>
Obtains the value of an audio parameter. This API uses a promise to return the result.
This API is used to extend the audio configuration based on the hardware capability. The supported audio parameters vary according to the device and can be obtained from the device manual. The example below is for reference only.
NOTE
This API is supported since API version 7 and deprecated since API version 11. Its substitute is available only to system applications.
System capability: SystemCapability.Multimedia.Audio.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | string | Yes | Key of the audio parameter whose value is to be obtained. |
Return value
| Type | Description |
|---|---|
| Promise<string> | Promise used to return the value of the audio parameter. |
Example
audioManager.getAudioParameter('key_example').then((value: string) => {
console.info(`Promise returned to indicate that the value of the audio parameter is obtained ${value}.`);
});
setVolume(deprecated)
setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void
Sets the volume for a stream. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Applications cannot directly adjust the system volume. They can use the system volume panel to control the volume. For details about the examples and descriptions, see Volume Panel in the API reference.
Required permissions: ohos.permission.ACCESS_NOTIFICATION_POLICY
This permission is required only for muting or unmuting the ringer when volumeType is set to AudioVolumeType.RINGTONE.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| volume | number | Yes | Volume to set. The value range can be obtained by calling getMinVolume and getMaxVolume. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err: BusinessError) => {
if (err) {
console.error(`Failed to set the volume. ${err}`);
return;
}
console.info('Callback invoked to indicate a successful volume setting.');
});
setVolume(deprecated)
setVolume(volumeType: AudioVolumeType, volume: number): Promise<void>
Sets the volume for a stream. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Applications cannot directly adjust the system volume. They can use the system volume panel to control the volume. For details about the examples and descriptions, see Volume Panel in the API reference.
Required permissions: ohos.permission.ACCESS_NOTIFICATION_POLICY
This permission is required only for muting or unmuting the ringer when volumeType is set to AudioVolumeType.RINGTONE.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| volume | number | Yes | Volume to set. The value range can be obtained by calling getMinVolume and getMaxVolume. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
console.info('Promise returned to indicate a successful volume setting.');
});
getVolume(deprecated)
getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the volume of a stream. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getVolume instead. For API version 20 and later, you are advised to use getVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the stream volume obtained; otherwise, err is an error object. The volume range of a specified stream can be obtained by calling getMinVolume and getMaxVolume. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
if (err) {
console.error(`Failed to obtain the volume. ${err}`);
return;
}
console.info('Callback invoked to indicate that the volume is obtained.');
});
getVolume(deprecated)
getVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the volume of a stream. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getVolume instead. For API version 20 and later, you are advised to use getVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
Return value
| Type | Description |
|---|---|
| Promise<number> | Promise used to return the volume of the stream. The volume range of a specified stream can be obtained by calling getMinVolume and getMaxVolume. |
Example
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => {
console.info(`Promise returned to indicate that the volume is obtained ${value} .`);
});
getMinVolume(deprecated)
getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the minimum volume allowed for a stream. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getMinVolume instead. For API version 20 and later, you are advised to use getMinVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the minimum stream volume obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
if (err) {
console.error(`Failed to obtain the minimum volume. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`);
});
getMinVolume(deprecated)
getMinVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the minimum volume allowed for a stream. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getMinVolume instead. For API version 20 and later, you are advised to use getMinVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
Return value
| Type | Description |
|---|---|
| Promise<number> | Promise used to return the minimum volume. |
Example
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => {
console.info(`Promised returned to indicate that the minimum volume is obtained. ${value}`);
});
getMaxVolume(deprecated)
getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the maximum volume allowed for a stream. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getMaxVolume instead. For API version 20 and later, you are advised to use getMaxVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the maximum stream volume obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
if (err) {
console.error(`Failed to obtain the maximum volume. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`);
});
getMaxVolume(deprecated)
getMaxVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the maximum volume allowed for a stream. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use getMaxVolume instead. For API version 20 and later, you are advised to use getMaxVolumeByStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
Return value
| Type | Description |
|---|---|
| Promise<number> | Promise used to return the maximum volume. |
Example
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => {
console.info('Promised returned to indicate that the maximum volume is obtained.');
});
mute(deprecated)
mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void
Mutes or unmutes a stream. This API uses an asynchronous callback to return the result.
When the minimum volume of a stream cannot be set to 0, muting the stream is not supported. Example scenarios: alarms or phone calls.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| mute | boolean | Yes | Whether to mute the stream. true to mute, false otherwise. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err: BusinessError) => {
if (err) {
console.error(`Failed to mute the stream. ${err}`);
return;
}
console.info('Callback invoked to indicate that the stream is muted.');
});
mute(deprecated)
mute(volumeType: AudioVolumeType, mute: boolean): Promise<void>
Mutes or unmutes a stream. This API uses a promise to return the result.
When the minimum volume of a stream cannot be set to 0, muting the stream is not supported. Example scenarios: alarms or phone calls.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| mute | boolean | Yes | Whether to mute the stream. true to mute, false otherwise. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
console.info('Promise returned to indicate that the stream is muted.');
});
isMute(deprecated)
isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void
Checks whether a stream is muted. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use isMute instead. For API version 20 and later, you are advised to use isSystemMutedForStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is true if the stream is muted or false if not muted; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => {
if (err) {
console.error(`Failed to obtain the mute status. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the mute status of the stream is obtained. ${value}`);
});
isMute(deprecated)
isMute(volumeType: AudioVolumeType): Promise<boolean>
Checks whether a stream is muted. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use isMute instead. For API version 20 and later, you are advised to use isSystemMutedForStream instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether the stream is muted. true if muted, false otherwise. |
Example
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => {
console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`);
});
isActive(deprecated)
isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void
Checks whether a stream is active. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use isActive instead. For API version 20 and later, you are advised to use isStreamActive instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is true if the stream is active or false if not active; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => {
if (err) {
console.error(`Failed to obtain the active status of the stream. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`);
});
isActive(deprecated)
isActive(volumeType: AudioVolumeType): Promise<boolean>
Checks whether a stream is active. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. From API version 9 to 19, you are advised to use isActive instead. For API version 20 and later, you are advised to use isStreamActive instead.
System capability: SystemCapability.Multimedia.Audio.Volume
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| volumeType | AudioVolumeType | Yes | Audio volume type. |
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether the stream is active. true if active, false otherwise. |
Example
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => {
console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`);
});
setRingerMode(deprecated)
setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void
Sets the ringer mode. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Required permissions: ohos.permission.ACCESS_NOTIFICATION_POLICY
This permission is required only for muting or unmuting the ringer.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | AudioRingMode | Yes | Ringer mode. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err: BusinessError) => {
if (err) {
console.error(`Failed to set the ringer mode. ${err}`);
return;
}
console.info('Callback invoked to indicate a successful setting of the ringer mode.');
});
setRingerMode(deprecated)
setRingerMode(mode: AudioRingMode): Promise<void>
Sets the ringer mode. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Required permissions: ohos.permission.ACCESS_NOTIFICATION_POLICY
This permission is required only for muting or unmuting the ringer.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | AudioRingMode | Yes | Ringer mode. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => {
console.info('Promise returned to indicate a successful setting of the ringer mode.');
});
getRingerMode(deprecated)
getRingerMode(callback: AsyncCallback<AudioRingMode>): void
Obtains the ringer mode. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use getRingerMode instead.
System capability: SystemCapability.Multimedia.Audio.Communication
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| callback | AsyncCallback<AudioRingMode> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the ringer mode obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => {
if (err) {
console.error(`Failed to obtain the ringer mode. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`);
});
getRingerMode(deprecated)
getRingerMode(): Promise<AudioRingMode>
Obtains the ringer mode. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use getRingerMode instead.
System capability: SystemCapability.Multimedia.Audio.Communication
Return value
| Type | Description |
|---|---|
| Promise<AudioRingMode> | Promise used to return the ringer mode. |
Example
audioManager.getRingerMode().then((value: audio.AudioRingMode) => {
console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`);
});
getDevices(deprecated)
getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void
Obtains the audio devices with a specific flag. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use getDevices instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceFlag | DeviceFlag | Yes | Audio device flag. |
| callback | AsyncCallback<AudioDeviceDescriptors> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the audio devices obtained; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => {
if (err) {
console.error(`Failed to obtain the device list. ${err}`);
return;
}
console.info('Callback invoked to indicate that the device list is obtained.');
});
getDevices(deprecated)
getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors>
Obtains the audio devices with a specific flag. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use getDevices instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceFlag | DeviceFlag | Yes | Audio device flag. |
Return value
| Type | Description |
|---|---|
| Promise<AudioDeviceDescriptors> | Promise used to return the device list. |
Example
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => {
console.info('Promise returned to indicate that the device list is obtained.');
});
setDeviceActive(deprecated)
setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void
Sets a device to the active state. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use setCommunicationDevice instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceType | ActiveDeviceType | Yes | Active audio device type. |
| active | boolean | Yes | Active state to set. true to set the device to the active state, false otherwise. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err: BusinessError) => {
if (err) {
console.error(`Failed to set the active status of the device. ${err}`);
return;
}
console.info('Callback invoked to indicate that the device is set to the active status.');
});
setDeviceActive(deprecated)
setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void>
Sets a device to the active state. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use setCommunicationDevice instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceType | ActiveDeviceType | Yes | Active audio device type. |
| active | boolean | Yes | Active state to set. true to set the device to the active state, false otherwise. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => {
console.info('Promise returned to indicate that the device is set to the active status.');
});
isDeviceActive(deprecated)
isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void
Checks whether a device is active. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use isCommunicationDeviceActive instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceType | ActiveDeviceType | Yes | Active audio device type. |
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is true if the device is active or false if not active; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err: BusinessError, value: boolean) => {
if (err) {
console.error(`Failed to obtain the active status of the device. ${err}`);
return;
}
console.info('Callback invoked to indicate that the active status of the device is obtained.');
});
isDeviceActive(deprecated)
isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean>
Checks whether a device is active. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use isCommunicationDeviceActive instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| deviceType | ActiveDeviceType | Yes | Active audio device type. |
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether the device is active. true if active, false otherwise. |
Example
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value: boolean) => {
console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`);
});
setMicrophoneMute(deprecated)
setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void
Mutes or unmutes the microphone. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Required permissions: ohos.permission.MICROPHONE
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mute | boolean | Yes | Mute status to set. true to mute, false otherwise. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.setMicrophoneMute(true, (err: BusinessError) => {
if (err) {
console.error(`Failed to mute the microphone. ${err}`);
return;
}
console.info('Callback invoked to indicate that the microphone is muted.');
});
setMicrophoneMute(deprecated)
setMicrophoneMute(mute: boolean): Promise<void>
Mutes or unmutes the microphone. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Its substitute is available only to system applications.
Required permissions: ohos.permission.MICROPHONE
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mute | boolean | Yes | Mute status to set. true to mute, false otherwise. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example
audioManager.setMicrophoneMute(true).then(() => {
console.info('Promise returned to indicate that the microphone is muted.');
});
isMicrophoneMute(deprecated)
isMicrophoneMute(callback: AsyncCallback<boolean>): void
Checks whether the microphone is muted. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use isMicrophoneMute instead.
Required permissions: ohos.permission.MICROPHONE
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is true if the microphone is muted or false if not muted; otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
audioManager.isMicrophoneMute((err: BusinessError, value: boolean) => {
if (err) {
console.error(`Failed to obtain the mute status of the microphone. ${err}`);
return;
}
console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`);
});
isMicrophoneMute(deprecated)
isMicrophoneMute(): Promise<boolean>
Checks whether the microphone is muted. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use isMicrophoneMute instead.
Required permissions: ohos.permission.MICROPHONE
System capability: SystemCapability.Multimedia.Audio.Device
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether the microphone is muted. true if muted, false otherwise. |
Example
audioManager.isMicrophoneMute().then((value: boolean) => {
console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`);
});
on('deviceChange')(deprecated)
on(type: 'deviceChange', callback: Callback<DeviceChangeAction>): void
Subscribes to the event indicating that the connection status of an audio device is changed. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use on('deviceChange') instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'deviceChange' is triggered when the connection status of an audio device is changed. |
| callback | Callback<DeviceChangeAction> | Yes | Callback used to return the device change details. |
Example
audioManager.on('deviceChange', (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('deviceChange')(deprecated)
off(type: 'deviceChange', callback?: Callback<DeviceChangeAction>): void
Unsubscribes from the audio device change event. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. You are advised to use off('deviceChange') instead.
System capability: SystemCapability.Multimedia.Audio.Device
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'deviceChange' is triggered when the connection status of an audio device is changed. |
| callback | Callback<DeviceChangeAction> | No | Callback used to return the device change details. |
Example
// Cancel all subscriptions to the event.
audioManager.off('deviceChange');
// 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 deviceChangeCallback = (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} `);
};
audioManager.on('deviceChange', deviceChangeCallback);
audioManager.off('deviceChange', deviceChangeCallback);
on('interrupt')(deprecated)
on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback<InterruptAction>): void
Subscribes to the audio interruption event, which is triggered when the audio focus is changed. This API uses an asynchronous callback to return the result.
Same as on('audioInterrupt'), this API is used to listen for focus changes. However, this API is used in scenarios without audio streams (no AudioRenderer instance is created), such as frequency modulation (FM) and voice wakeup.
NOTE
This API is supported since API version 7 and deprecated since API version 11. You are advised to use on('audioInterrupt') instead.
System capability: SystemCapability.Multimedia.Audio.Renderer
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'interrupt' is triggered when the audio focus is changed. |
| interrupt | AudioInterrupt | Yes | Audio interruption event type. |
| callback | Callback<InterruptAction> | Yes | Callback used to return the event information. |
Example
import { audio } from '@kit.AudioKit';
let interAudioInterrupt: audio.AudioInterrupt = {
streamUsage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
contentType: audio.ContentType.CONTENT_TYPE_UNKNOWN,
pauseWhenDucked: true
};
audioManager.on('interrupt', interAudioInterrupt, (interruptAction: audio.InterruptAction) => {
if (interruptAction.actionType === 0) {
console.info('An event to gain the audio focus starts.');
console.info(`Focus hint: ${interruptAction.hint} `);
}
if (interruptAction.actionType === 1) {
console.info('An audio interruption event starts.');
console.info(`Audio interruption hint: ${interruptAction.hint} `);
}
});
off('interrupt')(deprecated)
off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback<InterruptAction>): void
Unsubscribes from the audio interruption event. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 11. You are advised to use off('audioInterrupt') instead.
System capability: SystemCapability.Multimedia.Audio.Renderer
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The event 'interrupt' is triggered when the audio focus is changed. |
| interrupt | AudioInterrupt | Yes | Audio interruption event type. |
| callback | Callback<InterruptAction> | No | Callback used to return the event information. |
Example
import { audio } from '@kit.AudioKit';
let interAudioInterrupt: audio.AudioInterrupt = {
streamUsage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
contentType: audio.ContentType.CONTENT_TYPE_UNKNOWN,
pauseWhenDucked: true
};
// Cancel all subscriptions to the event.
audioManager.off('interrupt', interAudioInterrupt);
// 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 interruptCallback = (interruptAction: audio.InterruptAction) => {
if (interruptAction.actionType === 0) {
console.info('An event to gain the audio focus starts.');
console.info(`Focus hint: ${interruptAction.hint} `);
}
if (interruptAction.actionType === 1) {
console.info('An audio interruption event starts.');
console.info(`Audio interruption hint: ${interruptAction.hint} `);
}
};
audioManager.on('interrupt', interAudioInterrupt, interruptCallback);
audioManager.off('interrupt', interAudioInterrupt, interruptCallback);