js-apis-audio.md 123.8 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 146 147
**Parameters:**
| Name       | Type                                            | Mandatory | Description                                          |
| :--------- | :---------------------------------------------- | :-------- | :--------------------------------------------------- |
| options    | [AudioCapturerOptions](#AudioCapturerOptions)   | Yes       | Capturer configurations.                             |
| 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 192
**Parameters:**
| Name       | Type                                          | Mandatory | Description                 |
| :--------- | :-------------------------------------------- | :-------- | :-------------------------- |
| options    | [AudioCapturerOptions](#AudioCapturerOptions) | 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
**Parameters:**
V
Vaidegi B 已提交
484

485 486 487 488 489 490 491 492
| 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 已提交
493 494 495

Describes audio renderer information.

496 497 498
**System capability:** SystemCapability.Multimedia.Audio.Core

**Parameters:**
499

500 501 502 503 504
| 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 已提交
505

506
## AudioRendererOptions<sup>8+</sup>
V
Vaidegi B 已提交
507

508 509
Describes audio renderer configuration options.

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

512
**Parameters:**
513

514 515 516 517 518 519
| Name          | Type                                      | Mandatory | Description           |
| :------------ | :-----------------------------------------| :-------- | :-------------------- |
| streamInfo    | [AudioStreamInfo](#audiostreaminfo8)      | Yes       | Stream information.   |
| rendererInfo  | [AudioRendererInfo](#audiorendererinfo8)  | Yes       | Renderer information. |

## InterruptEvent<sup>9+</sup>
V
Vaidegi B 已提交
520 521 522

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

523 524 525 526 527 528 529 530 531 532 533
**System capability:** SystemCapability.Multimedia.Audio.Renderer

**Parameters:**

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

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

537
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 已提交
538

539
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
| 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.                                                                                                                                  |
| hint       | [InterruptHint](interrupthint)              | No        | Interrupt event prompts.                                                                                                                               |
| 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 已提交
563 564
Describes the volume event received by the app when the volume is changed.

565
This is a system API and cannot be called by third-party applications.
566

567
**System capability:** SystemCapability.Multimedia.Audio.Volume
V
Vaidegi B 已提交
568

569 570 571 572 573 574 575
**Parameters:**

| 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 已提交
576

577
## DeviceChangeAction
578

579 580
Describes the device change type and device information.

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

583
**Parameters:**
584

585 586 587 588
| Name                | Type                                              | Mandatory | Description         |
| :------------------ | :------------------------------------------------ | :-------- | :------------------ |
| type                | [DeviceChangeType](#DeviceChangeType)             | Yes       | Device change type. |
| deviceDescriptors   | [AudioDeviceDescriptors](#AudioDeviceDescriptors) | Yes       | Device information. |
589 590 591


## DeviceChangeType
592

593 594
Enumerates device change types.

595 596 597 598 599 600
**System capability:** SystemCapability.Multimedia.Audio.Device

| Name                   | Default Value | Description           |
| :--------------------- | :------------ | :-------------------- |
| CONNECT                | 0             | Device connection.    |
| DISCONNECT             | 1             | Device disconnection. |
601 602 603


## AudioCapturerInfo<sup>8+</sup><a name="audiocapturerinfo"></a>
604

605 606
Describes audio capturer information.

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

609
**Parameters:**
610

611 612 613 614
| Name            | Type                       | Mandatory | Description           |
| :---------------| :------------------------- | :-------- | :-------------------- |
| source          | [SourceType](#sourcetype)  | Yes       | Audio source type.    |
| capturerFlags   | number                     | Yes       | Audio capturer flags. |
615 616 617


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

619 620
Enumerates source types.

621 622 623 624 625 626 627
**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. |
628 629 630


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

632 633
Enumerates audio scenes.

634 635 636 637 638 639 640 641
**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 已提交
642

643
## AudioManager
W
wusongqing 已提交
644

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

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
### 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 已提交
664 665

```
666 667 668 669 670 671
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.');
672
});
W
wusongqing 已提交
673
```
674
### setVolume
W
wusongqing 已提交
675

676 677 678 679 680
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 已提交
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695
**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 已提交
696 697

```
698
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
W
wusongqing 已提交
699
    console.log('Promise returned to indicate a successful volume setting.');
700 701 702 703
});
```

### getVolume
W
wusongqing 已提交
704

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

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

709 710 711 712 713 714 715 716 717 718
**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 已提交
719 720 721 722

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

V
Vaidegi B 已提交
730

731
### getVolume
V
Vaidegi B 已提交
732

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

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

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

739
**Parameters:**
W
wusongqing 已提交
740

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

745 746 747 748 749
**Return value:**

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

751
**Example:**
W
wusongqing 已提交
752 753

```
754
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
755
    console.log('Promise returned to indicate that the volume is obtained.' + value);
756 757 758 759 760 761 762 763
});
```

### 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 已提交
764

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

767
**Parameters:**
W
wusongqing 已提交
768

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

**Example:**
W
wusongqing 已提交
775 776 777 778 779

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

V
Vaidegi B 已提交
786

787
### getMinVolume
V
Vaidegi B 已提交
788 789

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

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

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

795 796 797 798 799
**Parameters:**

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

801
**Return value:**
W
wusongqing 已提交
802

803 804 805
| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<number>     | Promise used to return the result.  |
W
wusongqing 已提交
806

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
**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 已提交
831 832 833 834 835 836 837 838

```
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);
839
});
W
wusongqing 已提交
840 841
```

V
Vaidegi B 已提交
842

843
### getMaxVolume
V
Vaidegi B 已提交
844

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

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

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

851
**Parameters:**
W
wusongqing 已提交
852

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

857 858 859 860 861
**Return value:**

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

863
**Example:**
W
wusongqing 已提交
864 865

```
866
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => {
W
wusongqing 已提交
867
    console.log('Promised returned to indicate that the maximum volume is obtained.');
868
});
W
wusongqing 已提交
869 870
```

871
### mute
V
Vaidegi B 已提交
872

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

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

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

879
**Parameters:**
W
wusongqing 已提交
880

881 882 883 884 885 886 887
| 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 已提交
888 889 890 891 892

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


900
### mute
V
Vaidegi B 已提交
901

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

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

906 907 908 909 910 911 912 913
**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 已提交
914

915 916 917 918 919 920 921
**Return value:**

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

**Example:**
W
wusongqing 已提交
922 923

```
924
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
W
wusongqing 已提交
925
    console.log('Promise returned to indicate that the stream is muted.');
926
});
W
wusongqing 已提交
927 928
```

V
Vaidegi B 已提交
929

930
### isMute
V
Vaidegi B 已提交
931

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

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

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

938
**Parameters:**
W
wusongqing 已提交
939

940 941 942 943 944 945
| 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 已提交
946 947 948 949

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


958
### isMute
V
Vaidegi B 已提交
959

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

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

964 965 966
**System capability:** SystemCapability.Multimedia.Audio.Volume

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

968 969 970 971 972 973 974 975 976 977 978
| 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 已提交
979 980

```
981
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
982
    console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value);
983
});
W
wusongqing 已提交
984 985
```

986
### isActive
V
Vaidegi B 已提交
987

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

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

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

994
**Parameters:**
W
wusongqing 已提交
995

996 997 998 999 1000 1001
| 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 已提交
1002 1003 1004 1005 1006

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

V
Vaidegi B 已提交
1013

1014
### isActive
V
Vaidegi B 已提交
1015

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

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

1020 1021 1022 1023 1024 1025 1026
**System capability:** SystemCapability.Multimedia.Audio.Volume

**Parameters:**

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

1028 1029 1030 1031 1032 1033 1034
**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 已提交
1035 1036

```
1037
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => {
W
wusongqing 已提交
1038
    console.log('Promise returned to indicate that the active status of the stream is obtained.' + value);
1039
});
W
wusongqing 已提交
1040 1041 1042
```


1043
### setRingerMode
V
Vaidegi B 已提交
1044

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

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

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

1051
**Parameters:**
W
wusongqing 已提交
1052

1053 1054 1055 1056 1057 1058
| Name       | Type                                | Mandatory | Description                         |
| ---------- | ----------------------------------- | --------- | ----------------------------------- |
| mode       | [AudioRingMode](#audioringmode)     | Yes       | Ringer mode.                        |
| callback   | AsyncCallback<void>                 | Yes       | Callback used to return the result. |

**Example:**
W
wusongqing 已提交
1059 1060 1061 1062 1063 1064 1065 1066

```
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.');
1067
});
W
wusongqing 已提交
1068 1069 1070
```


1071
### setRingerMode
V
Vaidegi B 已提交
1072

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

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

1077 1078 1079
**System capability:** SystemCapability.Multimedia.Audio.Communication

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

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
| 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 已提交
1092 1093

```
1094
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => {
W
wusongqing 已提交
1095
    console.log('Promise returned to indicate a successful setting of the ringer mode.');
1096
});
W
wusongqing 已提交
1097 1098
```

V
Vaidegi B 已提交
1099

1100
### getRingerMode
V
Vaidegi B 已提交
1101

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

1104 1105 1106
Obtains the ringer mode. This API uses an asynchronous callback to return the query result.

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

1108
**Parameters:**
W
wusongqing 已提交
1109

1110 1111 1112
| Name       | Type                                            | Mandatory | Description                              |
| ---------- | ----------------------------------------------- | --------- | ---------------------------------------- |
| callback   | AsyncCallback<[AudioRingMode](#audioringmode)>  | Yes       | Callback used to return the ringer mode. |
W
wusongqing 已提交
1113

1114
**Example:**
W
wusongqing 已提交
1115 1116 1117 1118

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

V
Vaidegi B 已提交
1126

1127
### getRingerMode
V
Vaidegi B 已提交
1128

W
wusongqing 已提交
1129
getRingerMode\(\): Promise<AudioRingMode\>
W
wusongqing 已提交
1130

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

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

1135
**Return value:**
W
wusongqing 已提交
1136

1137 1138 1139
| Type                                           | Description                             |
| ---------------------------------------------- | --------------------------------------- |
| Promise<[AudioRingMode](#audioringmode)>       | Promise used to return the ringer mode. |
W
wusongqing 已提交
1140

1141
**Example:**
W
wusongqing 已提交
1142 1143

```
1144
audioManager.getRingerMode().then((value) => {
W
wusongqing 已提交
1145
    console.log('Promise returned to indicate that the ringer mode is obtained.' + value);
1146
});
W
wusongqing 已提交
1147 1148 1149
```


1150
### setAudioParameter
V
Vaidegi B 已提交
1151

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

1154 1155 1156
Sets an audio parameter. This API uses an asynchronous callback to return the result.

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

1158
**Parameters:**
W
wusongqing 已提交
1159

1160 1161 1162 1163 1164
| 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 已提交
1165

1166
**Example:**
W
wusongqing 已提交
1167 1168 1169 1170 1171

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


1179
### setAudioParameter
V
Vaidegi B 已提交
1180

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

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

1185 1186 1187
**System capability:** SystemCapability.Multimedia.Audio.Core

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

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
| 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 已提交
1201 1202

```
1203
audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => {
W
wusongqing 已提交
1204
    console.log('Promise returned to indicate a successful setting of the audio parameter.');
1205
});
W
wusongqing 已提交
1206 1207
```

V
Vaidegi B 已提交
1208

1209
### getAudioParameter
V
Vaidegi B 已提交
1210

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

1213 1214 1215
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 已提交
1216

1217
**Parameters:**
W
wusongqing 已提交
1218

1219 1220 1221 1222
| 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 已提交
1223

1224
**Example:**
W
wusongqing 已提交
1225 1226 1227 1228 1229

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

V
Vaidegi B 已提交
1236

1237
### getAudioParameter
V
Vaidegi B 已提交
1238

W
wusongqing 已提交
1239
getAudioParameter\(key: string\): Promise<string\>
W
wusongqing 已提交
1240

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

1243 1244 1245 1246 1247 1248 1249 1250 1251
**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 已提交
1252

1253 1254 1255 1256 1257
| Type                | Description                         |
| ------------------- | ----------------------------------- |
| Promise<string>     | Promise used to return the value of the audio parameter.  |

**Example:**
W
wusongqing 已提交
1258 1259

```
1260
audioManager.getAudioParameter('PBits per sample').then((value) => {
W
wusongqing 已提交
1261
    console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value);
1262
});
W
wusongqing 已提交
1263 1264
```

V
Vaidegi B 已提交
1265

1266 1267 1268 1269 1270
### 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 已提交
1271

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

1274
**Parameters:**
W
wusongqing 已提交
1275

1276 1277 1278 1279 1280 1281
| 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 已提交
1282 1283

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

V
Vaidegi B 已提交
1293 1294


1295
### getDevices
V
Vaidegi B 已提交
1296

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

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

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

1303
**Parameters:**
W
wusongqing 已提交
1304

1305 1306 1307
| Name       | Type                        | Mandatory | Description         |
| ---------  | --------------------------- | --------- | ------------------- |
| deviceFlag | [DeviceFlag](#deviceflag)   | Yes       | Audio device flag.  |
W
wusongqing 已提交
1308

1309 1310 1311 1312 1313
**Return value:**

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

1315
**Example:**
W
wusongqing 已提交
1316 1317

```
1318
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
W
wusongqing 已提交
1319
    console.log('Promise returned to indicate that the device list is obtained.');
1320
});
W
wusongqing 已提交
1321 1322
```

1323
### setDeviceActive
V
Vaidegi B 已提交
1324

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

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

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

1331
**Parameters:**
W
wusongqing 已提交
1332

1333 1334 1335 1336 1337 1338 1339
| 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 已提交
1340 1341

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


V
Vaidegi B 已提交
1352

1353
### setDeviceActive
V
Vaidegi B 已提交
1354

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

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

1359 1360 1361 1362 1363 1364 1365 1366
**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 已提交
1367

1368 1369 1370 1371 1372 1373 1374 1375
**Return value:**

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


**Example:**
W
wusongqing 已提交
1376 1377

```
1378
audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(() => {
W
wusongqing 已提交
1379
    console.log('Promise returned to indicate that the device is set to the active status.');
1380
});
W
wusongqing 已提交
1381 1382
```

V
Vaidegi B 已提交
1383

1384
### isDeviceActive
V
Vaidegi B 已提交
1385

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

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

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

1392
**Parameters:**
W
wusongqing 已提交
1393

1394 1395 1396 1397 1398 1399
| 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 已提交
1400 1401

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

V
Vaidegi B 已提交
1411

1412
### isDeviceActive
V
Vaidegi B 已提交
1413

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

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

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

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
**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 已提交
1433 1434

```
1435
audioManager.isDeviceActive(audio.DeviceType.SPEAKER).then((value) => {
W
wusongqing 已提交
1436
    console.log('Promise returned to indicate that the active status of the device is obtained.' + value);
1437
});
W
wusongqing 已提交
1438 1439 1440
```


1441
### setMicrophoneMute
V
Vaidegi B 已提交
1442

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

1445 1446 1447
Mutes or unmutes the microphone. This API uses an asynchronous callback to return the result.

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

1449
**Parameters:**
W
wusongqing 已提交
1450

1451 1452 1453 1454
| 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 已提交
1455

1456
**Example:**
W
wusongqing 已提交
1457 1458 1459 1460 1461 1462 1463 1464

```
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.');
1465
});
W
wusongqing 已提交
1466 1467 1468
```


1469
### setMicrophoneMute
V
Vaidegi B 已提交
1470

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

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
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 已提交
1488

Z
zengyawen 已提交
1489
</table>
W
wusongqing 已提交
1490

1491
**Example:**
W
wusongqing 已提交
1492 1493

```
1494
audioManager.setMicrophoneMute(true).then(() => {
W
wusongqing 已提交
1495
    console.log('Promise returned to indicate that the microphone is muted.');
1496
});
W
wusongqing 已提交
1497 1498
```

V
Vaidegi B 已提交
1499

1500
### isMicrophoneMute
V
Vaidegi B 已提交
1501

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

1504 1505 1506
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 已提交
1507

1508
**Parameters:**
W
wusongqing 已提交
1509

1510 1511 1512
| 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 已提交
1513

1514
**Example:**
W
wusongqing 已提交
1515 1516

```
1517
var audioManager = audio.getAudioManager();
W
wusongqing 已提交
1518 1519 1520 1521 1522 1523
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);
1524
});
W
wusongqing 已提交
1525 1526
```

V
Vaidegi B 已提交
1527

1528
### isMicrophoneMute
V
Vaidegi B 已提交
1529

W
wusongqing 已提交
1530
isMicrophoneMute\(\): Promise<boolean\>
W
wusongqing 已提交
1531

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

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

1536
**Return value:**
W
wusongqing 已提交
1537

1538 1539 1540
| 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 已提交
1541

1542
**Example:**
W
wusongqing 已提交
1543 1544

```
1545
var audioManager = audio.getAudioManager();
1546
audioManager.isMicrophoneMute().then((value) => {
W
wusongqing 已提交
1547
    console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
1548
});
W
wusongqing 已提交
1549 1550
```

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

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

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

1557
This is a system API and cannot be called by third-party applications.
1558

1559
**System capability:** SystemCapability.Multimedia.Audio.Volume
V
Vaidegi B 已提交
1560

1561
**Parameters:**
V
Vaidegi B 已提交
1562

1563 1564 1565 1566
| 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 已提交
1567

1568
**Example:**
V
Vaidegi B 已提交
1569 1570 1571 1572 1573 1574

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


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

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

1583
Subscribes to ringer mode change events. This API uses a callback to get ringer mode changes.
1584

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

1587
**System capability:** SystemCapability.Multimedia.Audio.Communication
V
Vaidegi B 已提交
1588

1589
**Parameters:**
V
Vaidegi B 已提交
1590

1591 1592 1593 1594
| 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 已提交
1595

1596
**Example:**
V
Vaidegi B 已提交
1597 1598 1599 1600

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

1604
### on('deviceChange')
1605

1606
on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void
1607 1608 1609

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

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

1612
**Parameters:**
1613

1614 1615 1616 1617
| 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. |
| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)> | Yes       | Callback used to obtain the device update details.                                                                                              |
1618

1619
**Example:**
1620 1621 1622 1623 1624 1625 1626

```
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);
1627
});
1628 1629
```

1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 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 1725 1726 1727 1728 1729
### 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.              |
| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)> | Yes       | Callback used to obtain the device update details.  |

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

1731
setAudioScene\(scene: AudioScene, callback: AsyncCallback<void\>\): void
1732

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

1735
This is a system API and cannot be called by third-party applications.
1736

1737 1738 1739
**System capability:** SystemCapability.Multimedia.Audio.Communication

**Parameters:**
1740 1741 1742 1743 1744 1745

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

1746
**Example:**
1747 1748 1749 1750 1751 1752 1753 1754

```
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.');
1755
});
1756 1757 1758
```


1759
### setAudioScene<sup>8+</sup>
1760

1761
setAudioScene\(scene: AudioScene\): Promise<void\>
1762

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

1765
This is a system API and cannot be called by third-party applications.
1766

1767 1768 1769
**System capability:** SystemCapability.Multimedia.Audio.Communication

**Parameters:**
1770 1771 1772 1773 1774 1775

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


1776
**Return value:**
1777 1778 1779 1780 1781 1782

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


1783
**Example:**
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

```
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');
});
```


1794
### getAudioScene<sup>8+</sup>
1795

1796
getAudioScene\(callback: AsyncCallback<AudioScene\>\): void
1797

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

1800
**System capability:** SystemCapability.Multimedia.Audio.Communication
1801

1802
**Parameters:**
1803 1804 1805 1806 1807

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

1808
**Example:**
1809 1810 1811 1812 1813 1814 1815 1816

```
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);
1817
});
1818 1819 1820
```


1821
### getAudioScene<sup>8+</sup>
1822

1823
getAudioScene\(\): Promise<AudioScene\>
1824

1825
Obtains the audio scene mode. This API uses a promise to return the query result.
1826

1827
**System capability:** SystemCapability.Multimedia.Audio.Communication
1828

1829
**Return value:**
1830 1831 1832 1833 1834 1835

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


1836
**Example:**
1837 1838 1839 1840 1841 1842

```
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');
1843
});
1844 1845
```

1846
## AudioDeviceDescriptor
V
Vaidegi B 已提交
1847

1848
Describes an audio device.
1849

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

1852 1853 1854 1855
| Name       | Type                      | Readable | Writable | Description        |
| ---------- | ------------------------- | -------- | -------- | ------------------ |
| deviceRole | [DeviceRole](#devicerole) | Yes      | No       | Audio device role. |
| deviceType | [DeviceType](#devicetype) | Yes      | No       | Audio device type. |
V
Vaidegi B 已提交
1856

1857
## AudioDeviceDescriptors
V
Vaidegi B 已提交
1858

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

1861
**System capability:** SystemCapability.Multimedia.Audio.Device
V
Vaidegi B 已提交
1862

1863
**Example:**
V
Vaidegi B 已提交
1864

1865 1866
```
import audio from '@ohos.multimedia.audio';
V
Vaidegi B 已提交
1867

1868 1869 1870
function displayDeviceProp(value) {
    deviceRoleValue = value.deviceRole;
    deviceTypeValue = value.deviceType;
V
Vaidegi B 已提交
1871 1872 1873

}

1874 1875 1876 1877 1878 1879
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 已提交
1880
    if (deviceTypeValue != null && deviceRoleValue != null){
1881
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  PASS');
V
Vaidegi B 已提交
1882 1883
    }
    else{
1884 1885
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  FAIL');
    }
1886
});
V
Vaidegi B 已提交
1887
```
1888 1889 1890
## 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 已提交
1891

1892
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1893

1894
### state<sup>8+</sup>
V
Vaidegi B 已提交
1895

1896
readonly state: AudioState
V
Vaidegi B 已提交
1897 1898 1899

Defines the current render state.

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

1902 1903 1904
| Name  | Type                        | Readable | Writable | Description         |
| :---- | :-------------------------- | :------- | :------- | :------------------ |
| state |  [AudioState](#audiostate8) | Yes      | No       | Audio render state. |
V
Vaidegi B 已提交
1905

1906
**Example:**
V
Vaidegi B 已提交
1907 1908 1909 1910 1911

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

1912
### getRendererInfo<sup>8+</sup>
V
Vaidegi B 已提交
1913

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

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

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

1920
**Parameters:**
V
Vaidegi B 已提交
1921

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

1926
**Example:**
V
Vaidegi B 已提交
1927 1928

```
1929
audioRenderer.getRendererInfo((err, rendererInfo) => {
1930 1931 1932 1933
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
1934
});
V
Vaidegi B 已提交
1935 1936 1937
```


1938
### getRendererInfo<sup>8+</sup>
V
Vaidegi B 已提交
1939

1940
getRendererInfo(): Promise<AudioRendererInfo\>
1941

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

1944
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1945

1946
**Return value:**
V
Vaidegi B 已提交
1947

1948 1949 1950
| Type                                                | Description                                      |
| :-------------------------------------------------- | :----------------------------------------------- |
| Promise<[AudioRendererInfo](#audiorendererinfo8)>   | Promise used to return the renderer information. |
V
Vaidegi B 已提交
1951

1952
**Example:**
V
Vaidegi B 已提交
1953 1954

```
1955 1956 1957 1958 1959 1960 1961 1962 1963
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 已提交
1964

1965
```
V
Vaidegi B 已提交
1966

1967
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
1968

1969
getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void
1970

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

1973
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
1974

1975
**Parameters:**
V
Vaidegi B 已提交
1976

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

1981
**Example:**
V
Vaidegi B 已提交
1982 1983

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

1993
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
1994

1995
getStreamInfo(): Promise<AudioStreamInfo\>
V
Vaidegi B 已提交
1996

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

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

2001
**Return value:**
V
Vaidegi B 已提交
2002

2003 2004 2005
| Type                                               | Description                                    |
| :------------------------------------------------- | :--------------------------------------------- |
| Promise<[AudioStreamInfo](#audiostreaminfo8)\>     | Promise used to return the stream information. |
V
Vaidegi B 已提交
2006

2007
**Example:**
V
Vaidegi B 已提交
2008 2009

```
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
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 已提交
2020 2021
```

2022
### start<sup>8+</sup>
V
Vaidegi B 已提交
2023

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

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

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

2030
**Parameters:**
V
Vaidegi B 已提交
2031

2032 2033 2034 2035
| Name     | Type                    | Mandatory | Description                             |
| :------- | :---------------------- | :-------- | :-------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.     |
|          |                         |           |                                         |
V
Vaidegi B 已提交
2036

2037
**Example:**
V
Vaidegi B 已提交
2038 2039

```
2040
audioRenderer.start((err) => {
2041 2042
    if (err) {
        console.error('Renderer start failed.');
V
Vaidegi B 已提交
2043
    } else {
2044
        console.info('Renderer start success.');
V
Vaidegi B 已提交
2045
    }
2046
});
V
Vaidegi B 已提交
2047 2048
```

2049
### start<sup>8+</sup>
V
Vaidegi B 已提交
2050

2051
start(): Promise<void\>
V
Vaidegi B 已提交
2052

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

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

2057
**Return value:**
V
Vaidegi B 已提交
2058

2059 2060 2061
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2062

2063
**Example:**
V
Vaidegi B 已提交
2064 2065

```
2066 2067 2068 2069 2070
audioRenderer.start().then(() => {
    console.log('Renderer started');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2071 2072 2073
```


2074
### pause<sup>8+</sup>
V
Vaidegi B 已提交
2075

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

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

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

2082
**Parameters:**
V
Vaidegi B 已提交
2083

2084 2085 2086 2087
| Name     | Type                    | Mandatory | Description                           |
| :------- | :---------------------- | :-------- | :------------------------------------ |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.   |
|          |                         |           |                                       |
V
Vaidegi B 已提交
2088

2089
**Example:**
V
Vaidegi B 已提交
2090 2091

```
2092
audioRenderer.pause((err) => {
2093 2094
    if (err) {
        console.error('Renderer pause failed');
V
Vaidegi B 已提交
2095
    } else {
2096
        console.log('Renderer paused.');
V
Vaidegi B 已提交
2097
    }
2098
});
V
Vaidegi B 已提交
2099 2100
```

2101
### pause<sup>8+</sup>
V
Vaidegi B 已提交
2102

2103
pause(): Promise\<void>
V
Vaidegi B 已提交
2104

2105
Pauses rendering. This API uses a promise to return the result.
2106

2107
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2108

2109
**Return value:**
V
Vaidegi B 已提交
2110

2111 2112 2113
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2114

2115
**Example:**
V
Vaidegi B 已提交
2116 2117

```
2118 2119 2120 2121 2122
audioRenderer.pause().then(() => {
    console.log('Renderer paused');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2123 2124
```

2125
### drain<sup>8+</sup>
V
Vaidegi B 已提交
2126

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

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

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

2133
**Parameters:**
V
Vaidegi B 已提交
2134

2135 2136 2137 2138
| Name     | Type                    | Mandatory | Description                             |
| :------- | :---------------------- | :-------- | :---------------------------------------|
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.     |
|          |                         |           |                                         |
V
Vaidegi B 已提交
2139

2140
**Example:**
V
Vaidegi B 已提交
2141 2142

```
2143
audioRenderer.drain((err) => {
2144 2145
    if (err) {
        console.error('Renderer drain failed');
V
Vaidegi B 已提交
2146
    } else {
2147
        console.log('Renderer drained.');
V
Vaidegi B 已提交
2148
    }
2149
});
V
Vaidegi B 已提交
2150 2151
```

2152
### drain<sup>8+</sup>
V
Vaidegi B 已提交
2153

2154
drain(): Promise\<void>
V
Vaidegi B 已提交
2155

2156
Drains the playback buffer. This API uses a promise to return the result.
2157

2158
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2159

2160
**Return value:**
V
Vaidegi B 已提交
2161

2162 2163 2164
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2165

2166
**Example:**
V
Vaidegi B 已提交
2167 2168

```
2169 2170 2171 2172 2173
audioRenderer.drain().then(() => {
    console.log('Renderer drained successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2174 2175 2176
```


2177
### stop<sup>8+</sup>
V
Vaidegi B 已提交
2178

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

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

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

2185
**Parameters:**
V
Vaidegi B 已提交
2186

2187 2188 2189
| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |
V
Vaidegi B 已提交
2190

2191
**Example:**
V
Vaidegi B 已提交
2192 2193

```
2194
audioRenderer.stop((err) => {
2195 2196
    if (err) {
        console.error('Renderer stop failed');
V
Vaidegi B 已提交
2197
    } else {
2198
        console.log('Renderer stopped.');
V
Vaidegi B 已提交
2199
    }
2200
});
V
Vaidegi B 已提交
2201 2202
```

2203
### stop<sup>8+</sup>
V
Vaidegi B 已提交
2204

2205
stop(): Promise\<void>
V
Vaidegi B 已提交
2206

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

2209
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2210

2211
**Return value:**
V
Vaidegi B 已提交
2212

2213 2214 2215
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2216

2217
**Example:**
V
Vaidegi B 已提交
2218 2219

```
2220 2221 2222 2223 2224
audioRenderer.stop().then(() => {
    console.log('Renderer stopped successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2225 2226 2227
```


2228
### release<sup>8+</sup>
V
Vaidegi B 已提交
2229

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

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

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

2236
**Parameters:**
V
Vaidegi B 已提交
2237

2238 2239 2240
| Name     | Type                    | Mandatory | Description                            |
| :------- | :---------------------- | :-------- | :------------------------------------- |
| callback | AsyncCallback<void\>    | Yes       | Callback used to return the result.    |
V
Vaidegi B 已提交
2241

2242
**Example:**
V
Vaidegi B 已提交
2243 2244

```
2245
audioRenderer.release((err) => {
2246 2247
    if (err) {
        console.error('Renderer release failed');
V
Vaidegi B 已提交
2248
    } else {
2249
        console.log('Renderer released.');
V
Vaidegi B 已提交
2250
    }
2251
});
V
Vaidegi B 已提交
2252 2253
```

2254
### release<sup>8+</sup>
V
Vaidegi B 已提交
2255

2256
release(): Promise\<void>
V
Vaidegi B 已提交
2257

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

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

2262
**Return value:**
V
Vaidegi B 已提交
2263

2264 2265 2266
| Type           | Description                        |
| :------------- | :--------------------------------- |
| Promise<void\> | Promise used to return the result. |
V
Vaidegi B 已提交
2267

2268
**Example:**
V
Vaidegi B 已提交
2269 2270

```
2271 2272 2273 2274 2275
audioRenderer.release().then(() => {
    console.log('Renderer released successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2276 2277
```

2278
### write<sup>8+</sup>
V
Vaidegi B 已提交
2279

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

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

2284
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2285

2286
**Parameters:**
V
Vaidegi B 已提交
2287 2288 2289 2290 2291 2292

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

2293
**Example:**
V
Vaidegi B 已提交
2294 2295

```
2296 2297 2298
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

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


2312
### write<sup>8+</sup>
V
Vaidegi B 已提交
2313

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

2316
Writes the buffer. This API uses a promise to return the result.
2317

2318
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2319

2320
**Return value:**
V
Vaidegi B 已提交
2321 2322 2323 2324 2325

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

2326
**Example:**
V
Vaidegi B 已提交
2327 2328

```
2329 2330 2331 2332
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

var filePath = 'data/StarWars10s-2C-48000-4SW.wav';
V
Vaidegi B 已提交
2333 2334 2335
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
2336 2337 2338 2339 2340 2341 2342 2343 2344
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 已提交
2345 2346
```

2347
### getAudioTime<sup>8+</sup>
V
Vaidegi B 已提交
2348

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

2351
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 已提交
2352

2353
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2354

2355
**Parameters:**
V
Vaidegi B 已提交
2356 2357 2358 2359 2360

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

2361
**Example:**
V
Vaidegi B 已提交
2362 2363

```
2364
audioRenderer.getAudioTime((err, timestamp) => {
V
Vaidegi B 已提交
2365
    console.log('Current timestamp: ' + timestamp);
2366
});
V
Vaidegi B 已提交
2367 2368 2369
```


2370
### getAudioTime<sup>8+</sup>
V
Vaidegi B 已提交
2371

2372
getAudioTime(): Promise\<number>
V
Vaidegi B 已提交
2373

2374
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 已提交
2375

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

2378
**Return value:**
V
Vaidegi B 已提交
2379 2380 2381 2382 2383

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

2384
**Example:**
V
Vaidegi B 已提交
2385 2386

```
2387 2388 2389 2390 2391
audioRenderer.getAudioTime().then((timestamp) => {
    console.log('Current timestamp: ' + timestamp);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2392 2393 2394
```


2395
### getBufferSize<sup>8+</sup>
V
Vaidegi B 已提交
2396

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

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

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

2403
**Parameters:**
V
Vaidegi B 已提交
2404 2405 2406 2407 2408

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

2409
**Example:**
V
Vaidegi B 已提交
2410 2411

```
2412
audioRenderer.getBufferSize((err, bufferSize) => {
V
Vaidegi B 已提交
2413 2414 2415
    if (err) {
        console.error('getBufferSize error');
    }
2416
});
V
Vaidegi B 已提交
2417 2418 2419 2420 2421
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
```


2422
### getBufferSize<sup>8+</sup>
V
Vaidegi B 已提交
2423

2424
getBufferSize(): Promise\<number>
V
Vaidegi B 已提交
2425

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

2428
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2429

2430
**Return value:**
V
Vaidegi B 已提交
2431 2432 2433 2434 2435

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

2436
**Example:**
V
Vaidegi B 已提交
2437 2438

```
2439 2440 2441 2442 2443 2444
audioRenderer.getBufferSize().then((bufferSize) => {
    let buf = new ArrayBuffer(bufferSize);
    ss.readSync(buf);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2445 2446
```

2447
### setRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2448

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

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

2453
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2454

2455
**Parameters:**
2456

2457 2458 2459 2460
| Name     | Type                                        | Mandatory | Description                         |
| :------- | :------------------------------------------ | :-------- | :---------------------------------- |
| rate     | [AudioRendererRate](#audiorendererrate8)    | Yes       | Audio render rate.                  |
| callback | AsyncCallback<void\>                        | Yes       | Callback used to return the result. |
V
Vaidegi B 已提交
2461

2462
**Example:**
V
Vaidegi B 已提交
2463 2464

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


2475
### setRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2476

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

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

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

2483
**Parameters:**
V
Vaidegi B 已提交
2484

2485 2486 2487
| Name | Type                                     | Mandatory | Description        |
| :--- | :--------------------------------------- | :-------- | :----------------- |
| rate | [AudioRendererRate](#audiorendererrate8) | Yes       | Audio render rate. |
V
Vaidegi B 已提交
2488

2489
**Return value:**
V
Vaidegi B 已提交
2490 2491 2492 2493 2494

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

2495
**Example:**
V
Vaidegi B 已提交
2496 2497

```
2498 2499 2500 2501 2502
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => {
    console.log('setRenderRate SUCCESS');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2503 2504
```

2505
### getRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2506

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

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

2511
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2512

2513
**Parameters:**
2514

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

2519
**Example:**
V
Vaidegi B 已提交
2520 2521

```
2522
audioRenderer.getRenderRate((err, renderrate) => {
V
Vaidegi B 已提交
2523
    console.log('getRenderRate: ' + renderrate);
2524
});
V
Vaidegi B 已提交
2525 2526 2527
```


2528
### getRenderRate<sup>8+</sup>
V
Vaidegi B 已提交
2529

2530
getRenderRate(): Promise\<AudioRendererRate>
2531

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

2534
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2535

2536
**Return value:**
V
Vaidegi B 已提交
2537

2538 2539 2540
| Type                                                | Description                                   |
| :-------------------------------------------------- | :-------------------------------------------- |
| Promise<<[AudioRendererRate](#audiorendererrate8)\> | Promise used to return the audio render rate. |
V
Vaidegi B 已提交
2541

2542
**Example:**
V
Vaidegi B 已提交
2543 2544

```
2545 2546 2547 2548 2549
audioRenderer.getRenderRate().then((renderRate) => {
    console.log('getRenderRate: ' + renderRate);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
V
Vaidegi B 已提交
2550 2551 2552
```


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

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

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

2559
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2560

2561
**Parameters:**
V
Vaidegi B 已提交
2562

2563 2564 2565 2566
| 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 已提交
2567

2568
**Example:**
V
Vaidegi B 已提交
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594

```
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;
        }
    }
2595
});
V
Vaidegi B 已提交
2596 2597
```

2598
### on('markReach')<sup>8+</sup>
2599

2600
on(type: 'markReach', frame: number, callback: (position: number) => {}): void
2601 2602 2603

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

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

2606
**Parameters:**
2607 2608 2609 2610 2611

| 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. |
2612
| callback | (position: number) => {}  | Yes       | Callback invoked when the event is triggered.                            |
2613

2614
**Example:**
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624

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


2625
### off('markReach') <sup>8+</sup>
2626

2627
off(type: 'markReach'): void
2628 2629 2630

Unsubscribes from mark reached events.

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

2633
**Parameters:**
2634 2635 2636 2637 2638

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

2639
**Example:**
2640 2641 2642 2643 2644

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

2645
### on('periodReach') <sup>8+</sup>
2646

2647
on(type: "periodReach", frame: number, callback: (position: number) => {}): void
2648 2649 2650

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

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

2653
**Parameters:**
2654 2655 2656 2657 2658

| 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. |
2659
| callback |  (position: number) => {} | Yes       | Callback invoked when the event is triggered.                                      |
2660

2661
**Example:**
2662 2663 2664 2665 2666 2667 2668 2669 2670

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

2671
### off('periodReach') <sup>8+</sup>
2672

2673
off(type: 'periodReach'): void
2674 2675 2676

Unsubscribes from period reached events.

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

2679
**Parameters:**
2680 2681 2682 2683 2684

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

2685
**Example:**
2686 2687 2688 2689

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

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

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

2695
Subscribes to state change events.
V
Vaidegi B 已提交
2696

2697
**System capability:** SystemCapability.Multimedia.Audio.Renderer
V
Vaidegi B 已提交
2698

2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
**Parameters:**

| Name     | Type                       | Mandatory | Description                                                                              |
| :------- | :------------------------- | :-------- | :--------------------------------------------------------------------------------------- |
| type     | string                     | Yes       | Type of the event to subscribe to. The value 'stateChange' means the state change event. |
| callback | [AudioState](#AudioState8) | Yes       | Callback used to return the state change.                                                |

**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 已提交
2718

2719
## AudioCapturer<sup>8+</sup>
V
Vaidegi B 已提交
2720

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

2723
### state<sup>8+</sup>
V
Vaidegi B 已提交
2724

2725
readonly state: AudioState
V
Vaidegi B 已提交
2726

2727
Defines the current capture state.
V
Vaidegi B 已提交
2728

2729
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2730

2731 2732 2733
| Name  | Type                       | Readable | Writable | Description          |
| :---- | :------------------------- | :------- | :------- | :------------------- |
| state | [AudioState](#audiostate8) | Yes      | No       | Audio capture state. |
V
Vaidegi B 已提交
2734

2735
**Example:**
V
Vaidegi B 已提交
2736 2737

```
2738
var state = audioCapturer.state;
V
Vaidegi B 已提交
2739 2740 2741
```


2742
### getCapturerInfo<sup>8+</sup>
V
Vaidegi B 已提交
2743

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

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

2748
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2749

2750
**Parameters:**
V
Vaidegi B 已提交
2751

2752 2753 2754
| Name     | Type                                                     | Mandatory | Description                                       |
| :------- | :------------------------------------------------------- | :-------- | :------------------------------------------------ |
| callback | AsyncCallback<[AudioCapturerInfo](#audioCapturerInfo)\>  | Yes       | Callback used to return the capturer information. |
V
Vaidegi B 已提交
2755

2756
**Example:**
V
Vaidegi B 已提交
2757 2758

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


2771
### getCapturerInfo<sup>8+</sup>
V
Vaidegi B 已提交
2772

2773
getCapturerInfo(): Promise<AudioCapturerInfo\>
V
Vaidegi B 已提交
2774

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

2777
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2778

2779
**Return value:**
V
Vaidegi B 已提交
2780

2781 2782 2783
| Type                                                | Description                                      |
| :-------------------------------------------------- | :----------------------------------------------- |
| Promise<[AudioCapturerInfo](#audiocapturerinfo)\>   | Promise used to return the capturer information. |
V
Vaidegi B 已提交
2784

2785
**Example:**
V
Vaidegi B 已提交
2786 2787

```
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
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);
2799
});
V
Vaidegi B 已提交
2800

2801
```
V
Vaidegi B 已提交
2802

2803
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
2804

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

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

2809
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2810

2811
**Parameters:**
V
Vaidegi B 已提交
2812

2813 2814 2815
| Name     | Type                                                         | Mandatory | Description                                     |
| :------- | :----------------------------------------------------------- | :-------- | :---------------------------------------------- |
| callback | AsyncCallback<[AudioStreamInfo](#AudioRendererOptions8)\>    | Yes       | Callback used to return the stream information. |
V
Vaidegi B 已提交
2816

2817
**Example:**
V
Vaidegi B 已提交
2818 2819

```
2820
audioCapturer.getStreamInfo((err, streamInfo) => {
V
Vaidegi B 已提交
2821
    if (err) {
2822
        console.error('Failed to get stream info');
V
Vaidegi B 已提交
2823
    } else {
2824 2825 2826 2827 2828
        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 已提交
2829
    }
2830
});
V
Vaidegi B 已提交
2831 2832
```

2833
### getStreamInfo<sup>8+</sup>
V
Vaidegi B 已提交
2834

2835
getStreamInfo(): Promise<AudioStreamInfo\>
V
Vaidegi B 已提交
2836

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

2839
**System capability:** SystemCapability.Multimedia.Audio.Capturer
V
Vaidegi B 已提交
2840

2841
**Return value:**
V
Vaidegi B 已提交
2842

2843 2844 2845
| Type                                                  | Description                                      |
| :---------------------------------------------------- | :----------------------------------------------- |
| Promise<[AudioStreamInfo](#AudioRendererOptions8)\>   | Promise used to return the stream information.   |
V
Vaidegi B 已提交
2846

2847
**Example:**
V
Vaidegi B 已提交
2848 2849

```
2850 2851 2852 2853 2854 2855 2856 2857
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);
2858
});
2859

V
Vaidegi B 已提交
2860 2861
```

2862
### start<sup>8+</sup>
V
Vaidegi B 已提交
2863

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

2866
Starts capturing. This API uses an asynchronous callback to return the result.
2867

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

2870
**Parameters:**
2871 2872 2873 2874 2875

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

2876
**Example:**
2877 2878

```
2879
audioCapturer.start((err) => {
2880 2881 2882 2883 2884
    if (err) {
        console.error('Capturer start failed.');
    } else {
        console.info('Capturer start success.');
    }
2885
});
2886 2887 2888
```


2889
### start<sup>8+</sup>
2890

2891
start(): Promise<void\>
2892

2893
Starts capturing. This API uses a promise to return the result.
2894

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

2897
**Return value:**
2898 2899 2900 2901 2902

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

2903
**Example:**
2904 2905 2906

```
audioCapturer.start().then(() => {
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
    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;
2917 2918 2919
});
```

2920
### stop<sup>8+</sup>
2921

2922
stop(callback: AsyncCallback<void\>): void
2923

2924
Stops capturing. This API uses an asynchronous callback to return the result.
2925

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

2928
**Parameters:**
2929 2930 2931 2932 2933

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

2934
**Example:**
2935 2936

```
2937
audioCapturer.stop((err) => {
2938 2939 2940 2941 2942
    if (err) {
        console.error('Capturer stop failed');
    } else {
        console.log('Capturer stopped.');
    }
2943
});
2944 2945 2946
```


2947
### stop<sup>8+</sup>
2948

2949
stop(): Promise<void\>
2950

2951
Stops capturing. This API uses a promise to return the result.
2952

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

2955
**Return value:**
2956 2957 2958 2959 2960

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

2961
**Example:**
2962 2963 2964

```
audioCapturer.stop().then(() => {
2965 2966 2967 2968 2969 2970 2971 2972 2973
    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;
2974 2975 2976 2977
});
```


2978
### release<sup>8+</sup>
2979

2980
release(callback: AsyncCallback<void\>): void
2981

2982
Releases the capturer. This API uses an asynchronous callback to return the result.
2983

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

2986
**Parameters:**
2987 2988 2989 2990 2991

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

2992
**Example:**
2993 2994

```
2995
audioCapturer.release((err) => {
2996 2997 2998 2999 3000
    if (err) {
        console.error('capturer release failed');
    } else {
        console.log('capturer released.');
    }
3001
});
3002 3003 3004
```


3005
### release<sup>8+</sup>
3006

3007
release(): Promise<void\>
3008

3009
Releases the capturer. This API uses a promise to return the result.
3010

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

3013
**Return value:**
3014 3015 3016 3017 3018

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

3019
**Example:**
3020 3021

```
3022

3023
audioCapturer.release().then(() => {
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
    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
3034
});
3035

3036 3037 3038
```


3039
### read<sup>8+</sup>
3040

3041
read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void
3042

3043
Reads the buffer from the audio capturer. This API uses an asynchronous callback to return the result.
3044

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

3047
**Parameters:**
3048 3049 3050 3051 3052 3053 3054

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

3055
**Example:**
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065

```
audioCapturer.read(bufferSize, true, async(err, buffer) => {
    if (!err) {
        console.log("Success in reading the buffer data");
    }
};
```


3066
### read<sup>8+</sup>
3067

3068
read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\>
3069

3070
Reads the buffer from the audio capturer. This API uses a promise to return the result.
3071

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

3074
**Parameters:**
3075

3076 3077 3078 3079
| Name           | Type       | Mandatory | Description                                   |
| :------------- | :--------- | :-------- | :-------------------------------------------- |
| size           | number     | Yes       | Number of bytes to read.                      |
| isBlockingRead | boolean    | Yes       | Whether the read operation should be blocked. |
3080

3081
**Return value:**
3082 3083 3084 3085 3086

| Type                  | Description                                                                                      |
| :-------------------- | :----------------------------------------------------------------------------------------------- |
| Promise<ArrayBuffer\> | Returns the buffer data read if the operation is successful; returns an error code otherwise.    |

3087
**Example:**
3088 3089

```
3090 3091 3092 3093
audioCapturer.read(bufferSize, true).then((buffer) => {
    console.info('buffer read successfully');
}).catch((err) => {
    console.info('ERROR : '+err.message);
3094 3095 3096 3097
});
```


3098
### getAudioTime<sup>8+</sup>
3099

3100
getAudioTime(callback: AsyncCallback<number\>): void
3101

3102
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses an asynchronous callback to return the result.
3103

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

3106
**Parameters:**
3107 3108 3109 3110 3111 3112

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

3113
**Example:**
3114 3115

```
3116
audioCapturer.getAudioTime((err, timestamp) => {
3117
    console.log('Current timestamp: ' + timestamp);
3118
});
3119 3120 3121
```


3122
### getAudioTime<sup>8+</sup>
3123

3124
getAudioTime(): Promise<number\>
3125

3126
Obtains the timestamp in Unix epoch time (starts from January 1, 1970), in nanoseconds. This API uses a promise to return the result.
3127

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

3130
**Return value:**
3131 3132 3133 3134 3135

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

3136
**Example:**
3137 3138 3139

```
audioCapturer.getAudioTime().then((audioTime) => {
3140 3141 3142
    console.info('AudioFrameworkRecLog: AudioCapturer getAudioTime : Success' + audioTime );
}).catch((err) => {
    console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
3143
});
3144

3145 3146 3147
```


3148
### getBufferSize<sup>8+</sup>
3149

3150
getBufferSize(callback: AsyncCallback<number\>): void
3151

3152
Obtains a reasonable minimum buffer size in bytes for capturing. This API uses an asynchronous callback to return the result.
3153

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

3156
**Parameters:**
3157 3158 3159 3160 3161 3162

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

3163
**Example:**
3164 3165

```
3166
audioCapturer.getBufferSize((err, bufferSize) => {
3167 3168
    if (!err) {
        console.log('BufferSize : ' + bufferSize);
3169 3170 3171 3172 3173
        audioCapturer.read(bufferSize, true).then((buffer) => {
            console.info('Buffer read is ' + buffer );
        }).catch((err) => {
            console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
        });
3174 3175 3176 3177
    }
});
```

3178
### getBufferSize<sup>8+</sup>
3179

3180
getBufferSize(): Promise<number\>
3181

3182
Obtains a reasonable minimum buffer size in bytes for capturing. This API uses a promise to return the result.
3183

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

3186
**Return value:**
3187 3188 3189 3190 3191

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

3192
**Example:**
3193 3194 3195

```
audioCapturer.getBufferSize().then((bufferSize) => {
3196 3197 3198 3199 3200 3201 3202 3203
    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);
        });
    }
3204 3205 3206
});
```

3207
### on('markReach')<sup>8+</sup>
3208

3209
on(type: 'markReach', frame: number, callback: (position: number) => {}): void
3210 3211 3212

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

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

3215
**Parameters:**
3216 3217 3218 3219 3220

| 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. |
3221
| callback | position: number) => {}   | Yes       | Callback invoked when the event is triggered.                            |
3222

3223
**Example:**
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233

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


3234
### off('markReach')<sup>8+</sup>
3235

3236
off(type: 'markReach'): void
3237 3238 3239

Unsubscribes from the mark reached events.

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

3242
**Parameters:**
3243 3244 3245 3246 3247

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

3248
**Example:**
3249 3250 3251 3252 3253

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

3254
### on('periodReach')<sup>8+</sup>
3255

3256
on(type: "periodReach", frame: number, callback: (position: number) => {}): void
3257 3258 3259

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

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

3262
**Parameters:**
3263 3264 3265 3266 3267

| 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. |
3268
| callback | (position: number) => {}  | Yes       | Callback invoked when the event is triggered.                                      |
3269

3270
**Example:**
3271 3272 3273 3274 3275 3276 3277 3278 3279

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

3280
### off('periodReach')<sup>8+</sup>
3281

3282
off(type: 'periodReach'): void
3283 3284 3285

Unsubscribes from period reached events.

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

3288
**Parameters:**
3289 3290 3291 3292 3293

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

3294 3295 3296 3297 3298 3299 3300 3301 3302
**Example:**

```
audioCapturer.off('periodReach')
```

### on('stateChange') <sup>8+</sup>

on(type: 'stateChange', callback: Callback<AudioState\>): void
3303

3304
Subscribes to state change events.
3305

3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
**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. |
| callback | [AudioState](#AudioState8) | Yes       | Callback used to return the state change.                                                |

**Example:**
3316 3317

```
3318 3319 3320 3321 3322 3323 3324 3325
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");
    }
});
3326
```