# Audio This module provides the following functions: audio management, audio rendering and system sound management. --- ## ***Note:*** Changes to the AudioRenderer interface have been proposed. When the updated APIs have been integrated, the document will be revised, and apps must adapt to it. --- ## Modules to Import ``` import audio from '@ohos.multimedia.audio'; ``` ## Required Permissions None ## audioManager getAudioManager\(\): AudioManager Obtains an **AudioManager** instance. **Return value**

Type

Description

AudioManager

AudioManager object.

**Example** ``` var audioManager = audio.getAudioManager(); ``` ## audioRenderer createAudioRenderer(volumeType: AudioVolumeType): AudioRenderer Obtains an **AudioRenderer** instance. **Parameters** | Name | Type | Mandatory | Description | | :--------- | :-------------- | :-------- | :----------------- | | volumeType | AudioVolumeType | Yes | Audio stream type. | **Return value** | Type | Description | | ------------- | --------------------- | | AudioRenderer | AudioRenderer object. | **Example** ``` const volType = audio.AudioVolumeType.MEDIA; const audioRenderer = audio.createAudioRenderer(volType); ``` ## systemSoundManager getSystemSoundManager(): SystemSoundManager Obtains a **SystemSoundManager** instance. **Parameters** None **Return value** | Type | Description | | ------------------ | -------------------------- | | SystemSoundManager | SystemSoundManager object. | **Example** ``` const systemSoundManager = audio.getSystemSoundManager(); ``` ## 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.

