# Audio Management ## Modules to Import ``` import audio from '@ohos.multimedia.audio'; ``` ## Required Permissions None ## getAudioManager\(\): AudioManager Obtains an **AudioManager** instance. **Return values**

Type

Description

AudioManager

Audio manager

**Example** ``` var audioManager = audio.getAudioManager(); ``` ## AudioVolumeType Enumerates audio stream types.

Name

Default Value

Description

RINGTONE

2

Audio stream for ringtones

MEDIA

3

Audio stream for media purpose

## DeviceFlag Enumerates audio device flags.

Name

Default Value

Description

OUTPUT_DEVICES_FLAG

1

Output device

INPUT_DEVICES_FLAG

2

Input device

ALL_DEVICES_FLAG

3

All devices

## DeviceRole Enumerates audio device roles.

Name

Default Value

Description

INPUT_DEVICE

1

Input role

OUTPUT_DEVICE

2

Output role

## DeviceType Enumerates audio device types.

Name

Default Value

Description

INVALID

0

Invalid device

SPEAKER

1

Speaker

WIRED_HEADSET

2

Wired headset

BLUETOOTH_SCO

3

Bluetooth device using the synchronous connection oriented (SCO) link

BLUETOOTH_A2DP

4

Bluetooth device using the advanced audio distribution profile (A2DP)

MIC

5

Microphone

## AudioRingMode7+ Enumerates ringer modes.

Name

Default Value

Description

RINGER_MODE_SILENT

0

Silence mode

RINGER_MODE_VIBRATE

1

Vibration mode

RINGER_MODE_NORMAL

2

Normal mode

## AudioManager Implements audio volume and audio device management. ### setVolume\(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback\): void Sets the volume for a stream. This method uses an asynchronous callback to return the execution result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream 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 execution result

**Return values** None **Example** ``` audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err)=>{ if (err) { console.error('Failed to set the volume. ${err.message}'); return; } console.log('Callback invoked to indicate a successful volume setting.'); }) ``` ### setVolume\(volumeType: AudioVolumeType, volume: number\): Promise Sets the volume for a stream. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

volume

number

Yes

Volume to set. The value range can be obtained by calling getMinVolume and getMaxVolume.

**Return values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=> console.log('Promise returned to indicate a successful volume setting.'); ) ``` ### getVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void Obtains the volume of a stream. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

callback

AsyncCallback<number>

Yes

Callback used to return the volume

**Return values** None **Example** ``` audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the volume. ${err.message}'); return; } console.log('Callback invoked to indicate that the volume is obtained.'); }) ``` ### getVolume\(volumeType: AudioVolumeType\): Promise Obtains the volume of a stream. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

**Return values**

Type

Description

Promise<number>

Promise used to return the volume

**Example** ``` audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the volume is obtained.' + value); ) ``` ### getMinVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void Obtains the minimum volume allowed for a stream. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

callback

AsyncCallback<number>

Yes

Callback used to return the minimum volume

**Return values** None **Example** ``` audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the minimum volume. ${err.message}'); return; } console.log('Callback invoked to indicate that the minimum volume is obtained.' + value); }) ``` ### getMinVolume\(volumeType: AudioVolumeType\): Promise Obtains the minimum volume allowed for a stream. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

**Return values**

Type

Description

Promise<number>

Promise used to return the minimum volume

**Example** ``` audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promised returned to indicate that the minimum volume is obtained.' + value); ) ``` ### getMaxVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void Obtains the maximum volume allowed for a stream. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

callback

AsyncCallback<number>

Yes

Callback used to return the maximum volume

**Return values** None **Example** ``` audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the maximum volume. ${err.message}'); return; } console.log('Callback invoked to indicate that the maximum volume is obtained.' + value); }) ``` ### getMaxVolume\(volumeType: AudioVolumeType\): Promise Obtains the maximum volume allowed for a stream. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

**Return values**

Type

Description

Promise<number>

Promise used to return the maximum volume

**Example** ``` audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data)=> console.log('Promised returned to indicate that the maximum volume is obtained.'); ) ``` ### mute\(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback\): void7+ Mutes a stream. This method uses an asynchronous callback to return the execution result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

mute

boolean

Yes

Mute status to set. The value true means to mute the stream, and false means the opposite.

callback

AsyncCallback<void>

Yes

Callback used to return the execution result

**Return values** None **Example** ``` audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => { if (err) { console.error('Failed to mute the stream. ${err.message}'); return; } console.log('Callback invoked to indicate that the stream is muted.'); }) ``` ### mute\(volumeType: AudioVolumeType, mute: boolean\): Promise7+ Mutes a stream. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

mute

boolean

Yes

Mute status to set. The value true means to mute the stream, and false means the opposite.

