js-apis-audio.md 108.4 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
- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。
10
- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM音频数据。
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
## audio.createAudioRenderer<sup>8+</sup>

M
magekkkk 已提交
39
createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void
Z
zengyawen 已提交
40

41
获取音频渲染器。使用callback方式异步返回结果。
Z
zengyawen 已提交
42 43 44 45 46

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

**参数**

H
update  
HelloCrease 已提交
47 48 49
| 参数名   | 类型                                            | 必填 | 说明             |
| -------- | ----------------------------------------------- | ---- | ---------------- |
| options  | [AudioRendererOptions](#audiorendereroptions8)  | 是   | 配置渲染器。     |
M
magekkkk 已提交
50
| callback | AsyncCallback<[AudioRenderer](#audiorenderer8)> | 是   | 音频渲染器对象。 |
L
lwx1059628 已提交
51 52 53 54

**示例:**

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

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

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

audio.createAudioRenderer(audioRendererOptions,(err, data) => {
    if (err) {
L
lwx1059628 已提交
76
        console.error(`AudioRenderer Created : Error: ${err.message}`);
L
lwx1059628 已提交
77 78
    }
    else {
L
lwx1059628 已提交
79 80
        console.info('AudioRenderer Created : Success : SUCCESS');
        let audioRenderer = data;
L
lwx1059628 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }
});
```

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

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

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

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

**参数:**

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

**返回值:**

| 类型                                      | 说明             |
| ----------------------------------------- | ---------------- |
103
| Promise<[AudioRenderer](#audiorenderer8)> | 音频渲染器对象。 |
Z
zengyawen 已提交
104 105 106 107

**示例:**

```
L
lwx1059628 已提交
108 109
import audio from '@ohos.multimedia.audio';

Z
zengyawen 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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
}

L
lwx1059628 已提交
128 129 130 131 132 133 134
var audioRenderer;
audio.createAudioRenderer(audioRendererOptions).then((data) => {
    audioRenderer = data;
    console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS');
}).catch((err) => {
    console.info('AudioFrameworkRenderLog: AudioRenderer Created : ERROR : '+err.message);
});
Z
zengyawen 已提交
135
```
Z
zengyawen 已提交
136

L
lwx1059628 已提交
137 138 139 140 141 142 143 144 145 146
## audio.createAudioCapturer<sup>8+</sup>

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

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

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

**参数:**

H
update  
HelloCrease 已提交
147 148
| 参数名   | 类型                                            | 必填 | 说明             |
| :------- | :---------------------------------------------- | :--- | :--------------- |
Z
zengyawen 已提交
149
| options  | [AudioCapturerOptions](#audiocaptureroptions8)  | 是   | 配置音频采集器。 |
M
magekkkk 已提交
150
| callback | AsyncCallback<[AudioCapturer](#audiocapturer8)> | 是   | 音频采集器对象。 |
L
lwx1059628 已提交
151 152 153 154

**示例:**

```
L
lwx1059628 已提交
155
import audio from '@ohos.multimedia.audio';
L
lwx1059628 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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');
L
lwx1059628 已提交
179
        let audioCapturer = data;
L
lwx1059628 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }
});
```

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

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

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

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

**参数:**

Z
zengyawen 已提交
194 195 196
| 参数名  | 类型                                           | 必填 | 说明             |
| :------ | :--------------------------------------------- | :--- | :--------------- |
| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是   | 配置音频采集器。 |
L
lwx1059628 已提交
197 198 199 200 201

**返回值:**

| 类型                                      | 说明           |
| ----------------------------------------- | -------------- |
M
magekkkk 已提交
202
| Promise<[AudioCapturer](#audiocapturer8)> | 音频采集器对象 |
L
lwx1059628 已提交
203 204 205 206

**示例:**

```
L
lwx1059628 已提交
207 208
import audio from '@ohos.multimedia.audio';

L
lwx1059628 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
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 = {
L
lwx1059628 已提交
222 223
    streamInfo: audioStreamInfo,
    capturerInfo: audioCapturerInfo
L
lwx1059628 已提交
224 225
}