## AudioSampleFormat8+ Enumerates the audio sample formats. | Name | Default Value | Description | | :------------ | :------------ | :------------------------------------ | | INVALID_WIDTH | -1 | Invalid format. | | SAMPLE_U8 | 1 | Unsigned 8 bit integer. | | SAMPLE_S16LE | 0 | Signed 16 bit integer, little endian. | | SAMPLE_S24LE | 1 | Signed 24 bit integer, little endian. | | SAMPLE_S32LE | 2 | Signed 32 bit integer, little endian. | ## AudioChannel8+ Enumerates the audio channels. | Name | Default Value | Description | | :----- | :------------ | :--------------- | | MONO | 1 | Channel count 1. | | STEREO | 2 | Channel count 2. | ## AudioSamplingRate8+ Enumerates the audio sampling rates. | Name | Default Value | Description | | :---------------- | :------------ | :------------------- | | SAMPLE_RATE_8000 | 8000 | Sampling rate 8000. | | SAMPLE_RATE_11025 | 11025 | Sampling rate 11025. | | SAMPLE_RATE_12000 | 12000 | Sampling rate 12000. | | SAMPLE_RATE_16000 | 16000 | Sampling rate 16000. | | SAMPLE_RATE_22050 | 22050 | Sampling rate 22050. | | SAMPLE_RATE_24000 | 24000 | Sampling rate 24000. | | SAMPLE_RATE_32000 | 32000 | Sampling rate 32000. | | SAMPLE_RATE_44100 | 44100 | Sampling rate 44100. | | SAMPLE_RATE_48000 | 48000 | Sampling rate 48000. | | SAMPLE_RATE_64000 | 64000 | Sampling rate 64000. | | SAMPLE_RATE_96000 | 96000 | Sampling rate 96000. | ## AudioEncodingType8+ Enumerates the audio encoding types. | Name | Default Value | Description | | :--------------- | :------------ | :---------- | | ENCODING_PCM | 0 | PCM. | | ENCODING_INVALID | 1 | Invalid. | ## ContentType8+ Enumerates the content types. | Name | Default Value | Description | | :------------------------ | :------------ | :-------------------- | | CONTENT_TYPE_UNKNOWN | 0 | Unknown content. | | CONTENT_TYPE_SPEECH | 1 | Speech content. | | CONTENT_TYPE_MUSIC | 2 | Music content. | | CONTENT_TYPE_MOVIE | 3 | Movie content. | | CONTENT_TYPE_SONIFICATION | 4 | Notification content. | | CONTENT_TYPE_RINGTONE | 5 | Ringtone content. | ## StreamUsage8+ Enumerates the stream usage. | Name | Default Value | Description | | :--------------------------------- | :------------ | :------------------------------ | | STREAM_USAGE_UNKNOWN | 0 | Unknown usage. | | STREAM_USAGE_MEDIA | 1 | Media usage. | | STREAM_USAGE_VOICE_COMMUNICATION | 2 | Voice communication usage. | | STREAM_USAGE_NOTIFICATION_RINGTONE | 3 | Notification or ringtone usage. | ## AudioState8+ Enumerates the audio states. | Name | Default Value | Description | | :------------- | :------------ | :------------------------- | | STATE_INVALID | -1 | Invalid state. | | STATE_NEW | 0 | Create New instance state. | | STATE_PREPARED | 1 | Prepared state. | | STATE_RUNNING | 2 | Running state. | | STATE_STOPPED | 3 | Stopped state. | | STATE_RELEASED | 4 | Released state. | | STATE_PAUSED | 5 | Paused state. | ## AudioRendererRate8+ Enumerates the audio renderer rates. | Name | Default Value | Description | | :----------------- | :------------ | :----------- | | RENDER_RATE_NORMAL | 0 | Normal rate. | | RENDER_RATE_DOUBLE | 1 | Double rate. | | RENDER_RATE_HALF | 2 | Half rate. | ## InterruptType8+ Enumerates the interrupt types. | Name | Default Value | Description | | :------------------- | :------------ | :----------------------------------- | | INTERRUPT_TYPE_BEGIN | 1 | Audio playback interruption started. | | INTERRUPT_TYPE_END | 2 | Audio playback interruption ended. | ## InterruptForceType8+ Enumerates the interrupt force types. | Name | Default Value | Description | | :-------------- | :------------ | :--------------------------------------- | | INTERRUPT_FORCE | 0 | Forced action taken by system. | | INTERRUPT_SHARE | 1 | App can choose to take action or ignore. | ## InterruptHint8+ Enumerates the interrupt hints. | Name | Default Value | Description | | :-------------------- | :------------ | :------------------------- | | INTERRUPT_HINT_NONE | 0 | None. | | INTERRUPT_HINT_RESUME | 1 | Resume the playback. | | INTERRUPT_HINT_PAUSE | 2 | Paused/Pause the playback. | | INTERRUPT_HINT_STOP | 3 | Stopped/Stop the playback. | | INTERRUPT_HINT_DUCK | 4 | Ducked the playback. | | INTERRUPT_HINT_UNDUCK | 5 | Unducked the playback. | ## RingtoneType8+ Enumerates the ringtone types. | Name | Default Value | Description | | :--------------------- | :------------ | :-------------- | | RINGTONE_TYPE_DEFAULT | 0 | Default type. | | RINGTONE_TYPE_MULTISIM | 1 | Multi-SIM type. | ## AudioParameters8+ Describes audio parameters of playback files. **Parameters** | Name | Type | Mandatory | Description | | :----------- | :---------------- | :-------- | :-------------------------------------------- | | format | AudioSampleFormat | Yes | Sample format of the audio file to be played. | | channels | AudioChannel | Yes | Channel count of the audio file to be played. | | samplingRate | AudioSamplingRate | Yes | Sample rate of the audio file to be played. | | encoding | AudioEncodingType | Yes | Encoding type of the audio file to be played. | | contentType | ContentType | Yes | Content type. | | usage | StreamUsage | Yes | Stream usage. | | deviceRole | DeviceRole | Yes | Device role. | | deviceType | DeviceType | Yes | Device type. | ## AudioRendererInfo8+ Describes audio renderer information. **Parameters** | Name | Type | Mandatory | Description | | :------------ | :---------- | :-------- | :-------------------- | | contentType | ContentType | Yes | Content type. | | usage | StreamUsage | Yes | Stream usage. | | rendererFlags | number | Yes | Audio renderer flags. | ## InterruptEvent8+ Describes the interrupt event received by the app when playback is interrupted. **Parameters** | Name | Type | Mandatory | Description | | :-------- | :----------------- | :-------- | :-------------------------------------------------------------------------- | | eventType | InterruptType | Yes | Indicates whether the interruption has started or finished. | | forceType | InterruptForceType | Yes | Indicates whether the action is taken by system or to be taken by the app. | | hintType | InterruptHint | Yes | Indicates the kind of action. | ## VolumeEvent8+ Describes the volume event received by the app when the volume is changed. **Parameters** | Name | Type | Mandatory | Description | | :--------- | :-------------- | :-------- | :--------------------------------------- | | volumeType | AudioVolumeType | Yes | Volume type of the current stream. | | volume | number | Yes | Volume level. | | updateUi | boolean | Yes | Whether to show the volume change in UI. | ## RingtoneOptions8+ Describes ringtone options. **Parameters** | Name | Type | Mandatory | Description | | :----- | :------ | :-------- | :--------------- | | volume | number | Yes | Ringtone volume. | | loop | boolean | Yes | Loop value. | # AudioManager Implements audio volume and audio device management. ## audioManager.setVolume setVolume\(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback\): void Sets the volume for a stream. This method uses an asynchronous callback to return the 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 result.

**Return value** 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.'); }) ``` ## audioManager.setVolume setVolume\(volumeType: AudioVolumeType, volume: number\): Promise Sets the volume for a stream. This method uses a promise to return the 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 value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=> console.log('Promise returned to indicate a successful volume setting.'); ) ``` ## audioManager.getVolume 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 value** 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.'); }) ``` ## audioManager.getVolume 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 value**

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); ) ``` ## audioManager.getMinVolume 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 value** 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); }) ``` ## audioManager.getMinVolume 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 value**

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); ) ``` ## audioManager.getMaxVolume 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 value** 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); }) ``` ## audioManager.getMaxVolume 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 value**

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.'); ) ``` ## audioManager.mute mute\(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback\): void7+ Mutes a stream. This method uses an asynchronous callback to return the 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 result.