**Return values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => console.log('Promise returned to indicate that the stream is muted.'); ) ``` ### isMute\(volumeType: AudioVolumeType, callback: AsyncCallback\): void7+ Checks whether a stream is muted. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

callback

AsyncCallback<boolean>

Yes

Callback used to return the mute status of the stream. The value true means that the stream is muted, and false means the opposite.

**Return values** None **Example** ``` audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the mute status. ${err.message}'); return; } console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value); }) ``` ### isMute\(volumeType: AudioVolumeType\): Promise7+ Checks whether a stream is muted. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

**Return values**

Type

Description

Promise<boolean>

Promise used to return the mute status of the stream. The value true means that the stream is muted, and false means the opposite.

**Example** ``` audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value); ) ``` ### isActive\(volumeType: AudioVolumeType, callback: AsyncCallback\): void7+ Checks whether a stream is active. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

callback

AsyncCallback<boolean>

Yes

Callback used to return the active status of the stream. The value true means that the stream is active, and false means the opposite.

**Return values** None **Example** ``` audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the active status of the stream. ${err.message}'); return; } console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value); }) ``` ### isActive\(volumeType: AudioVolumeType\): Promise7+ Checks whether a stream is active. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type

**Return values**

Type

Description

Promise<boolean>

Promise used to return the active status of the stream. The value true means that the stream is active, and false means the opposite.

**Example** ``` audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the active status of the stream is obtained.' + value); ) ``` ### setRingerMode\(mode: AudioRingMode, callback: AsyncCallback\): void7+ Sets the ringer mode. This method uses an asynchronous callback to return the execution result. **Parameters**

Name

Type

Mandatory

Description

mode

AudioRingMode

Yes

Ringer mode

callback

AsyncCallback<void>

Yes

Callback used to return the execution result

**Return values** None **Example** ``` audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => { if (err) { console.error('Failed to set the ringer mode.​ ${err.message}'); return; } console.log('Callback invoked to indicate a successful setting of the ringer mode.'); }) ``` ### setRingerMode\(mode: AudioRingMode\): Promise7+ Sets the ringer mode. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

mode

AudioRingMode

Yes

Ringer mode

**Return values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => console.log('Promise returned to indicate a successful setting of the ringer mode.'); ) ``` ### getRingerMode\(callback: AsyncCallback\): void7+ Obtains the ringer mode. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

callback

AsyncCallback<AudioRingMode>

Yes

Callback used to return the ringer mode

**Return values** None **Example** ``` audioManager.getRingerMode((err, value) => { if (err) { console.error('Failed to obtain the ringer mode.​ ${err.message}'); return; } console.log('Callback invoked to indicate that the ringer mode is obtained.' + value); }) ``` ### getRingerMode\(\): Promise7+ Obtains the ringer mode. This method uses a promise to return the query result. **Parameters** None **Return values**

Type

Description

Promise<AudioRingMode>

Promise used to return the ringer mode

**Example** ``` audioManager.getRingerMode().then((value) => console.log('Promise returned to indicate that the ringer mode is obtained.' + value); ) ``` ### setAudioParameter\(key: string, value: string, callback: AsyncCallback\): void7+ Sets an audio parameter. This method uses an asynchronous callback to return the execution result. **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 execution result

**Return values** None **Example** ``` audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => { if (err) { console.error('Failed to set the audio parameter. ${err.message}'); return; } console.log('Callback invoked to indicate a successful setting of the audio parameter.'); }) ``` ### setAudioParameter\(key: string, value: string\): Promise7+ Sets an audio parameter. This method uses a promise to return the execution result. **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 values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => console.log('Promise returned to indicate a successful setting of the audio parameter.'); ) ``` ### getAudioParameter\(key: string, callback: AsyncCallback\): void7+ Obtains the value of an audio parameter. This method uses an asynchronous callback to return the query result. **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 value of the audio parameter

**Return values** None **Example** ``` audioManager.getAudioParameter('PBits per sample', (err, value) => { if (err) { console.error('Failed to obtain the value of the audio parameter. ${err.message}'); return; } console.log('Callback invoked to indicate that the value of the audio parameter is obtained.' + value); }) ``` ### getAudioParameter\(key: string\): Promise7+ Obtains the value of an audio parameter. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

key

string

Yes

Key of the audio parameter whose value is to be obtained

**Return values**

Type

Description

Promise<string>

Promise used to return the value of the audio parameter

**Example** ``` audioManager.getAudioParameter('PBits per sample').then((value) => console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value); ) ``` ### getDevices\(deviceFlag: DeviceFlag, callback: AsyncCallback\): void Obtains the audio devices with a specific flag. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

deviceFlag

DeviceFlag

Yes

Audio device flag

callback

AsyncCallback<AudioDeviceDescriptors>

Yes

Callback used to return the device list

**Return values** None **Example** ``` audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{ if (err) { console.error('Failed to obtain the device list. ${err.message}'); return; } console.log('Callback invoked to indicate that the device list is obtained.'); }) ``` ### getDevices\(deviceFlag: DeviceFlag\): Promise Obtains the audio devices with a specific flag. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