L
lwx1059628 已提交
226
var audioCapturer;
R
rahul 已提交
227
audio.createAudioCapturer(audioCapturerOptions).then((data) => {
L
lwx1059628 已提交
228 229 230 231 232
    audioCapturer = data;
    console.info('AudioCapturer Created : Success : Stream Type: SUCCESS');
}).catch((err) => {
    console.info('AudioCapturer Created : ERROR : '+err.message);
});
L
lwx1059628 已提交
233 234
```

Z
zengyawen 已提交
235
## AudioVolumeType
M
mamingshuai 已提交
236

237
枚举,音频流类型。
M
mamingshuai 已提交
238

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

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


## DeviceFlag
M
mamingshuai 已提交
250

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

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

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


## DeviceRole
M
mamingshuai 已提交
263

264
枚举,设备角色。
M
mamingshuai 已提交
265

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

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


Z
zengyawen 已提交
274 275 276
## DeviceType

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

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

H
update  
HelloCrease 已提交
280 281 282 283 284 285 286 287 288 289 290
| 名称             | 默认值 | 描述                                                      |
| ---------------- | ------ | --------------------------------------------------------- |
| INVALID          | 0      | 无效设备。                                                |
| EARPIECE         | 1      | 听筒。                                                    |
| SPEAKER          | 2      | 扬声器。                                                  |
| WIRED_HEADSET    | 3      | 有线耳机,带麦克风。                                      |
| WIRED_HEADPHONES | 4      | 有线耳机,无麦克风。                                      |
| BLUETOOTH_SCO    | 7      | 蓝牙设备SCO(Synchronous Connection Oriented)连接。      |
| BLUETOOTH_A2DP   | 8      | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。 |
| MIC              | 15     | 麦克风。                                                  |
| USB_HEADSET      | 22     | USB耳机,带麦克风。                                       |
M
magekkkk 已提交
291

Z
zengyawen 已提交
292
## ActiveDeviceType
M
magekkkk 已提交
293

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

Z
zengyawen 已提交
296 297 298 299 300 301
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device

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

Z
zengyawen 已提交
303
## AudioRingMode
304 305 306

枚举,铃声模式。

Z
zengyawen 已提交
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 358 359 360 361 362 363 364 365 366 367 368 369 370
**系统能力:** 以下各项对应的系统能力均为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 已提交
371
## ContentType
Z
zengyawen 已提交
372 373 374 375 376

枚举,音频内容类型。

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

L
lwx1059628 已提交
377 378 379 380 381 382 383 384
| 名称                               | 默认值 | 描述       |
| ---------------------------------- | ------ | ---------- |
| 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 已提交
385

L
lwx1059628 已提交
386
## StreamUsage
Z
zengyawen 已提交
387 388 389 390 391 392 393 394 395 396

枚举,音频流使用类型。

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

| 名称                               | 默认值 | 描述       |
| ---------------------------------- | ------ | ---------- |
| STREAM_USAGE_UNKNOWN               | 0      | 未知类型。 |
| STREAM_USAGE_MEDIA                 | 1      | 音频。     |
| STREAM_USAGE_VOICE_COMMUNICATION   | 2      | 语音通信。 |
L
lwx1059628 已提交
397
| STREAM_USAGE_NOTIFICATION_RINGTONE | 6      | 通知铃声。 |
Z
zengyawen 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

## 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 已提交
417
枚举,音频渲染速度。
Z
zengyawen 已提交
418 419 420 421 422 423 424 425 426

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

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

L
lwx1059628 已提交
427
## InterruptType
Z
zengyawen 已提交
428 429 430 431

枚举,中断类型。

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

Z
zengyawen 已提交
433 434 435 436 437
| 名称                 | 默认值 | 描述                   |
| -------------------- | ------ | ---------------------- |
| INTERRUPT_TYPE_BEGIN | 1      | 音频播放中断事件开始。 |
| INTERRUPT_TYPE_END   | 2      | 音频播放中断事件结束。 |

L
lwx1059628 已提交
438
## InterruptForceType<sup>9+</sup>
Z
zengyawen 已提交
439 440 441 442 443 444 445 446 447 448

枚举,强制打断类型。

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

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

L
lwx1059628 已提交
449
## InterruptHint
Z
zengyawen 已提交
450 451 452 453 454

枚举,中断提示。

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

L
lwx1059628 已提交
455 456 457 458 459 460 461 462
| 名称                               | 默认值 | 描述                                         |
| ---------------------------------- | ------ | -------------------------------------------- |
| 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 已提交
463

464 465 466 467 468 469
## InterruptActionType

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

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

H
update  
HelloCrease 已提交
470 471 472 473
| 名称           | 默认值 | 描述               |
| -------------- | ------ | ------------------ |
| TYPE_ACTIVATED | 0      | 表示触发焦点事件。 |
| TYPE_INTERRUPT | 1      | 表示音频打断事件。 |
474

Z
zengyawen 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
## 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 已提交
490
音频渲染器信息。
Z
zengyawen 已提交
491 492 493

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

L
lwx1059628 已提交
494 495
| 名称          | 类型                        | 必填 | 说明             |
| ------------- | --------------------------- | ---- | ---------------- |
Z
zengyawen 已提交
496
| content       | [ContentType](#contenttype) | 是   | 媒体类型。       |
L
lwx1059628 已提交
497 498
| usage         | [StreamUsage](#streamusage) | 是   | 音频流使用类型。 |
| rendererFlags | number                      | 是   | 音频渲染器标志。 |
Z
zengyawen 已提交
499 500 501

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

L
lwx1059628 已提交
502
音频渲染器选项信息。
Z
zengyawen 已提交
503 504 505 506 507 508

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

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

L
lwx1059628 已提交
511
## InterruptEvent<sup>9+</sup>
Z
zengyawen 已提交
512 513 514 515 516 517 518

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

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

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

523 524 525 526 527 528
## AudioInterrupt

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

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

H
update  
HelloCrease 已提交
529 530 531 532 533
| 名称            | 类型                        | 必填 | 说明                                                         |
| --------------- | --------------------------- | ---- | ------------------------------------------------------------ |
| streamUsage     | [StreamUsage](#streamusage) | 是   | 音频流使用类型。                                             |
| contentType     | [ContentType](#contenttype) | 是   | 音频打断媒体类型。                                           |
| pauseWhenDucked | boolean                     | 是   | 音频打断时是否可以暂停音频播放(true表示音频播放可以在音频打断期间暂停,false表示相反)。 |
534 535 536 537 538 539 540

## InterruptAction

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

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

H
update  
HelloCrease 已提交
541 542 543 544
| 名称       | 类型                                        | 必填 | 说明                                                         |
| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| actionType | [InterruptActionType](#interruptactiontype) | 是   | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。 |
| type       | [InterruptType](#interrupttype)             | 否   | 打断事件类型。                                               |
Z
zengyawen 已提交
545
| hint       | [InterruptHint](#interrupthint)              | 否   | 打断事件提示。                                               |
H
update  
HelloCrease 已提交
546
| activated  | boolean                                     | 否   | 获得/释放焦点。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 |
547

Z
zengyawen 已提交
548 549 550 551
## VolumeEvent<sup>8+</sup>

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

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

Z
zengyawen 已提交
554 555 556 557 558 559 560
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Volume

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

L
lwx1059628 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
## DeviceChangeAction

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

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

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

## DeviceChangeType

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

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

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

Z
zengyawen 已提交
584 585 586 587 588 589 590 591 592
## AudioCapturerOptions<sup>8+</sup>

音频采集器选项信息。

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

| 名称         | 类型                                    | 必填 | 说明             |
| ------------ | --------------------------------------- | ---- | ---------------- |
| streamInfo   | [AudioStreamInfo](#audiostreaminfo8)    | 是   | 表示音频流信息。 |
Z
zengyawen 已提交
593
| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo) | 是   | 表示采集器信息。 |
Z
zengyawen 已提交
594

L
lwx1059628 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
## 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

Z
update  
zengyawen 已提交
612 613 614 615 616
| 名称                            | 默认值 | 描述                   |
| :------------------------------ | :----- | :--------------------- |
| SOURCE_TYPE_INVALID             | -1     | 无效的音频源。         |
| SOURCE_TYPE_MIC                 | 0      | Mic音频源。            |
| SOURCE_TYPE_VOICE_COMMUNICATION | 7      | 语音通话场景的音频源。 |
L
lwx1059628 已提交
617 618 619 620 621 622 623

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

枚举,音频场景。

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

Z
zengyawen 已提交
624 625 626 627 628 629
| 名称                   | 默认值 | 描述                                          |
| :--------------------- | :----- | :-------------------------------------------- |
| AUDIO_SCENE_DEFAULT    | 0      | 默认音频场景。                                |
| AUDIO_SCENE_RINGING    | 1      | 响铃模式。<br/>系统接口,三方应用不支持调用。 |
| AUDIO_SCENE_PHONE_CALL | 2      | 电话模式。<br/>系统接口,三方应用不支持调用。 |
| AUDIO_SCENE_VOICE_CHAT | 3      | 语音聊天模式。                                |
L
lwx1059628 已提交
630

Z
zengyawen 已提交
631
## AudioManager
M
mamingshuai 已提交
632

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

Z
zengyawen 已提交
635 636 637
### setVolume

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

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

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

M
mamingshuai 已提交
643 644
**参数:**

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

M
mamingshuai 已提交
651 652 653
**示例:**

```
L
lwx1059628 已提交
654 655 656 657 658 659 660
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err) => {
    if (err) {
        console.error('Failed to set the volume. ${err.message}');
        return;
    }
    console.log('Callback invoked to indicate a successful volume setting.');
});
M
mamingshuai 已提交
661 662
```

Z
zengyawen 已提交
663 664 665
### setVolume

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

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

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

M
mamingshuai 已提交
671 672
**参数:**

Z
zengyawen 已提交
673 674 675 676
| 参数名     | 类型                                | 必填 | 说明                                                     |
| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                             |
| volume     | number                              | 是   | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 |
M
mamingshuai 已提交
677 678 679

**返回值:**

Z
zengyawen 已提交
680 681
| 类型                | 说明                          |
| ------------------- | ----------------------------- |
Z
zengyawen 已提交
682
| Promise&lt;void&gt; | Promise回调表示成功还是失败。 |
M
mamingshuai 已提交
683 684 685 686

**示例:**

```
A
AOL 已提交
687
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
688
    console.log('Promise returned to indicate a successful volume setting.');
L
lwx1059628 已提交
689
});
M
mamingshuai 已提交
690 691
```

Z
zengyawen 已提交
692 693 694
### getVolume

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

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

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

M
mamingshuai 已提交
700 701
**参数:**

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

M
mamingshuai 已提交
707 708 709
**示例:**

```
710
audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
M
mamingshuai 已提交
711
   if (err) {
L
lwx1059628 已提交
712 713
       console.error('Failed to obtain the volume. ${err.message}');
       return;
M
mamingshuai 已提交
714
   }
715
   console.log('Callback invoked to indicate that the volume is obtained.');
