diff --git a/en/application-dev/js-reference/audio-management.md b/en/application-dev/js-reference/audio-management.md index 28550f1bc4ffc9d550b67b56c0edb1497217b59b..e538c610ff6fc68f2f31dc88bf8ad8b7e6b3be18 100644 --- a/en/application-dev/js-reference/audio-management.md +++ b/en/application-dev/js-reference/audio-management.md @@ -1,61 +1,78 @@ -# Audio Management - -- [Modules to Import](#en-us_topic_0000001149807881_s56d19203690d4782bfc74069abb6bd71) -- [Required Permissions](#en-us_topic_0000001149807881_section11257113618419) -- [Functions](#en-us_topic_0000001149807881_section1580114415416) -- [getAudioManager\(\)](#en-us_topic_0000001149807881_section84581011418) -- [Enums](#en-us_topic_0000001149807881_section115029181495) -- [AudioVolumeType](#en-us_topic_0000001149807881_section92261857172218) -- [DeviceFlag](#en-us_topic_0000001149807881_section11285183164210) -- [DeviceRole](#en-us_topic_0000001149807881_section380038142619) -- [DeviceType](#en-us_topic_0000001149807881_section11727420122710) -- [Appendixes](#en-us_topic_0000001149807881_section1933416317165) -- [AudioManager](#en-us_topic_0000001149807881_section8265143814015) - - [setVolume\(AudioVolumeType, number, AsyncCallback\)](#en-us_topic_0000001149807881_section189141826104616) - - [setVolume\(AudioVolumeType, number\)](#en-us_topic_0000001149807881_section102021249114612) - - [getVolume\(AudioVolumeType, AsyncCallback\)](#en-us_topic_0000001149807881_section4387320194714) - - [getVolume\(AudioVolumeType\)](#en-us_topic_0000001149807881_section04121965119) - - [getMinVolume\(AudioVolumeType, AsyncCallback\)](#en-us_topic_0000001149807881_section188714283511) - - [getMinVolume\(AudioVolumeType\)](#en-us_topic_0000001149807881_section41556389511) - - [getMaxVolume\(AudioVolumeType, AsyncCallback\)](#en-us_topic_0000001149807881_section690395418516) - - [getMaxVolume\(AudioVolumeType\)](#en-us_topic_0000001149807881_section155151345217) - - [getDevices\(DeviceFlag, AsyncCallback\)](#en-us_topic_0000001149807881_section11536182020523) - - [getDevices\(DeviceFlag\)](#en-us_topic_0000001149807881_section181733125210) - -- [AudioDeviceDescriptor](#en-us_topic_0000001149807881_section17427121913310) -- [AudioDeviceDescriptors](#en-us_topic_0000001149807881_section5181155710523) - ->![](public_sys-resources/icon-note.gif) **NOTE:** ->Due to permission issues, these feature are temporarily unavailable for the standard system. - -## Modules to Import +# Audio Management + +- [Modules to Import](#s56d19203690d4782bfc74069abb6bd71) +- [Required Permissions](#section11257113618419) +- [getAudioManager\(\): AudioManager](#section84581011418) +- [AudioVolumeType](#section92261857172218) +- [DeviceFlag](#section11285183164210) +- [DeviceRole](#section380038142619) +- [DeviceType](#section11727420122710) +- [AudioRingMode7+](#section14948916131018) +- [AudioManager](#section8265143814015) + - [setVolume\(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback\): void](#section189141826104616) + - [setVolume\(volumeType: AudioVolumeType, volume: number\): Promise](#section102021249114612) + - [getVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void](#section4387320194714) + - [getVolume\(volumeType: AudioVolumeType\): Promise](#section04121965119) + - [getMinVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void](#section188714283511) + - [getMinVolume\(volumeType: AudioVolumeType\): Promise](#section41556389511) + - [getMaxVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void](#section690395418516) + - [getMaxVolume\(volumeType: AudioVolumeType\): Promise](#section155151345217) + - [mute\(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback\): void7+](#section13516136134613) + - [mute\(volumeType: AudioVolumeType, mute: boolean\): Promise7+](#section7519036144616) + - [isMute\(volumeType: AudioVolumeType, callback: AsyncCallback\): void7+](#section10684183819585) + - [isMute\(volumeType: AudioVolumeType\): Promise7+](#section6920211145610) + - [isActive\(volumeType: AudioVolumeType, callback: AsyncCallback\): void7+](#section380012544121) + - [isActive\(volumeType: AudioVolumeType\): Promise7+](#section1880315481216) + - [setRingerMode\(mode: AudioRingMode, callback: AsyncCallback\): void7+](#section18572131483613) + - [setRingerMode\(mode: AudioRingMode\): Promise7+](#section55741914143615) + - [getRingerMode\(callback: AsyncCallback\): void7+](#section149044108162) + - [getRingerMode\(\): Promise7+](#section13908210101620) + - [setAudioParameter\(key: string, value: string, callback: AsyncCallback\): void7+](#section1691957174818) + - [setAudioParameter\(key: string, value: string\): Promise7+](#section18121057174820) + - [getAudioParameter\(key: string, callback: AsyncCallback\): void7+](#section1415145714812) + - [getAudioParameter\(key: string\): Promise7+](#section3185577485) + - [getDevices\(deviceFlag: DeviceFlag, callback: AsyncCallback\): void](#section11536182020523) + - [getDevices\(deviceFlag: DeviceFlag\): Promise](#section181733125210) + - [setDeviceActive\(deviceType: DeviceType, active: boolean, callback: AsyncCallback\): void7+](#section103558400222) + - [setDeviceActive\(deviceType: DeviceType, active: boolean\): Promise7+](#section1235914401228) + - [isDeviceActive\(deviceType: DeviceType, callback: AsyncCallback\): void7+](#section12363240122219) + - [isDeviceActive\(deviceType: DeviceType\): Promise7+](#section18366184012213) + - [setMicrophoneMute\(mute: boolean, callback: AsyncCallback\): void7+](#section14703163618122) + - [setMicrophoneMute\(mute: boolean\): Promise7+](#section56287111791) + - [isMicrophoneMute\(callback: AsyncCallback\): void7+](#section6632141119912) + - [isMicrophoneMute\(\): Promise7+](#section196363111918) + +- [AudioDeviceDescriptor](#section164657411927) + - [Attributes](#section4947115405) + +- [AudioDeviceDescriptors](#section5181155710523) + +## Modules to Import ``` import audio from '@ohos.multimedia.audio'; ``` -## Required Permissions +## Required Permissions None -## Functions - -## getAudioManager\(\) +## getAudioManager\(\): AudioManager Obtains an **AudioManager** instance. -**Return Values** +**Return values** - -