**Return value** 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.'); }) ``` ## audioManager.mute mute\(volumeType: AudioVolumeType, mute: boolean\): Promise7+ Mutes a stream. This method uses a promise to return the 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 value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => console.log('Promise returned to indicate that the stream is muted.'); ) ``` ## audioManager.isMute 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 value** 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); }) ``` ## audioManager.isMute isMute\(volumeType: AudioVolumeType\): Promise7+ Checks whether a stream is muted. This method uses a promise to return the result. **Parameters**

Name

Type

Mandatory

Description

volumeType

AudioVolumeType

Yes

Audio stream type.

**Return value**

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); ) ``` ## audioManager.isActive 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 value** 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); }) ``` ## audioManager.isActive 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 value**

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); ) ``` ## audioManager.setRingerMode setRingerMode\(mode: AudioRingMode, callback: AsyncCallback\): void7+ Sets the ringer mode. This method uses an asynchronous callback to return the result. **Parameters**

Name

Type

Mandatory

Description

mode

AudioRingMode

Yes

Ringer mode.

callback

AsyncCallback<void>

Yes

Callback used to return the result.

**Return value** 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.'); }) ``` ## audioManager.setRingerMode setRingerMode\(mode: AudioRingMode\): Promise7+ Sets the ringer mode. This method uses a promise to return the result. **Parameters**

Name

Type

Mandatory

Description

mode

AudioRingMode

Yes

Ringer mode.

**Return value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => console.log('Promise returned to indicate a successful setting of the ringer mode.'); ) ``` ## audioManager.getRingerMode 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 value** 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); }) ``` ## audioManager.getRingerMode getRingerMode\(\): Promise7+ Obtains the ringer mode. This method uses a promise to return the query result. **Parameters** None **Return value**

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); ) ``` ## audioManager.setAudioParameter setAudioParameter\(key: string, value: string, callback: AsyncCallback\): void7+ Sets an audio parameter. This method uses an asynchronous callback to return the 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 result.

**Return value** 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.'); }) ``` ## audioManager.setAudioParameter setAudioParameter\(key: string, value: string\): Promise7+ Sets an audio parameter. This method uses a promise to return the 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 value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => console.log('Promise returned to indicate a successful setting of the audio parameter.'); ) ``` ## audioManager.getAudioParameter 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 value** 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); }) ``` ## audioManager.getAudioParameter 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 value**

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); ) ``` ## audioManager.getDevices 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 value** 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.'); }) ``` ## audioManager.getDevices 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 value**

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.'); ) ``` ## audioManager.setDeviceActive setDeviceActive\(deviceType: DeviceType, active: boolean, callback: AsyncCallback\): void7+ Sets a device to the active state. This method uses an asynchronous callback to return the 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 result.

**Return value** 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.'); }) ``` ## audioManager.setDeviceActive setDeviceActive\(deviceType: DeviceType, active: boolean\): Promise7+ Sets a device to the active state. This method uses a promise to return the 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 value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(()=> console.log('Promise returned to indicate that the device is set to the active status.'); ) ``` ## audioManager.isDeviceActive 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 value** 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.'); }) ``` ## audioManager.isDeviceActive 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 value**

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); ) ``` ## audioManager.setMicrophoneMute setMicrophoneMute\(mute: boolean, callback: AsyncCallback\): void7+ Mutes or unmutes the microphone. This method uses an asynchronous callback to return the 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 result.

**Return value** 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.'); }) ``` ## audioManager.setMicrophoneMute setMicrophoneMute\(mute: boolean\): Promise7+ Mutes or unmutes the microphone. This method uses a promise to return the 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 value**

Type

Description

Promise<void>

Promise used to return the result.