L
lwx1059628 已提交
716
});
M
mamingshuai 已提交
717 718
```

Z
zengyawen 已提交
719 720 721
### getVolume

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

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

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

M
mamingshuai 已提交
727 728
**参数:**

Z
zengyawen 已提交
729 730 731
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
732 733 734

**返回值:**

Z
zengyawen 已提交
735 736
| 类型                  | 说明                      |
| --------------------- | ------------------------- |
Z
zengyawen 已提交
737
| Promise&lt;number&gt; | Promise回调返回音量大小。 |
M
mamingshuai 已提交
738 739 740 741

**示例:**

```
A
AOL 已提交
742
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
743
    console.log('Promise returned to indicate that the volume is obtained.' + value);
L
lwx1059628 已提交
744
});
M
mamingshuai 已提交
745 746
```

Z
zengyawen 已提交
747 748 749
### getMinVolume

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

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

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

M
mamingshuai 已提交
755 756
**参数:**

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

M
mamingshuai 已提交
762 763 764
**示例:**

```
Z
zengyawen 已提交
765
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
766 767
    if (err) {
        console.error('Failed to obtain the minimum volume. ${err.message}');
L
lwx1059628 已提交
768
        return;
769 770
    }
    console.log('Callback invoked to indicate that the minimum volume is obtained.' + value);
L
lwx1059628 已提交
771
});
M
mamingshuai 已提交
772 773
```

Z
zengyawen 已提交
774 775 776
### getMinVolume

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

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

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

M
mamingshuai 已提交
782 783
**参数:**

Z
zengyawen 已提交
784 785 786
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
787 788 789

**返回值:**

Z
zengyawen 已提交
790 791
| 类型                  | 说明                      |
| --------------------- | ------------------------- |
Z
zengyawen 已提交
792
| Promise&lt;number&gt; | Promise回调返回最小音量。 |
M
mamingshuai 已提交
793 794 795 796

**示例:**

```
A
AOL 已提交
797
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => {
L
lwx1059628 已提交
798 799
    console.log('Promised returned to indicate that the minimum volume is obtained.' + value);
});
M
mamingshuai 已提交
800 801
```

Z
zengyawen 已提交
802 803 804
### getMaxVolume

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

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

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

M
mamingshuai 已提交
810 811
**参数:**

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

M
mamingshuai 已提交
817 818 819
**示例:**

```
820 821 822 823 824 825
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);
L
lwx1059628 已提交
826
});
M
mamingshuai 已提交
827 828
```

Z
zengyawen 已提交
829 830 831
### getMaxVolume

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

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

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

M
mamingshuai 已提交
837 838
**参数:**

Z
zengyawen 已提交
839 840 841
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
M
mamingshuai 已提交
842 843 844

**返回值:**

Z
zengyawen 已提交
845 846
| 类型                  | 说明                          |
| --------------------- | ----------------------------- |
Z
zengyawen 已提交
847
| Promise&lt;number&gt; | Promise回调返回最大音量大小。 |
M
mamingshuai 已提交
848 849 850 851

**示例:**

```
A
AOL 已提交
852
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => {
853
    console.log('Promised returned to indicate that the maximum volume is obtained.');
L
lwx1059628 已提交
854
});
Z
zengyawen 已提交
855 856
```

Z
zengyawen 已提交
857
### mute
Z
zengyawen 已提交
858 859

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

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

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

Z
zengyawen 已提交
865 866
**参数:**

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

Z
zengyawen 已提交
873 874 875
**示例:**

```
876 877 878
audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => {
    if (err) {
        console.error('Failed to mute the stream. ${err.message}');
L
lwx1059628 已提交
879
        return;
880 881
    }
    console.log('Callback invoked to indicate that the stream is muted.');
L
lwx1059628 已提交
882
});
883 884
```

Z
zengyawen 已提交
885
### mute
Z
zengyawen 已提交
886 887

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

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

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

893 894
**参数:**

Z
zengyawen 已提交
895 896 897 898
| 参数名     | 类型                                | 必填 | 说明                                  |
| ---------- | ----------------------------------- | ---- | ------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                          |
| mute       | boolean                             | 是   | 静音状态,true为静音,false为非静音。 |
899 900 901

**返回值:**

Z
zengyawen 已提交
902 903
| 类型                | 说明                          |
| ------------------- | ----------------------------- |
Z
zengyawen 已提交
904
| Promise&lt;void&gt; | Promise回调表示成功还是失败。 |
905 906 907

**示例:**

Z
zengyawen 已提交
908

909
```
A
AOL 已提交
910
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
911
    console.log('Promise returned to indicate that the stream is muted.');
L
lwx1059628 已提交
912
});
913 914 915
```


Z
zengyawen 已提交
916
### isMute
917

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

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

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

Z
zengyawen 已提交
924
**参数:**
925

Z
zengyawen 已提交
926 927 928 929
| 参数名     | 类型                                | 必填 | 说明                                            |
| ---------- | ----------------------------------- | ---- | ----------------------------------------------- |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。                                    |
| callback   | AsyncCallback&lt;boolean&gt;        | 是   | 回调返回流静音状态,true为静音,false为非静音。 |
930 931 932 933 934

**示例:**

```
audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => {
M
mamingshuai 已提交
935
   if (err) {
L
lwx1059628 已提交
936 937
       console.error('Failed to obtain the mute status. ${err.message}');
       return;
M
mamingshuai 已提交
938
   }
939
   console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value);
L
lwx1059628 已提交
940
});
Z
zengyawen 已提交
941 942
```

Z
zengyawen 已提交
943

Z
zengyawen 已提交
944
### isMute
Z
zengyawen 已提交
945 946

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

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

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

Z
zengyawen 已提交
952 953
**参数:**

Z
zengyawen 已提交
954 955 956
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
Z
zengyawen 已提交
957 958 959

**返回值:**

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

Z
zengyawen 已提交
964 965 966
**示例:**

```
A
AOL 已提交
967
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => {
968
    console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value);
L
lwx1059628 已提交
969
});
Z
zengyawen 已提交
970 971
```

Z
zengyawen 已提交
972
### isActive
Z
zengyawen 已提交
973 974

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

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

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

980 981
**参数:**

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

**示例:**

```
audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => {
    if (err) {
        console.error('Failed to obtain the active status of the stream. ${err.message}');
L
lwx1059628 已提交
993
        return;
994 995
    }
    console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value);
