js-apis-audio.md 104.7 KB
Newer Older
Z
zengyawen 已提交
1
# 音频管理
2

Z
zengyawen 已提交
3
>  **说明:**
Z
zengyawen 已提交
4 5 6 7 8
>  本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

该模块提供以下音频相关的常用功能:

- [AudioManager](#audiomanager):音频管理。
L
lwx1059628 已提交
9 10
- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。
- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM(Pulse Code Modulation)音频数据。
Z
zengyawen 已提交
11

Z
zengyawen 已提交
12
## 导入模块
M
mamingshuai 已提交
13 14 15 16 17

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

Z
zengyawen 已提交
18

Z
zengyawen 已提交
19
## audio.getAudioManager
Z
zengyawen 已提交
20 21

getAudioManager(): AudioManager
M
mamingshuai 已提交
22 23 24

获取音频管理器。

Z
zengyawen 已提交
25 26
**系统能力:** SystemCapability.Multimedia.Audio.Core

M
mamingshuai 已提交
27
**返回值:**
Z
zengyawen 已提交
28 29
| 类型                          | 说明         |
| ----------------------------- | ------------ |
Z
zengyawen 已提交
30
| [AudioManager](#audiomanager) | 音频管理类。 |
M
mamingshuai 已提交
31 32 33 34 35 36

**示例:**
```
var audioManager = audio.getAudioManager();
```

Z
zengyawen 已提交
37 38 39 40
## audio.createAudioRenderer<sup>8+</sup>

createAudioRenderer(options: AudioRendererOptions): AudioRenderer

L
lwx1059628 已提交
41
获取音频渲染器。
Z
zengyawen 已提交
42 43 44 45 46

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数**

L
lwx1059628 已提交
47 48 49
| 参数名  | 类型                                           | 必填 | 说明         |
| ------- | ---------------------------------------------- | ---- | ------------ |
| options | [AudioRendererOptions](#audiorendereroptions8) | 是   | 配置渲染器。 |
Z
zengyawen 已提交
50 51 52 53 54

**返回值**

| 类型                             | 说明             |
| -------------------------------- | ---------------- |
L
lwx1059628 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| [AudioRenderer](#audiorenderer8) | 音频渲染器对象。 |

**示例:**

```
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) {
        console.error(`AudioRender Created : Error: ${err.message}`);
    }
    else {
        console.info('AudioRender Created : Success : SUCCESS');
        audioRenderer = data;
    }
});
```

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

createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\>

获取音频渲染器。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名  | 类型                                           | 必填 | 说明         |
| :------ | :--------------------------------------------- | :--- | :----------- |
| options | [AudioRendererOptions](#audiorendereroptions8) | 是   | 配置渲染器。 |

**返回值:**

| 类型                                      | 说明             |
| ----------------------------------------- | ---------------- |
| Promise<[AudioRenderer](#audiorenderer8)> | 音频播放器对象。 |
Z
zengyawen 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

**示例:**

```
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
}

let audioRenderer = await audio.createAudioRenderer(audioRendererOptions);
```
Z
zengyawen 已提交
132

L
lwx1059628 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
## audio.createAudioCapturer<sup>8+</sup>

createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void

获取音频采集器。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                                           | 必填 | 说明             |
| :------- | :--------------------------------------------- | :--- | :--------------- |
| options  | [AudioCapturerOptions](#AudioCapturerOptions)  | 是   | 配置音频采集器。 |
| callback | AsyncCallback<[AudioCapturer](#AudioCapturer)> | 是   | 音频采集器对象。 |

**示例:**

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

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

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

获取音频采集器。使用promise 方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名  | 类型                 | 必填 | 说明             |
| :------ | :------------------- | :--- | :--------------- |
| options | AudioCapturerOptions | 是   | 配置音频采集器。 |

**返回值:**

| 类型                                      | 说明           |
| ----------------------------------------- | -------------- |
| Promise<[AudioCapturer](#AudioCapturer8)> | 音频采集器对象 |

**示例:**

```
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
}

let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
```

Z
zengyawen 已提交
222
## AudioVolumeType
M
mamingshuai 已提交
223

224
枚举,音频流类型。
M
mamingshuai 已提交
225

Z
zengyawen 已提交
226 227 228 229 230 231 232 233
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Volume

| 名称                         | 默认值 | 描述       |
| ---------------------------- | ------ | ---------- |
| VOICE_CALL<sup>8+</sup>      | 0      | 语音电话。 |
| RINGTONE                     | 2      | 铃声。     |
| MEDIA                        | 3      | 媒体。     |
| VOICE_ASSISTANT<sup>8+</sup> | 9      | 语音助手。 |
Z
zengyawen 已提交
234 235 236


## DeviceFlag
M
mamingshuai 已提交
237

238
枚举,可获取的设备种类。
M
mamingshuai 已提交
239

Z
zengyawen 已提交
240 241 242 243 244 245 246
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

| 名称                | 默认值 | 描述       |
| ------------------- | ------ | ---------- |
| OUTPUT_DEVICES_FLAG | 1      | 输出设备。 |
| INPUT_DEVICES_FLAG  | 2      | 输入设备。 |
| ALL_DEVICES_FLAG    | 3      | 所有设备。 |
Z
zengyawen 已提交
247 248 249


## DeviceRole
M
mamingshuai 已提交
250

251
枚举,设备角色。
M
mamingshuai 已提交
252

Z
zengyawen 已提交
253 254 255 256 257 258
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

| 名称          | 默认值 | 描述           |
| ------------- | ------ | -------------- |
| INPUT_DEVICE  | 1      | 输入设备角色。 |
| OUTPUT_DEVICE | 2      | 输出设备角色。 |
M
mamingshuai 已提交
259 260


Z
zengyawen 已提交
261 262 263
## DeviceType

枚举,设备类型。
M
magekkkk 已提交
264

Z
zengyawen 已提交
265 266
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

L
lwx1059628 已提交
267 268 269 270 271 272 273 274 275 276 277
| 名称            | 默认值 | 描述                                                      |
| --------------- | ------ | --------------------------------------------------------- |
| INVALID         | 0      | 无效设备。                                                |
| EARPIECE        | 1      | 听筒。                                                    |
| SPEAKER         | 2      | 扬声器。                                                  |
| WIRED_HEADSET   | 3      | 有线耳机,带麦克风。                                      |
| WIRED_HEADPHONE | 4      | 有线耳机,无麦克风。                                      |
| BLUETOOTH_SCO   | 7      | 蓝牙设备SCO(Synchronous Connection Oriented)连接。      |
| BLUETOOTH_A2DP  | 8      | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。 |
| MIC             | 15     | 麦克风。                                                  |
| USB_HEADSET     | 22     | USB耳机,带麦克风。                                       |
M
magekkkk 已提交
278

Z
zengyawen 已提交
279
## ActiveDeviceType
M
magekkkk 已提交
280

Z
zengyawen 已提交
281
枚举,活跃设备类型。
M
magekkkk 已提交
282

Z
zengyawen 已提交
283 284 285 286 287 288
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

| 名称          | 默认值 | 描述                                                 |
| ------------- | ------ | ---------------------------------------------------- |
| SPEAKER       | 2      | 扬声器。                                             |
| BLUETOOTH_SCO | 7      | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 |
M
mamingshuai 已提交
289

Z
zengyawen 已提交
290
## AudioRingMode
291 292 293

枚举,铃声模式。

Z
zengyawen 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Communication

| 名称                | 默认值 | 描述       |
| ------------------- | ------ | ---------- |
| RINGER_MODE_SILENT  | 0      | 静音模式。 |
| RINGER_MODE_VIBRATE | 1      | 震动模式。 |
| RINGER_MODE_NORMAL  | 2      | 响铃模式。 |

## AudioSampleFormat<sup>8+</sup>

枚举,音频采样格式。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称                  | 默认值 | 描述                       |
| --------------------- | ------ | -------------------------- |
| SAMPLE_FORMAT_INVALID | -1     | 无效格式。                 |
| SAMPLE_FORMAT_U8      | 0      | 无符号8位整数。            |
| SAMPLE_FORMAT_S16LE   | 1      | 带符号的16位整数,小尾数。 |
| SAMPLE_FORMAT_S24LE   | 2      | 带符号的24位整数,小尾数。 |
| SAMPLE_FORMAT_S32LE   | 3      | 带符号的32位整数,小尾数。 |

## AudioChannel<sup>8+</sup>

枚举, 音频声道。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称      | 默认值   | 描述     |
| --------- | -------- | -------- |
| CHANNEL_1 | 0x1 << 0 | 单声道。 |
| CHANNEL_2 | 0x1 << 1 | 双声道。 |

## AudioSamplingRate<sup>8+</sup>

枚举,音频采样率。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称              | 默认值 | 描述            |
| ----------------- | ------ | --------------- |
| SAMPLE_RATE_8000  | 8000   | 采样率为8000。  |
| SAMPLE_RATE_11025 | 11025  | 采样率为11025。 |
| SAMPLE_RATE_12000 | 12000  | 采样率为12000。 |
| SAMPLE_RATE_16000 | 16000  | 采样率为16000。 |
| SAMPLE_RATE_22050 | 22050  | 采样率为22050。 |
| SAMPLE_RATE_24000 | 24000  | 采样率为24000。 |
| SAMPLE_RATE_32000 | 32000  | 采样率为32000。 |
| SAMPLE_RATE_44100 | 44100  | 采样率为44100。 |
| SAMPLE_RATE_48000 | 48000  | 采样率为48000。 |
| SAMPLE_RATE_64000 | 64000  | 采样率为64000。 |
| SAMPLE_RATE_96000 | 96000  | 采样率为96000。 |

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

枚举,音频编码类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称                  | 默认值 | 描述      |
| --------------------- | ------ | --------- |
| ENCODING_TYPE_INVALID | -1     | 无效。    |
| ENCODING_TYPE_RAW     | 0      | PCM编码。 |

L
lwx1059628 已提交
358
## ContentType
Z
zengyawen 已提交
359 360 361 362 363

枚举,音频内容类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

L
lwx1059628 已提交
364 365 366 367 368 369 370 371
| 名称                               | 默认值 | 描述       |
| ---------------------------------- | ------ | ---------- |
| CONTENT_TYPE_UNKNOWN               | 0      | 未知类型。 |
| CONTENT_TYPE_SPEECH                | 1      | 语音。     |
| CONTENT_TYPE_MUSIC                 | 2      | 音乐。     |
| CONTENT_TYPE_MOVIE                 | 3      | 电影。     |
| CONTENT_TYPE_SONIFICATION          | 4      | 加密类型。 |
| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5      | 铃声。     |
Z
zengyawen 已提交
372

L
lwx1059628 已提交
373
## StreamUsage
Z
zengyawen 已提交
374 375 376 377 378 379 380 381 382 383

枚举,音频流使用类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称                               | 默认值 | 描述       |
| ---------------------------------- | ------ | ---------- |
| STREAM_USAGE_UNKNOWN               | 0      | 未知类型。 |
| STREAM_USAGE_MEDIA                 | 1      | 音频。     |
| STREAM_USAGE_VOICE_COMMUNICATION   | 2      | 语音通信。 |
L
lwx1059628 已提交
384
| STREAM_USAGE_NOTIFICATION_RINGTONE | 6      | 通知铃声。 |
Z
zengyawen 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

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

枚举,音频状态。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称           | 默认值 | 描述             |
| -------------- | ------ | ---------------- |
| STATE_INVALID  | -1     | 无效状态。       |
| STATE_NEW      | 0      | 创建新实例状态。 |
| STATE_PREPARED | 1      | 准备状态。       |
| STATE_RUNNING  | 2      | 可运行状态。     |
| STATE_STOPPED  | 3      | 停止状态。       |
| STATE_RELEASED | 4      | 释放状态。       |
| STATE_PAUSED   | 5      | 暂停状态。       |

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

L
lwx1059628 已提交
404
枚举,音频渲染速度。
Z
zengyawen 已提交
405 406 407 408 409 410 411 412 413

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称               | 默认值 | 描述       |
| ------------------ | ------ | ---------- |
| RENDER_RATE_NORMAL | 0      | 正常速度。 |
| RENDER_RATE_DOUBLE | 1      | 2倍速。    |
| RENDER_RATE_HALF   | 2      | 0.5倍数。  |

L
lwx1059628 已提交
414
## InterruptType
Z
zengyawen 已提交
415 416 417 418

枚举,中断类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer
Z
zengyawen 已提交
419

Z
zengyawen 已提交
420 421 422 423 424
| 名称                 | 默认值 | 描述                   |
| -------------------- | ------ | ---------------------- |
| INTERRUPT_TYPE_BEGIN | 1      | 音频播放中断事件开始。 |
| INTERRUPT_TYPE_END   | 2      | 音频播放中断事件结束。 |

L
lwx1059628 已提交
425
## InterruptForceType<sup>9+</sup>
Z
zengyawen 已提交
426 427 428 429 430 431 432 433 434 435

枚举,强制打断类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称            | 默认值 | 描述                                 |
| --------------- | ------ | ------------------------------------ |
| INTERRUPT_FORCE | 0      | 由系统进行操作,强制打断音频播放。   |
| INTERRUPT_SHARE | 1      | 由应用进行操作,可以选择打断或忽略。 |

L
lwx1059628 已提交
436
## InterruptHint
Z
zengyawen 已提交
437 438 439 440 441

枚举,中断提示。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

L
lwx1059628 已提交
442 443 444 445 446 447 448 449
| 名称                               | 默认值 | 描述                                         |
| ---------------------------------- | ------ | -------------------------------------------- |
| INTERRUPT_HINT_NONE<sup>8+</sup>   | 0      | 无提示。                                     |
| INTERRUPT_HINT_RESUME              | 1      | 提示音频恢复。                               |
| INTERRUPT_HINT_PAUSE               | 2      | 提示音频暂停。                               |
| INTERRUPT_HINT_STOP                | 3      | 提示音频停止。                               |
| INTERRUPT_HINT_DUCK                | 4      | 提示音频躲避。(躲避:音量减弱,而不会停止) |
| INTERRUPT_HINT_UNDUCK<sup>8+</sup> | 5      | 提示音量恢复。                               |
Z
zengyawen 已提交
450

451 452 453 454 455 456 457 458 459 460 461 462 463
## InterruptActionType

枚举,中断事件返回类型。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称                  | 默认值 | 描述                                         |
| --------------------- | ------ | -------------------------------------------- |
| TYPE_ACTIVATED | 0    | 表示触发焦点事件。 |
| TYPE_INTERRUPT | 1    | 表示音频打断事件。 |

Z
zengyawen 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
## AudioStreamInfo<sup>8+</sup>

音频流信息。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称         | 类型                                     | 必填 | 说明               |
| ------------ | ---------------------------------------- | ---- | ------------------ |
| samplingRate | [AudioSamplingRate](#audiosamplingrate8) | 是   | 音频文件的采样率。 |
| channels     | [AudioChannel](#audiochannel8)           | 是   | 音频文件的通道数。 |
| sampleFormat | [AudioSampleFormat](#audiosampleformat8) | 是   | 音频采样格式。     |
| encodingType | [AudioEncodingType](#audioencodingtype8) | 是   | 音频编码格式。     |

## AudioRendererInfo<sup>8+</sup>

L
lwx1059628 已提交
479
音频渲染器信息。
Z
zengyawen 已提交
480 481 482

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

L
lwx1059628 已提交
483 484 485 486 487
| 名称          | 类型                        | 必填 | 说明             |
| ------------- | --------------------------- | ---- | ---------------- |
| contentType   | [ContentType](#contenttype) | 是   | 媒体类型。       |
| usage         | [StreamUsage](#streamusage) | 是   | 音频流使用类型。 |
| rendererFlags | number                      | 是   | 音频渲染器标志。 |
Z
zengyawen 已提交
488 489 490

## AudioRendererOptions<sup>8+</sup>

L
lwx1059628 已提交
491
音频渲染器选项信息。
Z
zengyawen 已提交
492 493 494 495 496 497

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称         | 类型                                     | 必填 | 说明             |
| ------------ | ---------------------------------------- | ---- | ---------------- |
| streamInfo   | [AudioStreamInfo](#audiostreaminfo8)     | 是   | 表示音频流信息。 |
L
lwx1059628 已提交
498
| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是   | 表示渲染器信息。 |
Z
zengyawen 已提交
499

L
lwx1059628 已提交
500
## InterruptEvent<sup>9+</sup>
Z
zengyawen 已提交
501 502 503 504 505 506 507

播放中断时,应用接收的中断事件。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称      | 类型                                       | 必填 | 说明                                 |
| --------- | ------------------------------------------ | ---- | ------------------------------------ |
L
lwx1059628 已提交
508 509 510
| eventType | [InterruptType](#interrupttype)            | 是   | 中断事件类型,开始或是结束。         |
| forceType | [InterruptForceType](#interruptforcetype9) | 是   | 操作是由系统执行或是由应用程序执行。 |
| hintType  | [InterruptHint](#interrupthint)            | 是   | 中断提示。                           |
Z
zengyawen 已提交
511

512 513 514 515 516 517 518 519 520 521
## AudioInterrupt

音频监听事件传入的参数。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称      | 类型                                       | 必填 | 说明                                 |
| --------- | ------------------------------------------ | ---- | ------------------------------------ |
L
lwx1059628 已提交
522 523
| streamUsage | [StreamUsage](#streamusage)           | 是   | 音频流使用类型。         |
| contentType | [ContentType](#contenttype) | 是   | 音频打断媒体类型。 |
524 525 526 527 528 529 530 531 532 533 534 535 536
| pauseWhenDucked  | boolean           | 是   | 音频打断时是否可以暂停音频播放(true表示音频播放可以在音频打断期间暂停,false表示相反)。                           |

## InterruptAction

音频打断/获取焦点事件的回调方法。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer

| 名称      | 类型                                       | 必填 | 说明                                 |
| --------- | ------------------------------------------ | ---- | ------------------------------------ |
| actionType | [InterruptActionType](#interruptactiontype) | 是   | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。         |
L
lwx1059628 已提交
537 538
| type  | [InterruptType](#interrupttype) | 否   | 打断事件类型。 |
| hint  | [InterruptHint](interrupthint) | 否   | 打断事件提示。 |
539 540
| activated | boolean | 否   | 获得/释放焦点。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 |

Z
zengyawen 已提交
541 542 543 544
## VolumeEvent<sup>8+</sup>

音量改变时,应用接收的事件。

L
lwx1059628 已提交
545 546
此接口为系统接口,三方应用不支持。

Z
zengyawen 已提交
547 548 549 550 551 552 553
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Volume

| 名称       | 类型                                | 必填 | 说明                                                     |
| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                             |
| volume     | number                              | 是   | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 |
| updateUi   | boolean                             | 是   | 在UI中显示音量变化。                                     |
Z
zengyawen 已提交
554

L
lwx1059628 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
## DeviceChangeAction

描述设备连接状态变化和设备信息。

**系统能力:**SystemCapability.Multimedia.Audio.Device

**参数:**

| 名称              | 类型                                              | 必填 | 说明               |
| :---------------- | :------------------------------------------------ | :--- | :----------------- |
| type              | [DeviceChangeType](#DeviceChangeType)             | 是   | 设备连接状态变化。 |
| deviceDescriptors | [AudioDeviceDescriptors](#AudioDeviceDescriptors) | 是   | 设备信息。         |

## DeviceChangeType

枚举,设备连接状态变化。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

| 名称       | 默认值 | 描述           |
| :--------- | :----- | :------------- |
| CONNECT    | 0      | 设备连接。     |
| DISCONNECT | 1      | 断开设备连接。 |

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

描述音频采集器信息。

**系统能力:** SystemCapability.Multimedia.Audio.Core

| 名称          | 类型                      | 必填 | 说明             |
| :------------ | :------------------------ | :--- | :--------------- |
| source        | [SourceType](#sourcetype) | 是   | 音源类型。       |
| capturerFlags | number                    | 是   | 音频采集器标志。 |

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

枚举,音源类型。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core

| 名称                | 默认值 | 描述           |
| :------------------ | :----- | :------------- |
| SOURCE_TYPE_INVALID | -1     | 无效的音频源。 |
| SOURCE_TYPE_MIC     | 0      | Mic音频源。    |

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

枚举,音频场景。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Communication

| 名称                                               | 默认值 | 描述           |
| :------------------------------------------------- | :----- | :------------- |
| AUDIO_SCENE_DEFAULT                                | 0      | 默认音频场景。 |
| AUDIO_SCENE_RINGING(系统接口,三方应用不支持)    | 1      | 响铃模式。     |
| AUDIO_SCENE_PHONE_CALL(系统接口,三方应用不支持) | 2      | 电话模式。     |
| AUDIO_SCENE_VOICE_CHAT                             | 3      | 语音聊天模式。 |

Z
zengyawen 已提交
614
## AudioManager
M
mamingshuai 已提交
615

Z
zengyawen 已提交
616
管理音频音量和音频设备。在调用AudioManager的接口前,需要先通过[getAudioManager](#audiogetaudiomanager)创建实例。
M
mamingshuai 已提交
617

Z
zengyawen 已提交
618 619 620
### setVolume

setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback&lt;void&gt;): void
M
mamingshuai 已提交
621

Z
zengyawen 已提交
622
设置指定流的音量,使用callback方式异步返回结果。
M
mamingshuai 已提交
623

Z
zengyawen 已提交
624 625
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
626 627
**参数:**

Z
zengyawen 已提交
628 629 630 631 632
| 参数名     | 类型                                | 必填 | 说明                                                     |
| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                             |
| volume     | number                              | 是   | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 |
| callback   | AsyncCallback&lt;void&gt;           | 是   | 回调表示成功还是失败。                                   |
633

M
mamingshuai 已提交
634 635 636
**示例:**

```
Z
zengyawen 已提交
637
var audioManager = audio.getAudioManager();
638
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err)=>{
M
mamingshuai 已提交
639
   if (err) {
640
	   console.error('Failed to set the volume. ${err.message}');
M
mamingshuai 已提交
641 642
	   return;
   }
643
   console.log('Callback invoked to indicate a successful volume setting.');
M
mamingshuai 已提交
644 645 646
})
```

Z
zengyawen 已提交
647 648 649
### setVolume

setVolume(volumeType: AudioVolumeType, volume: number): Promise&lt;void&gt;
M
mamingshuai 已提交
650

Z
zengyawen 已提交
651
设置指定流的音量,使用Promise方式异步返回结果。
M
mamingshuai 已提交
652

Z
zengyawen 已提交
653 654
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
655 656
**参数:**

Z
zengyawen 已提交
657 658 659 660
| 参数名     | 类型                                | 必填 | 说明                                                     |
| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                             |
| volume     | number                              | 是   | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 |
M
mamingshuai 已提交
661 662 663

**返回值:**

Z
zengyawen 已提交
664 665
| 类型                | 说明                          |
| ------------------- | ----------------------------- |
Z
zengyawen 已提交
666
| Promise&lt;void&gt; | Promise回调表示成功还是失败。 |
M
mamingshuai 已提交
667 668 669 670

**示例:**

```
Z
zengyawen 已提交
671
var audioManager = audio.getAudioManager();
A
AOL 已提交
672
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
673
    console.log('Promise returned to indicate a successful volume setting.');
A
AOL 已提交
674
})
M
mamingshuai 已提交
675 676
```

Z
zengyawen 已提交
677 678 679
### getVolume

getVolume(volumeType: AudioVolumeType, callback: AsyncCallback&lt;number&gt;): void
M
mamingshuai 已提交
680

Z
zengyawen 已提交
681
获取指定流的音量,使用callback方式异步返回结果。
M
mamingshuai 已提交
682

Z
zengyawen 已提交
683 684
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
685 686
**参数:**

Z
zengyawen 已提交
687 688 689 690
| 参数名     | 类型                                | 必填 | 说明               |
| ---------- | ----------------------------------- | ---- | ------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。       |
| callback   | AsyncCallback&lt;number&gt;         | 是   | 回调返回音量大小。 |
691

M
mamingshuai 已提交
692 693 694
**示例:**

```
Z
zengyawen 已提交
695
var audioManager = audio.getAudioManager();
696
audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
M
mamingshuai 已提交
697
   if (err) {
698
	   console.error('Failed to obtain the volume. ${err.message}');
M
mamingshuai 已提交
699 700
	   return;
   }
701
   console.log('Callback invoked to indicate that the volume is obtained.');
M
mamingshuai 已提交
702 703 704
})
```

Z
zengyawen 已提交
705 706 707
### getVolume

getVolume(volumeType: AudioVolumeType): Promise&lt;number&gt;
M
mamingshuai 已提交
708

Z
zengyawen 已提交
709
获取指定流的音量,使用Promise方式异步返回结果。
M
mamingshuai 已提交
710

Z
zengyawen 已提交
711 712
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
713 714
**参数:**

Z
zengyawen 已提交
715 716 717
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
718 719 720

**返回值:**

Z
zengyawen 已提交
721 722
| 类型                  | 说明                      |
| --------------------- | ------------------------- |
Z
zengyawen 已提交
723
| Promise&lt;number&gt; | Promise回调返回音量大小。 |
M
mamingshuai 已提交
724 725 726 727

**示例:**

```
Z
zengyawen 已提交
728
var audioManager = audio.getAudioManager();
A
AOL 已提交
729
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
730
    console.log('Promise returned to indicate that the volume is obtained.' + value);
A
AOL 已提交
731
})
M
mamingshuai 已提交
732 733
```

Z
zengyawen 已提交
734 735 736
### getMinVolume

getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback&lt;number&gt;): void
M
mamingshuai 已提交
737

Z
zengyawen 已提交
738
获取指定流的最小音量,使用callback方式异步返回结果。
M
mamingshuai 已提交
739

Z
zengyawen 已提交
740 741
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
742 743
**参数:**

Z
zengyawen 已提交
744 745 746 747
| 参数名     | 类型                                | 必填 | 说明               |
| ---------- | ----------------------------------- | ---- | ------------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。       |
| callback   | AsyncCallback&lt;number&gt;         | 是   | 回调返回最小音量。 |
748

M
mamingshuai 已提交
749 750 751
**示例:**

```
Z
zengyawen 已提交
752
var audioManager = audio.getAudioManager();
Z
zengyawen 已提交
753
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
754 755 756 757 758
    if (err) {
        console.error('Failed to obtain the minimum volume. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the minimum volume is obtained.' + value);
M
mamingshuai 已提交
759 760 761
})
```

Z
zengyawen 已提交
762 763 764
### getMinVolume

getMinVolume(volumeType: AudioVolumeType): Promise&lt;number&gt;
M
mamingshuai 已提交
765

Z
zengyawen 已提交
766
获取指定流的最小音量,使用Promise方式异步返回结果。
M
mamingshuai 已提交
767

Z
zengyawen 已提交
768 769
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
770 771
**参数:**

Z
zengyawen 已提交
772 773 774
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
775 776 777

**返回值:**

Z
zengyawen 已提交
778 779
| 类型                  | 说明                      |
| --------------------- | ------------------------- |
Z
zengyawen 已提交
780
| Promise&lt;number&gt; | Promise回调返回最小音量。 |
M
mamingshuai 已提交
781 782 783 784

**示例:**

```
Z
zengyawen 已提交
785
var audioManager = audio.getAudioManager();
A
AOL 已提交
786
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => {
787
    console.log('Promised returned to indicate that the minimum  volume is obtained.' + value);
A
AOL 已提交
788
})
M
mamingshuai 已提交
789 790
```

Z
zengyawen 已提交
791 792 793
### getMaxVolume

getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback&lt;number&gt;): void
M
mamingshuai 已提交
794

Z
zengyawen 已提交
795
获取指定流的最大音量,使用callback方式异步返回结果。
M
mamingshuai 已提交
796

Z
zengyawen 已提交
797 798
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
799 800
**参数:**

Z
zengyawen 已提交
801 802 803 804
| 参数名     | 类型                                | 必填 | 说明                   |
| ---------- | ----------------------------------- | ---- | ---------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。           |
| callback   | AsyncCallback&lt;number&gt;         | 是   | 回调返回最大音量大小。 |
805

M
mamingshuai 已提交
806 807 808
**示例:**

```
Z
zengyawen 已提交
809
var audioManager = audio.getAudioManager();
810 811 812 813 814 815
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);
M
mamingshuai 已提交
816 817 818
})
```

Z
zengyawen 已提交
819 820 821
### getMaxVolume

getMaxVolume(volumeType: AudioVolumeType): Promise&lt;number&gt;
M
mamingshuai 已提交
822

Z
zengyawen 已提交
823
获取指定流的最大音量,使用Promise方式异步返回结果。
M
mamingshuai 已提交
824

Z
zengyawen 已提交
825 826
**系统能力:** SystemCapability.Multimedia.Audio.Volume

M
mamingshuai 已提交
827 828
**参数:**

Z
zengyawen 已提交
829 830 831
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
832 833 834

**返回值:**

Z
zengyawen 已提交
835 836
| 类型                  | 说明                          |
| --------------------- | ----------------------------- |
Z
zengyawen 已提交
837
| Promise&lt;number&gt; | Promise回调返回最大音量大小。 |
M
mamingshuai 已提交
838 839 840 841

**示例:**

```
Z
zengyawen 已提交
842
var audioManager = audio.getAudioManager();
A
AOL 已提交
843
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => {
844
    console.log('Promised returned to indicate that the maximum volume is obtained.');
A
AOL 已提交
845
})
Z
zengyawen 已提交
846 847
```

Z
zengyawen 已提交
848
### mute
Z
zengyawen 已提交
849 850

mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
851

Z
zengyawen 已提交
852
设置指定音量流静音,使用callback方式异步返回结果。
Z
zengyawen 已提交
853

Z
zengyawen 已提交
854 855
**系统能力:** SystemCapability.Multimedia.Audio.Volume

Z
zengyawen 已提交
856 857
**参数:**

Z
zengyawen 已提交
858 859 860 861 862
| 参数名     | 类型                                | 必填 | 说明                                  |
| ---------- | ----------------------------------- | ---- | ------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                          |
| mute       | boolean                             | 是   | 静音状态,true为静音,false为非静音。 |
| callback   | AsyncCallback&lt;void&gt;           | 是   | 回调表示成功还是失败。                |
863

Z
zengyawen 已提交
864 865 866
**示例:**

```
Z
zengyawen 已提交
867
var audioManager = audio.getAudioManager();
868 869 870 871 872 873 874 875 876
audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => {
    if (err) {
        console.error('Failed to mute the stream. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the stream is muted.');
})
```

Z
zengyawen 已提交
877
### mute
Z
zengyawen 已提交
878 879

mute(volumeType: AudioVolumeType, mute: boolean): Promise&lt;void&gt;
880

Z
zengyawen 已提交
881
设置指定音量流静音,使用Promise方式异步返回结果。
882

Z
zengyawen 已提交
883 884
**系统能力:** SystemCapability.Multimedia.Audio.Volume

885 886
**参数:**

Z
zengyawen 已提交
887 888 889 890
| 参数名     | 类型                                | 必填 | 说明                                  |
| ---------- | ----------------------------------- | ---- | ------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                          |
| mute       | boolean                             | 是   | 静音状态,true为静音,false为非静音。 |
891 892 893

**返回值:**

Z
zengyawen 已提交
894 895
| 类型                | 说明                          |
| ------------------- | ----------------------------- |
Z
zengyawen 已提交
896
| Promise&lt;void&gt; | Promise回调表示成功还是失败。 |
897 898 899

**示例:**

Z
zengyawen 已提交
900

901
```
Z
zengyawen 已提交
902
var audioManager = audio.getAudioManager();
A
AOL 已提交
903
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
904
    console.log('Promise returned to indicate that the stream is muted.');
A
AOL 已提交
905
})
906 907 908
```


Z
zengyawen 已提交
909
### isMute
910

Z
zengyawen 已提交
911
isMute(volumeType: AudioVolumeType, callback: AsyncCallback&lt;boolean&gt;): void
912

Z
zengyawen 已提交
913
获取指定音量流是否被静音,使用callback方式异步返回结果。
914

Z
zengyawen 已提交
915 916
**系统能力:** SystemCapability.Multimedia.Audio.Volume

Z
zengyawen 已提交
917
**参数:**
918

Z
zengyawen 已提交
919 920 921 922
| 参数名     | 类型                                | 必填 | 说明                                            |
| ---------- | ----------------------------------- | ---- | ----------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                    |
| callback   | AsyncCallback&lt;boolean&gt;        | 是   | 回调返回流静音状态,true为静音,false为非静音。 |
923 924 925 926

**示例:**

```
Z
zengyawen 已提交
927
var audioManager = audio.getAudioManager();
928
audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => {
M
mamingshuai 已提交
929
   if (err) {
930
	   console.error('Failed to obtain the mute status. ${err.message}');
M
mamingshuai 已提交
931 932
	   return;
   }
933
   console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value);
Z
zengyawen 已提交
934 935 936
})
```

Z
zengyawen 已提交
937

Z
zengyawen 已提交
938
### isMute
Z
zengyawen 已提交
939 940

isMute(volumeType: AudioVolumeType): Promise&lt;boolean&gt;
Z
zengyawen 已提交
941

Z
zengyawen 已提交
942
获取指定音量流是否被静音,使用Promise方式异步返回结果。
Z
zengyawen 已提交
943

Z
zengyawen 已提交
944 945
**系统能力:** SystemCapability.Multimedia.Audio.Volume

Z
zengyawen 已提交
946 947
**参数:**

Z
zengyawen 已提交
948 949 950
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
Z
zengyawen 已提交
951 952 953

**返回值:**

Z
zengyawen 已提交
954 955
| 类型                   | 说明                                                   |
| ---------------------- | ------------------------------------------------------ |
Z
zengyawen 已提交
956
| Promise&lt;boolean&gt; | Promise回调返回流静音状态,true为静音,false为非静音。 |
M
mamingshuai 已提交
957

Z
zengyawen 已提交
958 959 960
**示例:**

```
Z
zengyawen 已提交
961
var audioManager = audio.getAudioManager();
A
AOL 已提交
962
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => {
963
    console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value);
A
AOL 已提交
964
})
Z
zengyawen 已提交
965 966
```

Z
zengyawen 已提交
967
### isActive
Z
zengyawen 已提交
968 969

isActive(volumeType: AudioVolumeType, callback: AsyncCallback&lt;boolean&gt;): void
Z
zengyawen 已提交
970

Z
zengyawen 已提交
971
获取指定音量流是否为活跃状态,使用callback方式异步返回结果。
972

Z
zengyawen 已提交
973 974
**系统能力:** SystemCapability.Multimedia.Audio.Volume

975 976
**参数:**

Z
zengyawen 已提交
977 978 979 980
| 参数名     | 类型                                | 必填 | 说明                                              |
| ---------- | ----------------------------------- | ---- | ------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                      |
| callback   | AsyncCallback&lt;boolean&gt;        | 是   | 回调返回流的活跃状态,true为活跃,false为不活跃。 |
981 982 983 984

**示例:**

```
Z
zengyawen 已提交
985
var audioManager = audio.getAudioManager();
986 987 988 989 990 991 992 993 994
audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => {
    if (err) {
        console.error('Failed to obtain the active status of the stream. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value);
})
```

Z
zengyawen 已提交
995
### isActive
Z
zengyawen 已提交
996 997

isActive(volumeType: AudioVolumeType): Promise&lt;boolean&gt;
998

Z
zengyawen 已提交
999
获取指定音量流是否为活跃状态,使用Promise方式异步返回结果。
1000

Z
zengyawen 已提交
1001 1002
**系统能力:** SystemCapability.Multimedia.Audio.Volume

1003 1004
**参数:**

Z
zengyawen 已提交
1005 1006 1007
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
1008 1009 1010

**返回值:**

Z
zengyawen 已提交
1011 1012
| 类型                   | 说明                                                     |
| ---------------------- | -------------------------------------------------------- |
Z
zengyawen 已提交
1013
| Promise&lt;boolean&gt; | Promise回调返回流的活跃状态,true为活跃,false为不活跃。 |
Z
zengyawen 已提交
1014

1015 1016 1017
**示例:**

```
Z
zengyawen 已提交
1018
var audioManager = audio.getAudioManager();
A
AOL 已提交
1019
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => {
1020
    console.log('Promise returned to indicate that the active status of the stream is obtained.' + value);
A
AOL 已提交
1021
})
1022 1023
```

Z
zengyawen 已提交
1024
### setRingerMode
Z
zengyawen 已提交
1025 1026

setRingerMode(mode: AudioRingMode, callback: AsyncCallback&lt;void&gt;): void
1027

Z
zengyawen 已提交
1028
设置铃声模式,使用callback方式异步返回结果。
1029

Z
zengyawen 已提交
1030 1031
**系统能力:** SystemCapability.Multimedia.Audio.Communication

1032 1033
**参数:**

Z
zengyawen 已提交
1034 1035 1036 1037
| 参数名   | 类型                            | 必填 | 说明                     |
| -------- | ------------------------------- | ---- | ------------------------ |
| mode     | [AudioRingMode](#audioringmode) | 是   | 音频铃声模式。           |
| callback | AsyncCallback&lt;void&gt;       | 是   | 回调返回设置成功或失败。 |
1038 1039 1040 1041

**示例:**

```
Z
zengyawen 已提交
1042
var audioManager = audio.getAudioManager();
1043 1044
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => {
   if (err) {
Z
zengyawen 已提交
1045
       console.error('Failed to set the ringer mode. ${err.message}');
1046 1047 1048 1049 1050 1051
       return;
    }
    console.log('Callback invoked to indicate a successful setting of the ringer mode.');
})
```

Z
zengyawen 已提交
1052
### setRingerMode
Z
zengyawen 已提交
1053 1054

setRingerMode(mode: AudioRingMode): Promise&lt;void&gt;
1055

Z
zengyawen 已提交
1056
设置铃声模式,使用Promise方式异步返回结果。
1057

Z
zengyawen 已提交
1058 1059
**系统能力:** SystemCapability.Multimedia.Audio.Communication

1060 1061
**参数:**

Z
zengyawen 已提交
1062 1063 1064
| 参数名 | 类型                            | 必填 | 说明           |
| ------ | ------------------------------- | ---- | -------------- |
| mode   | [AudioRingMode](#audioringmode) | 是   | 音频铃声模式。 |
1065 1066 1067

**返回值:**

Z
zengyawen 已提交
1068 1069
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1070
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1071 1072 1073 1074

**示例:**

```
Z
zengyawen 已提交
1075
var audioManager = audio.getAudioManager();
A
AOL 已提交
1076
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => {
1077
    console.log('Promise returned to indicate a successful setting of the ringer mode.');
A
AOL 已提交
1078
})
1079 1080 1081
```


Z
zengyawen 已提交
1082
### getRingerMode
1083

Z
zengyawen 已提交
1084
getRingerMode(callback: AsyncCallback&lt;AudioRingMode&gt;): void
1085

Z
zengyawen 已提交
1086
获取铃声模式,使用callback方式异步返回结果。
1087

Z
zengyawen 已提交
1088 1089
**系统能力:** SystemCapability.Multimedia.Audio.Communication

Z
zengyawen 已提交
1090
**参数:**
1091

Z
zengyawen 已提交
1092 1093 1094
| 参数名   | 类型                                                 | 必填 | 说明                     |
| -------- | ---------------------------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;[AudioRingMode](#audioringmode)&gt; | 是   | 回调返回系统的铃声模式。 |
1095 1096 1097 1098

**示例:**

```
Z
zengyawen 已提交
1099
var audioManager = audio.getAudioManager();
1100 1101
audioManager.getRingerMode((err, value) => {
   if (err) {
Z
zengyawen 已提交
1102
	   console.error('Failed to obtain the ringer mode. ${err.message}');
1103 1104 1105 1106 1107 1108 1109
	   return;
   }
   console.log('Callback invoked to indicate that the ringer mode is obtained.' + value);
})
```


Z
zengyawen 已提交
1110
### getRingerMode
1111

Z
zengyawen 已提交
1112
getRingerMode(): Promise&lt;AudioRingMode&gt;
1113

Z
zengyawen 已提交
1114
获取铃声模式,使用Promise方式异步返回结果。
1115

Z
zengyawen 已提交
1116 1117
**系统能力:** SystemCapability.Multimedia.Audio.Communication

1118 1119
**返回值:**

Z
zengyawen 已提交
1120 1121
| 类型                                           | 说明                            |
| ---------------------------------------------- | ------------------------------- |
1122
| Promise&lt;[AudioRingMode](#audioringmode)&gt; | Promise回调返回系统的铃声模式。 |
1123 1124 1125 1126

**示例:**

```
Z
zengyawen 已提交
1127
var audioManager = audio.getAudioManager();
A
AOL 已提交
1128
audioManager.getRingerMode().then((value) => {
1129
    console.log('Promise returned to indicate that the ringer mode is obtained.' + value);
A
AOL 已提交
1130
})
1131 1132
```

Z
zengyawen 已提交
1133
### setAudioParameter
Z
zengyawen 已提交
1134 1135

setAudioParameter(key: string, value: string, callback: AsyncCallback&lt;void&gt;): void
1136

Z
zengyawen 已提交
1137
音频参数设置,使用callback方式异步返回结果。
1138

Z
zengyawen 已提交
1139 1140
**系统能力:** SystemCapability.Multimedia.Audio.Core

1141 1142
**参数:**

Z
zengyawen 已提交
1143 1144 1145 1146 1147
| 参数名   | 类型                      | 必填 | 说明                     |
| -------- | ------------------------- | ---- | ------------------------ |
| key      | string                    | 是   | 被设置的音频参数的键。   |
| value    | string                    | 是   | 被设置的音频参数的值。   |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回设置成功或失败。 |
1148 1149 1150 1151

**示例:**

```
Z
zengyawen 已提交
1152
var audioManager = audio.getAudioManager();
1153 1154 1155 1156 1157 1158 1159 1160 1161
audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => {
    if (err) {
        console.error('Failed to set the audio parameter. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate a successful setting of the audio parameter.');
})
```

Z
zengyawen 已提交
1162
### setAudioParameter
Z
zengyawen 已提交
1163 1164

setAudioParameter(key: string, value: string): Promise&lt;void&gt;
1165

Z
zengyawen 已提交
1166
音频参数设置,使用Promise方式异步返回结果。
1167

Z
zengyawen 已提交
1168 1169
**系统能力:** SystemCapability.Multimedia.Audio.Core

1170 1171
**参数:**

Z
zengyawen 已提交
1172 1173 1174 1175
| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| key    | string | 是   | 被设置的音频参数的键。 |
| value  | string | 是   | 被设置的音频参数的值。 |
1176 1177 1178

**返回值:**

Z
zengyawen 已提交
1179 1180
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1181
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1182 1183 1184 1185

**示例:**

```
Z
zengyawen 已提交
1186
var audioManager = audio.getAudioManager();
A
AOL 已提交
1187
audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => {
1188
    console.log('Promise returned to indicate a successful setting of the audio parameter.');
A
AOL 已提交
1189
})
1190 1191
```

Z
zengyawen 已提交
1192
### getAudioParameter
Z
zengyawen 已提交
1193 1194

getAudioParameter(key: string, callback: AsyncCallback&lt;string&gt;): void
1195

Z
zengyawen 已提交
1196
获取指定音频参数值,使用callback方式异步返回结果。
1197

Z
zengyawen 已提交
1198 1199
**系统能力:** SystemCapability.Multimedia.Audio.Core

1200 1201
**参数:**

Z
zengyawen 已提交
1202 1203 1204 1205
| 参数名   | 类型                        | 必填 | 说明                         |
| -------- | --------------------------- | ---- | ---------------------------- |
| key      | string                      | 是   | 待获取的音频参数的键。       |
| callback | AsyncCallback&lt;string&gt; | 是   | 回调返回获取的音频参数的值。 |
1206 1207 1208 1209

**示例:**

```
Z
zengyawen 已提交
1210
var audioManager = audio.getAudioManager();
1211 1212 1213 1214 1215 1216 1217 1218 1219
audioManager.getAudioParameter('PBits per sample', (err, value) => {
    if (err) {
        console.error('Failed to obtain the value of the audio parameter. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the value of the audio parameter is obtained.' + value);
})
```

Z
zengyawen 已提交
1220
### getAudioParameter
Z
zengyawen 已提交
1221 1222

getAudioParameter(key: string): Promise&lt;string&gt;
1223

Z
zengyawen 已提交
1224
获取指定音频参数值,使用Promise方式异步返回结果。
1225

Z
zengyawen 已提交
1226 1227
**系统能力:** SystemCapability.Multimedia.Audio.Core

1228 1229
**参数:**

Z
zengyawen 已提交
1230 1231 1232
| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| key    | string | 是   | 待获取的音频参数的键。 |
1233 1234 1235

**返回值:**

Z
zengyawen 已提交
1236 1237
| 类型                  | 说明                                |
| --------------------- | ----------------------------------- |
Z
zengyawen 已提交
1238
| Promise&lt;string&gt; | Promise回调返回获取的音频参数的值。 |
1239 1240 1241 1242

**示例:**

```
Z
zengyawen 已提交
1243
var audioManager = audio.getAudioManager();
A
AOL 已提交
1244
audioManager.getAudioParameter('PBits per sample').then((value) => {
1245
    console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value);
A
AOL 已提交
1246
})
1247 1248
```

Z
zengyawen 已提交
1249 1250 1251
### getDevices

getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback&lt;AudioDeviceDescriptors&gt;): void
1252

Z
zengyawen 已提交
1253
获取音频设备列表,使用callback方式异步返回结果。
1254

Z
zengyawen 已提交
1255 1256
**系统能力:** SystemCapability.Multimedia.Audio.Device

1257 1258
**参数:**

Z
zengyawen 已提交
1259 1260 1261 1262
| 参数名     | 类型                                                         | 必填 | 说明                 |
| ---------- | ------------------------------------------------------------ | ---- | -------------------- |
| deviceFlag | [DeviceFlag](#deviceflag)                                    | 是   | 设备类型的flag。     |
| callback   | AsyncCallback&lt;[AudioDeviceDescriptors](#audiodevicedescriptors)&gt; | 是   | 回调,返回设备列表。 |
1263 1264 1265

**示例:**
```
Z
zengyawen 已提交
1266
var audioManager = audio.getAudioManager();
A
AOL 已提交
1267
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value) => {
1268 1269 1270 1271 1272 1273 1274 1275
   if (err) {
	   console.error('Failed to obtain the device list. ${err.message}');
	   return;
   }
   console.log('Callback invoked to indicate that the device list is obtained.');
})
```

Z
zengyawen 已提交
1276 1277
### getDevices

A
AOL 已提交
1278
getDevices(deviceFlag: DeviceFlag): Promise&lt;AudioDeviceDescriptors&gt;
1279

Z
zengyawen 已提交
1280
获取音频设备列表,使用Promise方式异步返回结果。
1281

Z
zengyawen 已提交
1282 1283
**系统能力:** SystemCapability.Multimedia.Audio.Device

1284 1285
**参数:**

Z
zengyawen 已提交
1286 1287 1288
| 参数名     | 类型                      | 必填 | 说明             |
| ---------- | ------------------------- | ---- | ---------------- |
| deviceFlag | [DeviceFlag](#deviceflag) | 是   | 设备类型的flag。 |
1289 1290 1291

**返回值:**

Z
zengyawen 已提交
1292 1293
| 类型                                                         | 说明                      |
| ------------------------------------------------------------ | ------------------------- |
Z
zengyawen 已提交
1294
| Promise&lt;[AudioDeviceDescriptors](#audiodevicedescriptors)&gt; | Promise回调返回设备列表。 |
1295 1296 1297 1298

**示例:**

```
Z
zengyawen 已提交
1299
var audioManager = audio.getAudioManager();
A
AOL 已提交
1300
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
1301
    console.log('Promise returned to indicate that the device list is obtained.');
A
AOL 已提交
1302
})
1303 1304
```

Z
zengyawen 已提交
1305
### setDeviceActive
Z
zengyawen 已提交
1306

A
AOL 已提交
1307
setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback&lt;void&gt;): void
1308

Z
zengyawen 已提交
1309
设置设备激活状态,使用callback方式异步返回结果。
1310

Z
zengyawen 已提交
1311 1312
**系统能力:** SystemCapability.Multimedia.Audio.Device

1313 1314
**参数:**

Z
zengyawen 已提交
1315 1316
| 参数名     | 类型                      | 必填 | 说明                     |
| ---------- | ------------------------- | ---- | ------------------------ |
A
AOL 已提交
1317
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。           |
Z
zengyawen 已提交
1318 1319
| active     | boolean                   | 是   | 设备激活状态。           |
| callback   | AsyncCallback&lt;void&gt; | 是   | 回调返回设置成功或失败。 |
1320 1321 1322 1323

**示例:**

```
Z
zengyawen 已提交
1324
var audioManager = audio.getAudioManager();
A
AOL 已提交
1325
audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true, (err) => {
1326 1327 1328 1329 1330 1331 1332 1333
    if (err) {
        console.error('Failed to set the active status of the device. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the device is set to the active status.');
})
```

Z
zengyawen 已提交
1334
### setDeviceActive
Z
zengyawen 已提交
1335

A
AOL 已提交
1336
setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise&lt;void&gt;
1337

Z
zengyawen 已提交
1338
设置设备激活状态,使用Promise方式异步返回结果。
1339

Z
zengyawen 已提交
1340 1341
**系统能力:** SystemCapability.Multimedia.Audio.Device

1342 1343
**参数:**

Z
zengyawen 已提交
1344 1345
| 参数名     | 类型                      | 必填 | 说明           |
| ---------- | ------------------------- | ---- | -------------- |
A
AOL 已提交
1346
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。 |
Z
zengyawen 已提交
1347
| active     | boolean                   | 是   | 设备激活状态。 |
1348 1349 1350

**返回值:**

Z
zengyawen 已提交
1351 1352
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1353
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1354 1355 1356

**示例:**

Z
zengyawen 已提交
1357

1358
```
Z
zengyawen 已提交
1359
var audioManager = audio.getAudioManager();
A
AOL 已提交
1360
audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(() => {
1361
    console.log('Promise returned to indicate that the device is set to the active status.');
A
AOL 已提交
1362
})
1363 1364
```

Z
zengyawen 已提交
1365
### isDeviceActive
Z
zengyawen 已提交
1366

A
AOL 已提交
1367
isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback&lt;boolean&gt;): void
1368

Z
zengyawen 已提交
1369
获取指定设备的激活状态,使用callback方式异步返回结果。
1370

Z
zengyawen 已提交
1371 1372
**系统能力:** SystemCapability.Multimedia.Audio.Device

1373 1374
**参数:**

Z
zengyawen 已提交
1375 1376
| 参数名     | 类型                         | 必填 | 说明                     |
| ---------- | ---------------------------- | ---- | ------------------------ |
A
AOL 已提交
1377
| deviceType | [ActiveDeviceType](#activedevicetype)    | 是   | 活跃音频设备类型。 |
Z
zengyawen 已提交
1378
| callback   | AsyncCallback&lt;boolean&gt; | 是   | 回调返回设备的激活状态。 |
1379 1380 1381 1382

**示例:**

```
Z
zengyawen 已提交
1383
var audioManager = audio.getAudioManager();
Z
zengyawen 已提交
1384
audioManager.isDeviceActive(audio.DeviceType.SPEAKER, (err, value) => {
1385 1386 1387 1388 1389 1390 1391 1392
    if (err) {
        console.error('Failed to obtain the active status of the device. ${err.message}');
	return;
    }
    console.log('Callback invoked to indicate that the active status of the device is obtained.');
})
```

Z
zengyawen 已提交
1393

Z
zengyawen 已提交
1394
### isDeviceActive
Z
zengyawen 已提交
1395

A
AOL 已提交
1396
isDeviceActive(deviceType: ActiveDeviceType): Promise&lt;boolean&gt;
1397

Z
zengyawen 已提交
1398
获取指定设备的激活状态,使用Promise方式异步返回结果。
1399

Z
zengyawen 已提交
1400 1401
**系统能力:** SystemCapability.Multimedia.Audio.Device

1402 1403
**参数:**

Z
zengyawen 已提交
1404 1405
| 参数名     | 类型                      | 必填 | 说明           |
| ---------- | ------------------------- | ---- | -------------- |
A
AOL 已提交
1406
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。 |
1407 1408 1409

**返回值:**

Z
zengyawen 已提交
1410 1411
| Type                   | Description                     |
| ---------------------- | ------------------------------- |
Z
zengyawen 已提交
1412
| Promise&lt;boolean&gt; | Promise回调返回设备的激活状态。 |
1413 1414 1415 1416

**示例:**

```
Z
zengyawen 已提交
1417
var audioManager = audio.getAudioManager();
A
AOL 已提交
1418
audioManager.isDeviceActive(audio.DeviceType.SPEAKER).then((value) => {
1419
    console.log('Promise returned to indicate that the active status of the device is obtained.' + value);
A
AOL 已提交
1420
})
1421 1422
```

Z
zengyawen 已提交
1423
### setMicrophoneMute
Z
zengyawen 已提交
1424 1425

setMicrophoneMute(mute: boolean, callback: AsyncCallback&lt;void&gt;): void
1426

Z
zengyawen 已提交
1427
设置麦克风静音状态,使用callback方式异步返回结果。
1428

Z
zengyawen 已提交
1429 1430
**系统能力:** SystemCapability.Multimedia.Audio.Device

1431 1432
**参数:**

Z
zengyawen 已提交
1433 1434 1435 1436
| 参数名   | 类型                      | 必填 | 说明                                          |
| -------- | ------------------------- | ---- | --------------------------------------------- |
| mute     | boolean                   | 是   | 待设置的静音状态,true为静音,false为非静音。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回设置成功或失败。                      |
1437 1438 1439 1440

**示例:**

```
Z
zengyawen 已提交
1441
var audioManager = audio.getAudioManager();
1442 1443 1444 1445 1446 1447 1448 1449 1450
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.');
})
```

Z
zengyawen 已提交
1451
### setMicrophoneMute
Z
zengyawen 已提交
1452 1453

setMicrophoneMute(mute: boolean): Promise&lt;void&gt;
1454

Z
zengyawen 已提交
1455
设置麦克风静音状态,使用Promise方式异步返回结果。
1456

Z
zengyawen 已提交
1457 1458
**系统能力:** SystemCapability.Multimedia.Audio.Device

1459 1460
**参数:**

Z
zengyawen 已提交
1461 1462 1463
| 参数名 | 类型    | 必填 | 说明                                          |
| ------ | ------- | ---- | --------------------------------------------- |
| mute   | boolean | 是   | 待设置的静音状态,true为静音,false为非静音。 |
1464 1465 1466

**返回值:**

Z
zengyawen 已提交
1467 1468
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1469
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1470 1471 1472 1473

**示例:**

```
Z
zengyawen 已提交
1474
var audioManager = audio.getAudioManager();
A
AOL 已提交
1475
audioManager.setMicrophoneMute(true).then(() => {
1476
    console.log('Promise returned to indicate that the microphone is muted.');
A
AOL 已提交
1477
})
1478 1479
```

Z
zengyawen 已提交
1480
### isMicrophoneMute
Z
zengyawen 已提交
1481 1482

isMicrophoneMute(callback: AsyncCallback&lt;boolean&gt;): void
1483

Z
zengyawen 已提交
1484
获取麦克风静音状态,使用callback方式异步返回结果。
1485

Z
zengyawen 已提交
1486 1487
**系统能力:** SystemCapability.Multimedia.Audio.Device

1488 1489
**参数:**

Z
zengyawen 已提交
1490 1491 1492
| 参数名   | 类型                         | 必填 | 说明                                                    |
| -------- | ---------------------------- | ---- | ------------------------------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调返回系统麦克风静音状态,true为静音,false为非静音。 |
1493 1494 1495 1496

**示例:**

```
Z
zengyawen 已提交
1497
var audioManager = audio.getAudioManager();
1498 1499 1500 1501 1502 1503 1504 1505 1506
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);
})
```

Z
zengyawen 已提交
1507
### isMicrophoneMute
1508

Z
zengyawen 已提交
1509
isMicrophoneMute(): Promise&lt;boolean&gt;
1510

Z
zengyawen 已提交
1511
获取麦克风静音状态,使用Promise方式异步返回结果。
1512

Z
zengyawen 已提交
1513 1514
**系统能力:** SystemCapability.Multimedia.Audio.Device

1515 1516
**返回值:**

Z
zengyawen 已提交
1517 1518
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
Z
zengyawen 已提交
1519
| Promise&lt;boolean&gt; | Promise回调返回系统麦克风静音状态,true为静音,false为非静音。 |
1520 1521 1522

**示例:**

Z
zengyawen 已提交
1523

1524
```
Z
zengyawen 已提交
1525
var audioManager = audio.getAudioManager();
A
AOL 已提交
1526
audioManager.isMicrophoneMute().then((value) => {
1527
    console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
A
AOL 已提交
1528
})
1529 1530
```

L
lwx1059628 已提交
1531
### on('volumeChange')<sup>8+</sup>
Z
zengyawen 已提交
1532 1533 1534 1535 1536

on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void

监听系统音量变化事件。

L
lwx1059628 已提交
1537 1538
此接口为系统接口,三方应用不支持。

Z
zengyawen 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
**系统能力:** SystemCapability.Multimedia.Audio.Volume

**参数:**

| 参数名   | 类型                                   | 必填 | 说明                                                         |
| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                 | 是   | 事件回调类型,支持的事件为:'volumeChange'(系统音量变化事件,检测到系统音量改变时,触发该事件)。 |
| callback | Callback<[VolumeEvent](#volumeevent8)> | 是   | 回调方法。                                                   |

**示例:**

```
audioManager.on('volumeChange', (volumeEvent) => {
    console.log('VolumeType of stream: ' + volumeEvent.volumeType);
    console.log('Volume level: ' + volumeEvent.volume);
    console.log('Whether to updateUI: ' + volumeEvent.updateUi);
})
```

L
lwx1059628 已提交
1558
### on('ringerModeChange')<sup>8+</sup>
Z
zengyawen 已提交
1559

A
AOL 已提交
1560
on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void
Z
zengyawen 已提交
1561 1562 1563

监听铃声模式变化事件。

L
lwx1059628 已提交
1564 1565
此接口为系统接口,三方应用不支持。

Z
zengyawen 已提交
1566 1567 1568 1569 1570 1571 1572 1573
**系统能力:** SystemCapability.Multimedia.Audio.Communication

**参数:**

| 参数名   | 类型                                      | 必填 | 说明                                                         |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                    | 是   | 事件回调类型,支持的事件为:'ringerModeChange'(铃声模式变化事件,检测到铃声模式改变时,触发该事件)。 |
| callback | Callback<[AudioRingMode](#audioringmode)> | 是   | 回调方法。                                                   |
Z
zengyawen 已提交
1574

L
lwx1059628 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
### on('deviceChange')

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

设备更改。音频设备连接状态变化。

**系统能力:** SystemCapability.Multimedia.Audio.Device

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                                       |
| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- |
| type     | string                                               | 是   | 订阅的事件的类型。支持事件:'deviceChange' |
| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)\> | 是   | 获取设备更新详情。                         |

**示例:**

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

### off('deviceChange')

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

取消订阅音频设备连接变化事件。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** SystemCapability.Multimedia.Audio.Device

**参数:**

| 参数名   | 类型                                                | 必填 | 说明                                       |
| -------- | --------------------------------------------------- | ---- | ------------------------------------------ |
| type     | string                                              | 是   | 订阅的事件的类型。支持事件:'deviceChange' |
| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)> | 否   | 获取设备更新详情。                         |

**示例:**

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

1626 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
### on('interrupt')

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

请求焦点并开始监听音频打断事件(当应用程序的音频被另一个播放事件中断,回调通知此应用程序)

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                   | 必填 | 说明                                                         |
| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
| type      | string                         | 是   | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 |
| interrupt | AudioInterrupt                 | 是   | 音频打断事件类型的参数。 |
| callback  | Callback<[InterruptAction](#interruptaction)> | 是   | 音频打断事件回调方法。 |

**示例:**

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

取消监听音频打断事件(删除监听事件,取消打断)

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                   | 必填 | 说明                                                         |
| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
| type      | string                         | 是   | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 |
| interrupt | AudioInterrupt                 | 是   | 音频打断事件类型的参数。 |
| callback  | Callback<[InterruptAction](#interruptaction)> | 否   | 音频打断事件回调方法。 |

**示例:**

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

L
lwx1059628 已提交
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 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
### setAudioScene<sup>8+</sup>

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

设置音频场景模式,使用callback方式异步返回结果。

此接口为系统接口,三方应用不支持。

**系统能力:** SystemCapability.Multimedia.Audio.Communication

**参数:**

| 参数名   | 类型                                 | 必填 | 说明                 |
| :------- | :----------------------------------- | :--- | :------------------- |
| scene    | <a href="#audioscene">AudioScene</a> | 是   | 音频场景模式。       |
| callback | AsyncCallback<void\>                 | 是   | 用于返回结果的回调。 |

**示例:**

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

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

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

设置音频场景模式,使用Promise方式返回异步结果。

此接口为系统接口,三方应用不支持。

**系统能力:**: SystemCapability.Multimedia.Audio.Communication

**Parameters**

| 参数名 | 类型                                 | 必填 | 说明           |
| :----- | :----------------------------------- | :--- | :------------- |
| scene  | <a href="#audioscene">AudioScene</a> | 是   | 音频场景模式。 |

**返回值:**

| 类型           | 说明                 |
| :------------- | :------------------- |
| Promise<void\> | 用于返回结果的回调。 |

**示例:**

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

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

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

获取音频场景模式,使用callback方式返回异步结果。

**系统能力:**SystemCapability.Multimedia.Audio.Communication

**参数:**

| 参数名   | 类型                                                | 必填 | 说明                         |
| :------- | :-------------------------------------------------- | :--- | :--------------------------- |
| callback | AsyncCallback<<a href="#audioscene">AudioScene</a>> | 是   | 用于返回音频场景模式的回调。 |

**示例:**

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


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

getAudioScene\(\): Promise<AudioScene\>

获取音频场景模式,使用Promise方式返回异步结果。

**系统能力:** SystemCapability.Multimedia.Audio.Communication

**返回值:**

| 类型                                          | 说明                         |
| :-------------------------------------------- | :--------------------------- |
| Promise<<a href="#audioscene">AudioScene</a>> | 用于返回音频场景模式的回调。 |

**示例:**

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

Z
zengyawen 已提交
1810
## AudioDeviceDescriptor
1811 1812 1813

描述音频设备。

Z
zengyawen 已提交
1814
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device
Z
zengyawen 已提交
1815

Z
zengyawen 已提交
1816 1817 1818 1819
| 名称       | 参数型                    | 可读 | 可写 | 说明       |
| ---------- | ------------------------- | ---- | ---- | ---------- |
| deviceRole | [DeviceRole](#devicerole) | 是   | 否   | 设备角色。 |
| deviceType | [DeviceType](#devicetype) | 是   | 否   | 设备类型。 |
Z
zengyawen 已提交
1820 1821

## AudioDeviceDescriptors
M
mamingshuai 已提交
1822

Z
zengyawen 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

| 名称         | 描述                                |
| ------------ | ----------------------------------- |
| 设备属性数组 | AudioDeviceDescriptor的数组,只读。 |

**示例:**

```
function deviceProp(audioDeviceDescriptor, index, array) {
    deviceRoleValue = audioDeviceDescriptor.deviceRole;
    deviceTypeValue = audioDeviceDescriptor.deviceType;
}

deviceRoleValue = null;
deviceTypeValue = null;
const promise = audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG);
promise.then(async function (audioDeviceDescriptors) {
    console.info('getDevices OUTPUT_DEVICES_FLAG');
    audioDeviceDescriptors.forEach(deviceProp);
    if (deviceTypeValue != null && deviceRoleValue != null){
        console.info('OUTPUT_DEVICES_FLAG : Pass');
            expect(true).assertTrue();
    }
    else{
        console.error('OUTPUT_DEVICES_FLAG : fail');
        expect(false).assertTrue();
        }
    });
    await promise;
    done();
})
```

## AudioRenderer<sup>8+</sup>

L
lwx1059628 已提交
1859 1860 1861 1862 1863 1864 1865 1866 1867
提供音频渲染的相关接口。在调用AudioRenderer的接口前,需要先通过[createAudioRenderer](#audiocreateaudiorenderer8)创建实例。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

### state<sup>8+</sup>

只读 state: AudioState 

定义当前渲染器的状态。
Z
zengyawen 已提交
1868

L
lwx1059628 已提交
1869
**参数:**
Z
zengyawen 已提交
1870

L
lwx1059628 已提交
1871
| 名称  | 参数型                     | 可读 | 可写 | 说明               |
Z
zengyawen 已提交
1872
| ----- | -------------------------- | ---- | ---- | ------------------ |
L
lwx1059628 已提交
1873
| state | [AudioState](#audiostate8) | 是   | 否   | 音频渲染器的状态。 |
Z
zengyawen 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884

**示例:**

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

### getRendererInfo<sup>8+</sup>

getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void

L
lwx1059628 已提交
1885
获取当前被创建的音频渲染器的信息,使用callback方式异步返回结果。
Z
zengyawen 已提交
1886 1887 1888 1889 1890

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

L
lwx1059628 已提交
1891 1892 1893
| 参数名   | 类型                                                     | 必填 | 说明                   |
| :------- | :------------------------------------------------------- | :--- | :--------------------- |
| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是   | 返回音频渲染器的信息。 |
Z
zengyawen 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909

**示例:**

```
audioRenderer.getRendererInfo((err, rendererInfo)=>{
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
})
```

### getRendererInfo<sup>8+</sup>

getRendererInfo(): Promise<AudioRendererInfo\>

L
lwx1059628 已提交
1910
获取当前被创建的音频渲染器的信息,使用Promise方式异步返回结果。
Z
zengyawen 已提交
1911 1912 1913 1914 1915 1916 1917

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型                                               | 说明                            |
| -------------------------------------------------- | ------------------------------- |
L
lwx1059628 已提交
1918
| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise用于返回音频渲染器信息。 |
Z
zengyawen 已提交
1919 1920 1921 1922 1923 1924 1925

**示例:**

```
let streamInfo = await audioRenderer.getStreamInfo();
console.log('Renderer GetStreamInfo:');
console.log('Renderer sampling rate:' + streamInfo.samplingRate);
L
lwx1059628 已提交
1926 1927 1928
console.log('Renderer channel:' + streamInfo.channels);
console.log('Renderer format:' + streamInfo.sampleFormat);
console.log('Renderer encoding type:' + streamInfo.encodingType);
Z
zengyawen 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
```

### getStreamInfo<sup>8+</sup>

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

获取音频流信息,使用callback方式异步返回结果。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                 |
| :------- | :--------------------------------------------------- | :--- | :------------------- |
| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是   | 回调返回音频流信息。 |

**示例:**

```
audioRenderer.getStreamInfo((err, streamInfo)=>{
    console.log('Renderer GetStreamInfo:');
    console.log('Renderer sampling rate:' + streamInfo.samplingRate);
L
lwx1059628 已提交
1951 1952 1953
    console.log('Renderer channel:' + streamInfo.channels);
    console.log('Renderer format:' + streamInfo.sampleFormat);
    console.log('Renderer encoding type:' + streamInfo.encodingType);
Z
zengyawen 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
})
```

### getStreamInfo<sup>8+</sup>

getStreamInfo(): Promise<AudioStreamInfo\>

获取音频流信息,使用Promise方式异步返回结果。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型                                           | 说明                   |
| :--------------------------------------------- | :--------------------- |
| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise返回音频流信息. |

**示例:**

```
let streamInfo = await audioRenderer.getStreamInfo();
console.log('Renderer GetStreamInfo:');
console.log('Renderer sampling rate:' + streamInfo.samplingRate);
console.log('Renderer channel:' + streamInfo.AudioChannel);
console.log('Renderer format:' + streamInfo.AudioSampleFormat);
console.log('Renderer encoding type:' + streamInfo.AudioEncodingType);
```

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

start(callback: AsyncCallback<void\>): void

L
lwx1059628 已提交
1986
启动音频渲染器。使用callback方式异步返回结果。
Z
zengyawen 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |

**示例:**

```
audioRenderer.start((err)=>{
    if (err) {
        console.error('Renderer start failed.');
    } else {
        console.info('Renderer start success.');
    }
})
```

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

start(): Promise<void\>

L
lwx1059628 已提交
2012
启动音频渲染器。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise方式异步返回结果。 |

**示例:**

```
await audioRenderer.start();
```

### pause<sup>8+</sup>

pause(callback: AsyncCallback\<void>): void

L
lwx1059628 已提交
2032
暂停渲染。使用callback方式异步返回结果。
Z
zengyawen 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                 | 必填 | 说明             |
| -------- | -------------------- | ---- | ---------------- |
| callback | AsyncCallback\<void> | 是   | 返回回调的结果。 |

**示例:**

```
audioRenderer.pause((err)=>{
    if (err) {
        console.error('Renderer pause failed');
    } else {
        console.log('Renderer paused.');
    }
})
```

### pause<sup>8+</sup>

pause(): Promise\<void>

L
lwx1059628 已提交
2058
暂停渲染。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise方式异步返回结果。 |

**示例:**

```
await audioRenderer.pause();
```

### drain<sup>8+</sup>

drain(callback: AsyncCallback\<void>): void

L
lwx1059628 已提交
2078
检查缓冲区是否已被耗尽。使用callback方式异步返回结果。
Z
zengyawen 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                 | 必填 | 说明             |
| -------- | -------------------- | ---- | ---------------- |
| callback | AsyncCallback\<void> | 是   | 返回回调的结果。 |

**示例:**

```
audioRenderer.drain((err)=>{
    if (err) {
        console.error('Renderer drain failed');
    } else {
        console.log('Renderer drained.');
    }
})
```

### drain<sup>8+</sup>

drain(): Promise\<void>

L
lwx1059628 已提交
2104
检查缓冲区是否已被耗尽。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise方式异步返回结果。 |

**示例:**

```
await audioRenderer.drain();
```

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

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

L
lwx1059628 已提交
2124
停止渲染。使用callback方式异步返回结果。
Z
zengyawen 已提交
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                 | 必填 | 说明             |
| -------- | -------------------- | ---- | ---------------- |
| callback | AsyncCallback\<void> | 是   | 返回回调的结果。 |

**示例:**

```
audioRenderer.stop((err)=>{
    if (err) {
        console.error('Renderer stop failed');
    } else {
        console.log('Renderer stopped.');
    }
})
```

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

stop(): Promise\<void>

L
lwx1059628 已提交
2150
停止渲染。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise方式异步返回结果。 |

**示例:**

```
await audioRenderer.stop();
```

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

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

L
lwx1059628 已提交
2170
释放音频渲染器。使用callback方式异步返回结果。
Z
zengyawen 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                 | 必填 | 说明             |
| -------- | -------------------- | ---- | ---------------- |
| callback | AsyncCallback\<void> | 是   | 返回回调的结果。 |

**示例:**

```
audioRenderer.release((err)=>{
    if (err) {
        console.error('Renderer release failed');
    } else {
        console.log('Renderer released.');
    }
})
```

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

release(): Promise\<void>

释放渲染器。使用Promise方式异步返回结果。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise方式异步返回结果。 |

**示例:**

```
await audioRenderer.release();
```

### write<sup>8+</sup>

write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void

写入缓冲区。使用callback方式异步返回结果。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                                |
| -------- | ---------------------- | ---- | --------------------------------------------------- |
| buffer   | ArrayBuffer            | 是   | 要写入缓冲区的数据。                                |
| callback | AsyncCallback\<number> | 是   | 回调如果成功,返回写入的字节数,否则返回errorcode。 |

**示例:**

```
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
audioRenderer.write(buf, (err, writtenbytes)=>{
    if (writtenbytes < 0) {
	    console.error('write failed.');
    } else {
       console.log('Actual written bytes: ' + writtenbytes);
    }
})
```

### write<sup>8+</sup>

write(buffer: ArrayBuffer): Promise\<number>

写入缓冲区。使用Promise方式异步返回结果。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型             | 说明                                                         |
| ---------------- | ------------------------------------------------------------ |
| Promise\<number> | Promise返回结果,如果成功,返回写入的字节数,否则返回errorcode。 |

**示例:**

```
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
let writtenbytes = await audioRenderer.write(buf);
if (writtenbytes < 0) {
	console.error('write failed.');
} else {
    console.log('Actual written bytes: ' + writtenbytes);
}
```

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

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

L
lwx1059628 已提交
2274
获取时间戳(从 1970 年 1 月 1 日开始)。使用callback方式异步返回结果。
Z
zengyawen 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                   | 必填 | 说明             |
| -------- | ---------------------- | ---- | ---------------- |
| callback | AsyncCallback\<number> | 是   | 回调返回时间戳。 |

**示例:**

```
audioRenderer.getAudioTime((err, timestamp)=>{
    console.log('Current timestamp: ' + timestamp);
})
```

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

getAudioTime(): Promise\<number>

L
lwx1059628 已提交
2296
获取时间戳(从 1970 年 1 月 1 日开始)。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型             | 描述                    |
| ---------------- | ----------------------- |
| Promise\<number> | Promise回调返回时间戳。 |

**示例:**

```
let timestamp = await audioRenderer.getAudioTime();
console.log('Current timestamp: ' + timestamp);
```

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

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

L
lwx1059628 已提交
2317
获取音频渲染器的最小缓冲区大小。使用callback方式异步返回结果。
Z
zengyawen 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback\<number> | 是   | 回调返回缓冲区大小。 |

**示例:**

```
audioRenderer.getBufferSize((err, bufferSize)=>{
    if (err) {
        console.error('getBufferSize error');
    }
})
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
```

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

getBufferSize(): Promise\<number>

L
lwx1059628 已提交
2343
获取音频渲染器的最小缓冲区大小。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型             | 说明                        |
| ---------------- | --------------------------- |
| Promise\<number> | promise回调返回缓冲区大小。 |

**示例:**

```
var bufferSize = await audioRenderer.getBufferSize();
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
```

### setRenderRate<sup>8+</sup>

setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void

L
lwx1059628 已提交
2365
设置音频渲染速率。使用callback方式异步返回结果。
Z
zengyawen 已提交
2366 2367 2368 2369 2370 2371 2372

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                     | 必填 | 说明                     |
| -------- | ---------------------------------------- | ---- | ------------------------ |
L
lwx1059628 已提交
2373
| rate     | [AudioRendererRate](#audiorendererrate8) | 是   | 渲染的速率。             |
Z
zengyawen 已提交
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
| callback | AsyncCallback\<void>                     | 是   | 用于返回执行结果的回调。 |

**示例:**

```
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err)=> {
    if (err) {
	    console.error('Failed to set params');
    } else {
        console.log('Callback invoked to indicate a successful render rate setting.');
    }
})
```

### setRenderRate<sup>8+</sup>

setRenderRate(rate: AudioRendererRate): Promise\<void>

L
lwx1059628 已提交
2392
设置音频渲染速率。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2393 2394 2395 2396 2397 2398 2399

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名 | 类型                                     | 必填 | 说明         |
| ------ | ---------------------------------------- | ---- | ------------ |
L
lwx1059628 已提交
2400
| rate   | [AudioRendererRate](#audiorendererrate8) | 是   | 渲染的速率。 |
Z
zengyawen 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

**返回值:**

| 类型           | 说明                      |
| -------------- | ------------------------- |
| Promise\<void> | Promise用于返回执行结果。 |

**示例:**

```
await audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL);
```

### getRenderRate<sup>8+</sup>

getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void

L
lwx1059628 已提交
2418
获取当前渲染速率。使用callback方式异步返回结果。
Z
zengyawen 已提交
2419 2420 2421 2422 2423 2424 2425

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                                    | 必填 | 说明               |
| -------- | ------------------------------------------------------- | ---- | ------------------ |
L
lwx1059628 已提交
2426
| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是   | 回调返回渲染速率。 |
Z
zengyawen 已提交
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439

**示例:**

```
audioRenderer.getRenderRate((err, renderrate)=>{
    console.log('getRenderRate: ' + renderrate);
})
```

### getRenderRate<sup>8+</sup>

getRenderRate(): Promise\<AudioRendererRate>

L
lwx1059628 已提交
2440
获取当前渲染速率。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2441 2442 2443 2444 2445 2446 2447

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**返回值:**

| 类型                                              | 说明                      |
| ------------------------------------------------- | ------------------------- |
L
lwx1059628 已提交
2448
| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise回调返回渲染速率。 |
Z
zengyawen 已提交
2449 2450 2451 2452 2453 2454 2455 2456

**示例:**

```
let renderRate = await audioRenderer.getRenderRate();
console.log('getRenderRate: ' + renderrate);
```

L
lwx1059628 已提交
2457
### on('interrupt')<sup>9+</sup>
Z
zengyawen 已提交
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469

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

监听音频中断事件。使用callback获取中断事件。

**系统能力**: SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                       | 是   | 事件回调类型,支持的事件为:'interrupt'(中断事件被触发,音频播放被中断。) |
L
lwx1059628 已提交
2470
| callback | Callback<[InterruptEvent](#interruptevent9)> | 是   | 被监听的中断事件的回调。                                     |
Z
zengyawen 已提交
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501

**示例:**

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

L
lwx1059628 已提交
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
### on('markReach')<sup>8+</sup>

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

订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,回调被调用。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                      |
| :------- | :----------------------- | :--- | :---------------------------------------- |
| type     | string                   | 是   | 事件回调类型,支持的事件为:'markReach'。 |
| frame    | number                   | 是   | 触发事件的帧数。 该值必须大于 0。         |
| callback | (position: number) => {} | 是   | 触发事件时调用的回调。                    |

**示例:**

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


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

off(type: 'markReach'): void

取消订阅标记事件。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名 | 类型   | 必填 | 说明                                              |
| :----- | :----- | :--- | :------------------------------------------------ |
| type   | string | 是   | 要取消订阅事件的类型。支持的事件为:'markReach'。 |

**示例:**

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

### on('periodReach') <sup>8+</sup>
Z
zengyawen 已提交
2550

L
lwx1059628 已提交
2551
on(type: "periodReach", frame: number, callback: (position: number) => {}): void
Z
zengyawen 已提交
2552

L
lwx1059628 已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,回调被循环调用。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                        |
| :------- | :----------------------- | :--- | :------------------------------------------ |
| type     | string                   | 是   | 事件回调类型,支持的事件为:'periodReach'。 |
| frame    | number                   | 是   | 触发事件的帧数。 该值必须大于 0。           |
| callback | (position: number) => {} | 是   | 触发事件时调用的回调。                      |

**示例:**

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

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

off(type: 'periodReach'): void

取消订阅标记事件。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名 | 类型   | 必填 | 说明                                                |
| :----- | :----- | :--- | :-------------------------------------------------- |
| type   | string | 是   | 要取消订阅事件的类型。支持的事件为:'periodReach'。 |

**示例:**

```
audioRenderer.off('periodReach')
```

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

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

订阅监听状态变化。

**系统能力:** SystemCapability.Multimedia.Audio.Renderer

**参数:**

| 参数名   | 类型                       | 必填 | 说明                                        |
| :------- | :------------------------- | :--- | :------------------------------------------ |
| type     | string                     | 是   | 事件回调类型,支持的事件为:'stateChange'。 |
| callback | [AudioState](#AudioState8) | 是   | 返回监听的状态。                            |

**示例:**

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

## AudioCapturer<sup>8+</sup>

提供音频采集的相关接口。在调用AudioCapturer的接口前,需要先通过[createAudioCapturer](#audiocreateaudiocapturer8)创建实例。

### state<sup>8+</sup>

只读 state: AudioState

定义当前采集器状态。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

| 名称  | 参数型                     | 可读 | 可写 | 说明             |
| :---- | :------------------------- | :--- | :--- | :--------------- |
| state | [AudioState](#audiostate8) | Yes  | No   | 音频采集器状态。 |

**示例:**

```
var state = audioCapturer.state;
```

### getCapturerInfo<sup>8+</sup>

getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void

获取采集器信息。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                              | 必填 | 说明                                 |
| :------- | :-------------------------------- | :--- | :----------------------------------- |
| callback | AsyncCallback<AudioCapturerInfo\> | 是   | 使用callback方式异步返回采集器信息。 |

**示例:**

```
audioCapturer.getCapturerInfo((err, capturerInfo)=>{
    if (err) {
        console.error('Failed to get capture info');
    } else {
        console.log('Capturer getCapturerInfo:');
        console.log('Capturer source:' + capturerInfo.source);
        console.log('Capturer flags:' + capturerInfo.capturerFlags);
    }
});
```


### getCapturerInfo<sup>8+</sup>

getCapturerInfo(): Promise<AudioCapturerInfo\>

获取采集器信息。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型                                              | 说明                                |
| :------------------------------------------------ | :---------------------------------- |
| Promise<[AudioCapturerInfo](#audiocapturerinfo)\> | 使用Promise方式异步返回采集器信息。 |

**示例:**

```
audioCapturer.getCapturerInfo().then((capturerInfo) => {
    console.log('Capturer getCapturerInfo:');
    console.log('Capturer source:' + capturerInfo.source);
    console.log('Capturer flags:' + capturerInfo.capturerFlags);
}).catch ((err) => {
    console.log("Failed to get capturer info");
});
```

### getStreamInfo<sup>8+</sup>

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

获取采集器流信息。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                                                      | 必填 | 说明                             |
| :------- | :-------------------------------------------------------- | :--- | :------------------------------- |
| callback | AsyncCallback<[AudioStreamInfo](#AudioRendererOptions8)\> | 是   | 使用callback方式异步返回流信息。 |

**示例:**

```
audioCapturer.start((err)=>{
    if (err) {
        console.error('Failed to get stream info');
    } else {
        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);
    }
});
```

### getStreamInfo<sup>8+</sup>

getStreamInfo(): Promise<AudioStreamInfo\>

获取采集器流信息。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型                                                | 说明                            |
| :-------------------------------------------------- | :------------------------------ |
| Promise<[AudioStreamInfo](#AudioRendererOptions8)\> | 使用Promise方式异步返回流信息。 |

**示例:**

```
audioCapturer.getStreamInfo().then((streamInfo) => {
    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);
}).catch ((err) => {
    console.log("Failed to get stream info");
});
```

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

start(callback: AsyncCallback<void\>): void

启动音频采集器。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数**

| 参数名   | 类型                 | 必填 | 说明                           |
| :------- | :------------------- | :--- | :----------------------------- |
| callback | AsyncCallback<void\> | 是   | 使用callback方式异步返回结果。 |

**示例:**

```
audioCapturer.start((err)=>{
    if (err) {
        console.error('Capturer start failed.');
    } else {
        console.info('Capturer start success.');
    }
});
```


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

start(): Promise<void\>

启动音频采集器。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型           | 说明                          |
| :------------- | :---------------------------- |
| Promise<void\> | 使用Promise方式异步返回结果。 |

**示例:**

```
audioCapturer.start().then(() => {
    console.log("capturer start success");
}).catch ((err) => {
    console.log("Failed to start capturer");
});
```

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

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

停止采集。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                 | 必填 | 说明                           |
| :------- | :------------------- | :--- | :----------------------------- |
| callback | AsyncCallback<void\> | 是   | 使用callback方式异步返回结果。 |

**示例:**

```
audioCapturer.stop((err)=>{
    if (err) {
        console.error('Capturer stop failed');
    } else {
        console.log('Capturer stopped.');
    }
});
```


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

stop(): Promise<void\>

停止采集。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型           | 说明                          |
| :------------- | :---------------------------- |
| Promise<void\> | 使用Promise方式异步返回结果。 |

**示例:**

```
audioCapturer.stop().then(() => {
    console.log("capturer stop success");
}).catch ((err) => {
    console.log("Failed to stop capturer");
});
```

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

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

释放采集器。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                 | 必填 | 说明                                |
| :------- | :------------------- | :--- | :---------------------------------- |
| callback | AsyncCallback<void\> | 是   | Callback used to return the result. |

**示例:**

```
audioCapturer.release((err)=>{
    if (err) {
        console.error('capturer release failed');
    } else {
        console.log('capturer released.');
    }
});
```


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

release(): Promise<void\>

释放采集器。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型           | 说明                          |
| :------------- | :---------------------------- |
| Promise<void\> | 使用Promise方式异步返回结果。 |

**示例:**

```
audioCapturer.release().then(() => {
    console.log("capturer release success");
}).catch ((err) => {
    console.log("Failed to release capturer");
});
```


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

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

读入缓冲区。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数**

| 参数名         | 类型                        | 必填 | 说明                             |
| :------------- | :-------------------------- | :--- | :------------------------------- |
| size           | number                      | 是   | 读入的字节数。                   |
| isBlockingRead | boolean                     | 是   | 是否阻塞读操作。                 |
| callback       | AsyncCallback<ArrayBuffer\> | 是   | 使用callback方式异步返回缓冲区。 |

**示例:**

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


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

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

读入缓冲区。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名         | 类型    | 必填 | 说明             |
| :------------- | :------ | :--- | :--------------- |
| size           | number  | 是   | 读入的字节数。   |
| isBlockingRead | boolean | 是   | 是否阻塞读操作。 |

**返回值:**

| 类型                  | 说明                                                   |
| :-------------------- | :----------------------------------------------------- |
| Promise<ArrayBuffer\> | 如果操作成功,返回读取的缓冲区数据;否则返回错误代码。 |

**示例:**

```
audioCapturer.read(size, true).then((buffer) => {
    console.log("Success in reading the buffer data");
    var number = fileio.writeSync(fd, buffer);
}).catch ((err) => {
    console.log("Failed to read data!");
});
```


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

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

获取时间戳(从1970年1月1日开始),单位为纳秒。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                   | 必填 | 说明                           |
| :------- | :--------------------- | :--- | :----------------------------- |
| callback | AsyncCallback<number\> | 是   | 使用callback方式异步返回结果。 |

**示例:**

```
audioCapturer.getAudioTime((err, timestamp)=>{
    console.log('Current timestamp: ' + timestamp);
});
```


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

getAudioTime(): Promise<number\>

获取时间戳(从1970年1月1日开始),单位为纳秒。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型             | 说明                          |
| :--------------- | :---------------------------- |
| Promise<number\> | 使用Promise方式异步返回结果。 |

**示例:**

```
audioCapturer.getAudioTime().then((audioTime) => {
    console.log("Success in getting the audio time");
}).catch ((err) => {
    console.log("Failed to get the audio time");
});
```


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

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

获取采集器合理的最小缓冲区大小。使用callback方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                 |
| :------- | :--------------------- | :--- | :----------------------------------- |
| callback | AsyncCallback<number\> | 是   | 使用callback方式异步返回缓冲区大小。 |

**示例:**

```
audioCapturer.getBufferSize((err, bufferSize)=>{
    if (!err) {
        console.log('BufferSize : ' + bufferSize);
        var buffer = await audioCapturer.read(bufferSize, true);
    }
});
```


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

getBufferSize(): Promise<number\>

获取采集器合理的最小缓冲区大小。使用Promise方式异步返回结果。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**返回值:**

| 类型             | 说明                                |
| :--------------- | :---------------------------------- |
| Promise<number\> | 使用Promise方式异步返回缓冲区大小。 |

**示例:**

```
audioCapturer.getBufferSize().then((bufferSize) => {
    console.log("Success in getting the buffer size");
    var buffer = await audioCapturer.read(bufferSize, true);
}).catch ((err) => {
    console.log("Failed to get the buffer size");
});
```


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

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

订阅标记到达的事件。 当采集的帧数达到 frame 参数的值时,回调被触发。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                       |
| :------- | :---------------------- | :--- | :----------------------------------------- |
| type     | string                  | 是   | 事件回调类型,支持的事件为:'markReach'。  |
| frame    | number                  | 是   | 触发事件的帧数。 该值必须大于0。           |
| callback | position: number) => {} | 是   | 使用callback方式异步返回被触发事件的回调。 |

**示例:**

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

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

off(type: 'markReach'): void

取消订阅标记到达的事件。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名 | 类型   | 必填 | 说明                                          |
| :----- | :----- | :--- | :-------------------------------------------- |
| type   | string | 是   | 取消事件回调类型,支持的事件为:'markReach'。 |

**示例:**

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

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

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

订阅到达标记的事件。 当采集的帧数达到 frame 参数的值时,回调被循环调用。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                        |
| :------- | :----------------------- | :--- | :------------------------------------------ |
| type     | string                   | 是   | 事件回调类型,支持的事件为:'periodReach'。 |
| frame    | number                   | 是   | 触发事件的帧数。 该值必须大于0。            |
| callback | (position: number) => {} | 是   | 使用callback方式异步返回被触发事件的回调    |

**示例:**

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

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

off(type: 'periodReach'): void

取消订阅标记到达的事件。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名 | 类型   | 必填 | 说明                                            |
| :----- | :----- | :--- | :---------------------------------------------- |
| type   | string | Yes  | 取消事件回调类型,支持的事件为:'periodReach'。 |

**示例:**

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

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

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

订阅监听状态变化。

**系统能力:** SystemCapability.Multimedia.Audio.Capturer

**参数:**

| 参数名   | 类型                       | 必填 | 说明                                        |
| :------- | :------------------------- | :--- | :------------------------------------------ |
| type     | string                     | 是   | 事件回调类型,支持的事件为:'stateChange'。 |
| callback | [AudioState](#AudioState8) | 是   | 返回监听的状态。                            |

**示例:**

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