js-apis-audio.md 124.2 KB
Newer Older
1
# Audio Management
V
Vaidegi B 已提交
2

3 4
> **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
V
Vaidegi B 已提交
5

6 7 8 9 10 11 12
This module provides the following common audio-related functions:

- [AudioManager](#audiomanager): Audio management.
- [AudioRenderer](#audiorenderer8): Audio rendering, used to play Pulse Code Modulation (PCM) audio data.
- [AudioCapturer](#audiocapturer8): Audio capture, used to record PCM audio data.

## Modules to Import
W
wusongqing 已提交
13 14 15 16 17

```
import audio from '@ohos.multimedia.audio';
```

18
## audio.getAudioManager
V
Vaidegi B 已提交
19

20
getAudioManager(): AudioManager
21

22
Obtains an **AudioManager** instance.
W
wusongqing 已提交
23

24
**System capability:** SystemCapability.Multimedia.Audio.Core
25

26
**Return value:**
W
wusongqing 已提交
27

28 29 30
| Type                          | Description          |
| ----------------------------- | -------------------- |
| [AudioManager](#audiomanager) | AudioManager object. |
W
wusongqing 已提交
31

32
**Example:**
W
wusongqing 已提交
33 34 35 36 37

```
var audioManager = audio.getAudioManager();
```

38
## audio.createAudioRenderer<sup>8+</sup>
39

40
createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void
41

42
Obtains an **AudioRenderer** instance. This API uses an asynchronous callback to return the renderer instance.
43

44
**System capability:** SystemCapability.Multimedia.Audio.Renderer
45

46 47 48 49 50
**Parameters:**
| Name       | Type                                            | Mandatory | Description                                          |
| :--------- | :---------------------------------------------- | :-------- | :--------------------------------------------------- |
| options    | [AudioRendererOptions](#audiorendereroptions8)  | Yes       | Renderer configurations.                             |
| callback   | AsyncCallback<[AudioRenderer](#audiorenderer8)> | Yes       | Callback used to return the audio renderer instance. |
51

52
**Example:**
53 54

```
55
import audio from '@ohos.multimedia.audio';
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
var audioStreamInfo = {
    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
    channels: audio.AudioChannel.CHANNEL_1,
    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

var audioRendererInfo = {
    content: audio.ContentType.CONTENT_TYPE_SPEECH,
    usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
    rendererFlags: 1
}

var audioRendererOptions = {
    streamInfo: audioStreamInfo,
    rendererInfo: audioRendererInfo
}

audio.createAudioRenderer(audioRendererOptions,(err, data) => {
    if (err) {
76
        console.error(`AudioRenderer Created : Error: ${err.message}`);
77 78
    }
    else {
79 80
        console.info('AudioRenderer Created : Success : SUCCESS');
        let audioRenderer = data;
81 82 83
    }
});
```
84
## audio.createAudioRenderer<sup>8+</sup>
85

86
createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer>
V
Vaidegi B 已提交
87

88
Obtains an **AudioRenderer** instance. This API uses a promise to return the renderer instance.
V
Vaidegi B 已提交
89

90
**System capability:** SystemCapability.Multimedia.Audio.Renderer
91

92 93 94 95
**Parameters:**
| Name       | Type                                           | Mandatory | Description                 |
| :--------- | :--------------------------------------------- | :-------- | :---------------------------|
| options    | [AudioRendererOptions](#audiorendereroptions8) | Yes       | Renderer configurations.    |
V
Vaidegi B 已提交
96

97
**Return value:**
V
Vaidegi B 已提交
98

99 100 101
| Type                                      | Description                                         |
| ----------------------------------------- | --------------------------------------------------- |
| Promise<[AudioRenderer](#audiorenderer8)> | Promise used to return the audio renderer instance. |
V
Vaidegi B 已提交
102

103
**Example:**
V
Vaidegi B 已提交
104 105

```
106 107
import audio from '@ohos.multimedia.audio';

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
var audioStreamInfo = {
    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
    channels: audio.AudioChannel.CHANNEL_1,
    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

var audioRendererInfo = {
    content: audio.ContentType.CONTENT_TYPE_SPEECH,
    usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
    rendererFlags: 1
}

var audioRendererOptions = {
    streamInfo: audioStreamInfo,
    rendererInfo: audioRendererInfo
}

126 127 128 129 130 131 132
var audioRenderer;
audio.createAudioRenderer(audioRendererOptions).then((data) => {
    audioRenderer = data;
    console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS');
}).catch((err) => {
    console.info('AudioFrameworkRenderLog: AudioRenderer Created : ERROR : '+err.message);
});
V
Vaidegi B 已提交
133 134
```

135
## audio.createAudioCapturer<sup>8+</sup>
136

137
createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void
V
Vaidegi B 已提交
138

139
Obtains an **AudioCapturer** instance. This API uses an asynchronous callback to return the capturer instance.
140

141
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
142

143 144 145
**Parameters:**
| Name       | Type                                            | Mandatory | Description                                          |
| :--------- | :---------------------------------------------- | :-------- | :--------------------------------------------------- |
G
Geevarghese V K 已提交
146
| options    | [AudioCapturerOptions](#audiocaptureroptions8)   | Yes       | Capturer configurations.                             |
147
| callback   | AsyncCallback<[AudioCapturer](#audiocapturer8)> | Yes       | Callback used to return the audio capturer instance. |
V
Vaidegi B 已提交
148

149
**Example:**
150 151

```
152
import audio from '@ohos.multimedia.audio';
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
var audioStreamInfo = {
    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
    channels: audio.AudioChannel.CHANNEL_2,
    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

var audioCapturerInfo = {
    source: audio.SourceType.SOURCE_TYPE_MIC,
    capturerFlags: 1
}

var audioCapturerOptions = {
    streamInfo: audioStreamInfo,
    capturerInfo: audioCapturerInfo
}

audio.createAudioCapturer(audioCapturerOptions,(err, data) => {
    if (err) {
        console.error(`AudioCapturer Created : Error: ${err.message}`);
    }
    else {
        console.info('AudioCapturer Created : Success : SUCCESS');
176
        let audioCapturer = data;
177 178 179 180
    }
});
```

181
## audio.createAudioCapturer<sup>8+</sup>
182

183
createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\>
184

185
Obtains an **AudioCapturer** instance. This API uses a promise to return the capturer instance.
186

187
**System capability:** SystemCapability.Multimedia.Audio.Capturer
188

189 190 191
**Parameters:**
| Name       | Type                                          | Mandatory | Description                 |
| :--------- | :-------------------------------------------- | :-------- | :-------------------------- |
G
Geevarghese V K 已提交
192
| options    | [AudioCapturerOptions](#audiocaptureroptions8) | Yes       | Capturer configurations.    |
193

194
**Return value:**
V
Vaidegi B 已提交
195

196 197 198
| Type                                      | Description                                         |
| ----------------------------------------- | --------------------------------------------------- |
| Promise<[AudioCapturer](#audiocapturer8)> | Promise used to return the audio capturer instance. |
V
Vaidegi B 已提交
199

200
**Example:**
V
Vaidegi B 已提交
201 202

```
203 204
import audio from '@ohos.multimedia.audio';

205 206 207 208 209 210 211 212 213 214 215 216 217
var audioStreamInfo = {
    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
    channels: audio.AudioChannel.CHANNEL_2,
    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

var audioCapturerInfo = {
    source: audio.SourceType.SOURCE_TYPE_MIC,
    capturerFlags: 1
}

var audioCapturerOptions = {
218 219
    streamInfo: audioStreamInfo,
    capturerInfo: audioCapturerInfo
220
}
V
Vaidegi B 已提交
221

222 223 224 225 226 227 228
var audioCapturer;
audio.createAudioRenderer(audioCapturerOptions).then((data) => {
    audioCapturer = data;
    console.info('AudioCapturer Created : Success : Stream Type: SUCCESS');
}).catch((err) => {
    console.info('AudioCapturer Created : ERROR : '+err.message);
});
229
```
V
Vaidegi B 已提交
230

231
## AudioVolumeType
W
wusongqing 已提交
232 233 234

Enumerates audio stream types.

235
**System capability:** SystemCapability.Multimedia.Audio.Volume
Z
zengyawen 已提交
236

237 238 239 240 241 242
| Name                         | Default Value  | Description                       |
| ---------------------------- | -------------- | --------------------------------- |
| VOICE_CALL<sup>8+</sup>      | 0              | Audio stream for voice calls.     |
| RINGTONE                     | 2              | Audio stream for ringtones.       |
| MEDIA                        | 3              | Audio stream for media purpose.   |
| VOICE_ASSISTANT<sup>8+</sup> | 9              | Audio stream for voice assistant. |
V
Vaidegi B 已提交
243

244
## DeviceFlag
W
wusongqing 已提交
245 246 247

Enumerates audio device flags.

248
**System capability:** SystemCapability.Multimedia.Audio.Device
Z
zengyawen 已提交
249

250 251 252 253 254
| Name                | Default Value | Description    |
| ------------------- | ------------- | -------------- |
| OUTPUT_DEVICES_FLAG | 1             | Output device. |
| INPUT_DEVICES_FLAG  | 2             | Input device.  |
| ALL_DEVICES_FLAG    | 3             | All devices.   |
V
Vaidegi B 已提交
255

256 257

## DeviceRole
W
wusongqing 已提交
258 259 260

Enumerates audio device roles.

261
**System capability:** SystemCapability.Multimedia.Audio.Device
Z
zengyawen 已提交
262

263 264 265 266
| Name          | Default Value | Description  |
| ------------- | ------------- | ------------ |
| INPUT_DEVICE  | 1             | Input role.  |
| OUTPUT_DEVICE | 2             | Output role. |
V
Vaidegi B 已提交
267

268
## DeviceType
W
wusongqing 已提交
269 270 271

Enumerates audio device types.

272 273 274 275 276 277 278 279 280 281 282 283 284
**System capability:** SystemCapability.Multimedia.Audio.Device

| Name             | Default Value | Description                                                              |
| ---------------- | ------------- | ------------------------------------------------------------------------ |
| INVALID          | 0             | Invalid device.                                                          |
| EARPIECE         | 1             | Earpiece.                                                                |
| SPEAKER          | 2             | Speaker.                                                                 |
| WIRED_HEADSET    | 3             | Wired headset.                                                           |
| WIRED_HEADPHONES | 4             | Wired headset without microphone.                                        |
| BLUETOOTH_SCO    | 7             | Bluetooth device using Synchronous Connection Oriented (SCO) links.      |
| BLUETOOTH_A2DP   | 8             | Bluetooth device using Advanced Audio Distribution Profile (A2DP) links. |
| MIC              | 15            | Microphone.                                                              |
| USB_HEADSET      | 22            | USB Type-C headset.                                                      |
Z
zengyawen 已提交
285

W
wusongqing 已提交
286
## ActiveDeviceType
V
Vaidegi B 已提交
287

W
wusongqing 已提交
288 289
Enumerates the active device types.

290
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
291

292 293 294 295
| Name          | Default Value | Description                                                            |
| ------------- | ------------- | ---------------------------------------------------------------------- |
| SPEAKER       | 2             | Speaker.                                                               |
| BLUETOOTH_SCO | 7             | Bluetooth device using the SCO links.                                  |
W
wusongqing 已提交
296 297

## AudioRingMode
W
wusongqing 已提交
298 299 300

Enumerates ringer modes.

301
**System capability:** SystemCapability.Multimedia.Audio.Communication
W
wusongqing 已提交
302

303 304 305 306 307 308 309
| Name                | Default Value | Description      |
| ------------------- | ------------- | ---------------- |
| RINGER_MODE_SILENT  | 0             | Silent mode.     |
| RINGER_MODE_VIBRATE | 1             | Vibration mode.  |
| RINGER_MODE_NORMAL  | 2             | Normal mode.     |

## AudioSampleFormat<sup>8+</sup>
V
Vaidegi B 已提交
310 311 312

Enumerates the audio sample formats.

313 314 315 316 317 318 319 320 321 322 323
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name                  | Default Value | Description                           |
| :-------------------- | :------------ | :------------------------------------ |
| SAMPLE_FORMAT_INVALID | -1            | Invalid format.                       |
| SAMPLE_FORMAT_U8      | 0             | Unsigned 8 bit integer.               |
| SAMPLE_FORMAT_S16LE   | 1             | Signed 16 bit integer, little endian. |
| SAMPLE_FORMAT_S24LE   | 2             | Signed 24 bit integer, little endian. |
| SAMPLE_FORMAT_S32LE   | 3             | Signed 32 bit integer, little endian. |

## AudioChannel<sup>8+</sup>
V
Vaidegi B 已提交
324 325 326

Enumerates the audio channels.

327 328 329 330 331 332 333 334
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name      | Default Value | Description      |
| :-------- | :------------ | :--------------- |
| CHANNEL_1 | 0x1 << 0      | Channel count 1. |
| CHANNEL_2 | 0x1 << 1      | Channel count 2. |

## AudioSamplingRate<sup>8+</sup>
V
Vaidegi B 已提交
335 336 337

Enumerates the audio sampling rates.

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
**System capability:** SystemCapability.Multimedia.Audio.Core

| 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. |


## AudioEncodingType<sup>8+</sup>

V
Vaidegi B 已提交
357 358
Enumerates the audio encoding types.

359 360 361 362 363 364
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name                  | Default Value  | Description       |
| :-------------------- | :------------- | :---------------- |
| ENCODING_TYPE_INVALID | -1             | Invalid.          |
| ENCODING_TYPE_RAW     |  0             | PCM encoding.     |
V
Vaidegi B 已提交
365 366


367 368
## ContentType

V
Vaidegi B 已提交
369 370
Enumerates the content types.

371 372 373 374 375 376 377 378 379 380
**System capability:** SystemCapability.Multimedia.Audio.Core

| 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             | Sonification content.   |
| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5             | Ringtone content.       |
V
Vaidegi B 已提交
381 382


383 384
## StreamUsage

V
Vaidegi B 已提交
385 386
Enumerates the stream usage.

387 388 389 390 391 392 393 394
**System capability:** SystemCapability.Multimedia.Audio.Core

| 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 | 6             | Notification or ringtone usage.   |
V
Vaidegi B 已提交
395 396


397 398
## AudioState<sup>8+</sup>

V
Vaidegi B 已提交
399 400
Enumerates the audio states.

401 402 403 404 405 406 407 408 409 410 411
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name           | Default Value | Description                  |
| :------------- | :------------ | :--------------------------- |
| STATE_INVALID  | -1            | Invalid state.               |
| STATE_NEW      | 0             | Creating 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.                |
V
Vaidegi B 已提交
412 413


414 415
## AudioRendererRate<sup>8+</sup>

V
Vaidegi B 已提交
416 417
Enumerates the audio renderer rates.

418 419 420 421 422 423 424
**System capability:** SystemCapability.Multimedia.Audio.Renderer

| Name               | Default Value | Description   |
| :----------------- | :------------ | :------------ |
| RENDER_RATE_NORMAL | 0             | Normal rate.  |
| RENDER_RATE_DOUBLE | 1             | Double rate.  |
| RENDER_RATE_HALF   | 2             | Half rate.    |
V
Vaidegi B 已提交
425

426
## InterruptType
V
Vaidegi B 已提交
427 428 429

Enumerates the interrupt types.

430
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
431

432 433 434 435 436 437 438
| Name                 | Default Value | Description                          |
| :------------------- | :------------ | :----------------------------------- |
| INTERRUPT_TYPE_BEGIN | 1             | Audio playback interruption started. |
| INTERRUPT_TYPE_END   | 2             | Audio playback interruption ended.   |


## InterruptForceType<sup>9+</sup>
V
Vaidegi B 已提交
439 440 441

Enumerates the interrupt force types.

442
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
443

444 445 446 447 448 449
| Name            | Default Value | Description                                |
| :-------------- | :------------ | :----------------------------------------- |
| INTERRUPT_FORCE | 0             | Forced action taken by system.             |
| INTERRUPT_SHARE | 1             | App can choose to take action or ignore.   |

## InterruptHint
V
Vaidegi B 已提交
450 451 452

Enumerates the interrupt hints.

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
**System capability:** SystemCapability.Multimedia.Audio.Renderer

| Name                               | Default Value | Description                  |
| :--------------------------------- | :------------ | :--------------------------- |
| INTERRUPT_HINT_NONE<sup>8+</sup>   | 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<sup>8+</sup> | 5             | Unducked the playback.       |

## InterruptActionType

Enumerates the interrupt event return types.

This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.

**System capability:** SystemCapability.Multimedia.Audio.Renderer

| Name           | Default Value | Description                               |
| :------------- | :------------ | :---------------------------------------- |
| TYPE_ACTIVATED | 0             | Audio interrupt activated.                |
| TYPE_INTERRUPT | 1             | Audio interrupted.                        |

## AudioStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
478

479
Describes audio stream information.
V
Vaidegi B 已提交
480

481
**System capability:** SystemCapability.Multimedia.Audio.Core
482

483 484 485 486 487 488 489 490
| Name          | Type                                      | Mandatory | Description           |
| :------------ | :---------------------------------------- | :-------- | :-------------------- |
| samplingRate  | [AudioSamplingRate](#audiosamplingrate8)  | Yes       | Sampling rate.        |
| channels      | [AudioChannel](#audiochannel8)            | Yes       | Audio channels.       |
| sampleFormat  | [AudioSampleFormat](#audiosampleformat8)  | Yes       | Audio sample format.  |
| encodingType  | [AudioEncodingType](#audioencodingtype8)  | Yes       | Audio encoding type.  |

## AudioRendererInfo<sup>8+</sup>
V
Vaidegi B 已提交
491 492 493

Describes audio renderer information.

494 495 496 497 498 499 500
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name          | Type                        | Mandatory | Description           |
| :------------ | :-------------------------- | :-------- | :-------------------- |
| contentType   | [ContentType](#contenttype) | Yes       | Content type.         |
| usage         | [StreamUsage](#streamusage) | Yes       | Stream usage.         |
| rendererFlags | number                      | Yes       | Audio renderer flags. |
V
Vaidegi B 已提交
501

502
## AudioRendererOptions<sup>8+</sup>
V
Vaidegi B 已提交
503

504 505
Describes audio renderer configuration options.

506
**System capability:** SystemCapability.Multimedia.Audio.Renderer
507

508 509 510 511 512
| Name          | Type                                      | Mandatory | Description           |
| :------------ | :-----------------------------------------| :-------- | :-------------------- |
| streamInfo    | [AudioStreamInfo](#audiostreaminfo8)      | Yes       | Stream information.   |
| rendererInfo  | [AudioRendererInfo](#audiorendererinfo8)  | Yes       | Renderer information. |

G
Geevarghese V K 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
## AudioCapturerInfo<sup>8+</sup><a name="audiocapturerinfo"></a>

Describes audio capturer information.

**System capability:** SystemCapability.Multimedia.Audio.Core

| Name            | Type                       | Mandatory | Description           |
| :---------------| :------------------------- | :-------- | :-------------------- |
| source          | [SourceType](#sourcetype)  | Yes       | Audio source type.    |
| capturerFlags   | number                     | Yes       | Audio capturer flags. |

## AudioCapturerOptions<sup>8+</sup>

Describes audio capturer configuration options.

**System capability:** SystemCapability.Multimedia.Audio.Capturer

| Name          | Type                                      | Mandatory | Description           |
| :------------ | :-----------------------------------------| :-------- | :-------------------- |
| streamInfo    | [AudioStreamInfo](#audiostreaminfo8)      | Yes       | Stream information.   |
| capturerInfo  | [AudioCapturerInfo](#audiocapturerinfo8)  | Yes       | Capturer information. |

535
## InterruptEvent<sup>9+</sup>
V
Vaidegi B 已提交
536 537 538

Describes the interrupt event received by the app when playback is interrupted.

539 540 541 542 543 544 545 546 547
**System capability:** SystemCapability.Multimedia.Audio.Renderer

| Name      | Type                                          | Mandatory | Description                                                       |
| :-------- | :-------------------------------------------- | :-------- | :---------------------------------------------------------------- |
| eventType | [InterruptType](#interrupttype)               | Yes       | Whether the interruption has started or finished.                 |
| forceType | [InterruptForceType](#interruptforcetype9)    | Yes       | Whether the action is taken by system or to be taken by the app.  |
| hintType  | [InterruptHint](#interrupthint)               | Yes       | Type of action.                                                   |

## AudioInterrupt
548

549
The parameters passed in by the audio listener event.
V
Vaidegi B 已提交
550

551
This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.
V
Vaidegi B 已提交
552

553
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
| Name          | Type                                      | Mandatory | Description           |
| --------------- | --------------------------- | ---- | ------------------------------------------------------------ |
| streamUsage     | [StreamUsage](#streamusage) | Yes   | Audio stream usage type.                                             |
| contentType     | [ContentType](#contenttype) | Yes   | Audio interrupt media type.                                           |
| pauseWhenDucked | boolean                     | Yes   | Whether audio playback can be paused when audio is interrupted (true means audio playback can be paused during audio interruptions and false means the opposite). |

## InterruptAction

Callback method for the audio interrupt or audio interrupt activated event.

This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.

**System capability:** SystemCapability.Multimedia.Audio.Renderer

| Name       | Type                                        | Mandatory | Description                                                                                                                                            |
| ---------- | ------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| actionType | [InterruptActionType](#interruptactiontype) | Yes       | Event return type. TYPE_ACTIVATED is the audio interrupt activated event, and TYPE_INTERRUPT is the audio interrupt event.                                         |
| type       | [InterruptType](#interrupttype)             | No        | Interrupt event type.                                                                                                                                  |
G
Geevarghese V K 已提交
573
| hint       | [InterruptHint](#interrupthint)              | No        | Interrupt event prompts.                                                                                                                               |
574 575 576
| activated  | boolean                                     | No        | Acquire/release focus. true indicates that the focus acquisition/release is successful, and false indicates that the focus acquisition/release fails.  |

## VolumeEvent<sup>8+</sup>
V
Vaidegi B 已提交
577 578
Describes the volume event received by the app when the volume is changed.

579
This is a system API and cannot be called by third-party applications.
580

581
**System capability:** SystemCapability.Multimedia.Audio.Volume
V
Vaidegi B 已提交
582

583 584 585 586 587
| Name       | Type                                | Mandatory | Description                              |
| :--------- | :---------------------------------- | :-------- | :--------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Volume type of the current stream.       |
| volume     | number                              | Yes       | Volume level.                            |
| updateUi   | boolean                             | Yes       | Whether to show the volume change in UI. |
V
Vaidegi B 已提交
588

589
## DeviceChangeAction
590

591 592
Describes the device change type and device information.

593
**System capability:** SystemCapability.Multimedia.Audio.Device
594

595 596
| Name                | Type                                              | Mandatory | Description         |
| :------------------ | :------------------------------------------------ | :-------- | :------------------ |
G
Geevarghese V K 已提交
597 598
| type                | [DeviceChangeType](#devicechangetype)             | Yes       | Device change type. |
| deviceDescriptors   | [AudioDeviceDescriptors](#audiodevicedescriptors) | Yes       | Device information. |
599 600 601


## DeviceChangeType
602

603 604
Enumerates device change types.

605 606 607 608 609 610
**System capability:** SystemCapability.Multimedia.Audio.Device

| Name                   | Default Value | Description           |
| :--------------------- | :------------ | :-------------------- |
| CONNECT                | 0             | Device connection.    |
| DISCONNECT             | 1             | Device disconnection. |
611 612

## SourceType<sup>8+</sup><a name="sourcetype"></a>
613

614 615
Enumerates source types.

616 617 618 619 620 621 622
**System capability:** SystemCapability.Multimedia.Audio.Core

| Name                                                                                                                                                         | Default Value | Description                      |
| :----------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------ | :------------------------------- |
| SOURCE_TYPE_INVALID                                                                                                                                          | -1            | Invalid source type.             |
| SOURCE_TYPE_MIC                                                                                                                                              | 0             | Mic source type.                 |
| SOURCE_TYPE_VOICE_COMMUNICATION(This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR) | 7             | Voice communication source type. |
623 624 625


## AudioScene<sup>8+</sup><a name="audioscene"></a>
626

627 628
Enumerates audio scenes.

629 630 631 632 633 634 635 636
**System capability:** SystemCapability.Multimedia.Audio.Communication

| Name                                                                                  | Default Value | Description             |
| :------------------------------------------------------------------------------------ | :------------ | :---------------------- |
| AUDIO_SCENE_DEFAULT                                                                   | 0             | Default audio scene.    |
| AUDIO_SCENE_RINGING(system API, not supported by third-party applications)       | 1             | Ringing audio scene.    |
| AUDIO_SCENE_PHONE_CALL(system API, not supported by third-party applications)    | 2             | Phone call audio scene. |
| AUDIO_SCENE_VOICE_CHAT                                                                | 3             | Voice chat audio scene. |
W
wusongqing 已提交
637

638
## AudioManager
W
wusongqing 已提交
639

640
Implements audio volume and audio device management. Before calling the interface of AudioManager, you need to create an instance through [getAudioManager](#audiogetaudiomanager).
W
wusongqing 已提交
641

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
### setVolume

setVolume\(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void\>\): void

Sets the volume for a stream. This API uses an asynchronous callback to return the result.

**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

| Name       | Type                                | Mandatory | Description                                                                              |
| ---------- | ----------------------------------- | --------- | ---------------------------------------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Volume 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.                                                      |

**Example:**
W
wusongqing 已提交
659 660

```
661 662 663 664 665 666
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.');
667
});
W
wusongqing 已提交
668
```
669
### setVolume
W
wusongqing 已提交
670

671 672 673 674 675
setVolume\(volumeType: AudioVolumeType, volume: number\): Promise<void\>

Sets the volume for a stream. This API uses a promise to return the result.

**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690
**Parameters:**

| Name       | Type                                | Mandatory | Description                                                                              |
| ---------- | ----------------------------------- | --------- | ---------------------------------------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Volume 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:**
W
wusongqing 已提交
691 692

```
693
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
W
wusongqing 已提交
694
    console.log('Promise returned to indicate a successful volume setting.');
695 696 697 698
});
```

### getVolume
W
wusongqing 已提交
699

700
getVolume\(volumeType: AudioVolumeType, callback: AsyncCallback<number\>\): void
W
wusongqing 已提交
701

702
Obtains the volume of a stream. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
703

704 705 706 707 708 709 710 711 712 713
**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
| callback   | AsyncCallback<number>               | Yes       | Callback used to return the volume.  |

**Example:**
W
wusongqing 已提交
714 715 716 717

```
audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
   if (err) {
718 719
       console.error('Failed to obtain the volume. ${err.message}');
       return;
W
wusongqing 已提交
720 721
   }
   console.log('Callback invoked to indicate that the volume is obtained.');
722
});
W
wusongqing 已提交
723 724
```

V
Vaidegi B 已提交
725

726
### getVolume
V
Vaidegi B 已提交
727

728
getVolume\(volumeType: AudioVolumeType\): Promise<number\>
W
wusongqing 已提交
729

730
Obtains the volume of a stream. This API uses a promise to return the query result.
W
wusongqing 已提交
731

732
**System capability:** SystemCapability.Multimedia.Audio.Volume
733

734
**Parameters:**
W
wusongqing 已提交
735

736 737 738
| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
W
wusongqing 已提交
739

740 741 742 743 744
**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<number>       | Promise used to return the result.  |
W
wusongqing 已提交
745

746
**Example:**
W
wusongqing 已提交
747 748

```
749
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
750
    console.log('Promise returned to indicate that the volume is obtained.' + value);
751 752 753 754 755 756 757 758
});
```

### getMinVolume

getMinVolume\(volumeType: AudioVolumeType, callback: AsyncCallback<number\>\): void

Obtains the minimum volume allowed for a stream. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
759

760
**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
761

762
**Parameters:**
W
wusongqing 已提交
763

764 765 766 767 768 769
| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
| callback   | AsyncCallback<number>               | Yes       | Callback used to return the volume.  |

**Example:**
W
wusongqing 已提交
770 771 772 773 774

```
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
    if (err) {
        console.error('Failed to obtain the minimum volume. ${err.message}');
775
        return;
W
wusongqing 已提交
776 777
    }
    console.log('Callback invoked to indicate that the minimum volume is obtained.' + value);
778
});
W
wusongqing 已提交
779 780
```

V
Vaidegi B 已提交
781

782
### getMinVolume
V
Vaidegi B 已提交
783 784

getMinVolume\(volumeType: AudioVolumeType\): Promise<number\><a name="section41556389511"></a>
W
wusongqing 已提交
785

786
Obtains the minimum volume allowed for a stream. This API uses a promise to return the query result.
W
wusongqing 已提交
787

788
**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
789

790 791 792 793 794
**Parameters:**

| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
W
wusongqing 已提交
795

796
**Return value:**
W
wusongqing 已提交
797

798 799 800
| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<number>     | Promise used to return the result.  |
W
wusongqing 已提交
801

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
**Example:**

```
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => {
    console.log('Promised returned to indicate that the minimum volume is obtained.' + value);
});
```

### getMaxVolume

getMaxVolume\(volumeType: AudioVolumeType, callback: AsyncCallback<number\>\): void

Obtains the maximum volume allowed for a stream. This API uses an asynchronous callback to return the query result.

**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
| callback   | AsyncCallback<number>               | Yes       | Callback used to return the volume.  |

**Example:**
W
wusongqing 已提交
826 827 828 829 830 831 832 833

```
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);
834
});
W
wusongqing 已提交
835 836
```

V
Vaidegi B 已提交
837

838
### getMaxVolume
V
Vaidegi B 已提交
839

840
getMaxVolume\(volumeType: AudioVolumeType\): Promise<number\>
W
wusongqing 已提交
841

842
Obtains the maximum volume allowed for a stream. This API uses a promise to return the query result.
W
wusongqing 已提交
843

844
**System capability:** SystemCapability.Multimedia.Audio.Volume
845

846
**Parameters:**
W
wusongqing 已提交
847

848 849 850
| Name       | Type                                | Mandatory | Description                          |
| ---------- | ----------------------------------- | --------- | ------------------------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type.                   |
W
wusongqing 已提交
851

852 853 854 855 856
**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<number>       | Promise used to return the result.  |
W
wusongqing 已提交
857

858
**Example:**
W
wusongqing 已提交
859 860

```
861
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => {
W
wusongqing 已提交
862
    console.log('Promised returned to indicate that the maximum volume is obtained.');
863
});
W
wusongqing 已提交
864 865
```

866
### mute
V
Vaidegi B 已提交
867

W
wusongqing 已提交
868
mute\(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void\>\): void
W
wusongqing 已提交
869

870
Mutes a stream. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
871

872
**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
873

874
**Parameters:**
W
wusongqing 已提交
875

876 877 878 879 880 881 882
| Name       | Type                                | Mandatory | Description                                                                                |
| ---------- | ----------------------------------- | --------- | ------------------------------------------------------------------------------------------ |
| volumeType | [AudioVolumeType](#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<number>               | Yes       | Callback used to return the volume.                                                        |

**Example:**
W
wusongqing 已提交
883 884 885 886 887

```
audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => {
    if (err) {
        console.error('Failed to mute the stream. ${err.message}');
888
        return;
W
wusongqing 已提交
889 890
    }
    console.log('Callback invoked to indicate that the stream is muted.');
891
});
W
wusongqing 已提交
892 893 894
```


895
### mute
V
Vaidegi B 已提交
896

W
wusongqing 已提交
897
mute\(volumeType: AudioVolumeType, mute: boolean\): Promise<void\>
V
Vaidegi B 已提交
898

899
Mutes a stream. This API uses a promise to return the result.
W
wusongqing 已提交
900

901 902 903 904 905 906 907 908
**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

| Name       | Type                                | Mandatory | Description                                                                                |
| ---------- | ----------------------------------- | --------- | ------------------------------------------------------------------------------------------ |
| volumeType | [AudioVolumeType](#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. |
W
wusongqing 已提交
909

910 911 912 913 914 915 916
**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<void>       | Promise used to return the result.  |

**Example:**
W
wusongqing 已提交
917 918

```
919
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
W
wusongqing 已提交
920
    console.log('Promise returned to indicate that the stream is muted.');
921
});
W
wusongqing 已提交
922 923
```

V
Vaidegi B 已提交
924

925
### isMute
V
Vaidegi B 已提交
926

W
wusongqing 已提交
927
isMute\(volumeType: AudioVolumeType, callback: AsyncCallback<boolean\>\): void
W
wusongqing 已提交
928

929
Checks whether a stream is muted. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
930

931
**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
932

933
**Parameters:**
W
wusongqing 已提交
934

935 936 937 938 939 940
| Name       | Type                                | Mandatory | Description                                                                                                                         |
| ---------- | ----------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| volumeType | [AudioVolumeType](#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.|

**Example:**
W
wusongqing 已提交
941 942 943 944

```
audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => {
   if (err) {
945 946
       console.error('Failed to obtain the mute status. ${err.message}');
       return;
W
wusongqing 已提交
947 948
   }
   console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value);
949
});
W
wusongqing 已提交
950 951 952
```


953
### isMute
V
Vaidegi B 已提交
954

W
wusongqing 已提交
955
isMute\(volumeType: AudioVolumeType\): Promise<boolean\>
V
Vaidegi B 已提交
956

957
Checks whether a stream is muted. This API uses a promise to return the result.
W
wusongqing 已提交
958

959 960 961
**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**
W
wusongqing 已提交
962

963 964 965 966 967 968 969 970 971 972 973
| Name       | Type                                | Mandatory | Description         |
| ---------- | ----------------------------------- | --------- | ------------------- |
| volumeType | [AudioVolumeType](#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:**
W
wusongqing 已提交
974 975

```
976
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
977
    console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value);
978
});
W
wusongqing 已提交
979 980
```

981
### isActive
V
Vaidegi B 已提交
982

W
wusongqing 已提交
983
isActive\(volumeType: AudioVolumeType, callback: AsyncCallback<boolean\>\)
W
wusongqing 已提交
984

985
Checks whether a stream is active. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
986

987
**System capability:** SystemCapability.Multimedia.Audio.Volume
W
wusongqing 已提交
988

989
**Parameters:**
W
wusongqing 已提交
990

991 992 993 994 995 996
| Name       | Type                                | Mandatory | Description                                                                                                                           |
| ---------- | ----------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| volumeType | [AudioVolumeType](#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.|

**Example:**
W
wusongqing 已提交
997 998 999 1000 1001

```
audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => {
    if (err) {
        console.error('Failed to obtain the active status of the stream. ${err.message}');
1002
        return;
W
wusongqing 已提交
1003 1004
    }
    console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value);
1005
});
W
wusongqing 已提交
1006 1007
```

V
Vaidegi B 已提交
1008

1009
### isActive
V
Vaidegi B 已提交
1010

W
wusongqing 已提交
1011
isActive\(volumeType: AudioVolumeType\): Promise<boolean\>
W
wusongqing 已提交
1012

1013
Checks whether a stream is active. This API uses a promise to return the query result.
W
wusongqing 已提交
1014

1015 1016 1017 1018 1019 1020 1021
**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

| Name       | Type                                | Mandatory | Description        |
| ---------- | ----------------------------------- | --------- | ------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | Yes       | Audio stream type. |
W
wusongqing 已提交
1022

1023 1024 1025 1026 1027 1028 1029
**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:**
W
wusongqing 已提交
1030 1031

```
1032
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
1033
    console.log('Promise returned to indicate that the active status of the stream is obtained.' + value);
1034
});
W
wusongqing 已提交
1035 1036 1037
```


1038
### setRingerMode
V
Vaidegi B 已提交
1039

W
wusongqing 已提交
1040
setRingerMode\(mode: AudioRingMode, callback: AsyncCallback<void\>\): void
V
Vaidegi B 已提交
1041

1042
Sets the ringer mode. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1043

1044
**System capability:** SystemCapability.Multimedia.Audio.Communication
W
wusongqing 已提交
1045

1046
**Parameters:**
W
wusongqing 已提交
1047

1048 1049 1050 1051 1052 1053
| Name       | Type                                | Mandatory | Description                         |
| ---------- | ----------------------------------- | --------- | ----------------------------------- |
| mode       | [AudioRingMode](#audioringmode)     | Yes       | Ringer mode.                        |
| callback   | AsyncCallback<void>                 | Yes       | Callback used to return the result. |

**Example:**
W
wusongqing 已提交
1054 1055 1056 1057 1058 1059 1060 1061

```
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.');
1062
});
W
wusongqing 已提交
1063 1064 1065
```


1066
### setRingerMode
V
Vaidegi B 已提交
1067

W
wusongqing 已提交
1068
setRingerMode\(mode: AudioRingMode\): Promise<void\>
V
Vaidegi B 已提交
1069

1070
Sets the ringer mode. This API uses a promise to return the result.
W
wusongqing 已提交
1071

1072 1073 1074
**System capability:** SystemCapability.Multimedia.Audio.Communication

**Parameters:**
W
wusongqing 已提交
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
| Name       | Type                                | Mandatory | Description                         |
| ---------- | ----------------------------------- | --------- | ----------------------------------- |
| mode       | [AudioRingMode](#audioringmode)     | Yes       | Ringer mode.                        |

**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<void>       | Promise used to return the result.  |

**Example:**
W
wusongqing 已提交
1087 1088

```
1089
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => {
W
wusongqing 已提交
1090
    console.log('Promise returned to indicate a successful setting of the ringer mode.');
1091
});
W
wusongqing 已提交
1092 1093
```

V
Vaidegi B 已提交
1094

1095
### getRingerMode
V
Vaidegi B 已提交
1096

W
wusongqing 已提交
1097
getRingerMode\(callback: AsyncCallback<AudioRingMode\>\): void
W
wusongqing 已提交
1098

1099 1100 1101
Obtains the ringer mode. This API uses an asynchronous callback to return the query result.

**System capability:** SystemCapability.Multimedia.Audio.Communication
W
wusongqing 已提交
1102

1103
**Parameters:**
W
wusongqing 已提交
1104

1105 1106 1107
| Name       | Type                                            | Mandatory | Description                              |
| ---------- | ----------------------------------------------- | --------- | ---------------------------------------- |
| callback   | AsyncCallback<[AudioRingMode](#audioringmode)>  | Yes       | Callback used to return the ringer mode. |
W
wusongqing 已提交
1108

1109
**Example:**
W
wusongqing 已提交
1110 1111 1112 1113

```
audioManager.getRingerMode((err, value) => {
   if (err) {
1114 1115
       console.error('Failed to obtain the ringer mode.​ ${err.message}');
       return;
W
wusongqing 已提交
1116 1117
   }
   console.log('Callback invoked to indicate that the ringer mode is obtained.' + value);
1118
});
W
wusongqing 已提交
1119 1120
```

V
Vaidegi B 已提交
1121

1122
### getRingerMode
V
Vaidegi B 已提交
1123

W
wusongqing 已提交
1124
getRingerMode\(\): Promise<AudioRingMode\>
W
wusongqing 已提交
1125

1126
Obtains the ringer mode. This API uses a promise to return the query result.
W
wusongqing 已提交
1127

1128
**System capability:** SystemCapability.Multimedia.Audio.Communication
W
wusongqing 已提交
1129

1130
**Return value:**
W
wusongqing 已提交
1131

1132 1133 1134
| Type                                           | Description                             |
| ---------------------------------------------- | --------------------------------------- |
| Promise<[AudioRingMode](#audioringmode)>       | Promise used to return the ringer mode. |
W
wusongqing 已提交
1135

1136
**Example:**
W
wusongqing 已提交
1137 1138

```
1139
audioManager.getRingerMode().then((value) => {
W
wusongqing 已提交
1140
    console.log('Promise returned to indicate that the ringer mode is obtained.' + value);
1141
});
W
wusongqing 已提交
1142 1143 1144
```


1145
### setAudioParameter
V
Vaidegi B 已提交
1146

W
wusongqing 已提交
1147
setAudioParameter\(key: string, value: string, callback: AsyncCallback<void\>\): void
V
Vaidegi B 已提交
1148

1149 1150 1151
Sets an audio parameter. This API uses an asynchronous callback to return the result.

**System capability:** SystemCapability.Multimedia.Audio.Core
W
wusongqing 已提交
1152

1153
**Parameters:**
W
wusongqing 已提交
1154

1155 1156 1157 1158 1159
| 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.   |
W
wusongqing 已提交
1160

1161
**Example:**
W
wusongqing 已提交
1162 1163 1164 1165 1166

```
audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => {
    if (err) {
        console.error('Failed to set the audio parameter. ${err.message}');
1167
        return;
W
wusongqing 已提交
1168 1169
    }
    console.log('Callback invoked to indicate a successful setting of the audio parameter.');
1170
});
W
wusongqing 已提交
1171 1172 1173
```


1174
### setAudioParameter
V
Vaidegi B 已提交
1175

W
wusongqing 已提交
1176
setAudioParameter\(key: string, value: string\): Promise<void\>
V
Vaidegi B 已提交
1177

1178
Sets an audio parameter. This API uses a promise to return the result.
W
wusongqing 已提交
1179

1180 1181 1182
**System capability:** SystemCapability.Multimedia.Audio.Core

**Parameters:**
W
wusongqing 已提交
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
| 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:**
W
wusongqing 已提交
1196 1197

```
1198
audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => {
W
wusongqing 已提交
1199
    console.log('Promise returned to indicate a successful setting of the audio parameter.');
1200
});
W
wusongqing 已提交
1201 1202
```

V
Vaidegi B 已提交
1203

1204
### getAudioParameter
V
Vaidegi B 已提交
1205

W
wusongqing 已提交
1206
getAudioParameter\(key: string, callback: AsyncCallback<string\>\)
W
wusongqing 已提交
1207

1208 1209 1210
Obtains the value of an audio parameter. This API uses an asynchronous callback to return the query result.

**System capability:** SystemCapability.Multimedia.Audio.Core
W
wusongqing 已提交
1211

1212
**Parameters:**
W
wusongqing 已提交
1213

1214 1215 1216 1217
| 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.  |
W
wusongqing 已提交
1218

1219
**Example:**
W
wusongqing 已提交
1220 1221 1222 1223 1224

```
audioManager.getAudioParameter('PBits per sample', (err, value) => {
    if (err) {
        console.error('Failed to obtain the value of the audio parameter. ${err.message}');
1225
        return;
W
wusongqing 已提交
1226 1227
    }
    console.log('Callback invoked to indicate that the value of the audio parameter is obtained.' + value);
1228
});
W
wusongqing 已提交
1229 1230
```

V
Vaidegi B 已提交
1231

1232
### getAudioParameter
V
Vaidegi B 已提交
1233

W
wusongqing 已提交
1234
getAudioParameter\(key: string\): Promise<string\>
W
wusongqing 已提交
1235

1236
Obtains the value of an audio parameter. This API uses a promise to return the query result.
W
wusongqing 已提交
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246
**System capability:** SystemCapability.Multimedia.Audio.Core

**Parameters:**

| Name      | Type                  | Mandatory | Description                                                 |
| --------- | ----------------------| --------- | ----------------------------------------------------------- |
| key       | string                | Yes       | Key of the audio parameter whose value is to be obtained.   |

**Return value:**
W
wusongqing 已提交
1247

1248 1249 1250 1251 1252
| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<string>     | Promise used to return the value of the audio parameter.  |

**Example:**
W
wusongqing 已提交
1253 1254

```
1255
audioManager.getAudioParameter('PBits per sample').then((value) => {
W
wusongqing 已提交
1256
    console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value);
1257
});
W
wusongqing 已提交
1258 1259
```

V
Vaidegi B 已提交
1260

1261 1262 1263 1264 1265
### getDevices

getDevices\(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors\>\): void

Obtains the audio devices with a specific flag. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
1266

1267
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1268

1269
**Parameters:**
W
wusongqing 已提交
1270

1271 1272 1273 1274 1275 1276
| Name       | Type                                                             | Mandatory | Description                               |
| ---------  | ---------------------------------------------------------------- | --------- | ----------------------------------------- |
| deviceFlag | [DeviceFlag](#deviceflag)                                        | Yes       | Audio device flag.                        |
| callback   | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Yes       | Callback used to return the device list.  |

**Example:**
W
wusongqing 已提交
1277 1278

```
1279
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value) => {
W
wusongqing 已提交
1280
   if (err) {
1281 1282
       console.error('Failed to obtain the device list. ${err.message}');
       return;
W
wusongqing 已提交
1283 1284
   }
   console.log('Callback invoked to indicate that the device list is obtained.');
1285
});
W
wusongqing 已提交
1286 1287
```

V
Vaidegi B 已提交
1288 1289


1290
### getDevices
V
Vaidegi B 已提交
1291

1292
getDevices\(deviceFlag: DeviceFlag\): Promise<AudioDeviceDescriptors\>
W
wusongqing 已提交
1293

1294
Obtains the audio devices with a specific flag. This API uses a promise to return the query result.
W
wusongqing 已提交
1295

1296
**System capability:** SystemCapability.Multimedia.Audio.Device
1297

1298
**Parameters:**
W
wusongqing 已提交
1299

1300 1301 1302
| Name       | Type                        | Mandatory | Description         |
| ---------  | --------------------------- | --------- | ------------------- |
| deviceFlag | [DeviceFlag](#deviceflag)   | Yes       | Audio device flag.  |
W
wusongqing 已提交
1303

1304 1305 1306 1307 1308
**Return value:**

| Type                                                        | Description                              |
| ----------------------------------------------------------- | ---------------------------------------- |
| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)>  | Promise used to return the device list.  |
W
wusongqing 已提交
1309

1310
**Example:**
W
wusongqing 已提交
1311 1312

```
1313
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
W
wusongqing 已提交
1314
    console.log('Promise returned to indicate that the device list is obtained.');
1315
});
W
wusongqing 已提交
1316 1317
```

1318
### setDeviceActive
V
Vaidegi B 已提交
1319

W
wusongqing 已提交
1320
setDeviceActive\(deviceType: DeviceType, active: boolean, callback: AsyncCallback<void\>\): void
V
Vaidegi B 已提交
1321

1322
Sets a device to the active state. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1323

1324
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1325

1326
**Parameters:**
W
wusongqing 已提交
1327

1328 1329 1330 1331 1332 1333 1334
| Name       | Type                                   | Mandatory | Description                                                                                                      |
| ---------  | ---------------------------------------| --------- | ---------------------------------------------------------------------------------------------------------------- |
| deviceType | [ActiveDeviceType](#activedevicetype)  | 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.                                                                              |

**Example:**
W
wusongqing 已提交
1335 1336

```
1337
audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true, (err) => {
W
wusongqing 已提交
1338 1339
    if (err) {
        console.error('Failed to set the active status of the device. ${err.message}');
1340
        return;
W
wusongqing 已提交
1341 1342
    }
    console.log('Callback invoked to indicate that the device is set to the active status.');
1343
});
W
wusongqing 已提交
1344 1345 1346
```


V
Vaidegi B 已提交
1347

1348
### setDeviceActive
V
Vaidegi B 已提交
1349

W
wusongqing 已提交
1350
setDeviceActive\(deviceType: DeviceType, active: boolean\): Promise<void\>
V
Vaidegi B 已提交
1351

1352
Sets a device to the active state. This API uses a promise to return the result.
W
wusongqing 已提交
1353

1354 1355 1356 1357 1358 1359 1360 1361
**System capability:** SystemCapability.Multimedia.Audio.Device

**Parameters:**

| Name       | Type                                   | Mandatory | Description                                                                                                      |
| ---------  | ---------------------------------------| --------- | ---------------------------------------------------------------------------------------------------------------- |
| deviceType | [ActiveDeviceType](#activedevicetype)  | 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. |
W
wusongqing 已提交
1362

1363 1364 1365 1366 1367 1368 1369 1370
**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<void>       | Promise used to return the result.  |


**Example:**
W
wusongqing 已提交
1371 1372

```
1373
audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(() => {
W
wusongqing 已提交
1374
    console.log('Promise returned to indicate that the device is set to the active status.');
1375
});
W
wusongqing 已提交
1376 1377
```

V
Vaidegi B 已提交
1378

1379
### isDeviceActive
V
Vaidegi B 已提交
1380

W
wusongqing 已提交
1381
isDeviceActive\(deviceType: DeviceType, callback: AsyncCallback<boolean\>\): void
W
wusongqing 已提交
1382

1383
Checks whether a device is active. This API uses an asynchronous callback to return the query result.
W
wusongqing 已提交
1384

1385
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1386

1387
**Parameters:**
W
wusongqing 已提交
1388

1389 1390 1391 1392 1393 1394
| Name       | Type                                   | Mandatory | Description                                              |
| ---------  | ---------------------------------------| --------- | -------------------------------------------------------- |
| deviceType | [ActiveDeviceType](#activedevicetype)  | Yes       | Audio device type.                                       |
| callback   | AsyncCallback<boolean>                 | Yes       | Callback used to return the active status of the device. |

**Example:**
W
wusongqing 已提交
1395 1396

```
Z
zengyawen 已提交
1397
audioManager.isDeviceActive(audio.DeviceType.SPEAKER, (err, value) => {
W
wusongqing 已提交
1398 1399
    if (err) {
        console.error('Failed to obtain the active status of the device. ${err.message}');
1400
        return;
W
wusongqing 已提交
1401 1402
    }
    console.log('Callback invoked to indicate that the active status of the device is obtained.');
1403
});
W
wusongqing 已提交
1404 1405
```

V
Vaidegi B 已提交
1406

1407
### isDeviceActive
V
Vaidegi B 已提交
1408

W
wusongqing 已提交
1409
isDeviceActive\(deviceType: DeviceType\): Promise<boolean\>
W
wusongqing 已提交
1410

1411
Checks whether a device is active. This API uses a promise to return the query result.
W
wusongqing 已提交
1412

1413
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1414

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
**Parameters:**

| Name       | Type                                   | Mandatory | Description                               |
| ---------  | ---------------------------------------| --------- | ----------------------------------------- |
| deviceType | [ActiveDeviceType](#activedevicetype)  | Yes       | Audio device type.                        |

**Return value:**

| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<boolean>    | Promise used to return the active status of the device.  |

**Example:**
W
wusongqing 已提交
1428 1429

```
1430
audioManager.isDeviceActive(audio.DeviceType.SPEAKER).then((value) => {
W
wusongqing 已提交
1431
    console.log('Promise returned to indicate that the active status of the device is obtained.' + value);
1432
});
W
wusongqing 已提交
1433 1434 1435
```


1436
### setMicrophoneMute
V
Vaidegi B 已提交
1437

W
wusongqing 已提交
1438
setMicrophoneMute\(mute: boolean, callback: AsyncCallback<void\>\): void
V
Vaidegi B 已提交
1439

1440 1441 1442
Mutes or unmutes the microphone. This API uses an asynchronous callback to return the result.

**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1443

1444
**Parameters:**
W
wusongqing 已提交
1445

1446 1447 1448 1449
| 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.                                                            |
W
wusongqing 已提交
1450

1451
**Example:**
W
wusongqing 已提交
1452 1453 1454 1455 1456 1457 1458 1459

```
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.');
1460
});
W
wusongqing 已提交
1461 1462 1463
```


1464
### setMicrophoneMute
V
Vaidegi B 已提交
1465

W
wusongqing 已提交
1466
setMicrophoneMute\(mute: boolean\): Promise<void\>
V
Vaidegi B 已提交
1467

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
Mutes or unmutes the microphone. This API uses a promise to return the result.

**System capability:** SystemCapability.Multimedia.Audio.Device

**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.  |
W
wusongqing 已提交
1483

Z
zengyawen 已提交
1484
</table>
W
wusongqing 已提交
1485

1486
**Example:**
W
wusongqing 已提交
1487 1488

```
1489
audioManager.setMicrophoneMute(true).then(() => {
W
wusongqing 已提交
1490
    console.log('Promise returned to indicate that the microphone is muted.');
1491
});
W
wusongqing 已提交
1492 1493
```

V
Vaidegi B 已提交
1494

1495
### isMicrophoneMute
V
Vaidegi B 已提交
1496

W
wusongqing 已提交
1497
isMicrophoneMute\(callback: AsyncCallback<boolean\>\): void
W
wusongqing 已提交
1498

1499 1500 1501
Checks whether the microphone is muted. This API uses an asynchronous callback to return the query result.

**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1502

1503
**Parameters:**
W
wusongqing 已提交
1504

1505 1506 1507
| 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. |
W
wusongqing 已提交
1508

1509
**Example:**
W
wusongqing 已提交
1510 1511

```
1512
var audioManager = audio.getAudioManager();
W
wusongqing 已提交
1513 1514 1515 1516 1517 1518
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);
1519
});
W
wusongqing 已提交
1520 1521
```

V
Vaidegi B 已提交
1522

1523
### isMicrophoneMute
V
Vaidegi B 已提交
1524

W
wusongqing 已提交
1525
isMicrophoneMute\(\): Promise<boolean\>
W
wusongqing 已提交
1526

1527
Checks whether the microphone is muted. This API uses a promise to return the query result.
W
wusongqing 已提交
1528

1529
**System capability:** SystemCapability.Multimedia.Audio.Device
1530

1531
**Return value:**
W
wusongqing 已提交
1532

1533 1534 1535
| 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. |
W
wusongqing 已提交
1536

1537
**Example:**
W
wusongqing 已提交
1538 1539

```
1540
var audioManager = audio.getAudioManager();
1541
audioManager.isMicrophoneMute().then((value) => {
W
wusongqing 已提交
1542
    console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
1543
});
W
wusongqing 已提交
1544 1545
```

1546
### on('volumeChange')<sup>8+</sup>
W
wusongqing 已提交
1547

1548
on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void
Z
zengyawen 已提交
1549

1550
Subscribes to system volume change events. This API uses a callback to get volume change events.
V
Vaidegi B 已提交
1551

1552
This is a system API and cannot be called by third-party applications.
1553

1554
**System capability:** SystemCapability.Multimedia.Audio.Volume
V
Vaidegi B 已提交
1555

1556
**Parameters:**
V
Vaidegi B 已提交
1557

1558 1559 1560 1561
| Name     | Type                                   | Mandatory     | Description                                                                                                                                                   |
| :------- | :--------------------------------------| :-------------| :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| type     | string                                 | Yes           | Type of the event to subscribe to. The value 'volumeChange' means the system volume change event, which is triggered when a system volume change is detected. |
| callback | Callback<[VolumeEvent](#volumeevent8)> | Yes           | Callback used to get the system volume change event.                                                                                                          |
V
Vaidegi B 已提交
1562

1563
**Example:**
V
Vaidegi B 已提交
1564 1565 1566 1567 1568 1569

```
audioManager.on('volumeChange', (volumeEvent) => {
    console.log('VolumeType of stream: ' + volumeEvent.volumeType);
    console.log('Volume level: ' + volumeEvent.volume);
    console.log('Whether to updateUI: ' + volumeEvent.updateUi);
1570
});
V
Vaidegi B 已提交
1571 1572 1573
```


1574
### on('ringerModeChange')<sup>8+</sup>
V
Vaidegi B 已提交
1575

1576
on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void
V
Vaidegi B 已提交
1577

1578
Subscribes to ringer mode change events. This API uses a callback to get ringer mode changes.
1579

1580
This is a system API and cannot be called by third-party applications.
V
Vaidegi B 已提交
1581

1582
**System capability:** SystemCapability.Multimedia.Audio.Communication
V
Vaidegi B 已提交
1583

1584
**Parameters:**
V
Vaidegi B 已提交
1585

1586 1587 1588 1589
| Name     | Type                                       | Mandatory | Description                                                                                                                                                    |
| :------- | :----------------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type     | string                                     | Yes       | Type of the event to subscribe to. The value 'ringerModeChange' means the ringer mode change event, which is triggered when a ringer mode change is detected.  |
| callback | Callback<[AudioRingMode](#audioringmode)>  | Yes       | Callback used to get the updated ringer mode.                                                                                                                  |
V
Vaidegi B 已提交
1590

1591
**Example:**
V
Vaidegi B 已提交
1592 1593 1594 1595

```
audioManager.on('ringerModeChange', (ringerMode) => {
    console.log('Updated ringermode: ' + ringerMode);
1596
});
V
Vaidegi B 已提交
1597 1598
```

1599
### on('deviceChange')
1600

1601
on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void
1602 1603 1604

Subscribes to device change events. When a device is connected/disconnected, registered clients will receive the callback.

1605
**System capability:** SystemCapability.Multimedia.Audio.Device
1606

1607
**Parameters:**
1608

1609 1610 1611
| Name     | Type                                                | Mandatory | Description                                                                                                                                     |
| :------- | :-------------------------------------------------- | :---------| :---------------------------------------------------------------------------------------------------------------------------------------------- |
| type     | string                                              | Yes       | Type of the event to subscribe to. The value 'deviceChange' means the device change event, which is triggered when a device change is detected. |
G
Geevarghese V K 已提交
1612
| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | Yes       | Callback used to obtain the device update details.                                                                                              |
1613

1614
**Example:**
1615 1616 1617 1618 1619 1620 1621

```
audioManager.on('deviceChange', (deviceChanged) => {
    console.info("device change type : " + deviceChanged.type);
    console.info("device descriptor size : " + deviceChanged.deviceDescriptors.length);
    console.info("device change descriptor : " + deviceChanged.deviceDescriptors[0].deviceRole);
    console.info("device change descriptor : " + deviceChanged.deviceDescriptors[0].deviceType);
1622
});
1623 1624
```

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
### off('deviceChange')

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

Unsubscribes from audio device connection change events.

This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.

**System capability:** SystemCapability.Multimedia.Audio.Device

**Parameters:**

| Name     | Type                                                | Mandatory | Description                                                                                                                                         |
| :------- | :-------------------------------------------------- | :---------| :-------------------------------------------------- |
| type     | string                                              | Yes       | Type of the event to unsubscribe from.              |
G
Geevarghese V K 已提交
1640
| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | No        | Callback used to obtain the device update details.  |
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

**Example:**

```
audioManager.off('deviceChange', (deviceChanged) => {
    console.log("Should be no callback.");
});
```


### on('interrupt')

on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback\<InterruptAction>): void

Subscribes to audio interrupt events. When the application's audio is interrupted by another playback event, the application will receive the callback.

This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.

**System capability:** SystemCapability.Multimedia.Audio.Renderer

**Parameters:**

| Name     | Type                          | Mandatory | Description                                                                                                                                                                                                    |
| --------- | --------------------------------------------- | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type      | string                                        | Yes   | Type of event to subscribe to. The value 'interrupt' means the audio interrupt event, which is triggered when the audio playback of the current application is interrupted by another application.|
| interrupt | AudioInterrupt                                | Yes   | Audio interrupt event type.                                                                                                                                                                       |
| callback  | Callback<[InterruptAction](#interruptaction)> | Yes   | Audio interrupt event callback method.                                                                                                                                                            |

**Example:**

```
var interAudioInterrupt = {
    streamUsage:2,
    contentType:0,
    pauseWhenDucked:true
};
this.audioManager.on('interrupt', interAudioInterrupt, (InterruptAction) => {
    if (InterruptAction.actionType === 0) {
        console.log("An event to gain the audio focus starts.");
        console.log("Focus gain event:" + JSON.stringify(InterruptAction));
    }
    if (InterruptAction.actionType === 1) {
        console.log("An audio interruption event starts.");
        console.log("Audio interruption event:" + JSON.stringify(InterruptAction));
    }
});
```

### off('interrupt')

off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback\<InterruptAction>): void

Unsubscribes from audio interrupt events.

This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.

**System capability:** SystemCapability.Multimedia.Audio.Renderer

**Parameters:**

| Name      | Type                                          | Mandatory | Description                                                                                                                                                                                            |
| --------- | --------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| type      | string                                        | Yes       | Type of event to unsubscribe from. The value 'interrupt' means the audio interrupt event, which is triggered when the audio playback of the current application is interrupted by another application. |
| interrupt | AudioInterrupt                                | Yes       | Audio interrupt event type.                                                                                                                                                                            |
| callback  | Callback<[InterruptAction](#interruptaction)> | No        | Audio interrupt event callback method.                                                                                                                                                                 |

**Example:**

```
var interAudioInterrupt = {
    streamUsage:2,
    contentType:0,
    pauseWhenDucked:true
};
this.audioManager.off('interrupt', interAudioInterrupt, (InterruptAction) => {
    if (InterruptAction.actionType === 0) {
        console.log("An event to release the audio focus starts.");
        console.log("Focus release event:" + JSON.stringify(InterruptAction));
    }
});
```


### setAudioScene<sup>8+</sup>
1725

1726
setAudioScene\(scene: AudioScene, callback: AsyncCallback<void\>\): void
1727

1728
Sets the audio scene mode to change audio strategies. This API uses an asynchronous callback to return the result.
1729

1730
This is a system API and cannot be called by third-party applications.
1731

1732 1733 1734
**System capability:** SystemCapability.Multimedia.Audio.Communication

**Parameters:**
1735 1736 1737 1738 1739 1740

| Name     | Type                                  | Mandatory | Description                         |
| :------- | :------------------------------------ | :-------- | :---------------------------------- |
| scene    | <a href="#audioscene">AudioScene</a>  | Yes       | Audio scene mode.                   |
| callback | AsyncCallback<void\>                  | Yes       | Callback used to return the result. |

1741
**Example:**
1742 1743 1744 1745 1746 1747 1748 1749

```
audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL, (err) => {
   if (err) {
       console.error('Failed to set the audio scene mode.​ ${err.message}');
       return;
    }
    console.log('Callback invoked to indicate a successful setting of the audio scene mode.');
1750
});
1751 1752 1753
```


1754
### setAudioScene<sup>8+</sup>
1755

1756
setAudioScene\(scene: AudioScene\): Promise<void\>
1757

1758
Sets the audio scene mode to change audio strategies. This API uses a promise to return the result.
1759

1760
This is a system API and cannot be called by third-party applications.
1761

1762 1763 1764
**System capability:** SystemCapability.Multimedia.Audio.Communication

**Parameters:**
1765 1766 1767 1768 1769 1770

| Name     | Type                                    | Mandatory | Description       |
| :------- | :-------------------------------------- | :-------- | :---------------- |
| scene    | <a href="#audioscene">AudioScene</a>    | Yes       | Audio scene mode. |


1771
**Return value:**
1772 1773 1774 1775 1776 1777

| Type           | Description                         |
| :------------- | :---------------------------------- |
| Promise<void\> | Promise used to return the result.  |


1778
**Example:**
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788

```
audioManager.setAudioScene(audio.AudioSceneMode.AUDIO_SCENE_PHONE_CALL).then(() => {
    console.log('Promise returned to indicate a successful setting of the audio scene mode.');
}).catch ((err) => {
    console.log('Failed to set the audio scene mode');
});
```


1789
### getAudioScene<sup>8+</sup>
1790

1791
getAudioScene\(callback: AsyncCallback<AudioScene\>\): void
1792

1793
Obtains the audio scene mode. This API uses an asynchronous callback to return the query result.
1794

1795
**System capability:** SystemCapability.Multimedia.Audio.Communication
1796

1797
**Parameters:**
1798 1799 1800 1801 1802

| Name     | Type                                                | Mandatory | Description                                   |
| :------- | :-------------------------------------------------- | :-------- | :-------------------------------------------- |
| callback | AsyncCallback<<a href="#audioscene">AudioScene</a>> | Yes       | Callback used to return the audio scene mode. |

1803
**Example:**
1804 1805 1806 1807 1808 1809 1810 1811

```
audioManager.getAudioScene((err, value) => {
   if (err) {
       console.error('Failed to obtain the audio scene mode.​ ${err.message}');
       return;
   }
   console.log('Callback invoked to indicate that the audio scene mode is obtained.' + value);
1812
});
1813 1814 1815
```


1816
### getAudioScene<sup>8+</sup>
1817

1818
getAudioScene\(\): Promise<AudioScene\>
1819

1820
Obtains the audio scene mode. This API uses a promise to return the query result.
1821

1822
**System capability:** SystemCapability.Multimedia.Audio.Communication
1823

1824
**Return value:**
1825 1826 1827 1828 1829 1830

| Type                                          | Description                                  |
| :-------------------------------------------- | :------------------------------------------- |
| Promise<<a href="#audioscene">AudioScene</a>> | Promise used to return the audio scene mode. |


1831
**Example:**
1832 1833 1834 1835 1836 1837

```
audioManager.getAudioScene().then((value) => {
    console.log('Promise returned to indicate that the audio scene mode is obtained.' + value);
}).catch ((err) => {
    console.log('Failed to obtain the audio scene mode');
1838
});
1839 1840
```

1841
## AudioDeviceDescriptor
V
Vaidegi B 已提交
1842

1843
Describes an audio device.
1844

1845
**System capability:** SystemCapability.Multimedia.Audio.Device
W
wusongqing 已提交
1846

1847 1848 1849 1850
| Name       | Type                      | Readable | Writable | Description        |
| ---------- | ------------------------- | -------- | -------- | ------------------ |
| deviceRole | [DeviceRole](#devicerole) | Yes      | No       | Audio device role. |
| deviceType | [DeviceType](#devicetype) | Yes      | No       | Audio device type. |
V
Vaidegi B 已提交
1851

1852
## AudioDeviceDescriptors
V
Vaidegi B 已提交
1853

1854
Array of [AudioDeviceDescriptor](#audiodevicedescriptor), which is read-only.
V
Vaidegi B 已提交
1855

1856
**System capability:** SystemCapability.Multimedia.Audio.Device
V
Vaidegi B 已提交
1857

1858
**Example:**
V
Vaidegi B 已提交
1859

1860 1861
```
import audio from '@ohos.multimedia.audio';
V
Vaidegi B 已提交
1862

1863 1864 1865
function displayDeviceProp(value) {
    deviceRoleValue = value.deviceRole;
    deviceTypeValue = value.deviceType;
V
Vaidegi B 已提交
1866 1867 1868

}

1869 1870 1871 1872 1873 1874
var deviceRoleValue = null;
var deviceTypeValue = null;
const promise = audio.getAudioManager().getDevices(1);
promise.then(function (value) {
    console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG');
    value.forEach(displayDeviceProp);
V
Vaidegi B 已提交
1875
    if (deviceTypeValue != null && deviceRoleValue != null){
1876
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  PASS');
V
Vaidegi B 已提交
1877 1878
    }
    else{
1879 1880
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  FAIL');
    }
1881
});
V
Vaidegi B 已提交
1882
```
1883 1884 1885
## AudioRenderer<sup>8+</sup>

Provides related APIs for audio rendering. Before calling the AudioRenderer API, you need to create an instance through [createAudioRenderer](#audiocreateaudiorenderer8).
V
Vaidegi B 已提交
1886

1887
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1888

1889
### state<sup>8+</sup>
V
Vaidegi B 已提交
1890

1891
readonly state: AudioState
V
Vaidegi B 已提交
1892 1893 1894

Defines the current render state.

1895
**System capability:** SystemCapability.Multimedia.Audio.Renderer
1896

1897 1898 1899
| Name  | Type                        | Readable | Writable | Description         |
| :---- | :-------------------------- | :------- | :------- | :------------------ |
| state |  [AudioState](#audiostate8) | Yes      | No       | Audio render state. |
V
Vaidegi B 已提交
1900

1901
**Example:**
V
Vaidegi B 已提交
1902 1903 1904 1905 1906

```
    var state = audioRenderer.state;
```

1907
### getRendererInfo<sup>8+</sup>
V
Vaidegi B 已提交
1908

1909
getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void
V
Vaidegi B 已提交
1910

1911
Obtains the renderer information provided while creating a renderer instance. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
1912

1913
**System capability:** SystemCapability.Multimedia.Audio.Renderer
1914

1915
**Parameters:**
V
Vaidegi B 已提交
1916

1917 1918 1919
| Name     | Type                                                     | Mandatory | Description                                       |
| :------- | :------------------------------------------------------- | :-------- | :------------------------------------------------ |
| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)>  | Yes       | Callback used to return the renderer information. |
V
Vaidegi B 已提交
1920

1921
**Example:**
V
Vaidegi B 已提交
1922 1923

```
1924
audioRenderer.getRendererInfo((err, rendererInfo) => {
1925 1926 1927 1928
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
1929
});
V
Vaidegi B 已提交
1930 1931 1932
```


1933
### getRendererInfo<sup>8+</sup>
V
Vaidegi B 已提交
1934

1935
getRendererInfo(): Promise<AudioRendererInfo\>
1936

1937
Obtains the renderer information provided while creating a renderer instance. This API uses a promise to return the result.
V
Vaidegi B 已提交
1938

1939
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1940

1941
**Return value:**
V
Vaidegi B 已提交
1942

1943 1944 1945
| Type                                                | Description                                      |
| :-------------------------------------------------- | :----------------------------------------------- |
| Promise<[AudioRendererInfo](#audiorendererinfo8)>   | Promise used to return the renderer information. |
V
Vaidegi B 已提交
1946

1947
**Example:**
V
Vaidegi B 已提交
1948 1949

```
1950 1951 1952 1953 1954 1955 1956 1957 1958
audioRenderer.getRendererInfo().then((rendererInfo) => {
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
}).catch((err) => {
    console.log('AudioFrameworkRenderLog: RendererInfo :ERROR: '+err.message);
    resultFlag = false;
});
V
Vaidegi B 已提交
1959

1960
```
V
Vaidegi B 已提交
1961

1962
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
1963

1964
getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void
1965

1966
Obtains the renderer stream information. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
1967

1968
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1969

1970
**Parameters:**
V
Vaidegi B 已提交
1971

1972 1973 1974
| Name     | Type                                                    | Mandatory | Description                                     |
| :------- | :------------------------------------------------------ | :-------- | :---------------------------------------------- |
| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\>    | Yes       | Callback used to return the stream information. |
V
Vaidegi B 已提交
1975

1976
**Example:**
V
Vaidegi B 已提交
1977 1978

```
1979
audioRenderer.getStreamInfo((err, streamInfo) => {
1980 1981
    console.log('Renderer GetStreamInfo:');
    console.log('Renderer sampling rate:' + streamInfo.samplingRate);
1982 1983 1984
    console.log('Renderer channel:' + streamInfo.channels);
    console.log('Renderer format:' + streamInfo.sampleFormat);
    console.log('Renderer encoding type:' + streamInfo.encodingType);
1985
});
V
Vaidegi B 已提交
1986 1987
```

1988
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
1989

1990
getStreamInfo(): Promise<AudioStreamInfo\>
V
Vaidegi B 已提交
1991

1992
Obtains the renderer stream information. This API uses a promise to return the result.
V
Vaidegi B 已提交
1993

1994
**System capability:** SystemCapability.Multimedia.Audio.Renderer
1995

1996
**Return value:**
V
Vaidegi B 已提交
1997

1998 1999 2000
| Type                                               | Description                                    |
| :------------------------------------------------- | :--------------------------------------------- |
| Promise<[AudioStreamInfo](#audiostreaminfo8)\>     | Promise used to return the stream information. |
V
Vaidegi B 已提交
2001

2002
**Example:**
V
Vaidegi B 已提交
2003 2004

```
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
audioRenderer.getStreamInfo().then((streamInfo) => {
    console.log('Renderer GetStreamInfo:');
    console.log('Renderer sampling rate:' + streamInfo.samplingRate);
    console.log('Renderer channel:' + streamInfo.channels);
    console.log('Renderer format:' + streamInfo.sampleFormat);
    console.log('Renderer encoding type:' + streamInfo.encodingType);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});

V
Vaidegi B 已提交
2015 2016
```

2017
### start<sup>8+</sup>
V
Vaidegi B 已提交
2018

2019
start(callback: AsyncCallback<void\>): void
V
Vaidegi B 已提交
2020

2021
Starts the renderer. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2022

2023
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2024

2025
**Parameters:**
V
Vaidegi B 已提交
2026

2027 2028 2029 2030
| Name     | Type                    | Mandatory | Description                             |
| :------- | :---------------------- | :-------- | :-------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.     |
|          |                         |           |                                         |
V
Vaidegi B 已提交
2031

2032
**Example:**
V
Vaidegi B 已提交
2033 2034

```
2035
audioRenderer.start((err) => {
2036 2037
    if (err) {
        console.error('Renderer start failed.');
V
Vaidegi B 已提交
2038
    } else {
2039
        console.info('Renderer start success.');
V
Vaidegi B 已提交
2040
    }
2041
});
V
Vaidegi B 已提交
2042 2043
```

2044
### start<sup>8+</sup>
V
Vaidegi B 已提交
2045

2046
start(): Promise<void\>
V
Vaidegi B 已提交
2047

2048
Starts the renderer. This API uses a promise to return the result.
V
Vaidegi B 已提交
2049

2050
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2051

2052
**Return value:**
V
Vaidegi B 已提交
2053

2054 2055 2056
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2057

2058
**Example:**
V
Vaidegi B 已提交
2059 2060

```
2061 2062 2063 2064 2065
audioRenderer.start().then(() => {
    console.log('Renderer started');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2066 2067 2068
```


2069
### pause<sup>8+</sup>
V
Vaidegi B 已提交
2070

2071
pause(callback: AsyncCallback\<void>): void
V
Vaidegi B 已提交
2072

2073
Pauses rendering. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2074

2075
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2076

2077
**Parameters:**
V
Vaidegi B 已提交
2078

2079 2080 2081 2082
| Name     | Type                    | Mandatory | Description                           |
| :------- | :---------------------- | :-------- | :------------------------------------ |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.   |
|          |                         |           |                                       |
V
Vaidegi B 已提交
2083

2084
**Example:**
V
Vaidegi B 已提交
2085 2086

```
2087
audioRenderer.pause((err) => {
2088 2089
    if (err) {
        console.error('Renderer pause failed');
V
Vaidegi B 已提交
2090
    } else {
2091
        console.log('Renderer paused.');
V
Vaidegi B 已提交
2092
    }
2093
});
V
Vaidegi B 已提交
2094 2095
```

2096
### pause<sup>8+</sup>
V
Vaidegi B 已提交
2097

2098
pause(): Promise\<void>
V
Vaidegi B 已提交
2099

2100
Pauses rendering. This API uses a promise to return the result.
2101

2102
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2103

2104
**Return value:**
V
Vaidegi B 已提交
2105

2106 2107 2108
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2109

2110
**Example:**
V
Vaidegi B 已提交
2111 2112

```
2113 2114 2115 2116 2117
audioRenderer.pause().then(() => {
    console.log('Renderer paused');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2118 2119
```

2120
### drain<sup>8+</sup>
V
Vaidegi B 已提交
2121

2122
drain(callback: AsyncCallback\<void>): void
V
Vaidegi B 已提交
2123

2124
Drains the playback buffer. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2125

2126
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2127

2128
**Parameters:**
V
Vaidegi B 已提交
2129

2130 2131 2132 2133
| Name     | Type                    | Mandatory | Description                             |
| :------- | :---------------------- | :-------- | :---------------------------------------|
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.     |
|          |                         |           |                                         |
V
Vaidegi B 已提交
2134

2135
**Example:**
V
Vaidegi B 已提交
2136 2137

```
2138
audioRenderer.drain((err) => {
2139 2140
    if (err) {
        console.error('Renderer drain failed');
V
Vaidegi B 已提交
2141
    } else {
2142
        console.log('Renderer drained.');
V
Vaidegi B 已提交
2143
    }
2144
});
V
Vaidegi B 已提交
2145 2146
```

2147
### drain<sup>8+</sup>
V
Vaidegi B 已提交
2148

2149
drain(): Promise\<void>
V
Vaidegi B 已提交
2150

2151
Drains the playback buffer. This API uses a promise to return the result.
2152

2153
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2154

2155
**Return value:**
V
Vaidegi B 已提交
2156

2157 2158 2159
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2160

2161
**Example:**
V
Vaidegi B 已提交
2162 2163

```
2164 2165 2166 2167 2168
audioRenderer.drain().then(() => {
    console.log('Renderer drained successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2169 2170 2171
```


2172
### stop<sup>8+</sup>
V
Vaidegi B 已提交
2173

2174
stop(callback: AsyncCallback\<void>): void
V
Vaidegi B 已提交
2175

2176
Stops rendering. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2177

2178
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2179

2180
**Parameters:**
V
Vaidegi B 已提交
2181

2182 2183 2184
| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |
V
Vaidegi B 已提交
2185

2186
**Example:**
V
Vaidegi B 已提交
2187 2188

```
2189
audioRenderer.stop((err) => {
2190 2191
    if (err) {
        console.error('Renderer stop failed');
V
Vaidegi B 已提交
2192
    } else {
2193
        console.log('Renderer stopped.');
V
Vaidegi B 已提交
2194
    }
2195
});
V
Vaidegi B 已提交
2196 2197
```

2198
### stop<sup>8+</sup>
V
Vaidegi B 已提交
2199

2200
stop(): Promise\<void>
V
Vaidegi B 已提交
2201

2202
Stops rendering. This API uses a promise to return the result.
V
Vaidegi B 已提交
2203

2204
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2205

2206
**Return value:**
V
Vaidegi B 已提交
2207

2208 2209 2210
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2211

2212
**Example:**
V
Vaidegi B 已提交
2213 2214

```
2215 2216 2217 2218 2219
audioRenderer.stop().then(() => {
    console.log('Renderer stopped successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2220 2221 2222
```


2223
### release<sup>8+</sup>
V
Vaidegi B 已提交
2224

2225
release(callback: AsyncCallback\<void>): void
V
Vaidegi B 已提交
2226

2227
Releases the renderer. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2228

2229
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2230

2231
**Parameters:**
V
Vaidegi B 已提交
2232

2233 2234 2235
| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |
V
Vaidegi B 已提交
2236

2237
**Example:**
V
Vaidegi B 已提交
2238 2239

```
2240
audioRenderer.release((err) => {
2241 2242
    if (err) {
        console.error('Renderer release failed');
V
Vaidegi B 已提交
2243
    } else {
2244
        console.log('Renderer released.');
V
Vaidegi B 已提交
2245
    }
2246
});
V
Vaidegi B 已提交
2247 2248
```

2249
### release<sup>8+</sup>
V
Vaidegi B 已提交
2250

2251
release(): Promise\<void>
V
Vaidegi B 已提交
2252

2253
Releases the renderer. This API uses a promise to return the result.
V
Vaidegi B 已提交
2254

2255
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2256

2257
**Return value:**
V
Vaidegi B 已提交
2258

2259 2260 2261
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2262

2263
**Example:**
V
Vaidegi B 已提交
2264 2265

```
2266 2267 2268 2269 2270
audioRenderer.release().then(() => {
    console.log('Renderer released successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2271 2272
```

2273
### write<sup>8+</sup>
V
Vaidegi B 已提交
2274

2275
write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void
V
Vaidegi B 已提交
2276

2277
Writes the buffer. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2278

2279
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2280

2281
**Parameters:**
V
Vaidegi B 已提交
2282 2283 2284 2285 2286 2287

| Name     | Type                    | Mandatory | Description                                                                                          |
| :------- | :---------------------- | :-------- | :--------------------------------------------------------------------------------------------------- |
| buffer   | ArrayBuffer             | Yes       | Buffer to be written.                                                                                |
| callback | AsyncCallback<boolean\> | Yes       | Returns the number of bytes written if the operation is successful; returns an error code otherwise. |

2288
**Example:**
V
Vaidegi B 已提交
2289 2290

```
2291 2292 2293
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

V
Vaidegi B 已提交
2294 2295 2296
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
2297
audioRenderer.write(buf, (err, writtenbytes) => {
V
Vaidegi B 已提交
2298
    if (writtenbytes < 0) {
2299
        console.error('write failed.');
V
Vaidegi B 已提交
2300 2301 2302
    } else {
       console.log('Actual written bytes: ' + writtenbytes);
    }
2303
});
V
Vaidegi B 已提交
2304 2305 2306
```


2307
### write<sup>8+</sup>
V
Vaidegi B 已提交
2308

2309
write(buffer: ArrayBuffer): Promise\<number>
V
Vaidegi B 已提交
2310

2311
Writes the buffer. This API uses a promise to return the result.
2312

2313
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2314

2315
**Return value:**
V
Vaidegi B 已提交
2316 2317 2318 2319 2320

| Type             | Description                                                                                          |
| :--------------- | :--------------------------------------------------------------------------------------------------- |
| Promise<number\> | Returns the number of bytes written if the operation is successful; returns an error code otherwise. |

2321
**Example:**
V
Vaidegi B 已提交
2322 2323

```
2324 2325 2326 2327
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

var filePath = 'data/StarWars10s-2C-48000-4SW.wav';
V
Vaidegi B 已提交
2328 2329 2330
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
2331 2332 2333 2334 2335 2336 2337 2338 2339
audioRenderer.write(buf).then((writtenbytes) => {
    if (writtenbytes < 0) {
        console.error('write failed.');
    } else {
        console.log('Actual written bytes: ' + writtenbytes);
    }
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2340 2341
```

2342
### getAudioTime<sup>8+</sup>
V
Vaidegi B 已提交
2343

2344
getAudioTime(callback: AsyncCallback\<number>): void
V
Vaidegi B 已提交
2345

2346
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2347

2348
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2349

2350
**Parameters:**
V
Vaidegi B 已提交
2351 2352 2353 2354 2355

| Name     | Type                   | Mandatory | Description                            |
| :------- | :--------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<number\> | Yes       | Callback used to return the timestamp. |

2356
**Example:**
V
Vaidegi B 已提交
2357 2358

```
2359
audioRenderer.getAudioTime((err, timestamp) => {
V
Vaidegi B 已提交
2360
    console.log('Current timestamp: ' + timestamp);
2361
});
V
Vaidegi B 已提交
2362 2363 2364
```


2365
### getAudioTime<sup>8+</sup>
V
Vaidegi B 已提交
2366

2367
getAudioTime(): Promise\<number>
V
Vaidegi B 已提交
2368

2369
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses a promise to return the result.
V
Vaidegi B 已提交
2370

2371
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2372

2373
**Return value:**
V
Vaidegi B 已提交
2374 2375 2376 2377 2378

| Type             | Description                           |
| :--------------- | :------------------------------------ |
| Promise<number\> | Promise used to return the timestamp. |

2379
**Example:**
V
Vaidegi B 已提交
2380 2381

```
2382 2383 2384 2385 2386
audioRenderer.getAudioTime().then((timestamp) => {
    console.log('Current timestamp: ' + timestamp);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2387 2388 2389
```


2390
### getBufferSize<sup>8+</sup>
V
Vaidegi B 已提交
2391

2392
getBufferSize(callback: AsyncCallback\<number>): void
V
Vaidegi B 已提交
2393

2394
Obtains a reasonable minimum buffer size in bytes for rendering. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2395

2396
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2397

2398
**Parameters:**
V
Vaidegi B 已提交
2399 2400 2401 2402 2403

| Name     | Type                   | Mandatory | Description                              |
| :------- | :--------------------- | :-------- | :--------------------------------------- |
| callback | AsyncCallback<number\> | Yes       | Callback used to return the buffer size. |

2404
**Example:**
V
Vaidegi B 已提交
2405 2406

```
2407
audioRenderer.getBufferSize((err, bufferSize) => {
V
Vaidegi B 已提交
2408 2409 2410
    if (err) {
        console.error('getBufferSize error');
    }
2411
});
V
Vaidegi B 已提交
2412 2413 2414 2415 2416
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
```


2417
### getBufferSize<sup>8+</sup>
V
Vaidegi B 已提交
2418

2419
getBufferSize(): Promise\<number>
V
Vaidegi B 已提交
2420

2421
Obtains a reasonable minimum buffer size in bytes for rendering. This API uses a promise to return the result.
2422

2423
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2424

2425
**Return value:**
V
Vaidegi B 已提交
2426 2427 2428 2429 2430

| Type             | Description                             |
| :--------------- | :-------------------------------------- |
| Promise<number\> | Promise used to return the buffer size. |

2431
**Example:**
V
Vaidegi B 已提交
2432 2433

```
2434 2435 2436 2437 2438 2439
audioRenderer.getBufferSize().then((bufferSize) => {
    let buf = new ArrayBuffer(bufferSize);
    ss.readSync(buf);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2440 2441
```

2442
### setRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2443

2444
setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void
V
Vaidegi B 已提交
2445

2446
Sets the render rate. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2447

2448
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2449

2450
**Parameters:**
2451

2452 2453 2454 2455
| Name     | Type                                        | Mandatory | Description                         |
| :------- | :------------------------------------------ | :-------- | :---------------------------------- |
| rate     | [AudioRendererRate](#audiorendererrate8)    | Yes       | Audio render rate.                  |
| callback | AsyncCallback<void\>                        | Yes       | Callback used to return the result. |
V
Vaidegi B 已提交
2456

2457
**Example:**
V
Vaidegi B 已提交
2458 2459

```
2460
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err) => {
V
Vaidegi B 已提交
2461
    if (err) {
2462
        console.error('Failed to set params');
V
Vaidegi B 已提交
2463 2464 2465
    } else {
        console.log('Callback invoked to indicate a successful render rate setting.');
    }
2466
});
V
Vaidegi B 已提交
2467 2468 2469
```


2470
### setRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2471

2472
setRenderRate(rate: AudioRendererRate): Promise\<void>
V
Vaidegi B 已提交
2473

2474
Sets the render rate. This API uses a promise to return the result.
V
Vaidegi B 已提交
2475

2476
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2477

2478
**Parameters:**
V
Vaidegi B 已提交
2479

2480 2481 2482
| Name | Type                                     | Mandatory | Description        |
| :--- | :--------------------------------------- | :-------- | :----------------- |
| rate | [AudioRendererRate](#audiorendererrate8) | Yes       | Audio render rate. |
V
Vaidegi B 已提交
2483

2484
**Return value:**
V
Vaidegi B 已提交
2485 2486 2487 2488 2489

| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |

2490
**Example:**
V
Vaidegi B 已提交
2491 2492

```
2493 2494 2495 2496 2497
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => {
    console.log('setRenderRate SUCCESS');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2498 2499
```

2500
### getRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2501

2502
getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void
V
Vaidegi B 已提交
2503

2504
Obtains the current render rate. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2505

2506
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2507

2508
**Parameters:**
2509

2510 2511 2512
| Name     | Type                                                     | Mandatory | Description                                    |
| :------- | :------------------------------------------------------- | :-------- | :--------------------------------------------- |
| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)\> | Yes       | Callback used to return the audio render rate. |
V
Vaidegi B 已提交
2513

2514
**Example:**
V
Vaidegi B 已提交
2515 2516

```
2517
audioRenderer.getRenderRate((err, renderrate) => {
V
Vaidegi B 已提交
2518
    console.log('getRenderRate: ' + renderrate);
2519
});
V
Vaidegi B 已提交
2520 2521 2522
```


2523
### getRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2524

2525
getRenderRate(): Promise\<AudioRendererRate>
2526

2527
Obtains the current render rate. This API uses a promise to return the result.
V
Vaidegi B 已提交
2528

2529
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2530

2531
**Return value:**
V
Vaidegi B 已提交
2532

2533 2534 2535
| Type                                                | Description                                   |
| :-------------------------------------------------- | :-------------------------------------------- |
| Promise<<[AudioRendererRate](#audiorendererrate8)\> | Promise used to return the audio render rate. |
V
Vaidegi B 已提交
2536

2537
**Example:**
V
Vaidegi B 已提交
2538 2539

```
2540 2541 2542 2543 2544
audioRenderer.getRenderRate().then((renderRate) => {
    console.log('getRenderRate: ' + renderRate);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2545 2546 2547
```


2548
### on('interrupt')<sup>9+</sup>
V
Vaidegi B 已提交
2549

2550
on(type: 'interrupt', callback: Callback\<InterruptEvent>): void
V
Vaidegi B 已提交
2551

2552
Subscribes to audio interrupt events. This API uses a callback to get interrupt events. The interrupt event is triggered when audio rendering is interrupted.
2553

2554
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2555

2556
**Parameters:**
V
Vaidegi B 已提交
2557

2558 2559 2560 2561
| Name     | Type                                          | Mandatory | Description                                     |
| :------- | :-------------------------------------------- | :-------- | :---------------------------------------------- |
| type     | string                                        | Yes       | Type of the playback event to subscribe to.     |
| callback | Callback<[InterruptEvent](#interruptevent9)\> | Yes       | Callback used to listen for interrupt callback. |
V
Vaidegi B 已提交
2562

2563
**Example:**
V
Vaidegi B 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589

```
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;
        }
    }
2590
});
V
Vaidegi B 已提交
2591 2592
```

2593
### on('markReach')<sup>8+</sup>
2594

2595
on(type: 'markReach', frame: number, callback: (position: number) => {}): void
2596 2597 2598

Subscribes to mark reached events. When the number of frames rendered reaches the value of the frame parameter, the callback is invoked.

2599
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2600

2601
**Parameters:**
2602 2603 2604 2605 2606

| Name     | Type                      | Mandatory | Description                                                              |
| :------- | :------------------------ | :-------- | :----------------------------------------------------------------------- |
| type     | string                    | Yes       | Type of the renderer event to subscribe to.                              |
| frame    | number                    | Yes       | Number of frames to trigger the event. The value must be greater than 0. |
2607
| callback | (position: number) => {}  | Yes       | Callback invoked when the event is triggered.                            |
2608

2609
**Example:**
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619

```
audioRenderer.on('markReach', 1000, (position) => {
    if (position == "1000") {
        console.log('ON Triggered successfully');
    }
});
```


2620
### off('markReach') <sup>8+</sup>
2621

2622
off(type: 'markReach'): void
2623 2624 2625

Unsubscribes from mark reached events.

2626
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2627

2628
**Parameters:**
2629 2630 2631 2632 2633

| Name     | Type                      | Mandatory | Description                                      |
| :------- | :------------------------ | :-------- | :----------------------------------------------- |
| type     | string                    | Yes       | Type of the renderer event to unsubscribe from.  |

2634
**Example:**
2635 2636 2637 2638 2639

```
audioRenderer.off('markReach');
```

2640
### on('periodReach') <sup>8+</sup>
2641

2642
on(type: "periodReach", frame: number, callback: (position: number) => {}): void
2643 2644 2645

Subscribes to period reached events. When the period of frame rendering reaches the value of frame parameter, the callback is invoked.

2646
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2647

2648
**Parameters:**
2649 2650 2651 2652 2653

| Name     | Type                      | Mandatory | Description                                                                        |
| :------- | :------------------------ | :-------- | :--------------------------------------------------------------------------------- |
| type     | string                    | Yes       | Type of the renderer event to subscribe to.                                        |
| frame    | number                    | Yes       | Period during which frame rendering is listened. The value must be greater than 0. |
2654
| callback |  (position: number) => {} | Yes       | Callback invoked when the event is triggered.                                      |
2655

2656
**Example:**
2657 2658 2659 2660 2661 2662 2663 2664 2665

```
audioRenderer.on('periodReach', 1000, (position) => {
    if (position == "1000") {
        console.log('ON Triggered successfully');
    }
});
```

2666
### off('periodReach') <sup>8+</sup>
2667

2668
off(type: 'periodReach'): void
2669 2670 2671

Unsubscribes from period reached events.

2672
**System capability:** SystemCapability.Multimedia.Audio.Renderer
2673

2674
**Parameters:**
2675 2676 2677 2678 2679

| Name     | Type                      | Mandatory | Description                                      |
| :------- | :------------------------ | :-------- | :----------------------------------------------- |
| type     | string                    | Yes       | Type of the renderer event to unsubscribe from.  |

2680
**Example:**
2681 2682 2683 2684

```
audioRenderer.off('periodReach')
```
V
Vaidegi B 已提交
2685

2686
### on('stateChange') <sup>8+</sup>
V
Vaidegi B 已提交
2687

2688
on(type: 'stateChange', callback: Callback<AudioState\>): void
V
Vaidegi B 已提交
2689

2690
Subscribes to state change events.
V
Vaidegi B 已提交
2691

2692
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2693

2694 2695 2696 2697 2698
**Parameters:**

| Name     | Type                       | Mandatory | Description                                                                              |
| :------- | :------------------------- | :-------- | :--------------------------------------------------------------------------------------- |
| type     | string                     | Yes       | Type of the event to subscribe to. The value 'stateChange' means the state change event. |
G
Geevarghese V K 已提交
2699
| callback | [AudioState](#audiostate8) | Yes       | Callback used to return the state change.                                                |
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712

**Example:**

```
audioRenderer.on('stateChange', (state) => {
    if (state == 1) {
        console.log("audio renderer state is: STATE_PREPARED");
    }
    if (state == 2) {
        console.log("audio renderer state is: STATE_RUNNING");
    }
});
```
V
Vaidegi B 已提交
2713

2714
## AudioCapturer<sup>8+</sup>
V
Vaidegi B 已提交
2715

2716
Provides related APIs for audio capture. Before calling the API of AudioCapturer, you need to create an instance through [createAudioCapturer](#audiocreateaudiocapturer8).
V
Vaidegi B 已提交
2717

2718
### state<sup>8+</sup>
V
Vaidegi B 已提交
2719

2720
readonly state: AudioState
V
Vaidegi B 已提交
2721

2722
Defines the current capture state.
V
Vaidegi B 已提交
2723

2724
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2725

2726 2727 2728
| Name  | Type                       | Readable | Writable | Description          |
| :---- | :------------------------- | :------- | :------- | :------------------- |
| state | [AudioState](#audiostate8) | Yes      | No       | Audio capture state. |
V
Vaidegi B 已提交
2729

2730
**Example:**
V
Vaidegi B 已提交
2731 2732

```
2733
var state = audioCapturer.state;
V
Vaidegi B 已提交
2734 2735 2736
```


2737
### getCapturerInfo<sup>8+</sup>
V
Vaidegi B 已提交
2738

2739
getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void
V
Vaidegi B 已提交
2740

2741
Obtains the capturer information provided while creating a capturer instance. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2742

2743
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2744

2745
**Parameters:**
V
Vaidegi B 已提交
2746

2747 2748
| Name     | Type                                                     | Mandatory | Description                                       |
| :------- | :------------------------------------------------------- | :-------- | :------------------------------------------------ |
G
Geevarghese V K 已提交
2749
| callback | AsyncCallback<[AudioCapturerInfo](#audiocapturerinfo)\>  | Yes       | Callback used to return the capturer information. |
V
Vaidegi B 已提交
2750

2751
**Example:**
V
Vaidegi B 已提交
2752 2753

```
2754
audioCapturer.getCapturerInfo((err, capturerInfo) => {
V
Vaidegi B 已提交
2755
    if (err) {
2756
        console.error('Failed to get capture info');
V
Vaidegi B 已提交
2757
    } else {
2758 2759 2760
        console.log('Capturer getCapturerInfo:');
        console.log('Capturer source:' + capturerInfo.source);
        console.log('Capturer flags:' + capturerInfo.capturerFlags);
V
Vaidegi B 已提交
2761
    }
2762
});
V
Vaidegi B 已提交
2763 2764 2765
```


2766
### getCapturerInfo<sup>8+</sup>
V
Vaidegi B 已提交
2767

2768
getCapturerInfo(): Promise<AudioCapturerInfo\>
V
Vaidegi B 已提交
2769

2770
Obtains the capturer information provided while creating a capturer instance. This API uses a promise to return the result.
V
Vaidegi B 已提交
2771

2772
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2773

2774
**Return value:**
V
Vaidegi B 已提交
2775

2776 2777 2778
| Type                                                | Description                                      |
| :-------------------------------------------------- | :----------------------------------------------- |
| Promise<[AudioCapturerInfo](#audiocapturerinfo)\>   | Promise used to return the capturer information. |
V
Vaidegi B 已提交
2779

2780
**Example:**
V
Vaidegi B 已提交
2781 2782

```
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
audioCapturer.getCapturerInfo().then((audioParamsGet) => {
    if (audioParamsGet != undefined) {
        console.info('AudioFrameworkRecLog: Capturer CapturerInfo:');
        console.info('AudioFrameworkRecLog: Capturer SourceType:' + audioParamsGet.source);
        console.info('AudioFrameworkRecLog: Capturer capturerFlags:' + audioParamsGet.capturerFlags);
    }else {
        console.info('AudioFrameworkRecLog: audioParamsGet is : '+audioParamsGet);
        console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect: ');
    }
}).catch((err) => {
    console.log('AudioFrameworkRecLog: CapturerInfo :ERROR: '+err.message);
2794
});
V
Vaidegi B 已提交
2795

2796
```
V
Vaidegi B 已提交
2797

2798
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
2799

2800
getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void
V
Vaidegi B 已提交
2801

2802
Obtains the capturer stream information. This API uses an asynchronous callback to return the result.
V
Vaidegi B 已提交
2803

2804
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2805

2806
**Parameters:**
V
Vaidegi B 已提交
2807

2808 2809
| Name     | Type                                                         | Mandatory | Description                                     |
| :------- | :----------------------------------------------------------- | :-------- | :---------------------------------------------- |
G
Geevarghese V K 已提交
2810
| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\>    | Yes       | Callback used to return the stream information. |
V
Vaidegi B 已提交
2811

2812
**Example:**
V
Vaidegi B 已提交
2813 2814

```
2815
audioCapturer.getStreamInfo((err, streamInfo) => {
V
Vaidegi B 已提交
2816
    if (err) {
2817
        console.error('Failed to get stream info');
V
Vaidegi B 已提交
2818
    } else {
2819 2820 2821 2822 2823
        console.log('Capturer GetStreamInfo:');
        console.log('Capturer sampling rate:' + streamInfo.samplingRate);
        console.log('Capturer channel:' + streamInfo.channels);
        console.log('Capturer format:' + streamInfo.sampleFormat);
        console.log('Capturer encoding type:' + streamInfo.encodingType);
V
Vaidegi B 已提交
2824
    }
2825
});
V
Vaidegi B 已提交
2826 2827
```

2828
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
2829

2830
getStreamInfo(): Promise<AudioStreamInfo\>
V
Vaidegi B 已提交
2831

2832
Obtains the capturer stream information. This API uses a promise to return the result.
V
Vaidegi B 已提交
2833

2834
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2835

2836
**Return value:**
V
Vaidegi B 已提交
2837

2838 2839
| Type                                                  | Description                                      |
| :---------------------------------------------------- | :----------------------------------------------- |
G
Geevarghese V K 已提交
2840
| Promise<[AudioStreamInfo](#audiostreaminfo8)\>   | Promise used to return the stream information.   |
V
Vaidegi B 已提交
2841

2842
**Example:**
V
Vaidegi B 已提交
2843 2844

```
2845 2846 2847 2848 2849 2850 2851 2852
audioCapturer.getStreamInfo().then((audioParamsGet) => {
    console.info('getStreamInfo:');
    console.info('sampleFormat:' + audioParamsGet.sampleFormat);
    console.info('samplingRate:' + audioParamsGet.samplingRate);
    console.info('channels:' + audioParamsGet.channels);
    console.info('encodingType:' + audioParamsGet.encodingType);
}).catch((err) => {
    console.log('getStreamInfo :ERROR: ' + err.message);
2853
});
2854

V
Vaidegi B 已提交
2855 2856
```

2857
### start<sup>8+</sup>
V
Vaidegi B 已提交
2858

2859
start(callback: AsyncCallback<void\>): void
V
Vaidegi B 已提交
2860

2861
Starts capturing. This API uses an asynchronous callback to return the result.
2862

2863
**System capability:** SystemCapability.Multimedia.Audio.Capturer
2864

2865
**Parameters:**
2866 2867 2868 2869 2870

| Name     | Type                    | Mandatory | Description                             |
| :------- | :---------------------- | :-------- | :-------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.     |

2871
**Example:**
2872 2873

```
2874
audioCapturer.start((err) => {
2875 2876 2877 2878 2879
    if (err) {
        console.error('Capturer start failed.');
    } else {
        console.info('Capturer start success.');
    }
2880
});
2881 2882 2883
```


2884
### start<sup>8+</sup>
2885

2886
start(): Promise<void\>
2887

2888
Starts capturing. This API uses a promise to return the result.
2889

2890
**System capability:** SystemCapability.Multimedia.Audio.Capturer
2891

2892
**Return value:**
2893 2894 2895 2896 2897

| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |

2898
**Example:**
2899 2900 2901

```
audioCapturer.start().then(() => {
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
    console.info('AudioFrameworkRecLog: ---------START---------');
    console.info('AudioFrameworkRecLog: Capturer started :SUCCESS ');
    console.info('AudioFrameworkRecLog: AudioCapturer : STATE : '+audioCapturer.state);
    console.info('AudioFrameworkRecLog: Capturer started :SUCCESS ');
    if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) {
        stateFlag = true;
    }
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer start :ERROR : '+err.message);
    stateFlag=false;
2912 2913 2914
});
```

2915
### stop<sup>8+</sup>
2916

2917
stop(callback: AsyncCallback<void\>): void
2918

2919
Stops capturing. This API uses an asynchronous callback to return the result.
2920

2921
**System capability:** SystemCapability.Multimedia.Audio.Capturer
2922

2923
**Parameters:**
2924 2925 2926 2927 2928

| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |

2929
**Example:**
2930 2931

```
2932
audioCapturer.stop((err) => {
2933 2934 2935 2936 2937
    if (err) {
        console.error('Capturer stop failed');
    } else {
        console.log('Capturer stopped.');
    }
2938
});
2939 2940 2941
```


2942
### stop<sup>8+</sup>
2943

2944
stop(): Promise<void\>
2945

2946
Stops capturing. This API uses a promise to return the result.
2947

2948
**System capability:** SystemCapability.Multimedia.Audio.Capturer
2949

2950
**Return value:**
2951 2952 2953 2954 2955

| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |

2956
**Example:**
2957 2958 2959

```
audioCapturer.stop().then(() => {
2960 2961 2962 2963 2964 2965 2966 2967 2968
    console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------');
    console.info('AudioFrameworkRecLog: Capturer stopped : SUCCESS');
    if ((audioCapturer.state == audioCapturer.AudioState.STATE_STOPPED)){
        stateFlag=true;
        console.info('AudioFrameworkRecLog: resultFlag : '+stateFlag);
    }
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer stop:ERROR : '+err.message);
    stateFlag=false;
2969 2970 2971 2972
});
```


2973
### release<sup>8+</sup>
2974

2975
release(callback: AsyncCallback<void\>): void
2976

2977
Releases the capturer. This API uses an asynchronous callback to return the result.
2978

2979
**System capability:** SystemCapability.Multimedia.Audio.Capturer
2980

2981
**Parameters:**
2982 2983 2984 2985 2986

| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |

2987
**Example:**
2988 2989

```
2990
audioCapturer.release((err) => {
2991 2992 2993 2994 2995
    if (err) {
        console.error('capturer release failed');
    } else {
        console.log('capturer released.');
    }
2996
});
2997 2998 2999
```


3000
### release<sup>8+</sup>
3001

3002
release(): Promise<void\>
3003

3004
Releases the capturer. This API uses a promise to return the result.
3005

3006
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3007

3008
**Return value:**
3009 3010 3011 3012 3013

| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |

3014
**Example:**
3015 3016

```
3017

3018
audioCapturer.release().then(() => {
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
    console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------');
    console.info('AudioFrameworkRecLog: Capturer release : SUCCESS');
    console.info('AudioFrameworkRecLog: AudioCapturer : STATE : '+audioCapturer.state);
    stateFlag=true;
    console.info('AudioFrameworkRecLog: stateFlag : '+stateFlag);
    expect(stateFlag).assertTrue();
    done();
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer stop:ERROR : '+err.message);
    stateFlag=false
3029
});
3030

3031 3032 3033
```


3034
### read<sup>8+</sup>
3035

3036
read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void
3037

3038
Reads the buffer from the audio capturer. This API uses an asynchronous callback to return the result.
3039

3040
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3041

3042
**Parameters:**
3043 3044 3045 3046 3047 3048 3049

| Name           | Type                        | Mandatory | Description                                   |
| :------------- | :-------------------------- | :-------- | :-------------------------------------------- |
| size           | number                      | Yes       | Number of bytes to read.                      |
| isBlockingRead | boolean                     | Yes       | Whether the read operation should be blocked. |
| callback       | AsyncCallback<ArrayBuffer\> | Yes       | Callback used to return the buffer.           |

3050
**Example:**
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060

```
audioCapturer.read(bufferSize, true, async(err, buffer) => {
    if (!err) {
        console.log("Success in reading the buffer data");
    }
};
```


3061
### read<sup>8+</sup>
3062

3063
read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\>
3064

3065
Reads the buffer from the audio capturer. This API uses a promise to return the result.
3066

3067
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3068

3069
**Parameters:**
3070

3071 3072 3073 3074
| Name           | Type       | Mandatory | Description                                   |
| :------------- | :--------- | :-------- | :-------------------------------------------- |
| size           | number     | Yes       | Number of bytes to read.                      |
| isBlockingRead | boolean    | Yes       | Whether the read operation should be blocked. |
3075

3076
**Return value:**
3077 3078 3079 3080 3081

| Type                  | Description                                                                                      |
| :-------------------- | :----------------------------------------------------------------------------------------------- |
| Promise<ArrayBuffer\> | Returns the buffer data read if the operation is successful; returns an error code otherwise.    |

3082
**Example:**
3083 3084

```
3085 3086 3087 3088
audioCapturer.read(bufferSize, true).then((buffer) => {
    console.info('buffer read successfully');
}).catch((err) => {
    console.info('ERROR : '+err.message);
3089 3090 3091 3092
});
```


3093
### getAudioTime<sup>8+</sup>
3094

3095
getAudioTime(callback: AsyncCallback<number\>): void
3096

3097
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses an asynchronous callback to return the result.
3098

3099
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3100

3101
**Parameters:**
3102 3103 3104 3105 3106 3107

| Name     | Type                   | Mandatory | Description                            |
| :------- | :--------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<number\> | Yes       | Callback used to return the timestamp. |
|          |                        |           |                                        |

3108
**Example:**
3109 3110

```
3111
audioCapturer.getAudioTime((err, timestamp) => {
3112
    console.log('Current timestamp: ' + timestamp);
3113
});
3114 3115 3116
```


3117
### getAudioTime<sup>8+</sup>
3118

3119
getAudioTime(): Promise<number\>
3120

3121
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses a promise to return the result.
3122

3123
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3124

3125
**Return value:**
3126 3127 3128 3129 3130

| Type             | Description                           |
| :--------------- | :------------------------------------ |
| Promise<number\> | Promise used to return the timestamp. |

3131
**Example:**
3132 3133 3134

```
audioCapturer.getAudioTime().then((audioTime) => {
3135 3136 3137
    console.info('AudioFrameworkRecLog: AudioCapturer getAudioTime : Success' + audioTime );
}).catch((err) => {
    console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
3138
});
3139

3140 3141 3142
```


3143
### getBufferSize<sup>8+</sup>
3144

3145
getBufferSize(callback: AsyncCallback<number\>): void
3146

3147
Obtains a reasonable minimum buffer size in bytes for capturing. This API uses an asynchronous callback to return the result.
3148

3149
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3150

3151
**Parameters:**
3152 3153 3154 3155 3156 3157

| Name     | Type                   | Mandatory | Description                              |
| :------- | :--------------------- | :-------- | :--------------------------------------- |
| callback | AsyncCallback<number\> | Yes       | Callback used to return the buffer size. |
|          |                        |           |                                          |

3158
**Example:**
3159 3160

```
3161
audioCapturer.getBufferSize((err, bufferSize) => {
3162 3163
    if (!err) {
        console.log('BufferSize : ' + bufferSize);
3164 3165 3166 3167 3168
        audioCapturer.read(bufferSize, true).then((buffer) => {
            console.info('Buffer read is ' + buffer );
        }).catch((err) => {
            console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
        });
3169 3170 3171 3172
    }
});
```

3173
### getBufferSize<sup>8+</sup>
3174

3175
getBufferSize(): Promise<number\>
3176

3177
Obtains a reasonable minimum buffer size in bytes for capturing. This API uses a promise to return the result.
3178

3179
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3180

3181
**Return value:**
3182 3183 3184 3185 3186

| Type             | Description                             |
| :--------------- | :-------------------------------------- |
| Promise<number\> | Promise used to return the buffer size. |

3187
**Example:**
3188 3189 3190

```
audioCapturer.getBufferSize().then((bufferSize) => {
3191 3192 3193 3194 3195 3196 3197 3198
    if (!err) {
        console.log('BufferSize : ' + bufferSize);
        audioCapturer.read(bufferSize, true).then((buffer) => {
            console.info('Buffer read is ' + buffer );
        }).catch((err) => {
            console.info('ERROR : '+err.message);
        });
    }
3199 3200 3201
});
```

3202
### on('markReach')<sup>8+</sup>
3203

3204
on(type: 'markReach', frame: number, callback: (position: number) => {}): void
3205 3206 3207

Subscribes to mark reached events. When the number of frames captured reaches the value of the frame parameter, the callback is invoked.

3208
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3209

3210
**Parameters:**
3211 3212 3213 3214 3215

| Name     | Type                      | Mandatory | Description                                                              |
| :------- | :------------------------ | :-------- | :----------------------------------------------------------------------- |
| type     | string                    | Yes       | Type of the capturer event to subscribe to.                              |
| frame    | number                    | Yes       | Number of frames to trigger the event. The value must be greater than 0. |
3216
| callback | position: number) => {}   | Yes       | Callback invoked when the event is triggered.                            |
3217

3218
**Example:**
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228

```
audioCapturer.on('markReach', 1000, (position) => {
    if (position == "1000") {
        console.log('ON Triggered successfully');
    }
});
```


3229
### off('markReach')<sup>8+</sup>
3230

3231
off(type: 'markReach'): void
3232 3233 3234

Unsubscribes from the mark reached events.

3235
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3236

3237
**Parameters:**
3238 3239 3240 3241 3242

| Name     | Type                      | Mandatory | Description                                     |
| :------- | :------------------------ | :-------- | :---------------------------------------------- |
| type     | string                    | Yes       | Type of the capturer event to unsubscribe from. |

3243
**Example:**
3244 3245 3246 3247 3248

```
audioCapturer.off('markReach');
```

3249
### on('periodReach')<sup>8+</sup>
3250

3251
on(type: "periodReach", frame: number, callback: (position: number) => {}): void
3252 3253 3254

Subscribes to period reached events. When the period of frame capturing reaches the value of frame parameter, the callback is invoked.

3255
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3256

3257
**Parameters:**
3258 3259 3260 3261 3262

| Name     | Type                      | Mandatory | Description                                                                        |
| :------- | :------------------------ | :-------- | :--------------------------------------------------------------------------------- |
| type     | string                    | Yes       | Type of the capturer event to subscribe to.                                        |
| frame    | number                    | Yes       | Period during which frame capturing is listened. The value must be greater than 0. |
3263
| callback | (position: number) => {}  | Yes       | Callback invoked when the event is triggered.                                      |
3264

3265
**Example:**
3266 3267 3268 3269 3270 3271 3272 3273 3274

```
audioCapturer.on('periodReach', 1000, (position) => {
    if (position == "1000") {
        console.log('ON Triggered successfully');
    }
});
```

3275
### off('periodReach')<sup>8+</sup>
3276

3277
off(type: 'periodReach'): void
3278 3279 3280

Unsubscribes from period reached events.

3281
**System capability:** SystemCapability.Multimedia.Audio.Capturer
3282

3283
**Parameters:**
3284 3285 3286 3287 3288

| Name     | Type                      | Mandatory | Description                                     |
| :------- | :------------------------ | :-------- | :---------------------------------------------- |
| type     | string                    | Yes       | Type of the capturer event to unsubscribe from. |

3289 3290 3291 3292 3293 3294 3295 3296 3297
**Example:**

```
audioCapturer.off('periodReach')
```

### on('stateChange') <sup>8+</sup>

on(type: 'stateChange', callback: Callback<AudioState\>): void
3298

3299
Subscribes to state change events.
3300

3301 3302 3303 3304 3305 3306 3307
**System capability:** SystemCapability.Multimedia.Audio.Capturer

**Parameters:**

| Name     | Type                       | Mandatory | Description                                                                              |
| :------- | :------------------------- | :-------- | :--------------------------------------------------------------------------------------- |
| type     | string                     | Yes       | Type of the event to subscribe to. The value 'stateChange' means the state change event. |
G
Geevarghese V K 已提交
3308
| callback | [AudioState](#audiostate8) | Yes       | Callback used to return the state change.                                                |
3309 3310

**Example:**
3311 3312

```
3313 3314 3315 3316 3317 3318 3319 3320
audioCapturer.on('stateChange', (state) => {
    if (state == 1) {
        console.log("audio capturer state is: STATE_PREPARED");
    }
    if (state == 2) {
        console.log("audio capturer state is: STATE_RUNNING");
    }
});
3321
```