Type

+ + - - - @@ -67,278 +84,315 @@ Obtains an **AudioManager** instance. var audioManager = audio.getAudioManager(); ``` -## Enums - -## AudioVolumeType +## AudioVolumeType Enumerates audio stream types. - -

Type

Description

+

Description

AudioManager

+

AudioManager

Audio manager

+

Audio manager

Name

+ + - - - - - - - -

Name

Default Value

+

Default Value

Description

+

Description

MEDIA

+

RINGTONE

1

+

2

Audio streams for media purpose

+

Audio stream for ringtones

RINGTONE

+

MEDIA

2

+

3

Audio streams for ring tones

+

Audio stream for media purpose

-## DeviceFlag +## DeviceFlag Enumerates audio device flags. - -

Name

+ + - - - - - - - - - - -

Name

Default Value

+

Default Value

Description

+

Description

OUTPUT_DEVICES_FLAG

+

OUTPUT_DEVICES_FLAG

1

+

1

Output devices

+

Output device

INPUT_DEVICES_FLAG

+

INPUT_DEVICES_FLAG

2

+

2

Input devices

+

Input device

ALL_DEVICES_FLAG

+

ALL_DEVICES_FLAG

3

+

3

All devices

+

All devices

-## DeviceRole +## DeviceRole -Enumerates device roles. +Enumerates audio device roles. - -

Name

+ + - - - - - - - -

Name

Default Value

+

Default Value

Description

+

Description

INPUT_DEVICE

+

INPUT_DEVICE

1

+

1

Input role

+

Input role

OUTPUT_DEVICE

+

OUTPUT_DEVICE

2

+

2

Output role

+

Output role

-## DeviceType +## DeviceType -Enumerates device types. +Enumerates audio device types. - -

Name

+ + - - - - - - - - - - - - - - - - - - - -

Name

Default Value

+

Default Value

Description

+

Description

INVALID

+

INVALID

0

+

0

Invalid device

+

Invalid device

SPEAKER

+

SPEAKER

1

+

1

Speaker

+

Speaker

WIRED_HEADSET

+

WIRED_HEADSET

2

+

2

Wired headset

+

Wired headset

BLUETOOTH_SCO

+

BLUETOOTH_SCO

3

+

3

Bluetooth device using the synchronous connection oriented link (SCO)

+

Bluetooth device using the synchronous connection oriented (SCO) link

BLUETOOTH_A2DP

+

BLUETOOTH_A2DP

4

+

4

Bluetooth device using advanced audio distribution profile (A2DP)

+

Bluetooth device using the advanced audio distribution profile (A2DP)

MIC

+

MIC

5

+

5

Microphone

+

Microphone

-## Appendixes +## 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 +## AudioManager -Manages audio volume and audio device information. +Implements audio volume and audio device management. -### setVolume\(AudioVolumeType, number, AsyncCallback\) +### setVolume\(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback\): void -Sets volume for a stream. This method uses an asynchronous callback to return the execution result. +Sets the volume for a stream. This method uses an asynchronous callback to return the execution result. **Parameters** - -

Name

+ + - - - - - - - - - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

volume

+

volume

number

+

number

Yes

+

Yes

Volume to set

+

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

callback

+

callback

AsyncCallback<void>

+

AsyncCallback<void>

Yes

+

Yes

Callback used to return whether the setting is successful

+

Callback used to return the execution result

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

Name

+ + - - - - - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

volume

+

volume

number

+

number

Yes

+

Yes

Volume to set

+

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

-**Return Values** +**Return values** - -

Type

+ + - - - @@ -347,102 +401,106 @@ Sets volume for a stream. This method uses a promise to return the execution res **Example** ``` -audioManager.setVolume(audio.AudioVolumeType.MEDIA, 30).then(()=> - console.log(`Media setVolume successful callback`); +audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=> + console.log('Promise returned to indicate a successful volume setting.'); ) ``` -### getVolume\(AudioVolumeType, AsyncCallback\) +### getVolume\(volumeType: AudioVolumeType, callback: AsyncCallback\): void -Obtains volume of a stream. This method uses an asynchronous callback to return the execution result. +Obtains the volume of a stream. This method uses an asynchronous callback to return the query result. **Parameters** - -

Type

Description

+

Description

Promise<void>

+

Promise<void>

Promise used to return whether the setting is successful

+

Promise used to return the execution result

Name

+ + - - - - - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

callback

+

callback

AsyncCallback<number>

+

AsyncCallback<number>

Yes

+

Yes

Callback used to return the volume

+

Callback used to return the volume

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

Name

+ + - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

-**Return Values** +**Return values** - -

Type

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

Type

Description

+

Description

Promise<number>

+

Promise<number>

Promise used to return stream volume

+

Promise used to return the volume

Name

+ + - - - - - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

callback

+

callback

AsyncCallback<number>

+

AsyncCallback<number>

Yes

+

Yes

Callback used to return the minimum volume

+

Callback used to return the minimum volume

+**Return values** + +None + **Example** ``` -audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value)=>{ - if (err) { - console.error(`failed to get minvolume ${err.message}`); - return; - } - console.log(`Media getMinVolume successful callback`); +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\(AudioVolumeType\) +### getMinVolume\(volumeType: AudioVolumeType\): Promise -Obtains the minimum volume allowed for a stream. This method uses a promise to return the execution result. +Obtains the minimum volume allowed for a stream. This method uses a promise to return the query result. **Parameters** - -

Name

+ + - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

-**Return Values** +**Return values** - -

Type

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

Type

Description

+

Description

Promise<number>

+

Promise<number>

Promise used to return the minimum volume

+

Promise used to return the minimum volume

Name

+ + - - - - - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

callback

+

callback

AsyncCallback<number>

+

AsyncCallback<number>

Yes

+

Yes

Callback used to return the maximum volume

+

Callback used to return the maximum volume

+**Return values** + +None + **Example** ``` -audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value)=>{ - if (err) { - console.error(`failed to get maxvolume ${err.message}`); - return; - } - console.log(`Media getMaxVolume successful callback`); +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\(AudioVolumeType\) +### getMaxVolume\(volumeType: AudioVolumeType\): Promise -Obtains the maximum volume allowed for a stream. This method uses a promise to return the execution result. +Obtains the maximum volume allowed for a stream. This method uses a promise to return the query result. **Parameters** - -

Name

+ + - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

audioType

+

volumeType

AudioVolumeType

+

AudioVolumeType

Yes

+

Yes

Audio stream type

+

Audio stream type

-**Return Values** +**Return values** - -

Type

+ + - - - @@ -660,101 +726,231 @@ Obtains the maximum volume allowed for a stream. This method uses a promise to r ``` audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data)=> - console.log(`Media getMaxVolume successful callback`); + console.log('Promised returned to indicate that the maximum volume is obtained.'); ) ``` -### getDevices\(DeviceFlag, AsyncCallback\) +### mute\(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback\): void7+ -Obtains the audio devices of a specified flag. This method uses an asynchronous callback to return the execution result. +Mutes a stream. This method uses an asynchronous callback to return the execution result. **Parameters** - -

Type

Description

+

Description

Promise<number>

+

Promise<number>

Promise used to return the maximum volume

+

Promise used to return the maximum volume

Name

+ + - - - - + + + - + + - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

deviceFlag

+

volumeType

+

AudioVolumeType

+

Yes

+

Audio stream type

DeviceFlag

+

mute

+

boolean

Yes

+

Yes

Audio device flag

+

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

callback

+

callback

AsyncCallback<AudioDeviceDescriptors>

+

AsyncCallback<void>

Yes

+

Yes

Callback used to return the device list

+

Callback used to return the execution result

+**Return values** + +None + **Example** ``` -audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{ +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 get getdevices ${err.message}`); + console.error('Failed to obtain the mute status. ${err.message}'); return; } - console.log(`Media getDevices successful callback`); + console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value); }) ``` -### getDevices\(DeviceFlag\) +### isMute\(volumeType: AudioVolumeType\): Promise7+ -Obtains the audio devices with a specified flag. This method uses a promise to return the execution result. +Checks whether a stream is muted. This method uses a promise to return the execution result. **Parameters** - -

Name

+ + - - - - - - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

deviceFlag

+

volumeType

DeviceFlag

+

AudioVolumeType

Yes

+

Yes

Audio device flag

+

Audio stream type

-**Return Values** +**Return values** - -

Type

+ + - - - @@ -763,59 +959,1123 @@ Obtains the audio devices with a specified flag. This method uses a promise to r **Example** ``` -audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data)=> - console.log(`Media getDevices successful callback`); +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** + + +

Type

Description

+

Description

Promise<AudioDeviceDescriptors>

+

Promise<boolean>

Promise used to return the obtained device list

+

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

+ + + + + + + + + + + + + + + + +

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); ) ``` -## AudioDeviceDescriptor +### 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. -Describes audio devices. +**Parameters** - -

Name

+ + - - - - + - - + + +

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

deviceRole

+

mode

+

AudioRingMode

DeviceRole

+

Yes

No

+

Ringer mode

+
+ +**Return values** + + + + + + + - - +

Type

+

Description

+

Promise<void>

Audio device role

+

Promise used to return the execution result

deviceType

+
+ +**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

DeviceType

+

Yes

No

+

No

Audio device type

+

Audio device type

-## AudioDeviceDescriptors +## AudioDeviceDescriptors - -

Name

+ + - - - diff --git a/en/application-dev/js-reference/audio-playback.md b/en/application-dev/js-reference/audio-playback.md index da51b1980731c35b113b3abec76a46ec3234d9b1..4bb9a494caac99634d572b1b0d17703fb747330f 100644 --- a/en/application-dev/js-reference/audio-playback.md +++ b/en/application-dev/js-reference/audio-playback.md @@ -1,61 +1,59 @@ -# Audio Playback - -- [Modules to Import](#en-us_topic_0000001103383404_s56d19203690d4782bfc74069abb6bd71) -- [Required Permissions](#en-us_topic_0000001103383404_section11257113618419) -- [Methods](#en-us_topic_0000001103383404_section125675489541) -- [createAudioPlayer\(\)](#en-us_topic_0000001103383404_section582314017253) -- [Appendixes](#en-us_topic_0000001103383404_section1933416317165) -- [AudioPlayer](#en-us_topic_0000001103383404_section5174142818365) - - [Attributes](#en-us_topic_0000001103383404_section4947115405) - - [play\(\)](#en-us_topic_0000001103383404_section964512672913) - - [pause\(\)](#en-us_topic_0000001103383404_section78173258296) - - [stop\(\)](#en-us_topic_0000001103383404_section122114334296) - - [seek\(number\)](#en-us_topic_0000001103383404_section1387113816298) - - [setVolume\(number\)](#en-us_topic_0000001103383404_section164235176552) - - [release\(\)](#en-us_topic_0000001103383404_section9224621145512) - - [Events](#en-us_topic_0000001103383404_section5453721192911) - - [play](#en-us_topic_0000001103383404_section87307411494) - - [pause](#en-us_topic_0000001103383404_section198217471590) - - [stop](#en-us_topic_0000001103383404_section437616531910) - - [dataLoad](#en-us_topic_0000001103383404_section982114219106) - - [timeUpdate](#en-us_topic_0000001103383404_section13687114181014) - - [volumeChange](#en-us_topic_0000001103383404_section139227715717) - - [finish](#en-us_topic_0000001103383404_section15181321181018) - - [error](#en-us_topic_0000001103383404_section5593132921016) - -- [AudioState](#en-us_topic_0000001103383404_section5181155710523) - ->![](public_sys-resources/icon-note.gif) **NOTE:** ->Due to permission issues, these feature are temporarily unavailable for the standard system. - -## Modules to Import +# Audio Playback + +- [Modules to Import](#s56d19203690d4782bfc74069abb6bd71) +- [Required Permissions](#section11257113618419) +- [Methods](#section125675489541) +- [createAudioPlayer\(\)](#section582314017253) +- [Appendixes](#section1933416317165) +- [AudioPlayer](#section5174142818365) + - [Attributes](#section4947115405) + - [play\(\)](#section964512672913) + - [pause\(\)](#section78173258296) + - [stop\(\)](#section122114334296) + - [seek\(number\)](#section1387113816298) + - [setVolume\(number\)](#section164235176552) + - [reset\(\)7+](#section1473283011356) + - [release\(\)](#section9224621145512) + - [Events](#section5453721192911) + - [play](#section87307411494) + - [pause](#section198217471590) + - [stop](#section437616531910) + - [dataLoad](#section982114219106) + - [timeUpdate](#section13687114181014) + - [volumeChange](#section139227715717) + - [finish](#section15181321181018) + - [error](#section5593132921016) + +- [AudioState](#section5181155710523) + +## Modules to Import ``` import audio from '@ohos.multimedia.media'; ``` -## Required Permissions +## Required Permissions None -## Methods +## Methods -## createAudioPlayer\(\) +## createAudioPlayer\(\) Creates an **AudioPlayer** instance to control audio playback. **Return Values** - -

Name

Description

+

Description

Device property queue

+

Device attribute array

A queue of AudioDeviceDescriptor, which is read-only.

+

Array of AudioDeviceDescriptors, which is read-only

Type

+ + - - - @@ -67,164 +65,201 @@ Creates an **AudioPlayer** instance to control audio playback. var audioplayer = audio.createAudioPlayer(); ``` -## Appendixes +## Appendixes -## AudioPlayer +## AudioPlayer Represents an audio player. -### Attributes +### Attributes - -

Type

Description

+

Description

AudioPlayer

+

AudioPlayer

Returns the AudioPlayer instance if the operation is successful; returns null otherwise.

+

Returns the AudioPlayer instance if the operation is successful; returns null otherwise.

Name

+ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

Type

+

Type

Readable

+

Readable

Writable

+

Writable

Description

+

Description

src

+

src

string

+

string

Yes

+

Yes

Yes

+

Yes

URI of the audio resources

+

URI of the audio resources

loop

+

loop

boolean

+

boolean

Yes

+

Yes

Yes

+

Yes

Whether to loop audio playback

+

Whether to loop audio playback

currentTime

+

currentTime

number

+

number

Yes

+

Yes

No

+

No

Current playback position

+

Current playback position

duration

+

duration

number

+

number

Yes

+

Yes

No

+

No

Playback duration

+

Playback duration

state

+

state

AudioState

+

AudioState

Yes

+

Yes

No

+

No

Playback status

+

Playback status

-### play\(\) +### play\(\) Starts audio playback. **Example** ``` -audioplayer.src = 'common/mydream.mp3'; +audioplayer.on('dataload', (err, action) => { + if (err) { + console.info('Error returned in the prepare() callback.'); + return; + } + console.info('Start to play the audio.'); + audioplayer.play(); +}; + audioplayer.on('play', (err, action) => { if (err) { - console.log(`err returned in play() callback`); + console.info('Error returned in the play() callback.'); return; } - console.log('start to play'); - audioplayer.pause(); + console.info('Succeeded in playing the audio.'); }; -audioplayer.play(); +audioplayer.src = 'common/mydream.mp3'; ``` -### pause\(\) +### pause\(\) Pauses audio playback. **Example** ``` -audioplayer.src = 'common/mydream.mp3'; +audioplayer.on('dataload', (err, action) => { + if (err) { + console.info('Error returned in the prepare() callback.'); + return; + } + console.info('Start to play the audio.'); + audioplayer.play(); +} +audioplayer.on('play', (err, action) => { + if (err) { + console.info('Error returned in the play() callback.'); + return; + } + console.info('Start to pause audio playback.'); + audioplayer.pause(); +}; audioplayer.on('pause', (err, action) => { if (err) { - console.log(`err returned in pause() callback`); + console.info('Error returned in the pause() callback.'); return; } - console.log('playback pause'); + console.info('Succeeded in pausing audio playback.'); }; -audioplayer.pause(); +audioplayer.src = 'common/mydream.mp3'; ``` -### stop\(\) +### stop\(\) Stops audio playback. **Example** ``` -audioplayer.src = 'common/mydream.mp3'; -audioplayer.on('stop',(err, action) => { +audioplayer.on('dataload', (err, action) => { if (err) { - console.log(`err returned in stop() callback`); + console.info('Error returned in the prepare() callback.'); return; } - console.log('playback stop'); + console.info('Start to play the audio.'); + audioplayer.play(); }; -audioplayer.stop(); +audioplayer.on('play', (err, action) => { + if (err) { + console.info('Error returned in the play() callback.'); + return; + } + console.info('Start to pause audio playback.'); + audioplayer.pause(); +}; +audioplayer.on('stop', (err, action) => { + if (err) { + console.info('Error returned in the stop() callback.'); + return; + } + console.info('Succeeded in stopping audio playback.'); +}; +audioplayer.src = 'common/mydream.mp3'; ``` -### seek\(number\) +### seek\(number\) Goes to a specified playback position. **Parameters** - -

Name

+ + - - - - - - - @@ -233,46 +268,58 @@ Goes to a specified playback position. **Example** ``` -audioplayer.src = 'common/mydream.mp3'; -audioplayer.on('timeupdate', (err, action) => { +audioplayer.on('dataload', (err, action) => { if (err) { - console.log(`err returned in seek() callback`); + console.info('Error returned in the prepare() callback.'); return; } + console.info('Start to play the audio.'); + audioplayer.play(); +}; +audioplayer.on('play', (err, action) => { + if (err) { + console.info('Error returned in the play() callback.'); + return; + } + console.info('Succeeded in playing the audio.') +}; +audioplayer.on('timeUpdate', (seekTime, action) => { + console.info('Seek time returned in the seek() callback: ' + seekTime); var newTime = audioplayer.currenTime; - if(newTime = 30000) { - console.info('Seek success, new time : ' + newTime); + if(newTime == 30000) { + console.info('Seek succeeded. New time: ' + newTime); } else { - console.info('Seek Failed'); + console.info('Seek failed.'); } }; +audioplayer.src = 'common/mydream.mp3'; audioplayer.seek(30000); ``` -### setVolume\(number\) +### setVolume\(number\) Sets the volume. **Parameters** - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

timeMs

+

timeMs

number

+

number

Yes

+

Yes

Target playback position

+

Target playback position

Name

+ + - - - - - - - @@ -281,18 +328,58 @@ Sets the volume. **Example** ``` -audioplayer.src = 'common/mydream.mp3'; +audioplayer.on('dataload', (err, action) => { + if (err) { + console.error('Error returned in the prepare() callback.'); + return; + } + console.info('Start to play the audio.'); + audioplayer.play(); +}; +audioplayer.on('play', (err, action) => { + if (err) { + console.error('Error returned in the play() callback.'); + return; + } + console.info('Succeeded in playing the audio.') +}; audioplayer.on('volumeChange', (err, action) => { if (err) { - console.log(`err returned in setVolume() callback`); + console.error('Error returned in the setVolume() callback.'); + return; + } + console.info('Playback volume changed.'); +}; +audioplayer.src = 'common/mydream.mp3'; +audioplayer.setVolume(0.5); +``` + +### reset\(\)7+ + +Switches the audio resource to be played. + +**Example** + +``` +audioplayer.on('dataload', (err, action) => { + if (err) { + console.error('Error returned in the prepare() callback.'); return; } - console.log('playback volume change'); + console.info('Start to reset audio playback.'); + audioplayer.reset(); }; -audioplayer.setVolume(30); +audioplayer.on('reset', (err, action) => { + if (err) { + console.error('Error returned in the reset() callback.'); + return; + } + console.info('Reset succeeded.'); +}; +audioplayer.src = 'common/mydream.mp3'; ``` -### release\(\) +### release\(\) Releases audio resources. @@ -302,83 +389,88 @@ Releases audio resources. audioplay.release(); ``` -### Events +### Events -### play +### play Triggered when **play\(\)** is called -### pause +### pause Triggered when **pause\(\)** is called -### stop +### stop Triggered when **stop\(\)** is called -### dataLoad +### dataLoad Triggered when audio data is loaded -### timeUpdate +### timeUpdate Triggered when the playback position changes -### volumeChange +### volumeChange Triggered when playback volume changes -### finish +### finish Triggered when playback is finished -### error +### error Triggered when a playback error occurs - -

Name

Type

+

Type

Mandatory

+

Mandatory

Description

+

Description

vol

+

vol

number

+

number

Yes

+

Yes

Volume to set

+

Volume to set. The value ranges from 0 to 1.

Parameter

+ + - - - - -

Name

Type

+

Type

Description

+

Description

callback

+

callback

ErrorCallback

+

ErrorCallback

Callback function for handling this event

+

Callback function for handling this event

-## AudioState +## AudioState Describes playback status. - -

Parameter

+ + - - + + + - - - - - diff --git a/en/application-dev/media/development-guidelines-on-audio-playback.md b/en/application-dev/media/development-guidelines-on-audio-playback.md index 724dda64ec919e3ef0217350bca9039cfbbbc0b7..e9b79f166c2af74fd79bc562f4314e1c9ec98b1c 100755 --- a/en/application-dev/media/development-guidelines-on-audio-playback.md +++ b/en/application-dev/media/development-guidelines-on-audio-playback.md @@ -63,6 +63,11 @@ You use audio playback APIs to convert audio data into audible analog signals, p + + + - - - - - @@ -103,11 +103,6 @@ You use audio playback APIs to convert audio data into audible analog signals, p - - - - + + + @@ -133,7 +133,7 @@ You use audio playback APIs to convert audio data into audible analog signals, p - @@ -143,7 +143,7 @@ You use audio playback APIs to convert audio data into audible analog signals, p - @@ -162,73 +162,70 @@ You use audio playback APIs to convert audio data into audible analog signals, p ``` player.on('play', (err, action) => { if (err) { - console.log(`err returned in play() callback`); + console.error('Error returned in the play() callback.'); return; } - console.info('Current Song duration: '+duration); - console.log(`In play callback current time: ${player.currentTime}`); - console.info('Current Player Status: '+player.state); - console.info('Pause Mp3'); + console.info('Current player duration: '+ player.duration); + console.info('Current player time: ' + player.currentTime); + console.info('Current player status: '+player.state); + console.info('Pause MP3'); player.pause(); }); player.on('pause', (err, action) => { if (err) { - console.log(`err returned in pause() callback`); + console.error('Error returned in the pause() callback.'); return; } - console.log(`pause callback, state: ${player.state}`); - console.info('Current Player Status: '+player.state); - var currentTime = player.currenTime; - console.info('Current Player Time: '+currentTime); - player.seek(30000); //seek for 30 seconds + console.info('Current player status: ' + player.state); + console.info('Current player time: ' + player.currentTime); + player.seek(30000); // Seek for 30 seconds. }); player.on('stop', (err, action) => { if (err) { - console.log(`err returned in stop() callback`); + console.error('Error returned in the stop() callback.'); return; } - console.log(`stop callback, state: ${player.state}`); + console.info('stop callback invoked. State:' + player.state); + player.reset(); }); player.on('dataLoad', (err, action) => { if (err) { - console.log(`err returned in dataLoad() callback`); + console.error('Error returned in the dataLoad() callback.'); return; } - console.log(`dataLoad callback is called, current time: ${player.currentTime}`); - console.log(`Duration of the source: ${player.duration}`); + console.info('dataLoad callback invoked. Current time: ' + player.currentTime); + console.info('Duration of the source:' + player.duration); + player.play(); + }); + player.on('reset', (err, action) => { + if (err) { + console.error('Error returned in the reset() callback.'); + return; + } + console.info('reset callback invoked.'); + player.release(); }); player.on('finish', (err, action) => { if (err) { - console.log(`err returned in finish() callback`); + console.error('Error returned in the finish() callback.'); return; } - console.log(`Player finish callback is called`); + console.info('finish callback invoked.'); }); - player.on('timeUpdate', (err, action) => { - if (err) { - console.log(`err returned in timeUpdate() callback`); - return; - } - console.log(`In timeupdate callback current time: ${player.currentTime}`); + player.on('timeUpdate', (seekTime, action) => { + console.info('Seek time: ' + seekTime); + console.info('Current player time: ' + player.currentTime); var newTime = player.currentTime; - if(newTime = 30000) { - console.info('Seek success, new time : '+newTime); + if(newTime == 30000) { + console.info('Seek succeeded. New time: ' + newTime); } else { - console.info('Seek Failed'); + console.error('Seek failed: ', + newTime); } player.stop(); }); - player.on('durationchange', (err, action) => { - if (err) { - console.log(`err returned in durationchange callback`); - return; - } - console.log(`Durationchange callback is called`); - }); player.on('error', (err) => { - console.error(`player error: ${err.message}`); + console.error('Player error: ${err.message}'); }); - ``` 3. Start playback. @@ -237,7 +234,6 @@ You use audio playback APIs to convert audio data into audible analog signals, p var audioSourceMp3 = 'file://test.mp3'; player.src = audioSourceMp3; player.loop = true; - player.play(); ``` diff --git a/en/application-dev/media/figures/playback-status.png b/en/application-dev/media/figures/playback-status.png index 8723a59a5edcbadec16133c71bc772907882c54a..e0777e28838f6d2455233f2068339f8548f50c67 100755 Binary files a/en/application-dev/media/figures/playback-status.png and b/en/application-dev/media/figures/playback-status.png differ

Name

Description

+

Description

playing

+

idle

+

Audio playback is idle.

+

playing

The audio is being played.

+

The audio is being played.

paused

+

paused

Audio playback is paused.

+

Audio playback is paused.

stopped

+

stopped

Audio playback is stopped.

+

Audio playback is stopped.

Stops playback.

reset()7+

+

Resets the audio source to be played.

+

setVolume(vol: number)

Sets playback volume.

@@ -78,17 +83,12 @@ You use audio playback APIs to convert audio data into audible analog signals, p

Defines the URI of an audio file to play.

state:string

+

state:AudioState

Defines a playback state.

autoplay:boolean

-

Defines whether playback starts automatically.

-

currenttime:number

+

currentTime:number

Defines the current playback position.

Defines whether to loop audio playback.

muted:boolean

-

Defines whether to mute the audio.

-

on('play', function callback)

Listens for the playback start event.

@@ -123,7 +118,12 @@ You use audio playback APIs to convert audio data into audible analog signals, p

Listens for the playback stop event.

on('ended',function callback)

+

on('reset', function callback)

+

Listens for the playback reset event.

+

on('finish',function callback)

Listens for the playback end event.

Listens for the playback error event.

on('loaderdata', function callback)

+

on('dataload', function callback)

Listens for the data loading event.

Listens for the volume change event.

on('timeupdate', function callback)

+

on('timeUpdate', function callback)

Listens for the progress change event.