deviceFlag

DeviceFlag

Yes

Audio device flag

**Return values**

Type

Description

Promise<AudioDeviceDescriptors>

Promise used to return the device list

**Example** ``` audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data)=> console.log('Promise returned to indicate that the device list is obtained.'); ) ``` ### setDeviceActive\(deviceType: DeviceType, active: boolean, callback: AsyncCallback\): void7+ Sets a device to the active state. This method uses an asynchronous callback to return the execution result. **Parameters**

Name

Type

Mandatory

Description

deviceType

DeviceType

Yes

Audio device type

active

boolean

Yes

Active status to set. The value true means to set the device to the active status, and false means the opposite.

callback

AsyncCallback<void>

Yes

Callback used to return the execution result

**Return values** None **Example** ``` audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true, (err)=> { if (err) { console.error('Failed to set the active status of the device. ${err.message}'); return; } console.log('Callback invoked to indicate that the device is set to the active status.'); }) ``` ### setDeviceActive\(deviceType: DeviceType, active: boolean\): Promise7+ Sets a device to the active state. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

deviceType

DeviceType

Yes

Audio device type

active

boolean

Yes

Active status to set. The value true means to set the device to the active status, and false means the opposite.

**Return values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(()=> console.log('Promise returned to indicate that the device is set to the active status.'); ) ``` ### isDeviceActive\(deviceType: DeviceType, callback: AsyncCallback\): void7+ Checks whether a device is active. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

deviceType

DeviceType

Yes

Audio device type

callback

AsyncCallback<boolean>

Yes

Callback used to return the active status of the device

**Return values** None **Example** ``` audioManager.isDeviceActive(audio.DeviceType.SPEAKER, (err, value) => { if (err) { console.error('Failed to obtain the active status of the device. ${err.message}'); return; } console.log('Callback invoked to indicate that the active status of the device is obtained.'); }) ``` ### isDeviceActive\(deviceType: DeviceType\): Promise7+ Checks whether a device is active. This method uses a promise to return the query result. **Parameters**

Name

Type

Mandatory

Description

deviceType

DeviceType

Yes

Audio device type

**Return values**

Type

Description

Promise<boolean>

Promise used to return the active status of the device

**Example** ``` audioManager.isDeviceActive(audio.DeviceType.SPEAKER).then((value) => console.log('Promise returned to indicate that the active status of the device is obtained.' + value); ) ``` ### setMicrophoneMute\(mute: boolean, callback: AsyncCallback\): void7+ Mutes or unmutes the microphone. This method uses an asynchronous callback to return the execution result. **Parameters**

Name

Type

Mandatory

Description

mute

boolean

Yes

Mute status to set. The value true means to mute the microphone, and false means the opposite.

callback

AsyncCallback<void>

Yes

Callback used to return the execution result

**Return values** None **Example** ``` audioManager.setMicrophoneMute(true, (err) => { if (err) { console.error('Failed to mute the microphone. ${err.message}'); return; } console.log('Callback invoked to indicate that the microphone is muted.'); }) ``` ### setMicrophoneMute\(mute: boolean\): Promise7+ Mutes or unmutes the microphone. This method uses a promise to return the execution result. **Parameters**

Name

Type

Mandatory

Description

mute

boolean

Yes

Mute status to set. The value true means to mute the microphone, and false means the opposite.

**Return values**

Type

Description

Promise<void>

Promise used to return the execution result

**Example** ``` audioManager.setMicrophoneMute(true).then(() => console.log('Promise returned to indicate that the microphone is muted.'); ) ``` ### isMicrophoneMute\(callback: AsyncCallback\): void7+ Checks whether the microphone is muted. This method uses an asynchronous callback to return the query result. **Parameters**

Name

Type

Mandatory

Description

callback

AsyncCallback<boolean>

Yes

Callback used to return the mute status of the microphone. The value true means that the microphone is muted, and false means the opposite.

**Return values** None **Example** ``` audioManager.isMicrophoneMute((err, value) => { if (err) { console.error('Failed to obtain the mute status of the microphone. ${err.message}'); return; } console.log('Callback invoked to indicate that the mute status of the microphone is obtained.' + value); }) ``` ### isMicrophoneMute\(\): Promise7+ Checks whether the microphone is muted. This method uses a promise to return the query result. **Parameters** None **Return values**

Type

Description

Promise<boolean>

Promise used to return the mute status of the microphone. The value true means that the microphone is muted, and false means the opposite.

**Example** ``` audioManager.isMicrophoneMute().then((value) => console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value); ) ``` ## AudioDeviceDescriptor Describes an audio device. ### Attributes

Name

Type

Readable

Writable

Description

deviceRole

DeviceRole

Yes

No

Audio device role

deviceType

DeviceType

Yes

No

Audio device type

## AudioDeviceDescriptors

Name

Description

Device attribute array

Array of AudioDeviceDescriptors, which is read-only