**Example** ``` audioManager.setMicrophoneMute(true).then(() => console.log('Promise returned to indicate that the microphone is muted.'); ) ``` ## audioManager.isMicrophoneMute 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 value** 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); }) ``` ## audioManager.isMicrophoneMute isMicrophoneMute\(\): Promise7+ Checks whether the microphone is muted. This method uses a promise to return the query result. **Parameters** None **Return value**

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); ) ``` ## audioManager.on on(type: 'volumeChange', callback: Callback): void8+ Listens for system volume change events. This method uses a callback to get volume change events. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :--------------------------------------------------- | | type | string | Yes | Type of the playback event to listen for. | | callback | Callback | Yes | Callback used to get the system volume change event. | **Return value** None **Example** ``` audioManager.on('volumeChange', (volumeEvent) => { console.log('VolumeType of stream: ' + volumeEvent.volumeType); console.log('Volume level: ' + volumeEvent.volume); console.log('Whether to updateUI: ' + volumeEvent.updateUi); }) ``` ## audioManager.on on(type: 'ringerModeChange', callback: Callback): void8+ Listens for ringer mode change events. This method uses a callback to get ringer mode changes. **Parameters** | Name | Type | Mandatory | Description | | :------- | :----------------------- | :-------- | :-------------------------------------------- | | type | string | Yes | Type of the playback event to listen for. | | callback | Callback | Yes | Callback used to get the updated ringer mode. | **Return value** None **Example** ``` audioManager.on('ringerModeChange', (ringerMode) => { console.log('Updated ringermode: ' + ringerMode); }) ``` # AudioDeviceDescriptor Describes an audio device. ## AudioDeviceDescriptors type AudioDeviceDescriptors = Array\> : void\
Array of AudioDeviceDescriptors, which is read-only. ## audioDeviceDescriptor.deviceRole readonly deviceRole: DeviceRole Defines the role of the device. | Name | Type | Readable | Writable | Description | | :--------- | :--------- | :------- | :------- | ------------------ | | deviceRole | DeviceRole | Yes | No | Audio device role. | ## audioDeviceDescriptor.deviceType readonly deviceType: DeviceType Defines the type of the device. | Name | Type | Readable | Writable | Description | | :--------- | :--------- | :------- | :------- | ------------------ | | deviceType | DeviceType | Yes | No | Audio device type. | ``` function deviceProp(audioDeviceDescriptor, index, array) { deviceRoleValue = audioDeviceDescriptor.deviceRole; deviceTypeValue = audioDeviceDescriptor.deviceType; } deviceRoleValue = null; deviceTypeValue = null; const promise = audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG); promise.then(async function (audioDeviceDescriptors) { console.info('getDevices OUTPUT_DEVICES_FLAG'); audioDeviceDescriptors.forEach(deviceProp); if (deviceTypeValue != null && deviceRoleValue != null){ console.info('OUTPUT_DEVICES_FLAG : Pass'); expect(true).assertTrue(); } else{ console.error('OUTPUT_DEVICES_FLAG : fail'); expect(false).assertTrue(); } }); await promise; done(); }) ``` # AudioRenderer Provides audio playback APIs. ## audioRenderer.state readonly state: AudioState 8+ Defines the current render state. | Name | Type | Readable | Writable | Description | | :---- | :--------- | :------- | :------- | :------------------ | | state | AudioState | Yes | No | Audio render state. | **Example** ``` var state = audioRenderer.state; ``` ## audioRenderer.setParams setParams(params: AudioParameters, callback: AsyncCallback): void8+ Sets audio parameters for rendering. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :-------------------------------------- | | params | AudioParameters | Yes | Audio parameters of the file to be set. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` var audioParams = { format: audio.AudioSampleFormat.SAMPLE_S16LE, channels: audio.AudioChannel.STEREO, samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, encoding: audio.AudioEncodingType.ENCODING_PCM, }; audioRenderer.setParams(audioParams, (err)=>{ if (err) { console.error('Failed to set params. ${err.message}'); return; } console.log('Callback invoked to indicate a successful params setting.'); }) ``` ## audioRenderer.setParams setParams(params: AudioParameters): Promise8+ Sets audio parameters for rendering. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :----- | :-------------- | :-------- | :-------------------------------------- | | params | AudioParameters | Yes | Audio parameters of the file to be set. | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` var audioParams = { format: audio.AudioSampleFormat.SAMPLE_S16LE, channels: audio.AudioChannel.STEREO, samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, encoding: audio.AudioEncodingType.ENCODING_PCM, }; await audioRenderer.setParams(audioParams); ``` ## audioRenderer.getParams getParams(callback: AsyncCallback): void8+ Gets audio parameters of the renderer. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------------------ | :-------- | :-------------------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the audio parameters. | | | | | | **Return value** None **Example** ``` audioRenderer.getParams((err, audioParams)=>{ console.log('Renderer GetParams:'); console.log('Renderer format:' + audioParams.format); console.log('Renderer samplingRate:' + audioParams.samplingRate); console.log('Renderer channels:' + audioParams.channels); console.log('Renderer encoding:' + audioParams.encoding); }) ``` ## audioRenderer.getParams getParams(): Promise8+ Gets audio parameters of the renderer. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :------------------------ | :------------------------------------------- | | Promise | Promise used to return the audio parameters. | **Example** ``` let audioParams = await audioRenderer.getParams(); console.log('Renderer GetParams:'); console.log('Renderer format:' + audioParams.format); console.log('Renderer samplingRate:' + audioParams.samplingRate); console.log('Renderer channels:' + audioParams.channels); console.log('Renderer encoding:' + audioParams.encoding); ``` ## audioRenderer.start start(callback: AsyncCallback): void8+ Starts the renderer. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :----------------------------------------------------------------------------- | | callback | AsyncCallback | Yes | Returns true if the renderer is started successfully; returns false otherwise. | | | | | | **Return value** None **Example** ``` audioRenderer.start((err, started)=>{ if (started) { console.log('Renderer started.'); } else { console.error('Renderer start rejected.'); } }) ``` ## audioRenderer.start start(): Promise8+ Starts the renderer. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :---------------- | :----------------------------------------------------------------------------- | | Promise | Returns true if the renderer is started successfully; returns false otherwise. | **Example** ``` var started = await audioRenderer.start(); if (started) { console.log('Renderer started'); } else { console.error('Renderer start rejected'); } ``` ## audioRenderer.pause pause(callback: AsyncCallback): void8+ Pauses rendering. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :---------------------------------------------------------------------------- | | callback | AsyncCallback | Yes | Returns true if the renderer is paused successfully; returns false otherwise. | | | | | | **Return value** None **Example** ``` audioRenderer.pause((err, paused)=>{ if (paused) { console.log('Renderer paused.'); } else { console.error('Renderer pause failed'); } }) ``` ## audioRenderer.pause pause(): Promise8+ Pauses rendering. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :---------------- | :---------------------------------------------------------------------------- | | Promise | Returns true if the renderer is paused successfully; returns false otherwise. | **Example** ``` var paused = await audioRenderer.pause(); if (paused) { console.log('Renderer paused'); } else { console.error('Renderer pause failed'); } ``` ## audioRenderer.drain drain(callback: AsyncCallback): void8+ Drains the playback buffer. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :--------------------------------------------------------------------------- | | callback | AsyncCallback | Yes | Returns true if the buffer is drained successfully; returns false otherwise. | | | | | | **Return value** None **Example** ``` audioRenderer.drain((err, drained)=>{ if (drained) { console.log('Renderer drained.'); } else { console.error('Renderer drain failed'); } }) ``` ## audioRenderer.drain drain(): Promise8+ Drains the playback buffer. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :---------------- | :--------------------------------------------------------------------------- | | Promise | Returns true if the buffer is drained successfully; returns false otherwise. | **Example** ``` var drained = await audioRenderer.drain(); if (drained) { console.log('Renderer drained'); } else { console.error('Renderer drain failed'); } ``` ## audioRenderer.stop stop(callback: AsyncCallback): void8+ Stops rendering. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :------------------------------------------------------------------------------ | | callback | AsyncCallback | Yes | Returns true if the rendering is stopped successfully; returns false otherwise. | | | | | | **Return value** None **Example** ``` audioRenderer.stop((err, stopped)=>{ if (stopped) { console.log('Renderer stopped.'); } else { console.error('Renderer stop failed'); } }) ``` ## audioRenderer.stop stop(): Promise8+ Stops rendering. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :---------------- | :------------------------------------------------------------------------------ | | Promise | Returns true if the rendering is stopped successfully; returns false otherwise. | **Example** ``` var stopped = await audioRenderer.stop(); if (stopped) { console.log('Renderer stopped'); } else { console.error('Renderer stop failed'); } ``` ## audioRenderer.release release(callback: AsyncCallback): void8+ Releases the renderer. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :------------------------------------------------------------------------------ | | callback | AsyncCallback | Yes | Returns true if the renderer is released successfully; returns false otherwise. | | | | | | **Return value** None **Example** ``` audioRenderer.release((err, released)=>{ if (released) { console.log('Renderer released.'); } else { console.error('Renderer release failed'); } }) ``` ## audioRenderer.release release(): Promise8+ Releases the renderer. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :---------------- | :------------------------------------------------------------------------------ | | Promise | Returns true if the renderer is released successfully; returns false otherwise. | **Example** ``` var released = await audioRenderer.release(); if (released) { console.log('Renderer released'); } else { console.error('Renderer release failed'); } ``` ## audioRenderer.write write(buffer: ArrayBuffer, callback: AsyncCallback): void8+ Writes the buffer. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :---------------------- | :-------- | :--------------------------------------------------------------------------------------------------- | | buffer | ArrayBuffer | Yes | Buffer to be written. | | callback | AsyncCallback | Yes | Returns the number of bytes written if the operation is successful; returns an error code otherwise. | | | | | | **Return value** None **Example** ``` let ss = fileio.createStreamSync(filePath, 'r'); let buf = new ArrayBuffer(bufferSize); ss.readSync(buf); audioRenderer.write(buf, (err, writtenbytes)=>{ if (writtenbytes < 0) { console.error('write failed.'); } else { console.log('Actual written bytes: ' + writtenbytes); } }) ``` ## audioRenderer.write write(buffer: ArrayBuffer): Promise8+ Writes the buffer. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :--------------------------------------------------------------------------------------------------- | | Promise | Returns the number of bytes written if the operation is successful; returns an error code otherwise. | **Example** ``` let ss = fileio.createStreamSync(filePath, 'r'); let buf = new ArrayBuffer(bufferSize); ss.readSync(buf); let writtenbytes = await audioRenderer.write(buf); if (writtenbytes < 0) { console.error('write failed.'); } else { console.log('Actual written bytes: ' + writtenbytes); } ``` ## audioRenderer.getAudioTime getAudioTime(callback: AsyncCallback): void8+ Obtains the timestamp. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :------------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the timestamp. | | | | | | **Return value** None **Example** ``` audioRenderer.getAudioTime((err, timestamp)=>{ console.log('Current timestamp: ' + timestamp); }) ``` ## audioRenderer.getAudioTime getAudioTime(): Promise8+ Obtains the timestamp. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :------------------------------------ | | Promise | Promise used to return the timestamp. | **Example** ``` let timestamp = await audioRenderer.getAudioTime(); console.log('Current timestamp: ' + timestamp); ``` ## audioRenderer.getBufferSize getBufferSize(callback: AsyncCallback): void8+ Obtains a reasonable minimum buffer size for rendering. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :--------------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the buffer size. | | | | | | **Return value** None **Example** ``` audioRenderer.getBufferSize((err, bufferSize)=>{ if (err) { console.error('getBufferSize error'); } }) let buf = new ArrayBuffer(bufferSize); ss.readSync(buf); ``` ## audioRenderer.getBufferSize getBufferSize(): Promise8+ Obtains a reasonable minimum buffer size for rendering. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :-------------------------------------- | | Promise | Promise used to return the buffer size. | **Example** ``` var bufferSize = await audioRenderer.getBufferSize(); let buf = new ArrayBuffer(bufferSize); ss.readSync(buf); ``` ## audioRenderer.setRenderRate setRenderRate(rate: AudioRendererRate, callback: AsyncCallback): void8+ Sets the render rate. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | rate | AudioRendererRate | Yes | Audio render rate. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err)=> { if (err) { console.error('Failed to set params'); } else { console.log('Callback invoked to indicate a successful render rate setting.'); } }) ``` ## audioRenderer.setRenderRate setRenderRate(rate: AudioRendererRate): Promise8+ Sets the render rate. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :--- | :---------------- | :-------- | :----------------- | | rate | AudioRendererRate | Yes | Audio render rate. | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL); ``` ## audioRenderer.getRenderRate getRenderRate(callback: AsyncCallback): void8+ Obtains the current render rate. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :-------------------------------- | :-------- | :--------------------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the audio render rate. | | | | | | **Return value** None **Example** ``` audioRenderer.getRenderRate((err, renderrate)=>{ console.log('getRenderRate: ' + renderrate); }) ``` ## audioRenderer.getRenderRate getRenderRate(): Promise8+ Obtains the current render rate. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :-------------------------- | :-------------------------------------------- | | Promise | Promise used to return the audio render rate. | **Example** ``` let renderRate = await audioRenderer.getRenderRate(); console.log('getRenderRate: ' + renderrate); ``` ## audioRenderer.on on(type: 'interrupt', callback: Callback): void8+ Listens for audio interrupt events. This method uses a callback to get interrupt events. The interrupt event is triggered when audio playback is interrupted. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------------ | :-------- | :---------------------------------------------- | | type | string | Yes | Type of the playback event to listen for. | | callback | Callback | Yes | Callback used to listen for interrupt callback. | **Return value** None **Example** ``` audioRenderer.on('interrupt', (interruptEvent) => { if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { switch (interruptEvent.hintType) { case audio.InterruptHint.INTERRUPT_HINT_PAUSE: console.log('Force paused. Stop writing'); isPlay = false; break; case audio.InterruptHint.INTERRUPT_HINT_STOP: console.log('Force stopped. Stop writing'); isPlay = false; break; } } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { switch (interruptEvent.hintType) { case audio.InterruptHint.INTERRUPT_HINT_RESUME: console.log('Resume force paused renderer or ignore'); startRenderer(); break; case audio.InterruptHint.INTERRUPT_HINT_PAUSE: console.log('Choose to pause or ignore'); pauseRenderer(); break; } } }) ``` ## SystemSoundManager ## systemSoundManager.setSystemRingtoneUri setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType, callback: AsyncCallback): void8+ Sets the system ringtone URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | Ringtone URI to be set. | | type | RingtoneType | Yes | Ringtone type to be set. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtonetone.wav', audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err)=> { if (err) { console.error('Failed to setSystemRingtoneUri'); } else { console.log('Callback invoked to indicate a successful system ringtone URI setting.'); } }) ``` ## systemSoundManager.setSystemRingtoneUri setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType): Promise8+ Sets the system ringtone URI. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------ | :----------- | :-------- | :--------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | Ringtone URI to be set. | | type | RingtoneType | Yes | Ringtone type to be set. | | | | | | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtone.wav', audio.RingtoneType.RINGTONE_TYPE_DEFAULT); ``` ## systemSoundManager.getSystemRingtoneUri getSystemRingtoneUri(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ Obtains the system ringtone URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :---------------------------------- | | context | Context | Yes | Current application context. | | type | RingtoneType | Yes | Ringtone type to be obtained. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtoneUri)=>{ if (err) { console.err('getSystemRingtoneUri failed'); } else { console.log('getSystemRingtoneUri success: ' + ringtoneUri); } }) ``` ## systemSoundManager.getSystemRingtoneUri getSystemRingtoneUri(context: Context, type: RingtoneType): Promise8+ Obtains the system ringtone URI. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :-------------------------------- | | Promise | Promise used to the ringtone URI. | **Example** ``` let ringtoneUri = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); if (ringtoneUri == '/data/media/Believer60s.wav') { console.log('getSystemRingtoneUri success: ' + uri); } else { console.log('getSystemRingtoneUri fail: ' + uri); } ``` ## systemSoundManager.getSystemRingtonePlayer getSystemRingtonePlayer(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ Obtains the ringtone player. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------------------ | :-------- | :---------------------------------------- | | context | Context | Yes | Current application context. | | type | RingtoneType | Yes | Ringtone type to be obtained. | | callback | AsyncCallback) | Yes | Ringtone player maintained in the system. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ if (err) { console.err('getSystemRingtonePlayer failed'); } else { console.log('getSystemRingtonePlayer success: '); } }) ``` ## systemSoundManager.getSystemRingtonePlayer getSystemRingtonePlayer(context: Context, type: RingtoneType): Promise8+ Obtains the ringtone player. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :----------------------- | :--------------------------------------- | | Promise | Promise used return the ringtone player. | **Example** ``` let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); ``` ## systemSoundManager.setSystemNotificationUri setSystemNotificationUri(context: Context, uri: string, callback: AsyncCallback): void8+ Sets the system notification URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | System notification URI to be set. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'), (err)=> { if (err) { console.error('Failed to setSystemNotificationUri'); } else { console.log('Callback invoked to indicate a successful system notification URI setting.'); } }) ``` ## systemSoundManager.setSystemNotificationUri setSystemNotificationUri(context: Context, uri: string): Promise8+ Sets the system notification URI. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------ | :------ | :-------- | :--------------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | System notification URI to be set. | | | | | | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'); ``` ## systemSoundManager.getSystemNotificationUri getSystemNotificationUri(context: Context, callback: AsyncCallback): void8+ Obtains the system notification URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :--------------------------------------------------- | | context | Context | Yes | Current application context. | | callback | AsyncCallback | Yes | Callback used to return the system notification URI. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemNotificationUri(null, (err, notificationUri)=>{ if (err) { console.err('getSystemNotificationUri failed'); } else { console.log('getSystemNotificationUri success: ' + notificationUri); } }) ``` ## systemSoundManager.getSystemNotificationUri getSystemNotificationUri(context: Context): Promise8+ Obtains the system notification URI. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :-------------------------------------------------- | | Promise | Promise used to return the system notification URI. | **Example** ``` let notificationUri = await systemSoundManager.getSystemNotificationUri(null); console.log('getSystemNotificationUri : ' + uri); ``` ## systemSoundManager.setSystemAlarmUri setSystemAlarmUri(context: Context, uri: string, callback: AsyncCallback): void8+ Sets the system alarm URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | System alarm URI to be set. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'), (err)=> { if (err) { console.error('Failed to setSystemAlarmUri'); } else { console.log('Callback invoked to indicate a successful system alarm URI setting.'); } }) ``` ## systemSoundManager.setSystemAlarmUri setSystemAlarmUri(context: Context, uri: string): Promise8+ Sets the system alarm URI. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------ | :------ | :-------- | :--------------------------- | | context | Context | Yes | Current application context. | | uri | string | Yes | System alarm URI to be set. | | | | | | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'); ``` ## systemSoundManager.getSystemAlarmUri getSystemAlarmUri(context: Context, callback: AsyncCallback): void8+ Obtains the system alarm URI. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :-------------------------------------------- | | context | Context | Yes | Current application context. | | callback | AsyncCallback | Yes | Callback used to return the system alarm URI. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemAlarmUri(null, (err, alarmUri)=>{ if (err) { console.err('getSystemAlarmUri failed'); } else { console.log('getSystemAlarmUri success: ' + alarmUri); } }) ``` ## systemSoundManager.getSystemAlarmUri getSystemAlarmUri(context: Context): Promise8+ Obtains the system alarm URI. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :------------------------------------------- | | Promise | Promise used to return the system alarm URI. | **Example** ``` let alarmUri = await systemSoundManager.getSystemAlarmUri(null); console.log('getSystemAlarmUri success: ' + alarmUri); ``` # RingtonePlayer ## ringtonePlayer.state readonly state: AudioState 8+ Defines the current ringtone player state. | Name | Type | Readable | Writable | Description | | :---- | :--------- | :------- | :------- | :--------------------- | | state | AudioState | Yes | No | Ringtone player state. | **Example** ``` systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ if (err) { console.err('getSystemRingtonePlayer failed'); return; } else { console.log('getSystemRingtonePlayer success'); } }); var state = ringtonePlayer.state; ``` ## ringtonePlayer.getTitle getTitle(callback: AsyncCallback): void8+ Obtains the title of the ringtone. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :--------------------- | :-------- | :------------------------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the title of the ringtone. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ if (err) { console.err('getSystemRingtonePlayer failed'); return; } else { console.log('getSystemRingtonePlayer success'); } }); ringtonePlayer.getTitle((err, title)=>{ if (err) { console.err('getTitle failed'); } else { console.log('getTitle success: ' + title); } }) ``` ## ringtonePlayer.getTitle getTitle(): Promise8+ Obtains the title of the ringtone. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :--------------- | :------------------------------------------------ | | Promise | Promise used to return the title of the ringtone. | **Example** ``` let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); let title = await ringtonePlayer.getTitle(); ``` ## ringtonePlayer.getAudioRendererInfo getAudioRendererInfo(callback: AsyncCallback): void8+ Obtains the audio renderer information. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :-------------------------------- | :-------- | :------------------------------------------------------ | | callback | AsyncCallback | Yes | Callback used to return the audio renderer information. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ if (err) { console.err('getSystemRingtonePlayer failed'); return; } else { console.log('getSystemRingtonePlayer success: '); } }); ringtonePlayer.getAudioRendererInfo((err, rendererInfo)=>{ if (err) { console.err('getAudioRendererInfo failed'); } else { console.log('getAudioRendererInfo success'); } }); ``` ## ringtonePlayer.getAudioRendererInfo getAudioRendererInfo(): Promise8+ Obtains the audio renderer information. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :-------------------------- | :----------------------------------------------------- | | Promise | Promise used to return the audio renderer information. | **Example** ``` let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); let rendererInfo = await ringtonePlayer.getAudioRendererInfo(); ``` ## ringtonePlayer.configure configure(options: RingtoneOptions, callback: AsyncCallback): void8+ Configures ringtone options. This method uses an asynchronous callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | options | RingtoneOptions | Yes | Ringtone options. | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ if (err) { console.err('getSystemRingtonePlayer failed'); return; } else { console.log('getSystemRingtonePlayer success: '); } }); let ringtoneOptions = { volume: 1, loop: false }; ringtonePlayer.configure(ringtoneOptions, (err)=> { if (err) { console.error('Failed to configure ringtone options'); } else { console.log('Callback invoked to indicate a successful ringtone options configuration.'); } }) ``` ## ringtonePlayer.configure configure(options: RingtoneOptions): Promise8+ Configures ringtone options. This method uses a promise to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------ | :-------------- | :-------- | :---------------- | | options | RingtoneOptions | Yes | Ringtone options. | | | | | | **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); let ringtoneOptions = { volume: 1, loop: false }; await ringtonePlayer.configure(ringtoneOptions); ``` ## ringtonePlayer.start start(callback: AsyncCallback): void8+ Starts playing ringtone. This method uses a callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` ringtonePlayer.start((err)=> { if (err) { console.error('Failed to start playing ringtone'); } else { console.log('Ringtone start playing successfully.'); } }) ``` ## ringtonePlayer.start start(): Promise8+ Starts playing ringtone. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await ringtonePlayer.start(); ``` ## ringtonePlayer.stop stop(callback: AsyncCallback): void8+ Stops playing ringtone. This method uses a callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` ringtonePlayer.stop((err)=> { if (err) { console.error('Failed to stop playing ringtone'); } else { console.log('Ringtone stop playing successfully.'); } }) ``` ## ringtonePlayer.stop stop(): Promise8+ Stops playing ringtone. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await ringtonePlayer.stop(); ``` ## ringtonePlayer.release release(callback: AsyncCallback): void8+ Releases ringtone player resources. This method uses a callback to return the result. **Parameters** | Name | Type | Mandatory | Description | | :------- | :------------------- | :-------- | :---------------------------------- | | callback | AsyncCallback | Yes | Callback used to return the result. | | | | | | **Return value** None **Example** ``` ringtonePlayer.release((err)=> { if (err) { console.error('Failed to release ringtone player resource'); } else { console.log('Release ringtone player resource successfully.'); } }) ``` ## ringtonePlayer.release release(): Promise8+ Releases ringtone player resource. This method uses a promise to return the result. **Parameters** None **Return value** | Type | Description | | :------------- | :--------------------------------- | | Promise | Promise used to return the result. | **Example** ``` await ringtonePlayer.release(); ```