L
lwx1059628 已提交
996
});
997 998
```

Z
zengyawen 已提交
999
### isActive
Z
zengyawen 已提交
1000 1001

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

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

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

1007 1008
**参数:**

Z
zengyawen 已提交
1009 1010 1011
| 参数名     | 类型                                | 必填 | 说明         |
| ---------- | ----------------------------------- | ---- | ------------ |
| volumeType | [AudioVolumeType](#audiovolumetype) | 是   | 音量流类型。 |
1012 1013 1014

**返回值:**

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

1019 1020 1021
**示例:**

```
A
AOL 已提交
1022
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => {
1023
    console.log('Promise returned to indicate that the active status of the stream is obtained.' + value);
L
lwx1059628 已提交
1024
});
1025 1026
```

Z
zengyawen 已提交
1027
### setRingerMode
Z
zengyawen 已提交
1028 1029

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

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

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

1035 1036
**参数:**

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

**示例:**

```
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => {
   if (err) {
L
lwx1059628 已提交
1047
       console.error('Failed to set the ringer mode.​ ${err.message}');
1048 1049 1050
       return;
    }
    console.log('Callback invoked to indicate a successful setting of the ringer mode.');
L
lwx1059628 已提交
1051
});
1052 1053
```

Z
zengyawen 已提交
1054
### setRingerMode
Z
zengyawen 已提交
1055 1056

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

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

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

1062 1063
**参数:**

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

**返回值:**

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

**示例:**

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


Z
zengyawen 已提交
1083
### getRingerMode
1084

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

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

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

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

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

**示例:**

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


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

**示例:**

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

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

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

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

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

1140 1141
**参数:**

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

**示例:**

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

Z
zengyawen 已提交
1160
### setAudioParameter
Z
zengyawen 已提交
1161 1162

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

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

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

1168 1169
**参数:**

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

**返回值:**

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

**示例:**

```
A
AOL 已提交
1184
audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => {
1185
    console.log('Promise returned to indicate a successful setting of the audio parameter.');
L
lwx1059628 已提交
1186
});
1187 1188
```

Z
zengyawen 已提交
1189
### getAudioParameter
Z
zengyawen 已提交
1190 1191

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

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

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

1197 1198
**参数:**

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

**示例:**

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

Z
zengyawen 已提交
1216
### getAudioParameter
Z
zengyawen 已提交
1217 1218

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

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

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

1224 1225
**参数:**

Z
zengyawen 已提交
1226 1227 1228
| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| key    | string | 是   | 待获取的音频参数的键。 |
1229 1230 1231

**返回值:**

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

**示例:**

```
A
AOL 已提交
1239
audioManager.getAudioParameter('PBits per sample').then((value) => {
1240
    console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value);
L
lwx1059628 已提交
1241
});
1242 1243
```

Z
zengyawen 已提交
1244 1245 1246
### getDevices

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

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

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

1252 1253
**参数:**

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

**示例:**
```
A
AOL 已提交
1261
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value) => {
1262
   if (err) {
L
lwx1059628 已提交
1263 1264
       console.error('Failed to obtain the device list. ${err.message}');
       return;
1265 1266
   }
   console.log('Callback invoked to indicate that the device list is obtained.');
L
lwx1059628 已提交
1267
});
1268 1269
```

Z
zengyawen 已提交
1270 1271
### getDevices

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

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

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

1278 1279
**参数:**

Z
zengyawen 已提交
1280 1281 1282
| 参数名     | 类型                      | 必填 | 说明             |
| ---------- | ------------------------- | ---- | ---------------- |
| deviceFlag | [DeviceFlag](#deviceflag) | 是   | 设备类型的flag。 |
1283 1284 1285

**返回值:**

Z
zengyawen 已提交
1286 1287
| 类型                                                         | 说明                      |
| ------------------------------------------------------------ | ------------------------- |
Z
zengyawen 已提交
1288
| Promise&lt;[AudioDeviceDescriptors](#audiodevicedescriptors)&gt; | Promise回调返回设备列表。 |
1289 1290 1291 1292

**示例:**

```
A
AOL 已提交
1293
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
1294
    console.log('Promise returned to indicate that the device list is obtained.');
L
lwx1059628 已提交
1295
});
1296 1297
```

Z
zengyawen 已提交
1298
### setDeviceActive
Z
zengyawen 已提交
1299

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

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

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

1306 1307
**参数:**

H
update  
HelloCrease 已提交
1308 1309 1310 1311 1312
| 参数名     | 类型                                  | 必填 | 说明                     |
| ---------- | ------------------------------------- | ---- | ------------------------ |
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。       |
| active     | boolean                               | 是   | 设备激活状态。           |
| callback   | AsyncCallback&lt;void&gt;             | 是   | 回调返回设置成功或失败。 |
1313 1314 1315 1316

**示例:**

```
R
rahul 已提交
1317
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err) => {
1318 1319
    if (err) {
        console.error('Failed to set the active status of the device. ${err.message}');
L
lwx1059628 已提交
1320
        return;
1321 1322
    }
    console.log('Callback invoked to indicate that the device is set to the active status.');
L
lwx1059628 已提交
1323
});
1324 1325
```

Z
zengyawen 已提交
1326
### setDeviceActive
Z
zengyawen 已提交
1327

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

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

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

1334 1335
**参数:**

H
update  
HelloCrease 已提交
1336 1337
| 参数名     | 类型                                  | 必填 | 说明               |
| ---------- | ------------------------------------- | ---- | ------------------ |
A
AOL 已提交
1338
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。 |
H
update  
HelloCrease 已提交
1339
| active     | boolean                               | 是   | 设备激活状态。     |
1340 1341 1342

**返回值:**

Z
zengyawen 已提交
1343 1344
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1345
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1346 1347 1348

**示例:**

Z
zengyawen 已提交
1349

1350
```
R
rahul 已提交
1351
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => {
1352
    console.log('Promise returned to indicate that the device is set to the active status.');
L
lwx1059628 已提交
1353
});
1354 1355
```

Z
zengyawen 已提交
1356
### isDeviceActive
Z
zengyawen 已提交
1357

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

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

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

1364 1365
**参数:**

H
update  
HelloCrease 已提交
1366 1367 1368 1369
| 参数名     | 类型                                  | 必填 | 说明                     |
| ---------- | ------------------------------------- | ---- | ------------------------ |
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。       |
| callback   | AsyncCallback&lt;boolean&gt;          | 是   | 回调返回设备的激活状态。 |
1370 1371 1372 1373

**示例:**

```
R
rahul 已提交
1374
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err, value) => {
1375 1376
    if (err) {
        console.error('Failed to obtain the active status of the device. ${err.message}');
L
lwx1059628 已提交
1377
        return;
1378 1379
    }
    console.log('Callback invoked to indicate that the active status of the device is obtained.');
L
lwx1059628 已提交
1380
});
1381 1382
```

Z
zengyawen 已提交
1383

Z
zengyawen 已提交
1384
### isDeviceActive
Z
zengyawen 已提交
1385

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

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

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

1392 1393
**参数:**

H
update  
HelloCrease 已提交
1394 1395
| 参数名     | 类型                                  | 必填 | 说明               |
| ---------- | ------------------------------------- | ---- | ------------------ |
A
AOL 已提交
1396
| deviceType | [ActiveDeviceType](#activedevicetype) | 是   | 活跃音频设备类型。 |
1397 1398 1399

**返回值:**

Z
zengyawen 已提交
1400 1401
| Type                   | Description                     |
| ---------------------- | ------------------------------- |
Z
zengyawen 已提交
1402
| Promise&lt;boolean&gt; | Promise回调返回设备的激活状态。 |
1403 1404 1405 1406

**示例:**

```
R
rahul 已提交
1407
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value) => {
1408
    console.log('Promise returned to indicate that the active status of the device is obtained.' + value);
L
lwx1059628 已提交
1409
});
1410 1411
```

Z
zengyawen 已提交
1412
### setMicrophoneMute
Z
zengyawen 已提交
1413 1414

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

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

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

1420 1421
**参数:**

Z
zengyawen 已提交
1422 1423 1424 1425
| 参数名   | 类型                      | 必填 | 说明                                          |
| -------- | ------------------------- | ---- | --------------------------------------------- |
| mute     | boolean                   | 是   | 待设置的静音状态,true为静音,false为非静音。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回设置成功或失败。                      |
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

**示例:**

```
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.');
L
lwx1059628 已提交
1436
});
1437 1438
```

Z
zengyawen 已提交
1439
### setMicrophoneMute
Z
zengyawen 已提交
1440 1441

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

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

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

1447 1448
**参数:**

Z
zengyawen 已提交
1449 1450 1451
| 参数名 | 类型    | 必填 | 说明                                          |
| ------ | ------- | ---- | --------------------------------------------- |
| mute   | boolean | 是   | 待设置的静音状态,true为静音,false为非静音。 |
1452 1453 1454

**返回值:**

Z
zengyawen 已提交
1455 1456
| 类型                | 说明                            |
| ------------------- | ------------------------------- |
Z
zengyawen 已提交
1457
| Promise&lt;void&gt; | Promise回调返回设置成功或失败。 |
1458 1459 1460 1461

**示例:**

```
A
AOL 已提交
1462
audioManager.setMicrophoneMute(true).then(() => {
1463
    console.log('Promise returned to indicate that the microphone is muted.');
L
lwx1059628 已提交
1464
});
1465 1466
```

Z
zengyawen 已提交
1467
### isMicrophoneMute
Z
zengyawen 已提交
1468 1469

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

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

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

1475 1476
**参数:**

Z
zengyawen 已提交
1477 1478 1479
| 参数名   | 类型                         | 必填 | 说明                                                    |
| -------- | ---------------------------- | ---- | ------------------------------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调返回系统麦克风静音状态,true为静音,false为非静音。 |
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

**示例:**

```
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);
L
lwx1059628 已提交
1490
});
1491 1492
```

Z
zengyawen 已提交
1493
### isMicrophoneMute
1494

Z
zengyawen 已提交
1495
isMicrophoneMute(): Promise&lt;boolean&gt;
1496

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

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

1501 1502
**返回值:**

Z
zengyawen 已提交
1503 1504
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
Z
zengyawen 已提交
1505
| Promise&lt;boolean&gt; | Promise回调返回系统麦克风静音状态,true为静音,false为非静音。 |
1506 1507 1508

**示例:**

Z
zengyawen 已提交
1509

1510
```
A
AOL 已提交
1511
audioManager.isMicrophoneMute().then((value) => {
1512
    console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
L
lwx1059628 已提交
1513
});
1514 1515
```

L
lwx1059628 已提交
1516
### on('volumeChange')<sup>8+</sup>
Z
zengyawen 已提交
1517 1518 1519 1520 1521

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

监听系统音量变化事件。

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

Z
zengyawen 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
**系统能力:** 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 已提交
1540
});
Z
zengyawen 已提交
1541 1542
```

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

A
AOL 已提交
1545
on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void
Z
zengyawen 已提交
1546 1547 1548

监听铃声模式变化事件。

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

Z
zengyawen 已提交
1551 1552 1553 1554 1555 1556 1557 1558
**系统能力:** SystemCapability.Multimedia.Audio.Communication

**参数:**

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

L
lwx1059628 已提交
1560 1561 1562 1563 1564 1565 1566 1567
**示例:**

```
audioManager.on('ringerModeChange', (ringerMode) => {
    console.log('Updated ringermode: ' + ringerMode);
});
```

L
lwx1059628 已提交
1568 1569 1570 1571 1572 1573 1574 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
### 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

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

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

**参数:**

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

**示例:**

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

1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
### on('interrupt')

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

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

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

**参数:**

H
update  
HelloCrease 已提交
1627 1628 1629 1630 1631
| 参数名    | 类型                                          | 必填 | 说明                                                         |
| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type      | string                                        | 是   | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 |
| interrupt | AudioInterrupt                                | 是   | 音频打断事件类型的参数。                                     |
| callback  | Callback<[InterruptAction](#interruptaction)> | 是   | 音频打断事件回调方法。                                       |
1632 1633 1634 1635 1636 1637 1638 1639 1640

**示例:**

```
var interAudioInterrupt = {
    streamUsage:2,
    contentType:0,
    pauseWhenDucked:true
};
R
rahul 已提交
1641
audioManager.on('interrupt', interAudioInterrupt, (InterruptAction) => {
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
    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

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

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

**参数:**

H
update  
HelloCrease 已提交
1663 1664 1665 1666 1667
| 参数名    | 类型                                          | 必填 | 说明                                                         |
| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type      | string                                        | 是   | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 |
| interrupt | AudioInterrupt                                | 是   | 音频打断事件类型的参数。                                     |
| callback  | Callback<[InterruptAction](#interruptaction)> | 否   | 音频打断事件回调方法。                                       |
1668 1669 1670 1671 1672 1673 1674 1675 1676

**示例:**

```
var interAudioInterrupt = {
    streamUsage:2,
    contentType:0,
    pauseWhenDucked:true
};
R
rahul 已提交
1677
audioManager.off('interrupt', interAudioInterrupt, (InterruptAction) => {
1678 1679 1680 1681 1682 1683 1684
    if (InterruptAction.actionType === 0) {
        console.log("An event to release the audio focus starts.");
        console.log("Focus release event:" + JSON.stringify(InterruptAction));
    }
});
```

L
lwx1059628 已提交
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
### 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) {
L
lwx1059628 已提交
1707
       console.error('Failed to set the audio scene mode.​ ${err.message}');
L
lwx1059628 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
       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方式返回异步结果。

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

Z
zengyawen 已提交
1722
**系统能力:** SystemCapability.Multimedia.Audio.Communication
L
lwx1059628 已提交
1723

Z
zengyawen 已提交
1724
**参数:**
L
lwx1059628 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

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

**返回值:**

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

**示例:**

```
R
rahul 已提交
1739
audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL).then(() => {
L
lwx1059628 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
    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方式返回异步结果。

Z
zengyawen 已提交
1752
**系统能力:** SystemCapability.Multimedia.Audio.Communication
L
lwx1059628 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764

**参数:**

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

**示例:**

```
audioManager.getAudioScene((err, value) => {
   if (err) {
L
lwx1059628 已提交
1765
       console.error('Failed to obtain the audio scene mode.​ ${err.message}');
L
lwx1059628 已提交
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
       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 已提交
1797
## AudioDeviceDescriptor
1798 1799 1800

描述音频设备。

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

1803
| 名称       | 类型                    | 可读 | 可写 | 说明       |
Z
zengyawen 已提交
1804 1805 1806
| ---------- | ------------------------- | ---- | ---- | ---------- |
| deviceRole | [DeviceRole](#devicerole) | 是   | 否   | 设备角色。 |
| deviceType | [DeviceType](#devicetype) | 是   | 否   | 设备类型。 |
Z
zengyawen 已提交
1807 1808

## AudioDeviceDescriptors
M
mamingshuai 已提交
1809

H
update  
HelloCrease 已提交
1810
设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。
Z
zengyawen 已提交
1811 1812 1813 1814

**示例:**

```
L
lwx1059628 已提交
1815 1816 1817 1818 1819 1820
import audio from '@ohos.multimedia.audio';

function displayDeviceProp(value) {
    deviceRoleValue = value.deviceRole;
    deviceTypeValue = value.deviceType;

Z
zengyawen 已提交
1821 1822
}

L
lwx1059628 已提交
1823 1824 1825 1826 1827 1828
var deviceRoleValue = null;
var deviceTypeValue = null;
const promise = audio.getAudioManager().getDevices(1);
promise.then(function (value) {
    console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG');
    value.forEach(displayDeviceProp);
Z
zengyawen 已提交
1829
    if (deviceTypeValue != null && deviceRoleValue != null){
L
lwx1059628 已提交
1830
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  PASS');
Z
zengyawen 已提交
1831 1832
    }
    else{
L
lwx1059628 已提交
1833 1834 1835
        console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG :  FAIL');
    }
});
Z
zengyawen 已提交
1836 1837 1838 1839
```

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

L
lwx1059628 已提交
1840 1841
提供音频渲染的相关接口。在调用AudioRenderer的接口前,需要先通过[createAudioRenderer](#audiocreateaudiorenderer8)创建实例。

1842
### 属性
Z
zengyawen 已提交
1843

1844
**系统能力:** SystemCapability.Multimedia.Audio.Renderer
Z
zengyawen 已提交
1845

1846
| 名称  | 类型                     | 可读 | 可写 | 说明               |
Z
zengyawen 已提交
1847
| ----- | -------------------------- | ---- | ---- | ------------------ |
1848
| state<sup>8+</sup> | [AudioState](#audiostate8) | 是   | 否   | 音频渲染器的状态。 |
Z
zengyawen 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859

**示例:**

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

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

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

L
lwx1059628 已提交
1860
获取当前被创建的音频渲染器的信息,使用callback方式异步返回结果。
Z
zengyawen 已提交
1861 1862 1863 1864 1865

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

**参数:**

L
lwx1059628 已提交
1866 1867 1868
| 参数名   | 类型                                                     | 必填 | 说明                   |
| :------- | :------------------------------------------------------- | :--- | :--------------------- |
| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是   | 返回音频渲染器的信息。 |
Z
zengyawen 已提交
1869 1870 1871 1872

**示例:**

```
L
lwx1059628 已提交
1873
audioRenderer.getRendererInfo((err, rendererInfo) => {
Z
zengyawen 已提交
1874 1875 1876 1877
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
L
lwx1059628 已提交
1878
});
Z
zengyawen 已提交
1879 1880 1881 1882 1883 1884
```

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

getRendererInfo(): Promise<AudioRendererInfo\>

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

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

**返回值:**

| 类型                                               | 说明                            |
| -------------------------------------------------- | ------------------------------- |
L
lwx1059628 已提交
1893
| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise用于返回音频渲染器信息。 |
Z
zengyawen 已提交
1894 1895 1896 1897

**示例:**

```
R
rahul 已提交
1898
var resultFlag = true;
L
lwx1059628 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907
audioRenderer.getRendererInfo().then((rendererInfo) => {
    console.log('Renderer GetRendererInfo:');
    console.log('Renderer content:' + rendererInfo.content);
    console.log('Renderer usage:' + rendererInfo.usage);
    console.log('Renderer flags:' + rendererInfo.rendererFlags);
}).catch((err) => {
    console.log('AudioFrameworkRenderLog: RendererInfo :ERROR: '+err.message);
    resultFlag = false;
});
Z
zengyawen 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
```

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

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

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

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

**参数:**

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

**示例:**

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

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

getStreamInfo(): Promise<AudioStreamInfo\>

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

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961
audioRenderer.getStreamInfo().then((streamInfo) => {
    console.log('Renderer GetStreamInfo:');
    console.log('Renderer sampling rate:' + streamInfo.samplingRate);
    console.log('Renderer channel:' + streamInfo.channels);
    console.log('Renderer format:' + streamInfo.sampleFormat);
    console.log('Renderer encoding type:' + streamInfo.encodingType);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
1962 1963 1964 1965 1966 1967
```

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

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

L
lwx1059628 已提交
1968
启动音频渲染器。使用callback方式异步返回结果。
Z
zengyawen 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
1981
audioRenderer.start((err) => {
Z
zengyawen 已提交
1982 1983 1984 1985 1986
    if (err) {
        console.error('Renderer start failed.');
    } else {
        console.info('Renderer start success.');
    }
L
lwx1059628 已提交
1987
});
Z
zengyawen 已提交
1988 1989 1990 1991 1992 1993
```

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

start(): Promise<void\>

L
lwx1059628 已提交
1994
启动音频渲染器。使用Promise方式异步返回结果。
Z
zengyawen 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2007 2008 2009 2010 2011
audioRenderer.start().then(() => {
    console.log('Renderer started');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2012 2013 2014 2015 2016 2017
```

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

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

L
lwx1059628 已提交
2018
暂停渲染。使用callback方式异步返回结果。
Z
zengyawen 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2031
audioRenderer.pause((err) => {
Z
zengyawen 已提交
2032 2033 2034 2035 2036
    if (err) {
        console.error('Renderer pause failed');
    } else {
        console.log('Renderer paused.');
    }
L
lwx1059628 已提交
2037
});
Z
zengyawen 已提交
2038 2039 2040 2041 2042 2043
```

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

pause(): Promise\<void>

L
lwx1059628 已提交
2044
暂停渲染。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2057 2058 2059 2060 2061
audioRenderer.pause().then(() => {
    console.log('Renderer paused');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2062 2063 2064 2065 2066 2067
```

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

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

L
lwx1059628 已提交
2068
检查缓冲区是否已被耗尽。使用callback方式异步返回结果。
Z
zengyawen 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2081
audioRenderer.drain((err) => {
Z
zengyawen 已提交
2082 2083 2084 2085 2086
    if (err) {
        console.error('Renderer drain failed');
    } else {
        console.log('Renderer drained.');
    }
L
lwx1059628 已提交
2087
});
Z
zengyawen 已提交
2088 2089 2090 2091 2092 2093
```

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

drain(): Promise\<void>

L
lwx1059628 已提交
2094
检查缓冲区是否已被耗尽。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2107 2108 2109 2110 2111
audioRenderer.drain().then(() => {
    console.log('Renderer drained successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2112 2113 2114 2115 2116 2117
```

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

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

L
lwx1059628 已提交
2118
停止渲染。使用callback方式异步返回结果。
Z
zengyawen 已提交
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2131
audioRenderer.stop((err) => {
Z
zengyawen 已提交
2132 2133 2134 2135 2136
    if (err) {
        console.error('Renderer stop failed');
    } else {
        console.log('Renderer stopped.');
    }
L
lwx1059628 已提交
2137
});
Z
zengyawen 已提交
2138 2139 2140 2141 2142 2143
```

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

stop(): Promise\<void>

L
lwx1059628 已提交
2144
停止渲染。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2157 2158 2159 2160 2161
audioRenderer.stop().then(() => {
    console.log('Renderer stopped successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2162 2163 2164 2165 2166 2167
```

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

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

L
lwx1059628 已提交
2168
释放音频渲染器。使用callback方式异步返回结果。
Z
zengyawen 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2181
audioRenderer.release((err) => {
Z
zengyawen 已提交
2182 2183 2184 2185 2186
    if (err) {
        console.error('Renderer release failed');
    } else {
        console.log('Renderer released.');
    }
L
lwx1059628 已提交
2187
});
Z
zengyawen 已提交
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
```

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

release(): Promise\<void>

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

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2207 2208 2209 2210 2211
audioRenderer.release().then(() => {
    console.log('Renderer released successfully');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
```

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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2232 2233 2234
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

Z
zengyawen 已提交
2235 2236 2237
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
L
lwx1059628 已提交
2238
audioRenderer.write(buf, (err, writtenbytes) => {
Z
zengyawen 已提交
2239
    if (writtenbytes < 0) {
L
lwx1059628 已提交
2240
        console.error('write failed.');
Z
zengyawen 已提交
2241 2242 2243
    } else {
       console.log('Actual written bytes: ' + writtenbytes);
    }
L
lwx1059628 已提交
2244
});
Z
zengyawen 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
```

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

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

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

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2264 2265 2266 2267
import audio from '@ohos.multimedia.audio';
import fileio from '@ohos.fileio';

var filePath = 'data/StarWars10s-2C-48000-4SW.wav';
Z
zengyawen 已提交
2268 2269 2270
let ss = fileio.createStreamSync(filePath, 'r');
let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf);
L
lwx1059628 已提交
2271 2272 2273 2274 2275 2276 2277 2278 2279
audioRenderer.write(buf).then((writtenbytes) => {
    if (writtenbytes < 0) {
        console.error('write failed.');
    } else {
        console.log('Actual written bytes: ' + writtenbytes);
    }
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2280 2281 2282 2283 2284 2285
```

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

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

L
lwx1059628 已提交
2286
获取时间戳(从 1970 年 1 月 1 日开始)。使用callback方式异步返回结果。
Z
zengyawen 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2299
audioRenderer.getAudioTime((err, timestamp) => {
Z
zengyawen 已提交
2300
    console.log('Current timestamp: ' + timestamp);
L
lwx1059628 已提交
2301
});
Z
zengyawen 已提交
2302 2303 2304 2305 2306 2307
```

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

getAudioTime(): Promise\<number>

L
lwx1059628 已提交
2308
获取时间戳(从 1970 年 1 月 1 日开始)。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2321 2322 2323 2324 2325
audioRenderer.getAudioTime().then((timestamp) => {
    console.log('Current timestamp: ' + timestamp);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2326 2327 2328 2329 2330 2331
```

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

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

L
lwx1059628 已提交
2332
获取音频渲染器的最小缓冲区大小。使用callback方式异步返回结果。
Z
zengyawen 已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344

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

**参数:**

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

**示例:**

```
R
rahul 已提交
2345
var bufferSize = audioRenderer.getBufferSize(async(err, bufferSize) => {
Z
zengyawen 已提交
2346 2347 2348
    if (err) {
        console.error('getBufferSize error');
    }
L
lwx1059628 已提交
2349
});
Z
zengyawen 已提交
2350 2351 2352 2353 2354 2355
```

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

getBufferSize(): Promise\<number>

L
lwx1059628 已提交
2356
获取音频渲染器的最小缓冲区大小。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368

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

**返回值:**

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

**示例:**

```
R
rahul 已提交
2369 2370 2371 2372
var bufferSize;
await audioRenderer.getBufferSize().then(async function (data) => {
    console.info('AudioFrameworkRenderLog: getBufferSize :SUCCESS '+data);
    bufferSize=data;
L
lwx1059628 已提交
2373
}).catch((err) => {
R
rahul 已提交
2374
    console.info('AudioFrameworkRenderLog: getBufferSize :ERROR : '+err.message);
L
lwx1059628 已提交
2375
});
Z
zengyawen 已提交
2376 2377 2378 2379 2380 2381
```

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

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

L
lwx1059628 已提交
2382
设置音频渲染速率。使用callback方式异步返回结果。
Z
zengyawen 已提交
2383 2384 2385 2386 2387 2388 2389

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

**参数:**

| 参数名   | 类型                                     | 必填 | 说明                     |
| -------- | ---------------------------------------- | ---- | ------------------------ |
L
lwx1059628 已提交
2390
| rate     | [AudioRendererRate](#audiorendererrate8) | 是   | 渲染的速率。             |
Z
zengyawen 已提交
2391 2392 2393 2394 2395
| callback | AsyncCallback\<void>                     | 是   | 用于返回执行结果的回调。 |

**示例:**

```
L
lwx1059628 已提交
2396
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err) => {
Z
zengyawen 已提交
2397
    if (err) {
L
lwx1059628 已提交
2398
        console.error('Failed to set params');
Z
zengyawen 已提交
2399 2400 2401
    } else {
        console.log('Callback invoked to indicate a successful render rate setting.');
    }
L
lwx1059628 已提交
2402
});
Z
zengyawen 已提交
2403 2404 2405 2406 2407 2408
```

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

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

L
lwx1059628 已提交
2409
设置音频渲染速率。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2410 2411 2412 2413 2414 2415 2416

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

**参数:**

| 参数名 | 类型                                     | 必填 | 说明         |
| ------ | ---------------------------------------- | ---- | ------------ |
L
lwx1059628 已提交
2417
| rate   | [AudioRendererRate](#audiorendererrate8) | 是   | 渲染的速率。 |
Z
zengyawen 已提交
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
2428 2429 2430 2431 2432
audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => {
    console.log('setRenderRate SUCCESS');
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2433 2434 2435 2436 2437 2438
```

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

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

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

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

**参数:**

| 参数名   | 类型                                                    | 必填 | 说明               |
| -------- | ------------------------------------------------------- | ---- | ------------------ |
L
lwx1059628 已提交
2447
| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是   | 回调返回渲染速率。 |
Z
zengyawen 已提交
2448 2449 2450 2451

**示例:**

```
L
lwx1059628 已提交
2452
audioRenderer.getRenderRate((err, renderrate) => {
Z
zengyawen 已提交
2453
    console.log('getRenderRate: ' + renderrate);
L
lwx1059628 已提交
2454
});
Z
zengyawen 已提交
2455 2456 2457 2458 2459 2460
```

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

getRenderRate(): Promise\<AudioRendererRate>

L
lwx1059628 已提交
2461
获取当前渲染速率。使用Promise方式异步返回结果。
Z
zengyawen 已提交
2462 2463 2464 2465 2466 2467 2468

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

**返回值:**

| 类型                                              | 说明                      |
| ------------------------------------------------- | ------------------------- |
L
lwx1059628 已提交
2469
| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise回调返回渲染速率。 |
Z
zengyawen 已提交
2470 2471 2472 2473

**示例:**

```
L
lwx1059628 已提交
2474 2475 2476 2477 2478
audioRenderer.getRenderRate().then((renderRate) => {
    console.log('getRenderRate: ' + renderRate);
}).catch((err) => {
    console.log('ERROR: '+err.message);
});
Z
zengyawen 已提交
2479 2480
```

L
lwx1059628 已提交
2481
### on('interrupt')<sup>9+</sup>
Z
zengyawen 已提交
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493

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

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

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

**参数:**

| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                       | 是   | 事件回调类型,支持的事件为:'interrupt'(中断事件被触发,音频播放被中断。) |
L
lwx1059628 已提交
2494
| callback | Callback<[InterruptEvent](#interruptevent9)> | 是   | 被监听的中断事件的回调。                                     |
Z
zengyawen 已提交
2495 2496 2497 2498

**示例:**

```
R
rahul 已提交
2499 2500 2501
var isPlay;
var started;
audioRenderer.on('interrupt', async(interruptEvent) => {
Z
zengyawen 已提交
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
    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) {
R
rahul 已提交
2514
        switch (interruptEvent.hintType) {
Z
zengyawen 已提交
2515 2516
            case audio.InterruptHint.INTERRUPT_HINT_RESUME:
                console.log('Resume force paused renderer or ignore');
R
rahul 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
                await audioRenderer.start().then(async function () {
                    console.info('AudioInterruptMusic: renderInstant started :SUCCESS ');
                    started = true;
                }).catch((err) => {
                    console.info('AudioInterruptMusic: renderInstant start :ERROR : '+err.message);
                    started = false;
                });
                if (started) {
                    isPlay = true;
                    console.info('AudioInterruptMusic Renderer started : isPlay : '+isPlay);
                } else {
                    console.error('AudioInterruptMusic Renderer start failed');
                }
Z
zengyawen 已提交
2530 2531 2532
                break;
            case audio.InterruptHint.INTERRUPT_HINT_PAUSE:
                console.log('Choose to pause or ignore');
R
rahul 已提交
2533 2534 2535 2536 2537 2538 2539 2540
                if (isPlay == true) {
                    isPlay == false;
                    console.info('AudioInterruptMusic: Media PAUSE : TRUE');
                }
                else {
                    isPlay = true;
                    console.info('AudioInterruptMusic: Media PLAY : TRUE');
                }
Z
zengyawen 已提交
2541 2542 2543
                break;
        }
    }
L
lwx1059628 已提交
2544
});
Z
zengyawen 已提交
2545 2546
```

L
lwx1059628 已提交
2547 2548 2549 2550 2551 2552 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
### 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 已提交
2595

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

L
lwx1059628 已提交
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
订阅到达标记的事件。 当渲染的帧数达到 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'。 |
Z
zengyawen 已提交
2653
| callback | [AudioState](#audiostate8) | 是   | 返回监听的状态。                            |
L
lwx1059628 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671

**示例:**

```
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)创建实例。

2672
### 属性
L
lwx1059628 已提交
2673 2674 2675

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

2676
| 名称  | 类型                     | 可读 | 可写 | 说明             |
L
lwx1059628 已提交
2677
| :---- | :------------------------- | :--- | :--- | :--------------- |
2678
| state<sup>8+</sup>  | [AudioState](#audiostate8) | 是 | 否   | 音频采集器状态。 |
L
lwx1059628 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702

**示例:**

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

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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2703
audioCapturer.getCapturerInfo((err, capturerInfo) => {
L
lwx1059628 已提交
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
    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方式异步返回采集器信息。 |

**示例:**

```
L
lwx1059628 已提交
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
audioCapturer.getCapturerInfo().then((audioParamsGet) => {
    if (audioParamsGet != undefined) {
        console.info('AudioFrameworkRecLog: Capturer CapturerInfo:');
        console.info('AudioFrameworkRecLog: Capturer SourceType:' + audioParamsGet.source);
        console.info('AudioFrameworkRecLog: Capturer capturerFlags:' + audioParamsGet.capturerFlags);
    }else {
        console.info('AudioFrameworkRecLog: audioParamsGet is : '+audioParamsGet);
        console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect: ');
    }
}).catch((err) => {
    console.log('AudioFrameworkRecLog: CapturerInfo :ERROR: '+err.message);
L
lwx1059628 已提交
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
});
```

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

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

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

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

**参数:**

Z
zengyawen 已提交
2756 2757 2758
| 参数名   | 类型                                                 | 必填 | 说明                             |
| :------- | :--------------------------------------------------- | :--- | :------------------------------- |
| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是   | 使用callback方式异步返回流信息。 |
L
lwx1059628 已提交
2759 2760 2761 2762

**示例:**

```
L
lwx1059628 已提交
2763
audioCapturer.getStreamInfo((err, streamInfo) => {
L
lwx1059628 已提交
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
    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

**返回值:**

Z
zengyawen 已提交
2786 2787 2788
| 类型                                           | 说明                            |
| :--------------------------------------------- | :------------------------------ |
| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | 使用Promise方式异步返回流信息。 |
L
lwx1059628 已提交
2789 2790 2791 2792

**示例:**

```
L
lwx1059628 已提交
2793 2794 2795 2796 2797 2798 2799 2800
audioCapturer.getStreamInfo().then((audioParamsGet) => {
    console.info('getStreamInfo:');
    console.info('sampleFormat:' + audioParamsGet.sampleFormat);
    console.info('samplingRate:' + audioParamsGet.samplingRate);
    console.info('channels:' + audioParamsGet.channels);
    console.info('encodingType:' + audioParamsGet.encodingType);
}).catch((err) => {
    console.log('getStreamInfo :ERROR: ' + err.message);
L
lwx1059628 已提交
2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
});
```

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

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

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

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

**参数**

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

**示例:**

```
L
lwx1059628 已提交
2821
audioCapturer.start((err) => {
L
lwx1059628 已提交
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
    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(() => {
L
lwx1059628 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
    console.info('AudioFrameworkRecLog: ---------START---------');
    console.info('AudioFrameworkRecLog: Capturer started :SUCCESS ');
    console.info('AudioFrameworkRecLog: AudioCapturer : STATE : '+audioCapturer.state);
    console.info('AudioFrameworkRecLog: Capturer started :SUCCESS ');
    if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) {
        stateFlag = true;
    }
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer start :ERROR : '+err.message);
    stateFlag=false;
L
lwx1059628 已提交
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
});
```

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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2879
audioCapturer.stop((err) => {
L
lwx1059628 已提交
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
    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(() => {
L
lwx1059628 已提交
2907 2908
    console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------');
    console.info('AudioFrameworkRecLog: Capturer stopped : SUCCESS');
R
rahul 已提交
2909
    if ((audioCapturer.state == audio.AudioState.STATE_STOPPED)){
L
lwx1059628 已提交
2910 2911 2912 2913 2914 2915
        stateFlag=true;
        console.info('AudioFrameworkRecLog: resultFlag : '+stateFlag);
    }
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer stop:ERROR : '+err.message);
    stateFlag=false;
L
lwx1059628 已提交
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
});
```

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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
2936
audioCapturer.release((err) => {
L
lwx1059628 已提交
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
    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(() => {
L
lwx1059628 已提交
2964 2965 2966 2967 2968 2969 2970 2971
    console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------');
    console.info('AudioFrameworkRecLog: Capturer release : SUCCESS');
    console.info('AudioFrameworkRecLog: AudioCapturer : STATE : '+audioCapturer.state);
    stateFlag=true;
    console.info('AudioFrameworkRecLog: stateFlag : '+stateFlag);
}).catch((err) => {
    console.info('AudioFrameworkRecLog: Capturer stop:ERROR : '+err.message);
    stateFlag=false
L
lwx1059628 已提交
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
});
```


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


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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```
L
lwx1059628 已提交
3027 3028 3029 3030
audioCapturer.read(bufferSize, true).then((buffer) => {
    console.info('buffer read successfully');
}).catch((err) => {
    console.info('ERROR : '+err.message);
L
lwx1059628 已提交
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
});
```


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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
3052
audioCapturer.getAudioTime((err, timestamp) => {
L
lwx1059628 已提交
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
    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) => {
L
lwx1059628 已提交
3076 3077 3078
    console.info('AudioFrameworkRecLog: AudioCapturer getAudioTime : Success' + audioTime );
}).catch((err) => {
    console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
L
lwx1059628 已提交
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
});
```


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

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

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

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

**参数:**

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

**示例:**

```
L
lwx1059628 已提交
3100
audioCapturer.getBufferSize((err, bufferSize) => {
L
lwx1059628 已提交
3101 3102
    if (!err) {
        console.log('BufferSize : ' + bufferSize);
L
lwx1059628 已提交
3103 3104 3105 3106 3107
        audioCapturer.read(bufferSize, true).then((buffer) => {
            console.info('Buffer read is ' + buffer );
        }).catch((err) => {
            console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message);
        });
L
lwx1059628 已提交
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
    }
});
```


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

getBufferSize(): Promise<number\>

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

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

**返回值:**

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

**示例:**

```
R
rahul 已提交
3130 3131 3132 3133 3134 3135
await audioCapturer.getBufferSize().then(async function (bufferSize) {
    console.info('AudioFrameworkRecordLog: getBufferSize :SUCCESS '+ bufferSize);
    var buffer = await audioCapturer.read(bufferSize, true);
    console.info('Buffer read is ' + buffer );
    }).catch((err) => {
    console.info('AudioFrameworkRecordLog: getBufferSize :ERROR : '+err.message);
L
lwx1059628 已提交
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 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
});
```


### 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'。 |
Z
zengyawen 已提交
3245
| callback | [AudioState](#audiostate8) | 是   | 返回监听的状态。                            |
L
lwx1059628 已提交
3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257

**示例:**

```
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");
    }
});
H
update  
HelloCrease 已提交
3258
```