1# @ohos.multimedia.audio (音频管理) 2 3音频管理提供管理音频的一些基础能力,包括对音频音量、音频设备的管理,以及对音频数据的采集和渲染等。 4 5该模块提供以下音频相关的常用功能: 6 7- [AudioManager](#audiomanager):音频管理。 8- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。 9- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM音频数据。 10 11> **说明:** 12> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13 14## 导入模块 15 16```ts 17import { audio } from '@kit.AudioKit'; 18``` 19 20## 常量 21 22| 名称 | 类型 | 可读 | 可写 | 说明 | 23| --------------------------------------- | ----------| ---- | ---- | ------------------ | 24| DEFAULT_VOLUME_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音量组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Volume | 25| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音频中断组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Interrupt | 26 27**示例:** 28 29```ts 30import { audio } from '@kit.AudioKit'; 31 32const defaultVolumeGroupId = audio.DEFAULT_VOLUME_GROUP_ID; 33const defaultInterruptGroupId = audio.DEFAULT_INTERRUPT_GROUP_ID; 34``` 35 36## audio.getAudioManager 37 38getAudioManager(): AudioManager 39 40获取音频管理器。 41 42**系统能力:** SystemCapability.Multimedia.Audio.Core 43 44**返回值:** 45 46| 类型 | 说明 | 47| ----------------------------- | ------------ | 48| [AudioManager](#audiomanager) | 音频管理对象。 | 49 50**示例:** 51```ts 52import { audio } from '@kit.AudioKit'; 53 54let audioManager = audio.getAudioManager(); 55``` 56 57## audio.createAudioRenderer<sup>8+</sup> 58 59createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void 60 61获取音频渲染器。使用callback方式异步返回结果。 62 63**系统能力:** SystemCapability.Multimedia.Audio.Renderer 64 65**参数:** 66 67| 参数名 | 类型 | 必填 | 说明 | 68| -------- | ----------------------------------------------- | ---- | ---------------- | 69| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 70| callback | AsyncCallback<[AudioRenderer](#audiorenderer8)> | 是 | 回调函数。当获取音频渲染器成功,err为undefined,data为获取到的音频渲染器对象;否则为错误对象。 | 71 72**示例:** 73 74```ts 75import { audio } from '@kit.AudioKit'; 76 77let audioStreamInfo: audio.AudioStreamInfo = { 78 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 79 channels: audio.AudioChannel.CHANNEL_2, // 通道。 80 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 81 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 82}; 83 84let audioRendererInfo: audio.AudioRendererInfo = { 85 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 86 rendererFlags: 0 // 音频渲染器标志。 87}; 88 89let audioRendererOptions: audio.AudioRendererOptions = { 90 streamInfo: audioStreamInfo, 91 rendererInfo: audioRendererInfo 92}; 93 94audio.createAudioRenderer(audioRendererOptions,(err, data) => { 95 if (err) { 96 console.error(`AudioRenderer Created: Error: ${err}`); 97 } else { 98 console.info('AudioRenderer Created: Success: SUCCESS'); 99 let audioRenderer = data; 100 } 101}); 102``` 103 104## audio.createAudioRenderer<sup>8+</sup> 105 106createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\> 107 108获取音频渲染器。使用Promise方式异步返回结果。 109 110**系统能力:** SystemCapability.Multimedia.Audio.Renderer 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| :------ | :--------------------------------------------- | :--- | :----------- | 116| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 117 118**返回值:** 119 120| 类型 | 说明 | 121| ----------------------------------------- | ---------------- | 122| Promise<[AudioRenderer](#audiorenderer8)> | Promise对象,返回音频渲染器对象。 | 123 124**示例:** 125 126```ts 127import { audio } from '@kit.AudioKit'; 128import { BusinessError } from '@kit.BasicServicesKit'; 129 130let audioStreamInfo: audio.AudioStreamInfo = { 131 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 132 channels: audio.AudioChannel.CHANNEL_2, // 通道。 133 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 134 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 135}; 136 137let audioRendererInfo: audio.AudioRendererInfo = { 138 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 139 rendererFlags: 0 // 音频渲染器标志。 140}; 141 142let audioRendererOptions: audio.AudioRendererOptions = { 143 streamInfo: audioStreamInfo, 144 rendererInfo: audioRendererInfo 145}; 146 147let audioRenderer: audio.AudioRenderer; 148 149audio.createAudioRenderer(audioRendererOptions).then((data) => { 150 audioRenderer = data; 151 console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS'); 152}).catch((err: BusinessError) => { 153 console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`); 154}); 155``` 156 157## audio.createAudioCapturer<sup>8+</sup> 158 159createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void 160 161获取音频采集器。使用callback方式异步返回结果。 162 163**系统能力:** SystemCapability.Multimedia.Audio.Capturer 164 165**需要权限:** ohos.permission.MICROPHONE 166 167当设置Mic音频源(即[SourceType](#sourcetype8)为SOURCE_TYPE_MIC、SOURCE_TYPE_VOICE_RECOGNITION、SOURCE_TYPE_VOICE_COMMUNICATION、SOURCE_TYPE_VOICE_MESSAGE、SOURCE_TYPE_CAMCORDER)时需要该权限。 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| :------- | :---------------------------------------------- | :--- | :--------------- | 173| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 174| callback | AsyncCallback<[AudioCapturer](#audiocapturer8)> | 是 | Callback对象,成功将返回音频采集器对象,异常将返回error对象:<br>错误码6800301,表示包含参数校验异常、权限校验异常、系统处理异常(具体错误查看系统日志)。<br>错误码6800101,表示包含必选参数为空、参数类型错误。 | 175 176**示例:** 177 178```ts 179import { audio } from '@kit.AudioKit'; 180 181let audioStreamInfo: audio.AudioStreamInfo = { 182 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 183 channels: audio.AudioChannel.CHANNEL_2, // 通道。 184 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 185 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 186}; 187 188let audioCapturerInfo: audio.AudioCapturerInfo = { 189 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 190 capturerFlags: 0 // 音频采集器标志。 191}; 192 193let audioCapturerOptions: audio.AudioCapturerOptions = { 194 streamInfo: audioStreamInfo, 195 capturerInfo: audioCapturerInfo 196}; 197 198audio.createAudioCapturer(audioCapturerOptions, (err, data) => { 199 if (err) { 200 console.error(`AudioCapturer Created : Error: ${err}`); 201 } else { 202 console.info('AudioCapturer Created : Success : SUCCESS'); 203 let audioCapturer = data; 204 } 205}); 206``` 207 208## audio.createAudioCapturer<sup>8+</sup> 209 210createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\> 211 212获取音频采集器。使用Promise 方式异步返回结果。 213 214**系统能力:** SystemCapability.Multimedia.Audio.Capturer 215 216**需要权限:** ohos.permission.MICROPHONE 217 218当设置Mic音频源(即[SourceType](#sourcetype8)为SOURCE_TYPE_MIC、SOURCE_TYPE_VOICE_RECOGNITION、SOURCE_TYPE_VOICE_COMMUNICATION、SOURCE_TYPE_VOICE_MESSAGE、SOURCE_TYPE_CAMCORDER)时需要该权限。 219 220**参数:** 221 222| 参数名 | 类型 | 必填 | 说明 | 223| :------ | :--------------------------------------------- | :--- | :--------------- | 224| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 225 226**返回值:** 227 228| 类型 | 说明 | 229| ----------------------------------------- |----------------------| 230| Promise<[AudioCapturer](#audiocapturer8)> | Promise对象,成功将返回音频采集器对象,异常将返回error对象:<br>错误码6800301,表示包含参数校验异常、权限校验异常、系统处理异常(具体错误查看系统日志)。<br>错误码6800101,表示包含必选参数为空、参数类型错误。 | 231 232**示例:** 233 234```ts 235import { audio } from '@kit.AudioKit'; 236import { BusinessError } from '@kit.BasicServicesKit'; 237 238let audioStreamInfo: audio.AudioStreamInfo = { 239 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 240 channels: audio.AudioChannel.CHANNEL_2, // 通道。 241 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 242 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 243}; 244 245let audioCapturerInfo: audio.AudioCapturerInfo = { 246 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 247 capturerFlags: 0 // 音频采集器标志。 248}; 249 250let audioCapturerOptions:audio.AudioCapturerOptions = { 251 streamInfo: audioStreamInfo, 252 capturerInfo: audioCapturerInfo 253}; 254 255let audioCapturer: audio.AudioCapturer; 256 257audio.createAudioCapturer(audioCapturerOptions).then((data) => { 258 audioCapturer = data; 259 console.info('AudioCapturer Created : Success : Stream Type: SUCCESS'); 260}).catch((err: BusinessError) => { 261 console.error(`AudioCapturer Created : ERROR : ${err}`); 262}); 263``` 264 265## AudioVolumeType 266 267枚举,音频流类型。 268 269**系统能力:** SystemCapability.Multimedia.Audio.Volume 270 271| 名称 | 值 | 说明 | 272| ---------------------------- | ------ | ---------- | 273| VOICE_CALL<sup>8+</sup> | 0 | 语音电话。 | 274| RINGTONE | 2 | 铃声。 | 275| MEDIA | 3 | 媒体。 | 276| ALARM<sup>10+</sup> | 4 | 闹钟。 | 277| ACCESSIBILITY<sup>10+</sup> | 5 | 无障碍。 | 278| VOICE_ASSISTANT<sup>8+</sup> | 9 | 语音助手。 | 279 280## InterruptMode<sup>9+</sup> 281 282枚举,焦点模型。 283 284**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 285 286**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 287 288| 名称 | 值 | 说明 | 289| ---------------------------- | ------ | ---------- | 290| SHARE_MODE | 0 | 共享焦点模式。 | 291| INDEPENDENT_MODE | 1 | 独立焦点模式。 | 292 293## DeviceFlag 294 295枚举,可获取的设备种类。 296 297**系统能力:** SystemCapability.Multimedia.Audio.Device 298 299| 名称 | 值 | 说明 | 300| ------------------------------- | ------ |---------------------------| 301| OUTPUT_DEVICES_FLAG | 1 | 输出设备。 | 302| INPUT_DEVICES_FLAG | 2 | 输入设备。 | 303| ALL_DEVICES_FLAG | 3 | 所有设备。 | 304 305## DeviceUsage<sup>12+</sup> 306 307枚举,可获取的设备种类。 308 309**系统能力:** SystemCapability.Multimedia.Audio.Device 310 311| 名称 | 值 | 说明 | 312| ------------------------------- | ------ |---------------------------| 313| MEDIA_OUTPUT_DEVICES | 1 | 媒体输出设备。| 314| MEDIA_INPUT_DEVICES | 2 | 媒体输入设备。| 315| ALL_MEDIA_DEVICES | 3 | 所有媒体设备。| 316| CALL_OUTPUT_DEVICES | 4 | 通话输出设备。| 317| CALL_INPUT_DEVICES | 8 | 通话输入设备。| 318| ALL_CALL_DEVICES | 12 | 所有通话设备。| 319 320## DeviceRole 321 322枚举,设备角色。 323 324**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 325 326**系统能力:** SystemCapability.Multimedia.Audio.Device 327 328| 名称 | 值 | 说明 | 329| ------------- | ------ | -------------- | 330| INPUT_DEVICE | 1 | 输入设备角色。 | 331| OUTPUT_DEVICE | 2 | 输出设备角色。 | 332 333## DeviceType 334 335枚举,设备类型。 336 337**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 338 339**系统能力:** SystemCapability.Multimedia.Audio.Device 340 341| 名称 | 值 | 说明 | 342| ---------------------| ------ | --------------------------------------------------------- | 343| INVALID | 0 | 无效设备。 | 344| EARPIECE | 1 | 听筒。 | 345| SPEAKER | 2 | 扬声器。 | 346| WIRED_HEADSET | 3 | 有线耳机,带麦克风。 | 347| WIRED_HEADPHONES | 4 | 有线耳机,无麦克风。 | 348| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 349| BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。 | 350| MIC | 15 | 麦克风。 | 351| USB_HEADSET | 22 | USB耳机,带麦克风。 | 352| DISPLAY_PORT<sup>12+</sup> | 23 | DisplayPort(显示接口,简称DP),用于外接扩展设备。 | 353| REMOTE_CAST<sup>12+</sup> | 24 | 音频被系统应用投送到其他远程的设备。 | 354| DEFAULT<sup>9+</sup> | 1000 | 默认设备类型。 | 355 356## CommunicationDeviceType<sup>9+</sup> 357 358枚举,用于通信的可用设备类型。 359 360**系统能力:** SystemCapability.Multimedia.Audio.Communication 361 362| 名称 | 值 | 说明 | 363| ------------- | ------ | -------------| 364| SPEAKER | 2 | 扬声器。 | 365 366## AudioRingMode 367 368枚举,铃声模式。 369 370**系统能力:** SystemCapability.Multimedia.Audio.Communication 371 372| 名称 | 值 | 说明 | 373| ------------------- | ------ | ---------- | 374| RINGER_MODE_SILENT | 0 | 静音模式。 | 375| RINGER_MODE_VIBRATE | 1 | 震动模式。 | 376| RINGER_MODE_NORMAL | 2 | 响铃模式。 | 377 378## AudioSampleFormat<sup>8+</sup> 379 380枚举,音频采样格式。 381 382**系统能力:** SystemCapability.Multimedia.Audio.Core 383 384| 名称 | 值 | 说明 | 385| ---------------------------------- | ------ | -------------------------- | 386| SAMPLE_FORMAT_INVALID | -1 | 无效格式。 | 387| SAMPLE_FORMAT_U8 | 0 | 无符号8位整数。 | 388| SAMPLE_FORMAT_S16LE | 1 | 带符号的16位整数,小尾数。 | 389| SAMPLE_FORMAT_S24LE | 2 | 带符号的24位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 390| SAMPLE_FORMAT_S32LE | 3 | 带符号的32位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 391| SAMPLE_FORMAT_F32LE<sup>9+</sup> | 4 | 带符号的32位浮点数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 392 393## AudioErrors<sup>9+</sup> 394 395枚举,音频错误码。 396 397**系统能力:** SystemCapability.Multimedia.Audio.Core 398 399| 名称 | 值 | 说明 | 400| ---------------------| --------| ----------------- | 401| ERROR_INVALID_PARAM | 6800101 | 无效入参。 | 402| ERROR_NO_MEMORY | 6800102 | 分配内存失败。 | 403| ERROR_ILLEGAL_STATE | 6800103 | 状态不支持。 | 404| ERROR_UNSUPPORTED | 6800104 | 参数选项不支持。 | 405| ERROR_TIMEOUT | 6800105 | 处理超时。 | 406| ERROR_STREAM_LIMIT | 6800201 | 音频流数量达到限制。| 407| ERROR_SYSTEM | 6800301 | 系统处理异常。 | 408 409## AudioChannel<sup>8+</sup> 410 411枚举, 音频声道。 412 413**系统能力:** SystemCapability.Multimedia.Audio.Core 414 415| 名称 | 值 | 说明 | 416| --------- | -------- |------| 417| CHANNEL_1 | 1 | 单声道。 | 418| CHANNEL_2 | 2 | 双声道。 | 419| CHANNEL_3<sup>11+</sup> | 3 | 三声道。 | 420| CHANNEL_4<sup>11+</sup> | 4 | 四声道。 | 421| CHANNEL_5<sup>11+</sup> | 5 | 五声道。 | 422| CHANNEL_6<sup>11+</sup> | 6 | 六声道。 | 423| CHANNEL_7<sup>11+</sup> | 7 | 七声道。 | 424| CHANNEL_8<sup>11+</sup> | 8 | 八声道。 | 425| CHANNEL_9<sup>11+</sup> | 9 | 九声道。 | 426| CHANNEL_10<sup>11+</sup> | 10 | 十声道。 | 427| CHANNEL_12<sup>11+</sup> | 12 | 十二声道。 | 428| CHANNEL_14<sup>11+</sup> | 14 | 十四声道。 | 429| CHANNEL_16<sup>11+</sup> | 16 | 十六声道。 | 430 431## AudioSamplingRate<sup>8+</sup> 432 433枚举,音频采样率,具体设备支持的采样率规格会存在差异。 434 435**系统能力:** SystemCapability.Multimedia.Audio.Core 436 437| 名称 | 值 | 说明 | 438| ----------------- | ------ | --------------- | 439| SAMPLE_RATE_8000 | 8000 | 采样率为8000。 | 440| SAMPLE_RATE_11025 | 11025 | 采样率为11025。 | 441| SAMPLE_RATE_12000 | 12000 | 采样率为12000。 | 442| SAMPLE_RATE_16000 | 16000 | 采样率为16000。 | 443| SAMPLE_RATE_22050 | 22050 | 采样率为22050。 | 444| SAMPLE_RATE_24000 | 24000 | 采样率为24000。 | 445| SAMPLE_RATE_32000 | 32000 | 采样率为32000。 | 446| SAMPLE_RATE_44100 | 44100 | 采样率为44100。 | 447| SAMPLE_RATE_48000 | 48000 | 采样率为48000。 | 448| SAMPLE_RATE_64000 | 64000 | 采样率为64000。 | 449| SAMPLE_RATE_88200<sup>12+</sup> | 88200 | 采样率为88200。 | 450| SAMPLE_RATE_96000 | 96000 | 采样率为96000。 | 451| SAMPLE_RATE_176400<sup>12+</sup> | 176400 | 采样率为176400。 | 452| SAMPLE_RATE_192000<sup>12+</sup> | 192000 | 采样率为192000。 | 453 454## AudioEncodingType<sup>8+</sup> 455 456枚举,音频编码类型。 457 458**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 459 460**系统能力:** SystemCapability.Multimedia.Audio.Core 461 462| 名称 | 值 | 说明 | 463| --------------------- | ------ | --------- | 464| ENCODING_TYPE_INVALID | -1 | 无效。 | 465| ENCODING_TYPE_RAW | 0 | PCM编码。 | 466 467## AudioChannelLayout<sup>11+</sup> 468 469枚举,音频文件声道布局类型。 470 471**系统能力:** SystemCapability.Multimedia.Audio.Core 472 473| 名称 | 值 | 说明 | 474| ------------------------------ | ---------------- | --------------------------------------------- | 475| CH_LAYOUT_UNKNOWN | 0x0 | 未知声道布局。 | 476| CH_LAYOUT_MONO | 0x4 | 声道布局为MONO。 | 477| CH_LAYOUT_STEREO | 0x3 | 声道布局为STEREO。 | 478| CH_LAYOUT_STEREO_DOWNMIX | 0x60000000 | 声道布局为STEREO-DOWNMIX。 | 479| CH_LAYOUT_2POINT1 | 0xB | 声道布局为2.1。 | 480| CH_LAYOUT_3POINT0 | 0x103 | 声道布局为3.0。 | 481| CH_LAYOUT_SURROUND | 0x7 | 声道布局为SURROUND。 | 482| CH_LAYOUT_3POINT1 | 0xF | 声道布局为3.1。 | 483| CH_LAYOUT_4POINT0 | 0x107 | 声道布局为4.0。 | 484| CH_LAYOUT_QUAD | 0x33 | 声道布局为QUAD。 | 485| CH_LAYOUT_QUAD_SIDE | 0x603 | 声道布局为QUAD-SIDE。 | 486| CH_LAYOUT_2POINT0POINT2 | 0x3000000003 | 声道布局为2.0.2。 | 487| CH_LAYOUT_AMB_ORDER1_ACN_N3D | 0x100000000001 | 声道排序为ACN_N3D(根据ITU标准)的一阶FOA文件。 | 488| CH_LAYOUT_AMB_ORDER1_ACN_SN3D | 0x100000001001 | 声道排序为ACN_SN3D(根据ITU标准)的一阶FOA文件。 | 489| CH_LAYOUT_AMB_ORDER1_FUMA | 0x100000000101 | 声道排序为FUMA(根据ITU标准)的一阶FOA文件。 | 490| CH_LAYOUT_4POINT1 | 0x10F | 声道布局为4.1 | 491| CH_LAYOUT_5POINT0 | 0x607 | 声道布局为5.0。 | 492| CH_LAYOUT_5POINT0_BACK | 0x37 | 声道布局为5.0-BACK。 | 493| CH_LAYOUT_2POINT1POINT2 | 0x300000000B | 声道布局为2.1.2。 | 494| CH_LAYOUT_3POINT0POINT2 | 0x3000000007 | 声道布局为3.0.2。 | 495| CH_LAYOUT_5POINT1 | 0x60F | 声道布局为5.1。 | 496| CH_LAYOUT_5POINT1_BACK | 0x3F | 声道布局为5.1-BACK。 | 497| CH_LAYOUT_6POINT0 | 0x707 | 声道布局为6.0。 | 498| CH_LAYOUT_HEXAGONAL | 0x137 | 声道布局为HEXAGONAL。 | 499| CH_LAYOUT_3POINT1POINT2 | 0x500F | 声道布局为3.1.2。 | 500| CH_LAYOUT_6POINT0_FRONT | 0x6C3 | 声道布局为6.0-FRONT。 | 501| CH_LAYOUT_6POINT1 | 0x70F | 声道布局为6.1。 | 502| CH_LAYOUT_6POINT1_BACK | 0x13F | 声道布局为6.1-BACK。 | 503| CH_LAYOUT_6POINT1_FRONT | 0x6CB | 声道布局为6.1-FRONT。 | 504| CH_LAYOUT_7POINT0 | 0x637 | 声道布局为7.0。 | 505| CH_LAYOUT_7POINT0_FRONT | 0x6C7 | 声道布局为7.0-FRONT。 | 506| CH_LAYOUT_7POINT1 | 0x63F | 声道布局为7.1。 | 507| CH_LAYOUT_OCTAGONAL | 0x737 | 声道布局为OCTAGONAL。 | 508| CH_LAYOUT_5POINT1POINT2 | 0x300000060F | 声道布局为5.1.2。 | 509| CH_LAYOUT_7POINT1_WIDE | 0x6CF | 声道布局为7.1-WIDE。 | 510| CH_LAYOUT_7POINT1_WIDE_BACK | 0xFF | 声道布局为7.1-WIDE-BACK。 | 511| CH_LAYOUT_AMB_ORDER2_ACN_N3D | 0x100000000002 | 声道排序为ACN_N3D(根据ITU标准)的二阶HOA文件。 | 512| CH_LAYOUT_AMB_ORDER2_ACN_SN3D | 0x100000001002 | 声道排序为ACN_SN3D(根据ITU标准)的二阶HOA文件。 | 513| CH_LAYOUT_AMB_ORDER2_FUMA | 0x100000000102 | 声道排序为FUMA(根据ITU标准)的二阶HOA文件。 | 514| CH_LAYOUT_5POINT1POINT4 | 0x2D60F | 声道布局为5.1.4。 | 515| CH_LAYOUT_7POINT1POINT2 | 0x300000063F | 声道布局为7.1.2。 | 516| CH_LAYOUT_7POINT1POINT4 | 0x2D63F | 声道布局为7.1.4。 | 517| CH_LAYOUT_10POINT2 | 0x180005737 | 声道布局为10.2。 | 518| CH_LAYOUT_9POINT1POINT4 | 0x18002D63F | 声道布局为9.1.4。 | 519| CH_LAYOUT_9POINT1POINT6 | 0x318002D63F | 声道布局为9.1.6。 | 520| CH_LAYOUT_HEXADECAGONAL | 0x18003F737 | 声道布局为HEXADECAGONAL。 | 521| CH_LAYOUT_AMB_ORDER3_ACN_N3D | 0x100000000003 | 声道排序为ACN_N3D(根据ITU标准)的三阶HOA文件。 | 522| CH_LAYOUT_AMB_ORDER3_ACN_SN3D | 0x100000001003 | 声道排序为ACN_SN3D(根据ITU标准)的三阶HOA文件。 | 523| CH_LAYOUT_AMB_ORDER3_FUMA | 0x100000000103 | 声道排序为FUMA(根据ITU标准)的三阶HOA文件。 | 524 525## ContentType<sup>(deprecated)</sup> 526 527枚举,音频内容类型。 528 529> **说明:** 530> 从 API version 7 开始支持,从 API version 10 开始废弃。建议使用[StreamUsage](#streamusage)声明音频流使用类型即可。 531 532**系统能力:** SystemCapability.Multimedia.Audio.Core 533 534| 名称 | 值 | 说明 | 535| ---------------------------------- | ------ | ---------- | 536| CONTENT_TYPE_UNKNOWN | 0 | 未知类型。 | 537| CONTENT_TYPE_SPEECH | 1 | 语音。 | 538| CONTENT_TYPE_MUSIC | 2 | 音乐。 | 539| CONTENT_TYPE_MOVIE | 3 | 电影。 | 540| CONTENT_TYPE_SONIFICATION | 4 | 通知音。 | 541| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5 | 铃声。 | 542 543## StreamUsage 544 545枚举,音频流使用类型。 546 547**系统能力:** SystemCapability.Multimedia.Audio.Core 548 549| 名称 | 值 | 说明 | 550| ------------------------------------------| ------ |---------------------------------------------------------------------------------------------------------------------------------------------| 551| STREAM_USAGE_UNKNOWN | 0 | 未知类型。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 552| STREAM_USAGE_MEDIA<sup>(deprecated)</sup> | 1 | 媒体。<br/> 从API version 7开始支持,从API version 10 开始废弃。建议使用该枚举中的STREAM_USAGE_MUSIC、STREAM_USAGE_MOVIE、STREAM_USAGE_GAME或STREAM_USAGE_AUDIOBOOK替代。 | 553| STREAM_USAGE_MUSIC<sup>10+</sup> | 1 | 音乐。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 554| STREAM_USAGE_VOICE_COMMUNICATION | 2 | VoIP语音通话。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 555| STREAM_USAGE_VOICE_ASSISTANT<sup>9+</sup> | 3 | 语音播报。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 556| STREAM_USAGE_ALARM<sup>10+</sup> | 4 | 闹钟。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 557| STREAM_USAGE_VOICE_MESSAGE<sup>10+</sup> | 5 | 语音消息。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 558| STREAM_USAGE_NOTIFICATION_RINGTONE<sup>(deprecated)</sup> | 6 | 通知铃声。<br/> 从 API version 10 开始废弃。建议使用该枚举中的STREAM_USAGE_RINGTONE替代。 | 559| STREAM_USAGE_RINGTONE<sup>10+</sup> | 6 | 铃声。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 560| STREAM_USAGE_NOTIFICATION<sup>10+</sup> | 7 | 通知。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 561| STREAM_USAGE_ACCESSIBILITY<sup>10+</sup> | 8 | 无障碍。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 562| STREAM_USAGE_MOVIE<sup>10+</sup> | 10 | 电影或视频。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 563| STREAM_USAGE_GAME<sup>10+</sup> | 11 | 游戏。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 564| STREAM_USAGE_AUDIOBOOK<sup>10+</sup> | 12 | 有声读物(包括听书、相声、评书)、听新闻、播客等。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 565| STREAM_USAGE_NAVIGATION<sup>10+</sup> | 13 | 导航。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 566| STREAM_USAGE_VIDEO_COMMUNICATION<sup>12+</sup> | 17 | VoIP视频通话。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 567 568## AudioState<sup>8+</sup> 569 570枚举,音频状态。 571 572**系统能力:** SystemCapability.Multimedia.Audio.Core 573 574| 名称 | 值 | 说明 | 575| -------------- | ------ | ---------------- | 576| STATE_INVALID | -1 | 无效状态。 | 577| STATE_NEW | 0 | 创建新实例状态。 | 578| STATE_PREPARED | 1 | 准备状态。 | 579| STATE_RUNNING | 2 | 运行状态。 | 580| STATE_STOPPED | 3 | 停止状态。 | 581| STATE_RELEASED | 4 | 释放状态。 | 582| STATE_PAUSED | 5 | 暂停状态。 | 583 584## AudioEffectMode<sup>10+</sup> 585 586枚举,音效模式。 587 588**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 589 590**系统能力:** SystemCapability.Multimedia.Audio.Renderer 591 592| 名称 | 值 | 说明 | 593| ------------------ | ------ | ---------- | 594| EFFECT_NONE | 0 | 关闭音效。 | 595| EFFECT_DEFAULT | 1 | 默认音效。 | 596 597## AudioRendererRate<sup>8+</sup> 598 599枚举,音频渲染速度。 600 601**系统能力:** SystemCapability.Multimedia.Audio.Renderer 602 603| 名称 | 值 | 说明 | 604| ------------------ | ------ | ---------- | 605| RENDER_RATE_NORMAL | 0 | 正常速度。 | 606| RENDER_RATE_DOUBLE | 1 | 2倍速。 | 607| RENDER_RATE_HALF | 2 | 0.5倍数。 | 608 609## InterruptType 610 611枚举,中断类型。 612 613**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 614 615**系统能力:** SystemCapability.Multimedia.Audio.Renderer 616 617| 名称 | 值 | 说明 | 618| -------------------- | ------ | ---------------------- | 619| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断事件开始。 | 620| INTERRUPT_TYPE_END | 2 | 音频播放中断事件结束。 | 621 622## InterruptForceType<sup>9+</sup> 623 624枚举,音频打断类型。 625 626当用户监听到音频中断(即收到[InterruptEvent](#interruptevent9)事件)时,将获取此信息。 627 628此类型表示本次音频打断的操作是否已由系统强制执行,具体操作信息(如音频暂停、停止等)可通过[InterruptHint](#interrupthint)获取。关于音频打断策略的详细说明可参考文档[处理音频焦点事件](../../media/audio/audio-playback-concurrency.md)。 629 630**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 631 632**系统能力:** SystemCapability.Multimedia.Audio.Renderer 633 634| 名称 | 值 | 说明 | 635| --------------- | ------ | ------------------------------------ | 636| INTERRUPT_FORCE | 0 | 强制打断类型,即具体操作已由系统强制执行。 | 637| INTERRUPT_SHARE | 1 | 共享打断类型,即系统不执行具体操作,通过[InterruptHint](#interrupthint)提示并建议应用操作,应用可自行决策下一步处理方式。 | 638 639## InterruptHint 640 641枚举,中断提示。 642 643当用户监听到音频中断(即收到[InterruptEvent](#interruptevent9)事件)时,将获取此信息。 644 645此类型表示根据焦点策略,当前需要对音频流的具体操作(如暂停、调整音量等)。 646 647可以结合InterruptEvent中的[InterruptForceType](#interruptforcetype9)信息,判断该操作是否已由系统强制执行。关于音频打断策略的详细说明可参考文档[处理音频焦点事件](../../media/audio/audio-playback-concurrency.md)。 648 649**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 650 651**系统能力:** SystemCapability.Multimedia.Audio.Renderer 652 653| 名称 | 值 | 说明 | 654| ---------------------------------- | ------ | -------------------------------------------- | 655| INTERRUPT_HINT_NONE<sup>8+</sup> | 0 | 无提示。 | 656| INTERRUPT_HINT_RESUME | 1 | 提示音频恢复,应用可主动触发开始渲染或开始采集的相关操作。<br>此操作无法由系统强制执行,其对应的[InterruptForceType](#interruptforcetype9)一定为INTERRUPT_SHARE类型。 | 657| INTERRUPT_HINT_PAUSE | 2 | 提示音频暂停,暂时失去音频焦点。<br>后续待焦点可用时,会出现INTERRUPT_HINT_RESUME事件。 | 658| INTERRUPT_HINT_STOP | 3 | 提示音频停止,彻底失去音频焦点。 | 659| INTERRUPT_HINT_DUCK | 4 | 提示音频躲避开始,音频降低音量播放,而不会停止。 | 660| INTERRUPT_HINT_UNDUCK<sup>8+</sup> | 5 | 提示音量躲避结束,音频恢复正常音量。 | 661 662## AudioStreamInfo<sup>8+</sup> 663 664音频流信息。 665 666**系统能力:** SystemCapability.Multimedia.Audio.Core 667 668| 名称 | 类型 | 必填 | 说明 | 669| ------------ | ------------------------------------------------- | ---- | ------------------ | 670| samplingRate | [AudioSamplingRate](#audiosamplingrate8) | 是 | 音频文件的采样率。 | 671| channels | [AudioChannel](#audiochannel8) | 是 | 音频文件的通道数。 | 672| sampleFormat | [AudioSampleFormat](#audiosampleformat8) | 是 | 音频采样格式。 | 673| encodingType | [AudioEncodingType](#audioencodingtype8) | 是 | 音频编码格式。 | 674| channelLayout<sup>11+</sup> | [AudioChannelLayout](#audiochannellayout11) | 否 | 音频声道布局,默认值为0x0。 | 675 676## AudioRendererInfo<sup>8+</sup> 677 678音频渲染器信息。 679 680**系统能力:** SystemCapability.Multimedia.Audio.Core 681 682| 名称 | 类型 | 必填 | 说明 | 683| ------------- | --------------------------- | ---- | ---------------- | 684| content | [ContentType](#contenttypedeprecated) | 否 | 音频内容类型。<br>API version 8、9为必填参数,从API version 10开始,变更为可选参数,默认值为CONTENT_TYPE_UNKNOWN。同时,[ContentType](#contenttypedeprecated)废弃,建议直接使用[StreamUsage](#streamusage)声明音频流使用类型即可。 | 685| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 686| rendererFlags | number | 是 | 音频渲染器标志。<br>0代表音频渲染器。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 687 688## AudioRendererOptions<sup>8+</sup> 689 690音频渲染器选项信息。 691 692| 名称 | 类型 | 必填 | 说明 | 693| ------------ | ---------------------------------------- | ---- | ---------------- | 694| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 695| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 696| privacyType<sup>10+</sup> | [AudioPrivacyType](#audioprivacytype10) | 否 | 表示音频流是否可以被其他应用录制,默认值为0。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture | 697 698## AudioPrivacyType<sup>10+</sup> 699 700枚举类型,用于标识对应播放音频流是否支持被其他应用录制。 701 702**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 703 704| 名称 | 值 | 说明 | 705| -------------------- | ---- | -------------------------------- | 706| PRIVACY_TYPE_PUBLIC | 0 | 表示音频流可以被其他应用录制。 | 707| PRIVACY_TYPE_PRIVATE | 1 | 表示音频流不可以被其他应用录制。 | 708 709## InterruptEvent<sup>9+</sup> 710 711播放中断时,应用接收的中断事件。 712 713**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 714 715**系统能力:** SystemCapability.Multimedia.Audio.Renderer 716 717| 名称 | 类型 |必填 | 说明 | 718| --------- | ------------------------------------------ | ---- | ------------------------------------ | 719| eventType | [InterruptType](#interrupttype) | 是 | 中断事件类型,开始或是结束。 | 720| forceType | [InterruptForceType](#interruptforcetype9) | 是 | 操作是由系统执行或是由应用程序执行。 | 721| hintType | [InterruptHint](#interrupthint) | 是 | 中断提示。 | 722 723## VolumeEvent<sup>9+</sup> 724 725音量改变时,应用接收的事件。 726 727**系统能力:** SystemCapability.Multimedia.Audio.Volume 728 729| 名称 | 类型 | 必填 | 说明 | 730| ---------- | ----------------------------------- | ---- |-------------------------------------------| 731| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 732| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 733| updateUi | boolean | 是 | 在UI中显示音量变化,true为显示,false为不显示。 | 734 735## MicStateChangeEvent<sup>9+</sup> 736 737麦克风状态变化时,应用接收的事件。 738 739**系统能力:** SystemCapability.Multimedia.Audio.Device 740 741| 名称 | 类型 | 必填 | 说明 | 742| ---------- | ----------------------------------- | ---- |-------------------------------------------------------- | 743| mute | boolean | 是 | 回调返回系统麦克风静音状态,true为静音,false为非静音。 | 744 745## DeviceChangeAction 746 747描述设备连接状态变化和设备信息。 748 749**系统能力:** SystemCapability.Multimedia.Audio.Device 750 751| 名称 | 类型 | 必填 | 说明 | 752| :---------------- | :------------------------------------------------ | :--- | :----------------- | 753| type | [DeviceChangeType](#devicechangetype) | 是 | 设备连接状态变化。 | 754| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 755 756## DeviceBlockStatusInfo<sup>13+</sup> 757 758描述音频设备被堵塞状态和设备信息。 759 760**系统能力:** SystemCapability.Multimedia.Audio.Device 761 762| 名称 | 类型 | 必填 | 说明 | 763| :---------------- | :------------------------------------------------ | :--- | :----------------- | 764| blockStatus | [DeviceBlockStatus](#deviceblockstatus13) | 是 | 音频设备堵塞状态。 | 765| devices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 766 767## ChannelBlendMode<sup>11+</sup> 768 769枚举,声道混合模式类型。 770 771**系统能力:** SystemCapability.Multimedia.Audio.Core 772 773| 名称 | 值 | 说明 | 774| :------------------------------------------- | :----- | :--------------------- | 775| MODE_DEFAULT | 0 | 无声道混合。 | 776| MODE_BLEND_LR | 1 | 混合左右声道。 | 777| MODE_ALL_LEFT | 2 | 从左声道拷贝覆盖到右声道混合。 | 778| MODE_ALL_RIGHT | 3 | 从右声道拷贝覆盖到左声道混合。 | 779 780## AudioStreamDeviceChangeReason<sup>11+</sup> 781 782枚举,流设备变更原因。 783 784**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 785 786**系统能力:** SystemCapability.Multimedia.Audio.Device 787 788| 名称 | 值 | 说明 | 789|:------------------------------------------| :----- |:----------------| 790| REASON_UNKNOWN | 0 | 未知原因。 | 791| REASON_NEW_DEVICE_AVAILABLE | 1 | 新设备可用。 | 792| REASON_OLD_DEVICE_UNAVAILABLE | 2 | 旧设备不可用。当报告此原因时,应用程序应考虑暂停音频播放。 | 793| REASON_OVERRODE | 3 | 强选。 | 794 795## AudioStreamDeviceChangeInfo<sup>11+</sup> 796 797流设备变更时,应用接收的事件。 798 799**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 800 801**系统能力:** SystemCapability.Multimedia.Audio.Device 802 803| 名称 | 类型 | 必填 | 说明 | 804| :---------------- |:------------------------------------------------------------------| :--- | :----------------- | 805| devices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 806| changeReason | [AudioStreamDeviceChangeReason](#audiostreamdevicechangereason11) | 是 | 流设备变更原因。 | 807 808## DeviceChangeType 809 810枚举,设备连接状态变化。 811 812**系统能力:** SystemCapability.Multimedia.Audio.Device 813 814| 名称 | 值 | 说明 | 815| :--------- | :--- | :------------- | 816| CONNECT | 0 | 设备连接。 | 817| DISCONNECT | 1 | 断开设备连接。 | 818 819## DeviceBlockStatus<sup>13+</sup> 820 821枚举,表示音频设备是否被堵塞。 822 823**系统能力:** SystemCapability.Multimedia.Audio.Device 824 825| 名称 | 值 | 说明 | 826| :--------- | :--- | :------------- | 827| UNBLOCKED | 0 | 音频设备正常。 | 828| BLOCKED | 1 | 音频设备被堵塞。 | 829 830## AudioCapturerOptions<sup>8+</sup> 831 832音频采集器选项信息。 833 834| 名称 | 类型 | 必填 | 说明 | 835| ----------------------------------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 836| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 837| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 838| playbackCaptureConfig<sup>(deprecated)</sup> | [AudioPlaybackCaptureConfig](#audioplaybackcaptureconfigdeprecated) | 否 | 音频内录的配置信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture。<br/> 从API version 10 开始支持,从 API version 12 开始废弃。Audio不再提供内录接口,请通过[录屏接口AVScreenCapture](../apis-media-kit/_a_v_screen_capture.md)进行内录。 | 839 840## AudioCapturerInfo<sup>8+</sup> 841 842描述音频采集器信息。 843 844**系统能力:** SystemCapability.Multimedia.Audio.Core 845 846| 名称 | 类型 | 必填 | 说明 | 847| :------------ | :------------------------ | :--- | :--------------- | 848| source | [SourceType](#sourcetype8) | 是 | 音源类型。 | 849| capturerFlags | number | 是 | 音频采集器标志。<br>0代表音频采集器。 | 850 851## SourceType<sup>8+</sup> 852 853枚举,音源类型。 854 855| 名称 | 值 | 说明 | 856| :------------------------------------------- | :----- | :--------------------- | 857| SOURCE_TYPE_INVALID | -1 | 无效的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 858| SOURCE_TYPE_MIC | 0 | Mic音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 859| SOURCE_TYPE_VOICE_RECOGNITION<sup>9+</sup> | 1 | 语音识别源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 860| SOURCE_TYPE_PLAYBACK_CAPTURE<sup>(deprecated)</sup> | 2 | 播放音频流(内录)录制音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture。<br/> 从API version 10 开始支持,从 API version 12 开始废弃。Audio不再提供内录接口,请通过[录屏接口AVScreenCapture](../apis-media-kit/_a_v_screen_capture.md)进行内录。 | 861| SOURCE_TYPE_VOICE_COMMUNICATION | 7 | 语音通话场景的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 862| SOURCE_TYPE_VOICE_MESSAGE<sup>12+</sup> | 10 | 短语音消息的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 863| SOURCE_TYPE_CAMCORDER<sup>13+</sup> | 13 | 录像的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 864| SOURCE_TYPE_UNPROCESSED<sup>14+</sup> | 14 | 麦克风纯净录音的音频源(系统不做任何算法处理)。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 865 866## AudioPlaybackCaptureConfig<sup>(deprecated)</sup> 867 868播放音频流录制(内录)的配置信息。 869 870> **说明:** 871> 从 API version 10 开始支持,从 API version 12 开始废弃。Audio不再提供内录接口,请通过[录屏接口AVScreenCapture](../apis-media-kit/_a_v_screen_capture.md)进行内录。 872 873**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 874 875| 名称 | 类型 | 必填 | 说明 | 876| ------------- | --------------------------------------------- | ---- | -------------------------------- | 877| filterOptions | [CaptureFilterOptions](#capturefilteroptionsdeprecated) | 是 | 需要录制的播放音频流的筛选信息。 | 878 879## CaptureFilterOptions<sup>(deprecated)</sup> 880 881待录制的播放音频流的筛选信息。 882 883> **说明:** 884> 从 API version 10 开始支持,从 API version 12 开始废弃。Audio不再提供内录接口,请通过[录屏接口AVScreenCapture](../apis-media-kit/_a_v_screen_capture.md)进行内录。 885 886**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 887 888| 名称 | 类型 | 必填 | 说明 | 889| ------ | ---------------------------------- | ---- | ------------------------------------------------------------ | 890| usages | Array<[StreamUsage](#streamusage)> | 是 | 指定需要录制的音频播放流的StreamUsage类型。可同时指定0个或多个StreamUsage。Array为空时,默认录制StreamUsage为STREAM_USAGE_MUSIC、STREAM_USAGE_MOVIE、STREAM_USAGE_GAME和STREAM_USAGE_AUDIOBOOK的音频播放流。 <br>在API version 10时,CaptureFilterOptions支持使用StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,使用时需要申请权限ohos.permission.CAPTURE_VOICE_DOWNLINK_AUDIO,该权限仅系统应用可申请。<br>从API version 11开始,CaptureFilterOptions不再支持使用StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,所以当前接口不再涉及此权限。| 891 892## AudioScene<sup>8+</sup> 893 894枚举,音频场景。 895 896**系统能力:** SystemCapability.Multimedia.Audio.Communication 897 898| 名称 | 值 | 说明 | 899| :--------------------- | :----- | :-------------------------------------------- | 900| AUDIO_SCENE_DEFAULT | 0 | 默认音频场景。 | 901| AUDIO_SCENE_RINGING<sup>12+</sup> | 1 | 响铃模式。 | 902| AUDIO_SCENE_PHONE_CALL<sup>12+</sup> | 2 | 电话模式。 | 903| AUDIO_SCENE_VOICE_CHAT | 3 | 语音聊天模式。 | 904 905## AudioConcurrencyMode<sup>12+</sup> 906 907枚举,音频并发模式。 908 909**系统能力:** SystemCapability.Multimedia.Audio.Core 910 911| 名称 | 值 | 说明 | 912| :--------------------- |:--|:--------| 913| CONCURRENCY_DEFAULT | 0 | 默认使用系统策略。 | 914| CONCURRENCY_MIX_WITH_OTHERS | 1 | 和其它音频并发。 | 915| CONCURRENCY_DUCK_OTHERS | 2 | 压低其他音频的音量。 | 916| CONCURRENCY_PAUSE_OTHERS | 3 | 暂停其他音频。 | 917 918## AudioSessionDeactivatedReason<sup>12+</sup> 919 920枚举,音频会话停用原因。 921 922**系统能力:** SystemCapability.Multimedia.Audio.Core 923 924| 名称 | 值 | 说明 | 925| :--------------------- |:--|:-------| 926| DEACTIVATED_LOWER_PRIORITY | 0 | 应用焦点被抢占。 | 927| DEACTIVATED_TIMEOUT | 1 | 音频会话等待超时。 | 928 929## AudioSessionStrategy<sup>12+</sup> 930 931音频会话策略。 932 933**系统能力:** SystemCapability.Multimedia.Audio.Core 934 935| 名称 | 类型 | 必填 | 说明 | 936| :------------ |:------------------------------------------------| :--- | :--------------- | 937| concurrencyMode | [AudioConcurrencyMode](#audioconcurrencymode12) | 是 | 音频并发模式。 | 938 939## AudioSessionDeactivatedEvent<sup>12+</sup> 940 941音频会话已停用事件。 942 943**系统能力:** SystemCapability.Multimedia.Audio.Core 944 945| 名称 | 类型 | 必填 | 说明 | 946| :------------ |:------------------------------------------------------------------| :--- | :--------------- | 947| reason | [AudioSessionDeactivatedReason](#audiosessiondeactivatedreason12) | 是 | 音频会话停用原因。 | 948 949## AudioManager 950 951管理音频音量和音频设备。在调用AudioManager的接口前,需要先通过[getAudioManager](#audiogetaudiomanager)创建实例。 952 953### setAudioParameter<sup>(deprecated)</sup> 954 955setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void 956 957音频参数设置,使用callback方式异步返回结果。 958 959接口用于为根据硬件设备支持能力扩展音频配置。支持的参数与产品、设备强相关,非通用参数,示例代码内使用样例参数。 960 961> **说明:** 962> 从 API version 7 开始支持,从 API version 11 开始废弃。替代接口仅面向系统应用开放。 963 964**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 965 966**系统能力:** SystemCapability.Multimedia.Audio.Core 967 968**参数:** 969 970| 参数名 | 类型 | 必填 | 说明 | 971| -------- | ------------------------- | ---- | ------------------------ | 972| key | string | 是 | 被设置的音频参数的键。 | 973| value | string | 是 | 被设置的音频参数的值。 | 974| callback | AsyncCallback<void> | 是 | 回调函数。当音频参数设置成功,err为undefined,否则为错误对象。 | 975 976**示例:** 977 978```ts 979import { BusinessError } from '@kit.BasicServicesKit'; 980 981audioManager.setAudioParameter('key_example', 'value_example', (err: BusinessError) => { 982 if (err) { 983 console.error(`Failed to set the audio parameter. ${err}`); 984 return; 985 } 986 console.info('Callback invoked to indicate a successful setting of the audio parameter.'); 987}); 988``` 989 990### setAudioParameter<sup>(deprecated)</sup> 991 992setAudioParameter(key: string, value: string): Promise<void> 993 994音频参数设置,使用Promise方式异步返回结果。 995 996接口用于为根据硬件设备支持能力扩展音频配置。支持的参数与产品、设备强相关,非通用参数,示例代码内使用样例参数。 997 998> **说明:** 999> 从 API version 7 开始支持,从 API version 11 开始废弃。替代接口仅面向系统应用开放。 1000 1001**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 1002 1003**系统能力:** SystemCapability.Multimedia.Audio.Core 1004 1005**参数:** 1006 1007| 参数名 | 类型 | 必填 | 说明 | 1008| ------ | ------ | ---- | ---------------------- | 1009| key | string | 是 | 被设置的音频参数的键。 | 1010| value | string | 是 | 被设置的音频参数的值。 | 1011 1012**返回值:** 1013 1014| 类型 | 说明 | 1015| ------------------- | ------------------------------- | 1016| Promise<void> | Promise对象,无返回结果。 | 1017 1018**示例:** 1019 1020```ts 1021audioManager.setAudioParameter('key_example', 'value_example').then(() => { 1022 console.info('Promise returned to indicate a successful setting of the audio parameter.'); 1023}); 1024``` 1025 1026### getAudioParameter<sup>(deprecated)</sup> 1027 1028getAudioParameter(key: string, callback: AsyncCallback<string>): void 1029 1030获取指定音频参数值,使用callback方式异步返回结果。 1031 1032本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1033 1034> **说明:** 1035> 从 API version 7 开始支持,从 API version 11 开始废弃。替代接口仅面向系统应用开放。 1036 1037**系统能力:** SystemCapability.Multimedia.Audio.Core 1038 1039**参数:** 1040 1041| 参数名 | 类型 | 必填 | 说明 | 1042| -------- | --------------------------- | ---- | ---------------------------- | 1043| key | string | 是 | 待获取的音频参数的键。 | 1044| callback | AsyncCallback<string> | 是 | 回调函数。当获取指定音频参数值成功,err为undefined,data为获取到的指定音频参数值;否则为错误对象。 | 1045 1046**示例:** 1047 1048```ts 1049import { BusinessError } from '@kit.BasicServicesKit'; 1050 1051audioManager.getAudioParameter('key_example', (err: BusinessError, value: string) => { 1052 if (err) { 1053 console.error(`Failed to obtain the value of the audio parameter. ${err}`); 1054 return; 1055 } 1056 console.info(`Callback invoked to indicate that the value of the audio parameter is obtained ${value}.`); 1057}); 1058``` 1059 1060### getAudioParameter<sup>(deprecated)</sup> 1061 1062getAudioParameter(key: string): Promise<string> 1063 1064获取指定音频参数值,使用Promise方式异步返回结果。 1065 1066本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1067 1068> **说明:** 1069> 从 API version 7 开始支持,从 API version 11 开始废弃。替代接口仅面向系统应用开放。 1070 1071**系统能力:** SystemCapability.Multimedia.Audio.Core 1072 1073**参数:** 1074 1075| 参数名 | 类型 | 必填 | 说明 | 1076| ------ | ------ | ---- | ---------------------- | 1077| key | string | 是 | 待获取的音频参数的键。 | 1078 1079**返回值:** 1080 1081| 类型 | 说明 | 1082| --------------------- | ----------------------------------- | 1083| Promise<string> | Promise对象,返回获取的音频参数的值。 | 1084 1085**示例:** 1086 1087```ts 1088audioManager.getAudioParameter('key_example').then((value: string) => { 1089 console.info(`Promise returned to indicate that the value of the audio parameter is obtained ${value}.`); 1090}); 1091``` 1092 1093### getAudioScene<sup>8+</sup> 1094 1095getAudioScene\(callback: AsyncCallback<AudioScene\>\): void 1096 1097获取音频场景模式,使用callback方式返回异步结果。 1098 1099**系统能力:** SystemCapability.Multimedia.Audio.Communication 1100 1101**参数:** 1102 1103| 参数名 | 类型 | 必填 | 说明 | 1104| :------- | :-------------------------------------------------- | :--- | :--------------------------- | 1105| callback | AsyncCallback<[AudioScene](#audioscene8)> | 是 | 回调函数。当获取音频场景模式成功,err为undefined,data为获取到的音频场景模式;否则为错误对象。 | 1106 1107**示例:** 1108 1109```ts 1110import { BusinessError } from '@kit.BasicServicesKit'; 1111 1112audioManager.getAudioScene((err: BusinessError, value: audio.AudioScene) => { 1113 if (err) { 1114 console.error(`Failed to obtain the audio scene mode. ${err}`); 1115 return; 1116 } 1117 console.info(`Callback invoked to indicate that the audio scene mode is obtained ${value}.`); 1118}); 1119``` 1120 1121### getAudioScene<sup>8+</sup> 1122 1123getAudioScene\(\): Promise<AudioScene\> 1124 1125获取音频场景模式,使用Promise方式返回异步结果。 1126 1127**系统能力:** SystemCapability.Multimedia.Audio.Communication 1128 1129**返回值:** 1130 1131| 类型 | 说明 | 1132| :-------------------------------------------- | :--------------------------- | 1133| Promise<[AudioScene](#audioscene8)> | Promise对象,返回音频场景模式。 | 1134 1135**示例:** 1136 1137```ts 1138import { BusinessError } from '@kit.BasicServicesKit'; 1139 1140audioManager.getAudioScene().then((value: audio.AudioScene) => { 1141 console.info(`Promise returned to indicate that the audio scene mode is obtained ${value}.`); 1142}).catch ((err: BusinessError) => { 1143 console.error(`Failed to obtain the audio scene mode ${err}`); 1144}); 1145``` 1146 1147### getAudioSceneSync<sup>10+</sup> 1148 1149getAudioSceneSync\(\): AudioScene 1150 1151获取音频场景模式,同步返回结果。 1152 1153**系统能力:** SystemCapability.Multimedia.Audio.Communication 1154 1155**返回值:** 1156 1157| 类型 | 说明 | 1158| :-------------------------------------------- | :--------------------------- | 1159| [AudioScene](#audioscene8) | 音频场景模式。 | 1160 1161**示例:** 1162 1163```ts 1164import { BusinessError } from '@kit.BasicServicesKit'; 1165 1166try { 1167 let value: audio.AudioScene = audioManager.getAudioSceneSync(); 1168 console.info(`indicate that the audio scene mode is obtained ${value}.`); 1169} catch (err) { 1170 let error = err as BusinessError; 1171 console.error(`Failed to obtain the audio scene mode ${error}`); 1172} 1173``` 1174 1175### getVolumeManager<sup>9+</sup> 1176 1177getVolumeManager(): AudioVolumeManager 1178 1179获取音频音量管理器。 1180 1181**系统能力:** SystemCapability.Multimedia.Audio.Volume 1182 1183**返回值:** 1184 1185| 类型 | 说明 | 1186|-----------------------------------------| ----------------------------- | 1187| [AudioVolumeManager](#audiovolumemanager9) | AudioVolumeManager实例。 | 1188 1189**示例:** 1190 1191```ts 1192import { audio } from '@kit.AudioKit'; 1193 1194let audioVolumeManager: audio.AudioVolumeManager = audioManager.getVolumeManager(); 1195``` 1196 1197### getStreamManager<sup>9+</sup> 1198 1199getStreamManager(): AudioStreamManager 1200 1201获取音频流管理器。 1202 1203**系统能力:** SystemCapability.Multimedia.Audio.Core 1204 1205**返回值:** 1206 1207| 类型 | 说明 | 1208|--------------------------------------------| ----------------------------- | 1209| [AudioStreamManager](#audiostreammanager9) | AudioStreamManager实例。 | 1210 1211**示例:** 1212 1213```ts 1214import { audio } from '@kit.AudioKit'; 1215 1216let audioStreamManager: audio.AudioStreamManager = audioManager.getStreamManager(); 1217``` 1218 1219### getRoutingManager<sup>9+</sup> 1220 1221getRoutingManager(): AudioRoutingManager 1222 1223获取音频路由设备管理器。 1224 1225**系统能力:** SystemCapability.Multimedia.Audio.Device 1226 1227**返回值:** 1228 1229| 类型 | 说明 | 1230|------------------------------------------| ----------------------------- | 1231| [AudioRoutingManager](#audioroutingmanager9) | AudioRoutingManager实例。 | 1232 1233**示例:** 1234 1235```ts 1236import { audio } from '@kit.AudioKit'; 1237 1238let audioRoutingManager: audio.AudioRoutingManager = audioManager.getRoutingManager(); 1239``` 1240 1241### getSessionManager<sup>12+</sup> 1242 1243getSessionManager(): AudioSessionManager 1244 1245获取音频会话管理器。 1246 1247**系统能力:** SystemCapability.Multimedia.Audio.Core 1248 1249**返回值:** 1250 1251| 类型 | 说明 | 1252|----------------------------------------------| ----------------------------- | 1253| [AudioSessionManager](#audiosessionmanager12) | AudioSessionManager实例。 | 1254 1255**示例:** 1256 1257```ts 1258import { audio } from '@kit.AudioKit'; 1259 1260let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager(); 1261``` 1262 1263### setVolume<sup>(deprecated)</sup> 1264 1265setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void 1266 1267设置指定流的音量,使用callback方式异步返回结果。 1268 1269> **说明:** 1270> 1271> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1272> 1273> 应用无法直接调节系统音量,建议应用通过系统音量面板组件,让用户通过界面操作来调节音量。当用户通过应用内音量面板调节音量时,系统会展示音量提示界面,显性地提示用户系统音量发生改变。具体样例和介绍请查看[AVVolumePanel参考文档](ohos-multimedia-avvolumepanel.md)。 1274 1275**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1276 1277仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1278 1279**系统能力:** SystemCapability.Multimedia.Audio.Volume 1280 1281**参数:** 1282 1283| 参数名 | 类型 | 必填 | 说明 | 1284| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1285| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1286| volume | number | 是 | 音量等级,可设置范围通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1287| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定流的音量成功,err为undefined,否则为错误对象。 | 1288 1289**示例:** 1290 1291```ts 1292import { BusinessError } from '@kit.BasicServicesKit'; 1293 1294audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err: BusinessError) => { 1295 if (err) { 1296 console.error(`Failed to set the volume. ${err}`); 1297 return; 1298 } 1299 console.info('Callback invoked to indicate a successful volume setting.'); 1300}); 1301``` 1302 1303### setVolume<sup>(deprecated)</sup> 1304 1305setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> 1306 1307设置指定流的音量,使用Promise方式异步返回结果。 1308 1309> **说明:** 1310> 1311> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1312> 1313> 应用无法直接调节系统音量,建议应用通过系统音量面板组件,让用户通过界面操作来调节音量。当用户通过应用内音量面板调节音量时,系统会展示音量提示界面,显性地提示用户系统音量发生改变。具体样例和介绍请查看[AVVolumePanel参考文档](ohos-multimedia-avvolumepanel.md)。 1314 1315**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1316 1317仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1318 1319**系统能力:** SystemCapability.Multimedia.Audio.Volume 1320 1321**参数:** 1322 1323| 参数名 | 类型 | 必填 | 说明 | 1324| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1325| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1326| volume | number | 是 | 音量等级,可设置范围通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1327 1328**返回值:** 1329 1330| 类型 | 说明 | 1331| ------------------- | ----------------------------- | 1332| Promise<void> | Promise对象,无返回结果。 | 1333 1334**示例:** 1335 1336```ts 1337audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => { 1338 console.info('Promise returned to indicate a successful volume setting.'); 1339}); 1340``` 1341 1342### getVolume<sup>(deprecated)</sup> 1343 1344getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1345 1346获取指定流的音量,使用callback方式异步返回结果。 1347 1348> **说明:** 1349> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getVolume](#getvolume9)替代。 1350 1351**系统能力:** SystemCapability.Multimedia.Audio.Volume 1352 1353**参数:** 1354 1355| 参数名 | 类型 | 必填 | 说明 | 1356| ---------- | ----------------------------------- | ---- | ------------------ | 1357| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1358| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。指定流的音量等级范围可通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1359 1360**示例:** 1361 1362```ts 1363import { BusinessError } from '@kit.BasicServicesKit'; 1364 1365audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1366 if (err) { 1367 console.error(`Failed to obtain the volume. ${err}`); 1368 return; 1369 } 1370 console.info('Callback invoked to indicate that the volume is obtained.'); 1371}); 1372``` 1373 1374### getVolume<sup>(deprecated)</sup> 1375 1376getVolume(volumeType: AudioVolumeType): Promise<number> 1377 1378获取指定流的音量,使用Promise方式异步返回结果。 1379 1380> **说明:** 1381> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getVolume](#getvolume9)替代。 1382 1383**系统能力:** SystemCapability.Multimedia.Audio.Volume 1384 1385**参数:** 1386 1387| 参数名 | 类型 | 必填 | 说明 | 1388| ---------- | ----------------------------------- | ---- | ------------ | 1389| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1390 1391**返回值:** 1392 1393| 类型 | 说明 | 1394| --------------------- | ------------------------- | 1395| Promise<number> | Promise对象,返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1396 1397**示例:** 1398 1399```ts 1400audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1401 console.info(`Promise returned to indicate that the volume is obtained ${value} .`); 1402}); 1403``` 1404 1405### getMinVolume<sup>(deprecated)</sup> 1406 1407getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1408 1409获取指定流的最小音量,使用callback方式异步返回结果。 1410 1411> **说明:** 1412> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMinVolume](#getminvolume9)替代。 1413 1414**系统能力:** SystemCapability.Multimedia.Audio.Volume 1415 1416**参数:** 1417 1418| 参数名 | 类型 | 必填 | 说明 | 1419| ---------- | ----------------------------------- | ---- | ------------------ | 1420| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1421| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 1422 1423**示例:** 1424 1425```ts 1426import { BusinessError } from '@kit.BasicServicesKit'; 1427 1428audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1429 if (err) { 1430 console.error(`Failed to obtain the minimum volume. ${err}`); 1431 return; 1432 } 1433 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 1434}); 1435``` 1436 1437### getMinVolume<sup>(deprecated)</sup> 1438 1439getMinVolume(volumeType: AudioVolumeType): Promise<number> 1440 1441获取指定流的最小音量,使用Promise方式异步返回结果。 1442 1443> **说明:** 1444> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMinVolume](#getminvolume9)替代。 1445 1446**系统能力:** SystemCapability.Multimedia.Audio.Volume 1447 1448**参数:** 1449 1450| 参数名 | 类型 | 必填 | 说明 | 1451| ---------- | ----------------------------------- | ---- | ------------ | 1452| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1453 1454**返回值:** 1455 1456| 类型 | 说明 | 1457| --------------------- | ------------------------- | 1458| Promise<number> | Promise对象,返回最小音量。 | 1459 1460**示例:** 1461 1462```ts 1463audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1464 console.info(`Promised returned to indicate that the minimum volume is obtained. ${value}`); 1465}); 1466``` 1467 1468### getMaxVolume<sup>(deprecated)</sup> 1469 1470getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1471 1472获取指定流的最大音量,使用callback方式异步返回结果。 1473 1474> **说明:** 1475> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMaxVolume](#getmaxvolume9)替代。 1476 1477**系统能力:** SystemCapability.Multimedia.Audio.Volume 1478 1479**参数:** 1480 1481| 参数名 | 类型 | 必填 | 说明 | 1482| ---------- | ----------------------------------- | ---- | ---------------------- | 1483| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1484| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 1485 1486**示例:** 1487 1488```ts 1489import { BusinessError } from '@kit.BasicServicesKit'; 1490 1491audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1492 if (err) { 1493 console.error(`Failed to obtain the maximum volume. ${err}`); 1494 return; 1495 } 1496 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 1497}); 1498``` 1499 1500### getMaxVolume<sup>(deprecated)</sup> 1501 1502getMaxVolume(volumeType: AudioVolumeType): Promise<number> 1503 1504获取指定流的最大音量,使用Promise方式异步返回结果。 1505 1506> **说明:** 1507> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMaxVolume](#getmaxvolume9)替代。 1508 1509**系统能力:** SystemCapability.Multimedia.Audio.Volume 1510 1511**参数:** 1512 1513| 参数名 | 类型 | 必填 | 说明 | 1514| ---------- | ----------------------------------- | ---- | ------------ | 1515| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1516 1517**返回值:** 1518 1519| 类型 | 说明 | 1520| --------------------- | ----------------------------- | 1521| Promise<number> | Promise对象,返回最大音量。 | 1522 1523**示例:** 1524 1525```ts 1526audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 1527 console.info('Promised returned to indicate that the maximum volume is obtained.'); 1528}); 1529``` 1530 1531### mute<sup>(deprecated)</sup> 1532 1533mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void 1534 1535设置指定音量流静音,使用callback方式异步返回结果。 1536 1537当该音量流可设置的最小音量不能为0时,不支持静音操作,例如:闹钟、通话。 1538 1539> **说明:** 1540> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1541 1542**系统能力:** SystemCapability.Multimedia.Audio.Volume 1543 1544**参数:** 1545 1546| 参数名 | 类型 | 必填 | 说明 | 1547| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1548| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1549| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 1550| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定音量流静音成功,err为undefined,否则为错误对象。 | 1551 1552**示例:** 1553 1554```ts 1555import { BusinessError } from '@kit.BasicServicesKit'; 1556 1557audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err: BusinessError) => { 1558 if (err) { 1559 console.error(`Failed to mute the stream. ${err}`); 1560 return; 1561 } 1562 console.info('Callback invoked to indicate that the stream is muted.'); 1563}); 1564``` 1565 1566### mute<sup>(deprecated)</sup> 1567 1568mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> 1569 1570设置指定音量流静音,使用Promise方式异步返回结果。 1571 1572当该音量流可设置的最小音量不能为0时,不支持静音操作,例如:闹钟、通话。 1573 1574> **说明:** 1575> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1576 1577**系统能力:** SystemCapability.Multimedia.Audio.Volume 1578 1579**参数:** 1580 1581| 参数名 | 类型 | 必填 | 说明 | 1582| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1583| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1584| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 1585 1586**返回值:** 1587 1588| 类型 | 说明 | 1589| ------------------- | ----------------------------- | 1590| Promise<void> | Promise对象,无返回结果。 | 1591 1592**示例:** 1593 1594 1595```ts 1596audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => { 1597 console.info('Promise returned to indicate that the stream is muted.'); 1598}); 1599``` 1600 1601### isMute<sup>(deprecated)</sup> 1602 1603isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1604 1605获取指定音量流是否被静音,使用callback方式异步返回结果。 1606 1607> **说明:** 1608> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMute](#ismute9)替代。 1609 1610**系统能力:** SystemCapability.Multimedia.Audio.Volume 1611 1612**参数:** 1613 1614| 参数名 | 类型 | 必填 | 说明 | 1615| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 1616| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1617| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否被静音成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 1618 1619**示例:** 1620 1621```ts 1622import { BusinessError } from '@kit.BasicServicesKit'; 1623 1624audioManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1625 if (err) { 1626 console.error(`Failed to obtain the mute status. ${err}`); 1627 return; 1628 } 1629 console.info(`Callback invoked to indicate that the mute status of the stream is obtained. ${value}`); 1630}); 1631``` 1632 1633### isMute<sup>(deprecated)</sup> 1634 1635isMute(volumeType: AudioVolumeType): Promise<boolean> 1636 1637获取指定音量流是否被静音,使用Promise方式异步返回结果。 1638 1639> **说明:** 1640> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMute](#ismute9)替代。 1641 1642**系统能力:** SystemCapability.Multimedia.Audio.Volume 1643 1644**参数:** 1645 1646| 参数名 | 类型 | 必填 | 说明 | 1647| ---------- | ----------------------------------- | ---- | ------------ | 1648| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1649 1650**返回值:** 1651 1652| 类型 | 说明 | 1653| ---------------------- | ------------------------------------------------------ | 1654| Promise<boolean> | Promise对象,返回流静音状态,true为静音,false为非静音。 | 1655 1656**示例:** 1657 1658```ts 1659audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1660 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 1661}); 1662``` 1663 1664### isActive<sup>(deprecated)</sup> 1665 1666isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1667 1668获取指定音量流是否为活跃状态,使用callback方式异步返回结果。 1669 1670> **说明:** 1671> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioStreamManager中的[isActive](#isactive9)替代。 1672 1673**系统能力:** SystemCapability.Multimedia.Audio.Volume 1674 1675**参数:** 1676 1677| 参数名 | 类型 | 必填 | 说明 | 1678| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 1679| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1680| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否为活跃状态成功,err为undefined,data为true为活跃,false为不活跃;否则为错误对象。 | 1681 1682**示例:** 1683 1684```ts 1685import { BusinessError } from '@kit.BasicServicesKit'; 1686 1687audioManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1688 if (err) { 1689 console.error(`Failed to obtain the active status of the stream. ${err}`); 1690 return; 1691 } 1692 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 1693}); 1694``` 1695 1696### isActive<sup>(deprecated)</sup> 1697 1698isActive(volumeType: AudioVolumeType): Promise<boolean> 1699 1700获取指定音量流是否为活跃状态,使用Promise方式异步返回结果。 1701 1702> **说明:** 1703> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioStreamManager中的[isActive](#isactive9)替代。 1704 1705**系统能力:** SystemCapability.Multimedia.Audio.Volume 1706 1707**参数:** 1708 1709| 参数名 | 类型 | 必填 | 说明 | 1710| ---------- | ----------------------------------- | ---- | ------------ | 1711| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1712 1713**返回值:** 1714 1715| 类型 | 说明 | 1716| ---------------------- | -------------------------------------------------------- | 1717| Promise<boolean> | Promise对象,返回流的活跃状态,true为活跃,false为不活跃。 | 1718 1719**示例:** 1720 1721```ts 1722audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1723 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 1724}); 1725``` 1726 1727### setRingerMode<sup>(deprecated)</sup> 1728 1729setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void 1730 1731设置铃声模式,使用callback方式异步返回结果。 1732 1733> **说明:** 1734> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1735 1736**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1737 1738仅在静音和非静音状态切换时需要该权限。 1739 1740**系统能力:** SystemCapability.Multimedia.Audio.Communication 1741 1742**参数:** 1743 1744| 参数名 | 类型 | 必填 | 说明 | 1745| -------- | ------------------------------- | ---- | ------------------------ | 1746| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1747| callback | AsyncCallback<void> | 是 | 回调函数。当设置铃声模式成功,err为undefined,否则为错误对象。 | 1748 1749**示例:** 1750 1751```ts 1752import { BusinessError } from '@kit.BasicServicesKit'; 1753 1754audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err: BusinessError) => { 1755 if (err) { 1756 console.error(`Failed to set the ringer mode. ${err}`); 1757 return; 1758 } 1759 console.info('Callback invoked to indicate a successful setting of the ringer mode.'); 1760}); 1761``` 1762 1763### setRingerMode<sup>(deprecated)</sup> 1764 1765setRingerMode(mode: AudioRingMode): Promise<void> 1766 1767设置铃声模式,使用Promise方式异步返回结果。 1768 1769> **说明:** 1770> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 1771 1772 1773**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1774 1775仅在静音和非静音状态切换时需要该权限。 1776 1777**系统能力:** SystemCapability.Multimedia.Audio.Communication 1778 1779**参数:** 1780 1781| 参数名 | 类型 | 必填 | 说明 | 1782| ------ | ------------------------------- | ---- | -------------- | 1783| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1784 1785**返回值:** 1786 1787| 类型 | 说明 | 1788| ------------------- | ------------------------------- | 1789| Promise<void> | Promise对象,无返回结果。 | 1790 1791**示例:** 1792 1793```ts 1794audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => { 1795 console.info('Promise returned to indicate a successful setting of the ringer mode.'); 1796}); 1797``` 1798 1799### getRingerMode<sup>(deprecated)</sup> 1800 1801getRingerMode(callback: AsyncCallback<AudioRingMode>): void 1802 1803获取铃声模式,使用callback方式异步返回结果。 1804 1805> **说明:** 1806> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getRingerMode](#getringermode9)替代。 1807 1808**系统能力:** SystemCapability.Multimedia.Audio.Communication 1809 1810**参数:** 1811 1812| 参数名 | 类型 | 必填 | 说明 | 1813| -------- | ---------------------------------------------------- | ---- | ------------------------ | 1814| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 1815 1816**示例:** 1817 1818```ts 1819import { BusinessError } from '@kit.BasicServicesKit'; 1820 1821audioManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 1822 if (err) { 1823 console.error(`Failed to obtain the ringer mode. ${err}`); 1824 return; 1825 } 1826 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 1827}); 1828``` 1829 1830### getRingerMode<sup>(deprecated)</sup> 1831 1832getRingerMode(): Promise<AudioRingMode> 1833 1834获取铃声模式,使用Promise方式异步返回结果。 1835 1836> **说明:** 1837> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getRingerMode](#getringermode9)替代。 1838 1839**系统能力:** SystemCapability.Multimedia.Audio.Communication 1840 1841**返回值:** 1842 1843| 类型 | 说明 | 1844| ---------------------------------------------- | ------------------------------- | 1845| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 1846 1847**示例:** 1848 1849```ts 1850audioManager.getRingerMode().then((value: audio.AudioRingMode) => { 1851 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 1852}); 1853``` 1854 1855### getDevices<sup>(deprecated)</sup> 1856 1857getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 1858 1859获取音频设备列表,使用callback方式异步返回结果。 1860 1861> **说明:** 1862> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[getDevices](#getdevices9)替代。 1863 1864**系统能力:** SystemCapability.Multimedia.Audio.Device 1865 1866**参数:** 1867 1868| 参数名 | 类型 | 必填 | 说明 | 1869| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 1870| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1871| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 1872 1873**示例:** 1874```ts 1875import { BusinessError } from '@kit.BasicServicesKit'; 1876 1877audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 1878 if (err) { 1879 console.error(`Failed to obtain the device list. ${err}`); 1880 return; 1881 } 1882 console.info('Callback invoked to indicate that the device list is obtained.'); 1883}); 1884``` 1885 1886### getDevices<sup>(deprecated)</sup> 1887 1888getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 1889 1890获取音频设备列表,使用Promise方式异步返回结果。 1891 1892> **说明:** 1893> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[getDevices](#getdevices9)替代。 1894 1895**系统能力:** SystemCapability.Multimedia.Audio.Device 1896 1897**参数:** 1898 1899| 参数名 | 类型 | 必填 | 说明 | 1900| ---------- | ------------------------- | ---- | ---------------- | 1901| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1902 1903**返回值:** 1904 1905| 类型 | 说明 | 1906| ------------------------------------------------------------ | ------------------------- | 1907| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 1908 1909**示例:** 1910 1911```ts 1912audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 1913 console.info('Promise returned to indicate that the device list is obtained.'); 1914}); 1915``` 1916 1917### setDeviceActive<sup>(deprecated)</sup> 1918 1919setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void 1920 1921设置设备激活状态,使用callback方式异步返回结果。 1922 1923> **说明:** 1924> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[setCommunicationDevice](#setcommunicationdevice9)替代。 1925 1926**系统能力:** SystemCapability.Multimedia.Audio.Device 1927 1928**参数:** 1929 1930| 参数名 | 类型 | 必填 | 说明 | 1931| ---------- | ------------------------------------- | ---- |-------------| 1932| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1933| active | boolean | 是 | 设备激活状态。 | 1934| callback | AsyncCallback<void> | 是 | 回调函数。当设置设备激活状态成功,err为undefined,否则为错误对象。 | 1935 1936**示例:** 1937 1938```ts 1939import { BusinessError } from '@kit.BasicServicesKit'; 1940 1941audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err: BusinessError) => { 1942 if (err) { 1943 console.error(`Failed to set the active status of the device. ${err}`); 1944 return; 1945 } 1946 console.info('Callback invoked to indicate that the device is set to the active status.'); 1947}); 1948``` 1949 1950### setDeviceActive<sup>(deprecated)</sup> 1951 1952setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void> 1953 1954设置设备激活状态,使用Promise方式异步返回结果。 1955 1956> **说明:** 1957> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[setCommunicationDevice](#setcommunicationdevice9)替代。 1958 1959**系统能力:** SystemCapability.Multimedia.Audio.Device 1960 1961**参数:** 1962 1963| 参数名 | 类型 | 必填 | 说明 | 1964| ---------- | ------------------------------------- | ---- | ------------------ | 1965| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1966| active | boolean | 是 | 设备激活状态。 | 1967 1968**返回值:** 1969 1970| 类型 | 说明 | 1971| ------------------- | ------------------------------- | 1972| Promise<void> | Promise对象,无返回结果。 | 1973 1974**示例:** 1975 1976 1977```ts 1978audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => { 1979 console.info('Promise returned to indicate that the device is set to the active status.'); 1980}); 1981``` 1982 1983### isDeviceActive<sup>(deprecated)</sup> 1984 1985isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void 1986 1987获取指定设备的激活状态,使用callback方式异步返回结果。 1988 1989> **说明:** 1990> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 1991 1992**系统能力:** SystemCapability.Multimedia.Audio.Device 1993 1994**参数:** 1995 1996| 参数名 | 类型 | 必填 | 说明 | 1997| ---------- | ------------------------------------- | ---- | ------------------------ | 1998| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1999| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定设备的激活状态成功,err为undefined,data为true为激活,false为未激活;否则为错误对象。 | 2000 2001**示例:** 2002 2003```ts 2004import { BusinessError } from '@kit.BasicServicesKit'; 2005 2006audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 2007 if (err) { 2008 console.error(`Failed to obtain the active status of the device. ${err}`); 2009 return; 2010 } 2011 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 2012}); 2013``` 2014 2015### isDeviceActive<sup>(deprecated)</sup> 2016 2017isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean> 2018 2019获取指定设备的激活状态,使用Promise方式异步返回结果。 2020 2021> **说明:** 2022> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 2023 2024**系统能力:** SystemCapability.Multimedia.Audio.Device 2025 2026**参数:** 2027 2028| 参数名 | 类型 | 必填 | 说明 | 2029| ---------- | ------------------------------------- | ---- | ------------------ | 2030| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 2031 2032**返回值:** 2033 2034| Type | Description | 2035| ---------------------- |---------------------------------------| 2036| Promise<boolean> | Promise对象,返回设备的激活状态,true激活,false未激活。 | 2037 2038**示例:** 2039 2040```ts 2041audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value: boolean) => { 2042 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 2043}); 2044``` 2045 2046### setMicrophoneMute<sup>(deprecated)</sup> 2047 2048setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 2049 2050设置麦克风静音状态,使用callback方式异步返回结果。 2051 2052> **说明:** 2053> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 2054 2055**需要权限:** ohos.permission.MICROPHONE 2056 2057**系统能力:** SystemCapability.Multimedia.Audio.Device 2058 2059**参数:** 2060 2061| 参数名 | 类型 | 必填 | 说明 | 2062| -------- | ------------------------- | ---- | --------------------------------------------- | 2063| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 2064| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 2065 2066**示例:** 2067 2068```ts 2069import { BusinessError } from '@kit.BasicServicesKit'; 2070 2071audioManager.setMicrophoneMute(true, (err: BusinessError) => { 2072 if (err) { 2073 console.error(`Failed to mute the microphone. ${err}`); 2074 return; 2075 } 2076 console.info('Callback invoked to indicate that the microphone is muted.'); 2077}); 2078``` 2079 2080### setMicrophoneMute<sup>(deprecated)</sup> 2081 2082setMicrophoneMute(mute: boolean): Promise<void> 2083 2084设置麦克风静音状态,使用Promise方式异步返回结果。 2085 2086> **说明:** 2087> 从 API version 7 开始支持,从 API version 9 开始废弃。替代接口仅面向系统应用开放。 2088 2089**需要权限:** ohos.permission.MICROPHONE 2090 2091**系统能力:** SystemCapability.Multimedia.Audio.Device 2092 2093**参数:** 2094 2095| 参数名 | 类型 | 必填 | 说明 | 2096| ------ | ------- | ---- | --------------------------------------------- | 2097| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 2098 2099**返回值:** 2100 2101| 类型 | 说明 | 2102| ------------------- | ------------------------------- | 2103| Promise<void> | Promise对象,无返回结果。 | 2104 2105**示例:** 2106 2107```ts 2108audioManager.setMicrophoneMute(true).then(() => { 2109 console.info('Promise returned to indicate that the microphone is muted.'); 2110}); 2111``` 2112 2113### isMicrophoneMute<sup>(deprecated)</sup> 2114 2115isMicrophoneMute(callback: AsyncCallback<boolean>): void 2116 2117获取麦克风静音状态,使用callback方式异步返回结果。 2118 2119> **说明:** 2120> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMicrophoneMute](#ismicrophonemute9)替代。 2121 2122**需要权限:** ohos.permission.MICROPHONE 2123 2124**系统能力:** SystemCapability.Multimedia.Audio.Device 2125 2126**参数:** 2127 2128| 参数名 | 类型 | 必填 | 说明 | 2129| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 2130| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 2131 2132**示例:** 2133 2134```ts 2135import { BusinessError } from '@kit.BasicServicesKit'; 2136 2137audioManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 2138 if (err) { 2139 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 2140 return; 2141 } 2142 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 2143}); 2144``` 2145 2146### isMicrophoneMute<sup>(deprecated)</sup> 2147 2148isMicrophoneMute(): Promise<boolean> 2149 2150获取麦克风静音状态,使用Promise方式异步返回结果。 2151 2152> **说明:** 2153> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMicrophoneMute](#ismicrophonemute9)替代。 2154 2155**需要权限:** ohos.permission.MICROPHONE 2156 2157**系统能力:** SystemCapability.Multimedia.Audio.Device 2158 2159**返回值:** 2160 2161| 类型 | 说明 | 2162| ---------------------- | ------------------------------------------------------------ | 2163| Promise<boolean> | Promise对象,返回系统麦克风静音状态,true为静音,false为非静音。 | 2164 2165**示例:** 2166 2167```ts 2168audioManager.isMicrophoneMute().then((value: boolean) => { 2169 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 2170}); 2171``` 2172 2173### on('deviceChange')<sup>(deprecated)</sup> 2174 2175on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void 2176 2177监听音频设备连接变化事件(当音频设备连接状态发生变化时触发),使用callback方式返回结果。 2178 2179> **说明:** 2180> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[on('deviceChange')](#ondevicechange9)替代。 2181 2182**系统能力:** SystemCapability.Multimedia.Audio.Device 2183 2184**参数:** 2185 2186| 参数名 | 类型 | 必填 | 说明 | 2187| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 2188| type | string | 是 | 监听事件,固定为:'deviceChange'。 | 2189| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 2190 2191**示例:** 2192 2193```ts 2194audioManager.on('deviceChange', (deviceChanged: audio.DeviceChangeAction) => { 2195 console.info(`device change type : ${deviceChanged.type} `); 2196 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `); 2197 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `); 2198 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `); 2199}); 2200``` 2201 2202### off('deviceChange')<sup>(deprecated)</sup> 2203 2204off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 2205 2206取消监听音频设备连接变化事件,使用callback方式返回结果。 2207 2208> **说明:** 2209> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[off('deviceChange')](#offdevicechange9)替代。 2210 2211**系统能力:** SystemCapability.Multimedia.Audio.Device 2212 2213**参数:** 2214 2215| 参数名 | 类型 | 必填 | 说明 | 2216| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 2217| type | string | 是 | 监听事件,固定为:'deviceChange'。 | 2218| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 2219 2220**示例:** 2221 2222```ts 2223// 取消该事件的所有监听。 2224audioManager.off('deviceChange'); 2225 2226// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2227let deviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 2228 console.info(`device change type : ${deviceChanged.type} `); 2229 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `); 2230 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `); 2231 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `); 2232}; 2233 2234audioManager.on('deviceChange', deviceChangeCallback); 2235 2236audioManager.off('deviceChange', deviceChangeCallback); 2237``` 2238 2239### on('interrupt')<sup>(deprecated)</sup> 2240 2241on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback\<InterruptAction>): void 2242 2243监听音频打断事件(当应用程序的音频被另一个播放事件中断时触发,回调通知此应用程序),使用callback方式返回结果。 2244 2245与[on('audioInterrupt')](#onaudiointerrupt9)作用一致,均用于监听焦点变化。为无音频流的场景(未曾创建AudioRenderer对象),比如FM、语音唤醒等提供焦点变化监听功能。 2246 2247> **说明:** 2248> 从 API version 7 开始支持,从 API version 11 开始废弃,建议使用AudioCapturer中的[on('audioInterrupt')](#onaudiointerrupt10)替代。 2249 2250**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2251 2252**参数:** 2253 2254| 参数名 | 类型 | 必填 | 说明 | 2255| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2256| type | string | 是 | 监听事件,固定为:'interrupt'。 | 2257| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2258| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 是 | 回调函数,返回音频打断时,应用接收的中断事件信息。 | 2259 2260**示例:** 2261 2262```ts 2263import { audio } from '@kit.AudioKit'; 2264 2265let interAudioInterrupt: audio.AudioInterrupt = { 2266 streamUsage:2, 2267 contentType:0, 2268 pauseWhenDucked:true 2269}; 2270 2271audioManager.on('interrupt', interAudioInterrupt, (interruptAction: audio.InterruptAction) => { 2272 if (interruptAction.actionType === 0) { 2273 console.info('An event to gain the audio focus starts.'); 2274 console.info(`Focus gain event: ${interruptAction} `); 2275 } 2276 if (interruptAction.actionType === 1) { 2277 console.info('An audio interruption event starts.'); 2278 console.info(`Audio interruption event: ${interruptAction} `); 2279 } 2280}); 2281``` 2282 2283### off('interrupt')<sup>(deprecated)</sup> 2284 2285off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback\<InterruptAction>): void 2286 2287取消监听音频打断事件,使用callback方式返回结果。 2288 2289> **说明:** 2290> 从 API version 7 开始支持,从 API version 11 开始废弃,建议使用AudioCapturer中的[off('audioInterrupt')](#offaudiointerrupt10)替代。 2291 2292**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2293 2294**参数:** 2295 2296| 参数名 | 类型 | 必填 | 说明 | 2297| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2298| type | string | 是 | 监听事件,固定为:'interrupt'。 | 2299| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2300| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 否 | 回调函数,返回删除监听事件,取消打断时,应用接收的中断事件信息。 | 2301 2302**示例:** 2303 2304```ts 2305import { audio } from '@kit.AudioKit'; 2306 2307let interAudioInterrupt: audio.AudioInterrupt = { 2308 streamUsage:2, 2309 contentType:0, 2310 pauseWhenDucked:true 2311}; 2312 2313// 取消该事件的所有监听。 2314audioManager.off('interrupt', interAudioInterrupt); 2315 2316// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2317let interruptCallback = (interruptAction: audio.InterruptAction) => { 2318 if (interruptAction.actionType === 0) { 2319 console.info('An event to gain the audio focus starts.'); 2320 console.info(`Focus gain event: ${interruptAction} `); 2321 } 2322 if (interruptAction.actionType === 1) { 2323 console.info('An audio interruption event starts.'); 2324 console.info(`Audio interruption event: ${interruptAction} `); 2325 } 2326}; 2327 2328audioManager.on('interrupt', interAudioInterrupt, interruptCallback); 2329 2330audioManager.off('interrupt', interAudioInterrupt, interruptCallback); 2331``` 2332 2333## AudioVolumeManager<sup>9+</sup> 2334 2335音量管理。在使用AudioVolumeManager的接口前,需要使用[getVolumeManager](#getvolumemanager9)获取AudioVolumeManager实例。 2336 2337### getVolumeGroupManager<sup>9+</sup> 2338 2339getVolumeGroupManager(groupId: number, callback: AsyncCallback<AudioVolumeGroupManager\>\): void 2340 2341获取音频组管理器,使用callback方式异步返回结果。 2342 2343**系统能力:** SystemCapability.Multimedia.Audio.Volume 2344 2345**参数:** 2346 2347| 参数名 | 类型 | 必填 | 说明 | 2348| ---------- | ------------------------------------------------------------ | ---- |-----------------------------------------------------------| 2349| groupId | number | 是 | 音量组id,默认使用LOCAL_VOLUME_GROUP_ID。 | 2350| callback | AsyncCallback<[AudioVolumeGroupManager](#audiovolumegroupmanager9)> | 是 | 回调函数。当获取音频组管理器成功,err为undefined,data为获取到的音频组管理器对象;否则为错误对象。 | 2351 2352**示例:** 2353 2354```ts 2355import { BusinessError } from '@kit.BasicServicesKit'; 2356 2357let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2358 2359audioVolumeManager.getVolumeGroupManager(groupId, (err: BusinessError, value: audio.AudioVolumeGroupManager) => { 2360 if (err) { 2361 console.error(`Failed to obtain the volume group infos list. ${err}`); 2362 return; 2363 } 2364 console.info('Callback invoked to indicate that the volume group infos list is obtained.'); 2365}); 2366 2367``` 2368 2369### getVolumeGroupManager<sup>9+</sup> 2370 2371getVolumeGroupManager(groupId: number\): Promise<AudioVolumeGroupManager\> 2372 2373获取音频组管理器,使用Promise方式异步返回结果。 2374 2375**系统能力:** SystemCapability.Multimedia.Audio.Volume 2376 2377**参数:** 2378 2379| 参数名 | 类型 | 必填 | 说明 | 2380| ---------- | ---------------------------------------- | ---- |----------------------------------| 2381| groupId | number | 是 | 音量组id,默认使用LOCAL_VOLUME_GROUP_ID。 | 2382 2383**返回值:** 2384 2385| 类型 | 说明 | 2386| ------------------- | ----------------------------- | 2387| Promise< [AudioVolumeGroupManager](#audiovolumegroupmanager9) > | Promise对象,返回音量组实例。 | 2388 2389**示例:** 2390 2391```ts 2392import { audio } from '@kit.AudioKit'; 2393 2394let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2395let audioVolumeGroupManager: audio.AudioVolumeGroupManager | undefined = undefined; 2396 2397async function getVolumeGroupManager(){ 2398 audioVolumeGroupManager = await audioVolumeManager.getVolumeGroupManager(groupId); 2399 console.info('Promise returned to indicate that the volume group infos list is obtained.'); 2400} 2401``` 2402 2403### getVolumeGroupManagerSync<sup>10+</sup> 2404 2405getVolumeGroupManagerSync(groupId: number\): AudioVolumeGroupManager 2406 2407获取音频组管理器,同步返回结果。 2408 2409**系统能力:** SystemCapability.Multimedia.Audio.Volume 2410 2411**参数:** 2412 2413| 参数名 | 类型 | 必填 | 说明 | 2414| ---------- | ---------------------------------------- | ---- |----------------------------------| 2415| groupId | number | 是 | 音量组id,默认使用LOCAL_VOLUME_GROUP_ID。 | 2416 2417**返回值:** 2418 2419| 类型 | 说明 | 2420| ------------------- | ----------------------------- | 2421| [AudioVolumeGroupManager](#audiovolumegroupmanager9) | 音量组实例。 | 2422 2423**错误码:** 2424 2425以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2426 2427| 错误码ID | 错误信息 | 2428| ------- | --------------------------------------------| 2429| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2430| 6800101 | Parameter verification failed. | 2431 2432**示例:** 2433 2434```ts 2435import { BusinessError } from '@kit.BasicServicesKit'; 2436 2437try { 2438 let audioVolumeGroupManager: audio.AudioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(audio.DEFAULT_VOLUME_GROUP_ID); 2439 console.info(`Get audioVolumeGroupManager success.`); 2440} catch (err) { 2441 let error = err as BusinessError; 2442 console.error(`Failed to get audioVolumeGroupManager, error: ${error}`); 2443} 2444``` 2445 2446### on('volumeChange')<sup>9+</sup> 2447 2448on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void 2449 2450监听系统音量变化事件(当系统音量发生变化时触发),使用callback方式返回结果。 2451 2452**系统能力:** SystemCapability.Multimedia.Audio.Volume 2453 2454**参数:** 2455 2456| 参数名 | 类型 | 必填 | 说明 | 2457| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2458| type | string | 是 | 监听事件,固定为:'volumeChange'。 | 2459| callback | Callback<[VolumeEvent](#volumeevent9)> | 是 | 回调函数,返回变化后的音量信息。 | 2460 2461**错误码:** 2462 2463以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2464 2465| 错误码ID | 错误信息 | 2466| ------- | --------------------------------------------| 2467| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2468| 6800101 | Parameter verification failed. | 2469 2470**示例:** 2471 2472```ts 2473audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => { 2474 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2475 console.info(`Volume level: ${volumeEvent.volume} `); 2476 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2477}); 2478``` 2479 2480### off('volumeChange')<sup>12+</sup> 2481 2482off(type: 'volumeChange', callback?: Callback\<VolumeEvent>): void 2483 2484取消监听系统音量变化事件,使用callback方式返回结果。 2485 2486**系统能力:** SystemCapability.Multimedia.Audio.Volume 2487 2488**参数:** 2489 2490| 参数名 | 类型 | 必填 | 说明 | 2491| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2492| type | string | 是 | 监听事件,固定为:'volumeChange'。 | 2493| callback | Callback<[VolumeEvent](#volumeevent9)> | 否 | 回调函数,返回变化后的音量信息。 | 2494 2495**错误码:** 2496 2497以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2498 2499| 错误码ID | 错误信息 | 2500| ------- | --------------------------------------------| 2501| 401 | Parameter error. Possible causes: 1.Mandatory parameters missing; 2.Incorrect parameter types. | 2502| 6800101 | Parameter verification failed. | 2503 2504**示例:** 2505 2506```ts 2507// 取消该事件的所有监听。 2508audioVolumeManager.off('volumeChange'); 2509 2510// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2511let volumeChangeCallback = (volumeEvent: audio.VolumeEvent) => { 2512 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2513 console.info(`Volume level: ${volumeEvent.volume} `); 2514 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2515}; 2516 2517audioVolumeManager.on('volumeChange', volumeChangeCallback); 2518 2519audioVolumeManager.off('volumeChange', volumeChangeCallback); 2520``` 2521 2522## AudioVolumeGroupManager<sup>9+</sup> 2523 2524管理音频组音量。在调用AudioVolumeGroupManager的接口前,需要先通过 [getVolumeGroupManager](#getvolumegroupmanager9) 创建实例。 2525 2526### getVolume<sup>9+</sup> 2527 2528getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2529 2530获取指定流的音量,使用callback方式异步返回结果。 2531 2532**系统能力:** SystemCapability.Multimedia.Audio.Volume 2533 2534**参数:** 2535 2536| 参数名 | 类型 | 必填 | 说明 | 2537| ---------- | ----------------------------------- | ---- | ------------------ | 2538| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2539| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2540 2541**示例:** 2542 2543```ts 2544import { BusinessError } from '@kit.BasicServicesKit'; 2545 2546audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2547 if (err) { 2548 console.error(`Failed to obtain the volume. ${err}`); 2549 return; 2550 } 2551 console.info('Callback invoked to indicate that the volume is obtained.'); 2552}); 2553``` 2554 2555### getVolume<sup>9+</sup> 2556 2557getVolume(volumeType: AudioVolumeType): Promise<number> 2558 2559获取指定流的音量,使用Promise方式异步返回结果。 2560 2561**系统能力:** SystemCapability.Multimedia.Audio.Volume 2562 2563**参数:** 2564 2565| 参数名 | 类型 | 必填 | 说明 | 2566| ---------- | ----------------------------------- | ---- | ------------ | 2567| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2568 2569**返回值:** 2570 2571| 类型 | 说明 | 2572| --------------------- | ------------------------- | 2573| Promise<number> | Promise对象,返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2574 2575**示例:** 2576 2577```ts 2578audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2579 console.info(`Promise returned to indicate that the volume is obtained ${value}.`); 2580}); 2581``` 2582 2583### getVolumeSync<sup>10+</sup> 2584 2585getVolumeSync(volumeType: AudioVolumeType): number; 2586 2587获取指定流的音量,同步返回结果。 2588 2589**系统能力:** SystemCapability.Multimedia.Audio.Volume 2590 2591**参数:** 2592 2593| 参数名 | 类型 | 必填 | 说明 | 2594| ---------- | ----------------------------------- | ---- | ------------ | 2595| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2596 2597**返回值:** 2598 2599| 类型 | 说明 | 2600| --------------------- | ------------------------- | 2601| number | 返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2602 2603**错误码:** 2604 2605以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2606 2607| 错误码ID | 错误信息 | 2608| ------- | --------------------------------------------| 2609| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2610| 6800101 | Parameter verification failed. | 2611 2612**示例:** 2613 2614```ts 2615import { BusinessError } from '@kit.BasicServicesKit'; 2616 2617try { 2618 let value: number = audioVolumeGroupManager.getVolumeSync(audio.AudioVolumeType.MEDIA); 2619 console.info(`Indicate that the volume is obtained ${value}.`); 2620} catch (err) { 2621 let error = err as BusinessError; 2622 console.error(`Failed to obtain the volume, error ${error}.`); 2623} 2624``` 2625 2626### getMinVolume<sup>9+</sup> 2627 2628getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2629 2630获取指定流的最小音量,使用callback方式异步返回结果。 2631 2632**系统能力:** SystemCapability.Multimedia.Audio.Volume 2633 2634**参数:** 2635 2636| 参数名 | 类型 | 必填 | 说明 | 2637| ---------- | ----------------------------------- | ---- | ------------------ | 2638| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2639| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 2640 2641**示例:** 2642 2643```ts 2644import { BusinessError } from '@kit.BasicServicesKit'; 2645 2646audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2647 if (err) { 2648 console.error(`Failed to obtain the minimum volume. ${err}`); 2649 return; 2650 } 2651 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 2652}); 2653``` 2654 2655### getMinVolume<sup>9+</sup> 2656 2657getMinVolume(volumeType: AudioVolumeType): Promise<number> 2658 2659获取指定流的最小音量,使用Promise方式异步返回结果。 2660 2661**系统能力:** SystemCapability.Multimedia.Audio.Volume 2662 2663**参数:** 2664 2665| 参数名 | 类型 | 必填 | 说明 | 2666| ---------- | ----------------------------------- | ---- | ------------ | 2667| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2668 2669**返回值:** 2670 2671| 类型 | 说明 | 2672| --------------------- | ------------------------- | 2673| Promise<number> | Promise对象,返回最小音量。 | 2674 2675**示例:** 2676 2677```ts 2678audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2679 console.info(`Promised returned to indicate that the minimum volume is obtained ${value}.`); 2680}); 2681``` 2682 2683### getMinVolumeSync<sup>10+</sup> 2684 2685getMinVolumeSync(volumeType: AudioVolumeType): number; 2686 2687获取指定流的最小音量,同步返回结果。 2688 2689**系统能力:** SystemCapability.Multimedia.Audio.Volume 2690 2691**参数:** 2692 2693| 参数名 | 类型 | 必填 | 说明 | 2694| ---------- | ----------------------------------- | ---- | ------------ | 2695| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2696 2697**返回值:** 2698 2699| 类型 | 说明 | 2700| --------------------- | ------------------------- | 2701| number | 返回最小音量。 | 2702 2703**错误码:** 2704 2705以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2706 2707| 错误码ID | 错误信息 | 2708| ------- | --------------------------------------------| 2709| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2710| 6800101 | Parameter verification failed. | 2711 2712**示例:** 2713 2714```ts 2715import { BusinessError } from '@kit.BasicServicesKit'; 2716 2717try { 2718 let value: number = audioVolumeGroupManager.getMinVolumeSync(audio.AudioVolumeType.MEDIA); 2719 console.info(`Indicate that the minimum volume is obtained ${value}.`); 2720} catch (err) { 2721 let error = err as BusinessError; 2722 console.error(`Failed to obtain the minimum volume, error ${error}.`); 2723} 2724``` 2725 2726### getMaxVolume<sup>9+</sup> 2727 2728getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2729 2730获取指定流的最大音量,使用callback方式异步返回结果。 2731 2732**系统能力:** SystemCapability.Multimedia.Audio.Volume 2733 2734**参数:** 2735 2736| 参数名 | 类型 | 必填 | 说明 | 2737| ---------- | ----------------------------------- | ---- | ---------------------- | 2738| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2739| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 2740 2741**示例:** 2742 2743```ts 2744import { BusinessError } from '@kit.BasicServicesKit'; 2745 2746audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2747 if (err) { 2748 console.error(`Failed to obtain the maximum volume. ${err}`); 2749 return; 2750 } 2751 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 2752}); 2753``` 2754 2755### getMaxVolume<sup>9+</sup> 2756 2757getMaxVolume(volumeType: AudioVolumeType): Promise<number> 2758 2759获取指定流的最大音量,使用Promise方式异步返回结果。 2760 2761**系统能力:** SystemCapability.Multimedia.Audio.Volume 2762 2763**参数:** 2764 2765| 参数名 | 类型 | 必填 | 说明 | 2766| ---------- | ----------------------------------- | ---- | ------------ | 2767| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2768 2769**返回值:** 2770 2771| 类型 | 说明 | 2772| --------------------- | ----------------------------- | 2773| Promise<number> | Promise对象,返回最大音量大小。 | 2774 2775**示例:** 2776 2777```ts 2778audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 2779 console.info('Promised returned to indicate that the maximum volume is obtained.'); 2780}); 2781``` 2782 2783### getMaxVolumeSync<sup>10+</sup> 2784 2785getMaxVolumeSync(volumeType: AudioVolumeType): number; 2786 2787获取指定流的最大音量,同步返回结果。 2788 2789**系统能力:** SystemCapability.Multimedia.Audio.Volume 2790 2791**参数:** 2792 2793| 参数名 | 类型 | 必填 | 说明 | 2794| ---------- | ----------------------------------- | ---- | ------------ | 2795| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2796 2797**返回值:** 2798 2799| 类型 | 说明 | 2800| --------------------- | ----------------------------- | 2801| number | 返回最大音量大小。 | 2802 2803**错误码:** 2804 2805以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2806 2807| 错误码ID | 错误信息 | 2808| ------- | --------------------------------------------| 2809| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2810| 6800101 | Parameter verification failed. | 2811 2812**示例:** 2813 2814```ts 2815import { BusinessError } from '@kit.BasicServicesKit'; 2816 2817try { 2818 let value: number = audioVolumeGroupManager.getMaxVolumeSync(audio.AudioVolumeType.MEDIA); 2819 console.info(`Indicate that the maximum volume is obtained. ${value}`); 2820} catch (err) { 2821 let error = err as BusinessError; 2822 console.error(`Failed to obtain the maximum volume, error ${error}.`); 2823} 2824``` 2825 2826### isMute<sup>9+</sup> 2827 2828isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 2829 2830获取指定音量流是否被静音,使用callback方式异步返回结果。 2831 2832**系统能力:** SystemCapability.Multimedia.Audio.Volume 2833 2834**参数:** 2835 2836| 参数名 | 类型 | 必填 | 说明 | 2837| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 2838| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2839| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否被静音成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 2840 2841**示例:** 2842 2843```ts 2844import { BusinessError } from '@kit.BasicServicesKit'; 2845 2846audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 2847 if (err) { 2848 console.error(`Failed to obtain the mute status. ${err}`); 2849 return; 2850 } 2851 console.info(`Callback invoked to indicate that the mute status of the stream is obtained ${value}.`); 2852}); 2853``` 2854 2855### isMute<sup>9+</sup> 2856 2857isMute(volumeType: AudioVolumeType): Promise<boolean> 2858 2859获取指定音量流是否被静音,使用Promise方式异步返回结果。 2860 2861**系统能力:** SystemCapability.Multimedia.Audio.Volume 2862 2863**参数:** 2864 2865| 参数名 | 类型 | 必填 | 说明 | 2866| ---------- | ----------------------------------- | ---- | ------------ | 2867| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2868 2869**返回值:** 2870 2871| 类型 | 说明 | 2872| ---------------------- | ------------------------------------------------------ | 2873| Promise<boolean> | Promise对象,返回流静音状态,true为静音,false为非静音。 | 2874 2875**示例:** 2876 2877```ts 2878audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 2879 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 2880}); 2881``` 2882 2883### isMuteSync<sup>10+</sup> 2884 2885isMuteSync(volumeType: AudioVolumeType): boolean 2886 2887获取指定音量流是否被静音,同步返回结果。 2888 2889**系统能力:** SystemCapability.Multimedia.Audio.Volume 2890 2891**参数:** 2892 2893| 参数名 | 类型 | 必填 | 说明 | 2894| ---------- | ----------------------------------- | ---- | ------------ | 2895| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2896 2897**返回值:** 2898 2899| 类型 | 说明 | 2900| ---------------------- | ------------------------------------------------------ | 2901| boolean | 返回流静音状态,true为静音,false为非静音。 | 2902 2903**错误码:** 2904 2905以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2906 2907| 错误码ID | 错误信息 | 2908| ------- | --------------------------------------------| 2909| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2910| 6800101 | Parameter verification failed. | 2911 2912**示例:** 2913 2914```ts 2915import { BusinessError } from '@kit.BasicServicesKit'; 2916 2917try { 2918 let value: boolean = audioVolumeGroupManager.isMuteSync(audio.AudioVolumeType.MEDIA); 2919 console.info(`Indicate that the mute status of the stream is obtained ${value}.`); 2920} catch (err) { 2921 let error = err as BusinessError; 2922 console.error(`Failed to obtain the mute status of the stream, error ${error}.`); 2923} 2924``` 2925 2926### getRingerMode<sup>9+</sup> 2927 2928getRingerMode(callback: AsyncCallback<AudioRingMode>): void 2929 2930获取铃声模式,使用callback方式异步返回结果。 2931 2932**系统能力:** SystemCapability.Multimedia.Audio.Volume 2933 2934**参数:** 2935 2936| 参数名 | 类型 | 必填 | 说明 | 2937| -------- | ---------------------------------------------------- | ---- | ------------------------ | 2938| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 2939 2940**示例:** 2941 2942```ts 2943import { BusinessError } from '@kit.BasicServicesKit'; 2944 2945audioVolumeGroupManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 2946 if (err) { 2947 console.error(`Failed to obtain the ringer mode. ${err}`); 2948 return; 2949 } 2950 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 2951}); 2952``` 2953 2954### getRingerMode<sup>9+</sup> 2955 2956getRingerMode(): Promise<AudioRingMode> 2957 2958获取铃声模式,使用Promise方式异步返回结果。 2959 2960**系统能力:** SystemCapability.Multimedia.Audio.Volume 2961 2962**返回值:** 2963 2964| 类型 | 说明 | 2965| ---------------------------------------------- | ------------------------------- | 2966| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 2967 2968**示例:** 2969 2970```ts 2971audioVolumeGroupManager.getRingerMode().then((value: audio.AudioRingMode) => { 2972 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 2973}); 2974``` 2975 2976### getRingerModeSync<sup>10+</sup> 2977 2978getRingerModeSync(): AudioRingMode 2979 2980获取铃声模式,同步返回结果。 2981 2982**系统能力:** SystemCapability.Multimedia.Audio.Volume 2983 2984**返回值:** 2985 2986| 类型 | 说明 | 2987| ---------------------------------------------- | ------------------------------- | 2988| [AudioRingMode](#audioringmode) | 返回系统的铃声模式。 | 2989 2990**示例:** 2991 2992```ts 2993import { BusinessError } from '@kit.BasicServicesKit'; 2994 2995try { 2996 let value: audio.AudioRingMode = audioVolumeGroupManager.getRingerModeSync(); 2997 console.info(`Indicate that the ringer mode is obtained ${value}.`); 2998} catch (err) { 2999 let error = err as BusinessError; 3000 console.error(`Failed to obtain the ringer mode, error ${error}.`); 3001} 3002``` 3003 3004### on('ringerModeChange')<sup>9+</sup> 3005 3006on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void 3007 3008监听铃声模式变化事件(当[铃声模式](#audioringmode)发生变化时触发),使用callback方式返回结果。 3009 3010**系统能力:** SystemCapability.Multimedia.Audio.Volume 3011 3012**参数:** 3013 3014| 参数名 | 类型 | 必填 | 说明 | 3015| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 3016| type | string | 是 | 监听事件,固定为:'ringerModeChange'。 | 3017| callback | Callback<[AudioRingMode](#audioringmode)> | 是 | 回调函数,返回变化后的铃音模式。 | 3018 3019**错误码:** 3020 3021以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3022 3023| 错误码ID | 错误信息 | 3024| ------- | --------------------------------------------| 3025| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3026| 6800101 | Parameter verification failed. | 3027 3028**示例:** 3029 3030```ts 3031audioVolumeGroupManager.on('ringerModeChange', (ringerMode: audio.AudioRingMode) => { 3032 console.info(`Updated ringermode: ${ringerMode}`); 3033}); 3034``` 3035 3036### setMicrophoneMute<sup>(deprecated)</sup> 3037 3038setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 3039 3040设置麦克风静音状态,使用callback方式异步返回结果。 3041 3042> **说明:** 3043> 3044> 从 API version 9开始支持,从API version 11 开始废弃。替代接口仅面向系统应用开放。 3045 3046**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG,该权限仅系统应用可申请。 3047 3048**系统能力:** SystemCapability.Multimedia.Audio.Volume 3049 3050**参数:** 3051 3052| 参数名 | 类型 | 必填 | 说明 | 3053| -------- | ------------------------- | ---- | --------------------------------------------- | 3054| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 3055| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 3056 3057**示例:** 3058 3059```ts 3060import { BusinessError } from '@kit.BasicServicesKit'; 3061 3062audioVolumeGroupManager.setMicrophoneMute(true, (err: BusinessError) => { 3063 if (err) { 3064 console.error(`Failed to mute the microphone. ${err}`); 3065 return; 3066 } 3067 console.info('Callback invoked to indicate that the microphone is muted.'); 3068}); 3069``` 3070 3071### setMicrophoneMute<sup>(deprecated)</sup> 3072 3073setMicrophoneMute(mute: boolean): Promise<void> 3074 3075设置麦克风静音状态,使用Promise方式异步返回结果。 3076 3077> **说明:** 3078> 3079> 从 API version 9开始支持,从API version 11 开始废弃。替代接口仅面向系统应用开放。 3080 3081**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG,该权限仅系统应用可申请。 3082 3083**系统能力:** SystemCapability.Multimedia.Audio.Volume 3084 3085**参数:** 3086 3087| 参数名 | 类型 | 必填 | 说明 | 3088| ------ | ------- | ---- | --------------------------------------------- | 3089| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 3090 3091**返回值:** 3092 3093| 类型 | 说明 | 3094| ------------------- | ------------------------------- | 3095| Promise<void> | Promise对象,无返回结果。 | 3096 3097**示例:** 3098 3099```ts 3100audioVolumeGroupManager.setMicrophoneMute(true).then(() => { 3101 console.info('Promise returned to indicate that the microphone is muted.'); 3102}); 3103``` 3104 3105### isMicrophoneMute<sup>9+</sup> 3106 3107isMicrophoneMute(callback: AsyncCallback<boolean>): void 3108 3109获取麦克风静音状态,使用callback方式异步返回结果。 3110 3111**系统能力:** SystemCapability.Multimedia.Audio.Volume 3112 3113**参数:** 3114 3115| 参数名 | 类型 | 必填 | 说明 | 3116| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 3117| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 3118 3119**示例:** 3120 3121```ts 3122import { BusinessError } from '@kit.BasicServicesKit'; 3123 3124audioVolumeGroupManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 3125 if (err) { 3126 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 3127 return; 3128 } 3129 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 3130}); 3131``` 3132 3133### isMicrophoneMute<sup>9+</sup> 3134 3135isMicrophoneMute(): Promise<boolean> 3136 3137获取麦克风静音状态,使用Promise方式异步返回结果。 3138 3139**系统能力:** SystemCapability.Multimedia.Audio.Volume 3140 3141**返回值:** 3142 3143| 类型 | 说明 | 3144| ---------------------- | ------------------------------------------------------------ | 3145| Promise<boolean> | Promise对象,返回系统麦克风静音状态,true为静音,false为非静音。 | 3146 3147**示例:** 3148 3149```ts 3150audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => { 3151 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 3152}); 3153``` 3154 3155### isMicrophoneMuteSync<sup>10+</sup> 3156 3157isMicrophoneMuteSync(): boolean 3158 3159获取麦克风静音状态,同步返回结果。 3160 3161**系统能力:** SystemCapability.Multimedia.Audio.Volume 3162 3163**返回值:** 3164 3165| 类型 | 说明 | 3166| ---------------------- | ------------------------------------------------------------ | 3167| boolean | 返回系统麦克风静音状态,true为静音,false为非静音。 | 3168 3169**示例:** 3170 3171```ts 3172import { BusinessError } from '@kit.BasicServicesKit'; 3173 3174try { 3175 let value: boolean = audioVolumeGroupManager.isMicrophoneMuteSync(); 3176 console.info(`Indicate that the mute status of the microphone is obtained ${value}.`); 3177} catch (err) { 3178 let error = err as BusinessError; 3179 console.error(`Failed to obtain the mute status of the microphone, error ${error}.`); 3180} 3181``` 3182 3183### on('micStateChange')<sup>9+</sup> 3184 3185on(type: 'micStateChange', callback: Callback<MicStateChangeEvent>): void 3186 3187监听系统麦克风状态更改事件(当检测到系统麦克风状态发生改变时触发),使用callback方式返回结果。 3188 3189目前此订阅接口在单进程多AudioManager实例的使用场景下,仅最后一个实例的订阅生效,其他实例的订阅会被覆盖(即使最后一个实例没有进行订阅),因此推荐使用单一AudioManager实例进行开发。 3190 3191**系统能力:** SystemCapability.Multimedia.Audio.Volume 3192 3193**参数:** 3194 3195| 参数名 | 类型 | 必填 | 说明 | 3196| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 3197| type | string | 是 | 监听事件,固定为:'micStateChange'。 | 3198| callback | Callback<[MicStateChangeEvent](#micstatechangeevent9)> | 是 | 回调函数,返回变更后的麦克风状态。 | 3199 3200**错误码:** 3201 3202以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3203 3204| 错误码ID | 错误信息 | 3205| ------- | --------------------------------------------| 3206| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3207| 6800101 | Parameter verification failed. | 3208 3209**示例:** 3210 3211```ts 3212audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => { 3213 console.info(`Current microphone status is: ${micStateChange.mute} `); 3214}); 3215``` 3216 3217### off('micStateChange')<sup>12+</sup> 3218 3219off(type: 'micStateChange', callback?: Callback<MicStateChangeEvent>): void 3220 3221取消监听系统麦克风状态更改事件,使用callback方式返回结果。 3222 3223**系统能力:** SystemCapability.Multimedia.Audio.Volume 3224 3225**参数:** 3226 3227| 参数名 | 类型 | 必填 | 说明 | 3228| -------- | -------------------------------------- |----| ------------------------------------------------------------ | 3229| type | string | 是 | 监听事件,固定为:'micStateChange'。 | 3230| callback | Callback<[MicStateChangeEvent](#micstatechangeevent9)> | 否 | 回调函数,返回变更后的麦克风状态。 | 3231 3232**错误码:** 3233 3234以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3235 3236| 错误码ID | 错误信息 | 3237| ------- | --------------------------------------------| 3238| 401 | Parameter error. Possible causes: 1.Mandatory parameters missing; 2.Incorrect parameter types. | 3239| 6800101 | Parameter verification failed. | 3240 3241**示例:** 3242 3243```ts 3244// 取消该事件的所有监听。 3245audioVolumeGroupManager.off('micStateChange'); 3246 3247// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 3248let micStateChangeCallback = (micStateChange: audio.MicStateChangeEvent) => { 3249 console.info(`Current microphone status is: ${micStateChange.mute} `); 3250}; 3251 3252audioVolumeGroupManager.on('micStateChange', micStateChangeCallback); 3253 3254audioVolumeGroupManager.off('micStateChange', micStateChangeCallback); 3255``` 3256 3257### isVolumeUnadjustable<sup>10+</sup> 3258 3259isVolumeUnadjustable(): boolean 3260 3261获取固定音量模式开关状态,打开时进入固定音量模式,此时音量固定无法被调节,使用同步方式返回结果。 3262 3263**系统能力:** SystemCapability.Multimedia.Audio.Volume 3264 3265**返回值:** 3266 3267| 类型 | 说明 | 3268| ---------------------- | ------------------------------------------------------ | 3269| boolean | 同步接口,返回固定音量模式开关状态,true为固定音量模式,false为非固定音量模式。 | 3270 3271**示例:** 3272 3273```ts 3274let volumeAdjustSwitch: boolean = audioVolumeGroupManager.isVolumeUnadjustable(); 3275console.info(`Whether it is volume unadjustable: ${volumeAdjustSwitch}.`); 3276``` 3277 3278### getSystemVolumeInDb<sup>10+</sup> 3279 3280getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType, callback: AsyncCallback<number>): void 3281 3282获取音量增益dB值,使用callback方式异步返回结果。 3283 3284**系统能力:** SystemCapability.Multimedia.Audio.Volume 3285 3286**参数:** 3287 3288| 参数名 | 类型 | 必填 | 说明 | 3289| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3290| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3291| volumeLevel | number | 是 | 音量等级。 | 3292| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3293| callback | AsyncCallback<number> | 是 | 回调函数。当获取音量增益dB值成功,err为undefined,data为获取到的音量增益dB值;否则为错误对象。 | 3294 3295**错误码:** 3296 3297以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3298 3299| 错误码ID | 错误信息 | 3300| ------- | --------------------------------------------| 3301| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3302| 6800101 | Parameter verification failed. Return by callback. | 3303| 6800301 | System error. Return by callback. | 3304 3305**示例:** 3306 3307```ts 3308import { BusinessError } from '@kit.BasicServicesKit'; 3309 3310audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER, (err: BusinessError, dB: number) => { 3311 if (err) { 3312 console.error(`Failed to get the volume DB. ${err}`); 3313 } else { 3314 console.info(`Success to get the volume DB. ${dB}`); 3315 } 3316}); 3317``` 3318### getSystemVolumeInDb<sup>10+</sup> 3319 3320getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): Promise<number> 3321 3322获取音量增益dB值,使用Promise方式异步返回结果。 3323 3324**系统能力:** SystemCapability.Multimedia.Audio.Volume 3325 3326**参数:** 3327 3328| 参数名 | 类型 | 必填 | 说明 | 3329| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3330| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3331| volumeLevel | number | 是 | 音量等级。 | 3332| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3333 3334**返回值:** 3335 3336| 类型 | 说明 | 3337| --------------------- | ---------------------------------- | 3338| Promise<number> | Promise对象,返回对应的音量增益dB值。 | 3339 3340**错误码:** 3341 3342以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3343 3344| 错误码ID | 错误信息 | 3345| ------- | --------------------------------------------| 3346| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3347| 6800101 | Parameter verification failed. Return by promise. | 3348| 6800301 | System error. Return by promise. | 3349 3350**示例:** 3351 3352```ts 3353import { BusinessError } from '@kit.BasicServicesKit'; 3354 3355audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER).then((value: number) => { 3356 console.info(`Success to get the volume DB. ${value}`); 3357}).catch((error: BusinessError) => { 3358 console.error(`Fail to adjust the system volume by step. ${error}`); 3359}); 3360``` 3361 3362### getSystemVolumeInDbSync<sup>10+</sup> 3363 3364getSystemVolumeInDbSync(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): number 3365 3366获取音量增益dB值,同步返回结果。 3367 3368**系统能力:** SystemCapability.Multimedia.Audio.Volume 3369 3370**参数:** 3371 3372| 参数名 | 类型 | 必填 | 说明 | 3373| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3374| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3375| volumeLevel | number | 是 | 音量等级。 | 3376| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3377 3378**返回值:** 3379 3380| 类型 | 说明 | 3381| --------------------- | ---------------------------------- | 3382| number | 返回对应的音量增益dB值。 | 3383 3384**错误码:** 3385 3386以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3387 3388| 错误码ID | 错误信息 | 3389| ------- | --------------------------------------------| 3390| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3391| 6800101 | Parameter verification failed. | 3392 3393**示例:** 3394 3395```ts 3396import { BusinessError } from '@kit.BasicServicesKit'; 3397 3398try { 3399 let value: number = audioVolumeGroupManager.getSystemVolumeInDbSync(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER); 3400 console.info(`Success to get the volume DB. ${value}`); 3401} catch (err) { 3402 let error = err as BusinessError; 3403 console.error(`Fail to adjust the system volume by step. ${error}`); 3404} 3405``` 3406 3407### getMaxAmplitudeForInputDevice<sup>12+</sup> 3408 3409getMaxAmplitudeForInputDevice(inputDevice: AudioDeviceDescriptor): Promise<number> 3410 3411获取输入设备音频流的最大电平值,大小取值在0-1之间,最小为0,使用Promise方式异步返回结果。 3412 3413**系统能力:** SystemCapability.Multimedia.Audio.Volume 3414 3415**参数:** 3416 3417| 参数名 | 类型 | 必填 | 说明 | 3418| ----------- | ------------------------------------- | ---- | --------------------------------------------------- | 3419| inputDevice | [AudioDeviceDescriptor](#audiodevicedescriptor) | 是 | 获取最大电平值的设备信息。 | 3420 3421**返回值:** 3422 3423| 类型 | 说明 | 3424| --------------------- | ---------------------------------- | 3425| Promise<number> | Promise对象,返回对应设备的电平值,大小在0-1之间。 | 3426 3427**错误码:** 3428 3429以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3430 3431| 错误码ID | 错误信息 | 3432| ------- | --------------------------------------------| 3433| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3434| 6800101 | Parameter verification failed. Return by promise. | 3435| 6800301 | System error. Return by promise. | 3436 3437**示例:** 3438 3439```ts 3440import { BusinessError } from '@kit.BasicServicesKit'; 3441 3442let capturerInfo: audio.AudioCapturerInfo = { 3443 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 3444 capturerFlags: 0 // 音频采集器标志。 3445}; 3446 3447audio.getAudioManager().getRoutingManager().getPreferredInputDeviceForCapturerInfo(capturerInfo).then((data) => { 3448 audioVolumeGroupManager.getMaxAmplitudeForInputDevice(data[0]).then((value) => { 3449 console.info(`mic volatileume amplitude is: ${value}`); 3450 }).catch((err: BusinessError) => { 3451 console.error("getMaxAmplitudeForInputDevice error" + JSON.stringify(err)); 3452 }) 3453}).catch((err: BusinessError) => { 3454 console.error("get outputDeviceId error" + JSON.stringify(err)); 3455}) 3456``` 3457 3458### getMaxAmplitudeForOutputDevice<sup>12+</sup> 3459 3460getMaxAmplitudeForOutputDevice(outputDevice: AudioDeviceDescriptor): Promise<number> 3461 3462获取输出设备音频流的最大电平值,大小取值在0-1之间,最小为0,使用Promise方式异步返回结果。 3463 3464**系统能力:** SystemCapability.Multimedia.Audio.Volume 3465 3466**参数:** 3467 3468| 参数名 | 类型 | 必填 | 说明 | 3469| ------------ | --------------------------------------- | ---- | -------------------------------------------------------- | 3470| outputDevice | [AudioDeviceDescriptor](#audiodevicedescriptor) | 是 | 获取最大电平值的设备信息。 | 3471 3472**返回值:** 3473 3474| 类型 | 说明 | 3475| --------------------- | ---------------------------------- | 3476| Promise<number> | Promise对象,返回对应设备的电平值,大小在0-1之间。 | 3477 3478**错误码:** 3479 3480以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3481 3482| 错误码ID | 错误信息 | 3483| ------- | --------------------------------------------| 3484| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3485| 6800101 | Parameter verification failed. Return by promise. | 3486| 6800301 | System error. Return by promise. | 3487 3488**示例:** 3489 3490```ts 3491import { BusinessError } from '@kit.BasicServicesKit'; 3492 3493let rendererInfo: audio.AudioRendererInfo = { 3494 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 3495 rendererFlags: 0 // 音频渲染器标志。 3496}; 3497 3498audio.getAudioManager().getRoutingManager().getPreferOutputDeviceForRendererInfo(rendererInfo).then((data) => { 3499 audioVolumeGroupManager.getMaxAmplitudeForOutputDevice(data[0]).then((value) => { 3500 console.info(`mic volatileume amplitude is: ${value}`); 3501 }).catch((err: BusinessError) => { 3502 console.error("getMaxAmplitudeForOutputDevice error" + JSON.stringify(err)); 3503 }) 3504}).catch((err: BusinessError) => { 3505 console.error("getPreferOutputDeviceForRendererInfo error" + JSON.stringify(err)); 3506}) 3507``` 3508 3509## AudioStreamManager<sup>9+</sup> 3510 3511管理音频流。在使用AudioStreamManager的API前,需要使用[getStreamManager](#getstreammanager9)获取AudioStreamManager实例。 3512 3513### getCurrentAudioRendererInfoArray<sup>9+</sup> 3514 3515getCurrentAudioRendererInfoArray(callback: AsyncCallback<AudioRendererChangeInfoArray>): void 3516 3517获取当前音频渲染器的信息。使用callback异步回调。 3518 3519**系统能力**: SystemCapability.Multimedia.Audio.Renderer 3520 3521**参数:** 3522 3523| 参数名 | 类型 | 必填 | 说明 | 3524| -------- | ----------------------------------- | -------- | --------------------------- | 3525| callback | AsyncCallback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数。当获取当前音频渲染器的信息成功,err为undefined,data为获取到的当前音频渲染器的信息;否则为错误对象。 | 3526 3527**示例:** 3528 3529```ts 3530import { BusinessError } from '@kit.BasicServicesKit'; 3531 3532audioStreamManager.getCurrentAudioRendererInfoArray(async (err: BusinessError, AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3533 console.info('getCurrentAudioRendererInfoArray **** Get Callback Called ****'); 3534 if (err) { 3535 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3536 } else { 3537 if (AudioRendererChangeInfoArray != null) { 3538 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3539 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3540 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3541 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3542 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3543 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3544 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3545 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3546 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3547 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3548 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3549 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3550 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3551 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3552 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3553 } 3554 } 3555 } 3556 } 3557}); 3558``` 3559 3560### getCurrentAudioRendererInfoArray<sup>9+</sup> 3561 3562getCurrentAudioRendererInfoArray(): Promise<AudioRendererChangeInfoArray> 3563 3564获取当前音频渲染器的信息。使用Promise异步回调。 3565 3566**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3567 3568**返回值:** 3569 3570| 类型 | 说明 | 3571| ---------------------------------------------------------------------------------| --------------------------------------- | 3572| Promise<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | Promise对象,返回当前音频渲染器信息。 | 3573 3574**示例:** 3575 3576```ts 3577import { BusinessError } from '@kit.BasicServicesKit'; 3578 3579async function getCurrentAudioRendererInfoArray(){ 3580 await audioStreamManager.getCurrentAudioRendererInfoArray().then((AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3581 console.info(`getCurrentAudioRendererInfoArray ######### Get Promise is called ##########`); 3582 if (AudioRendererChangeInfoArray != null) { 3583 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3584 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3585 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3586 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3587 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3588 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3589 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3590 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3591 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3592 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3593 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3594 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3595 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3596 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3597 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3598 } 3599 } 3600 } 3601 }).catch((err: BusinessError) => { 3602 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3603 }); 3604} 3605``` 3606### getCurrentAudioRendererInfoArraySync<sup>10+</sup> 3607 3608getCurrentAudioRendererInfoArraySync(): AudioRendererChangeInfoArray 3609 3610获取当前音频渲染器的信息,同步返回结果。 3611 3612**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3613 3614**返回值:** 3615 3616| 类型 | 说明 | 3617| ---------------------------------------------------------------------------------| --------------------------------------- | 3618| [AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9) | 返回当前音频渲染器信息。 | 3619 3620**示例:** 3621 3622```ts 3623import { BusinessError } from '@kit.BasicServicesKit'; 3624 3625try { 3626 let audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray = audioStreamManager.getCurrentAudioRendererInfoArraySync(); 3627 console.info(`getCurrentAudioRendererInfoArraySync success.`); 3628 if (audioRendererChangeInfoArray != null) { 3629 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 3630 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 3631 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3632 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3633 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3634 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3635 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3636 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3637 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3638 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3639 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3640 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3641 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3642 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3643 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3644 } 3645 } 3646 } 3647} catch (err) { 3648 let error = err as BusinessError; 3649 console.error(`getCurrentAudioRendererInfoArraySync :ERROR: ${error}`); 3650} 3651``` 3652 3653### getCurrentAudioCapturerInfoArray<sup>9+</sup> 3654 3655getCurrentAudioCapturerInfoArray(callback: AsyncCallback<AudioCapturerChangeInfoArray>): void 3656 3657获取当前音频采集器的信息。使用callback异步回调。 3658 3659**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3660 3661**参数:** 3662 3663| 参数名 | 类型 | 必填 | 说明 | 3664| ---------- | ----------------------------------- | --------- | -------------------------------------------------------- | 3665| callback | AsyncCallback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数。当获取当前音频采集器的信息成功,err为undefined,data为获取到的当前音频采集器的信息;否则为错误对象。 | 3666 3667**示例:** 3668 3669```ts 3670import { BusinessError } from '@kit.BasicServicesKit'; 3671 3672audioStreamManager.getCurrentAudioCapturerInfoArray(async (err: BusinessError, AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 3673 console.info('getCurrentAudioCapturerInfoArray **** Get Callback Called ****'); 3674 if (err) { 3675 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 3676 } else { 3677 if (AudioCapturerChangeInfoArray != null) { 3678 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 3679 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 3680 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 3681 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 3682 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 3683 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 3684 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 3685 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 3686 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 3687 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 3688 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 3689 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 3690 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 3691 } 3692 } 3693 } 3694 } 3695}); 3696``` 3697 3698### getCurrentAudioCapturerInfoArray<sup>9+</sup> 3699 3700getCurrentAudioCapturerInfoArray(): Promise<AudioCapturerChangeInfoArray> 3701 3702获取当前音频采集器的信息。使用Promise异步回调。 3703 3704**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3705 3706**返回值:** 3707 3708| 类型 | 说明 | 3709| -----------------------------------------------------------------------------| ----------------------------------- | 3710| Promise<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | Promise对象,返回当前音频采集器信息。 | 3711 3712**示例:** 3713 3714```ts 3715import { BusinessError } from '@kit.BasicServicesKit'; 3716 3717async function getCurrentAudioCapturerInfoArray(){ 3718 await audioStreamManager.getCurrentAudioCapturerInfoArray().then((AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 3719 console.info('getCurrentAudioCapturerInfoArray **** Get Promise Called ****'); 3720 if (AudioCapturerChangeInfoArray != null) { 3721 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 3722 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 3723 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 3724 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 3725 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 3726 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 3727 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 3728 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 3729 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 3730 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 3731 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 3732 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 3733 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 3734 } 3735 } 3736 } 3737 }).catch((err: BusinessError) => { 3738 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 3739 }); 3740} 3741``` 3742### getCurrentAudioCapturerInfoArraySync<sup>10+</sup> 3743 3744getCurrentAudioCapturerInfoArraySync(): AudioCapturerChangeInfoArray 3745 3746获取当前音频采集器的信息,同步返回结果。 3747 3748**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3749 3750**返回值:** 3751 3752| 类型 | 说明 | 3753| -----------------------------------------------------------------------------| ----------------------------------- | 3754| [AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9) | 返回当前音频采集器信息。 | 3755 3756**示例:** 3757 3758```ts 3759import { BusinessError } from '@kit.BasicServicesKit'; 3760 3761try { 3762 let audioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray = audioStreamManager.getCurrentAudioCapturerInfoArraySync(); 3763 console.info('getCurrentAudioCapturerInfoArraySync success.'); 3764 if (audioCapturerChangeInfoArray != null) { 3765 for (let i = 0; i < audioCapturerChangeInfoArray.length; i++) { 3766 console.info(`StreamId for ${i} is: ${audioCapturerChangeInfoArray[i].streamId}`); 3767 console.info(`Source for ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.source}`); 3768 console.info(`Flag ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 3769 for (let j = 0; j < audioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 3770 console.info(`Id: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 3771 console.info(`Type: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 3772 console.info(`Role: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 3773 console.info(`Name: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 3774 console.info(`Address: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 3775 console.info(`SampleRate: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 3776 console.info(`ChannelCount: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 3777 console.info(`ChannelMask: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 3778 } 3779 } 3780 } 3781} catch (err) { 3782 let error = err as BusinessError; 3783 console.error(`getCurrentAudioCapturerInfoArraySync ERROR: ${error}`); 3784} 3785``` 3786 3787### on('audioRendererChange')<sup>9+</sup> 3788 3789on(type: 'audioRendererChange', callback: Callback<AudioRendererChangeInfoArray>): void 3790 3791监听音频渲染器更改事件(当音频播放流状态变化、设备变化时触发),使用callback方式返回结果。 3792 3793**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3794 3795**参数:** 3796 3797| 参数名 | 类型 | 必填 | 说明 | 3798| -------- | ---------- | --------- | ------------------------------------------------------------------------ | 3799| type | string | 是 | 监听事件,固定为:'audioRendererChange'。 | 3800| callback | Callback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数,返回当前音频渲染器信息。 | 3801 3802**错误码:** 3803 3804以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3805 3806| 错误码ID | 错误信息 | 3807| ------- | --------------------------------------------| 3808| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3809| 6800101 | Parameter verification failed. | 3810 3811**示例:** 3812 3813```ts 3814audioStreamManager.on('audioRendererChange', (audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3815 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 3816 let audioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 3817 console.info(`## RendererChange on is called for ${i} ##`); 3818 console.info(`StreamId for ${i} is: ${audioRendererChangeInfo.streamId}`); 3819 console.info(`Content ${i} is: ${audioRendererChangeInfo.rendererInfo.content}`); 3820 console.info(`Stream ${i} is: ${audioRendererChangeInfo.rendererInfo.usage}`); 3821 console.info(`Flag ${i} is: ${audioRendererChangeInfo.rendererInfo.rendererFlags}`); 3822 for (let j = 0;j < audioRendererChangeInfo.deviceDescriptors.length; j++) { 3823 console.info(`Id: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].id}`); 3824 console.info(`Type: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3825 console.info(`Role: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3826 console.info(`Name: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].name}`); 3827 console.info(`Address: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].address}`); 3828 console.info(`SampleRate: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3829 console.info(`ChannelCount: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3830 console.info(`ChannelMask: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3831 } 3832 } 3833}); 3834``` 3835 3836### off('audioRendererChange')<sup>9+</sup> 3837 3838off(type: 'audioRendererChange'): void 3839 3840取消监听音频渲染器更改事件。 3841 3842**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3843 3844**参数:** 3845 3846| 参数名 | 类型 | 必填 | 说明 | 3847| -------- | ------- | ---- | ---------------- | 3848| type | string | 是 | 监听事件,固定为:'audioRendererChange'。 | 3849 3850**错误码:** 3851 3852以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3853 3854| 错误码ID | 错误信息 | 3855| ------- |--------------------------| 3856| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3857| 6800101 | Parameter verification failed. | 3858 3859**示例:** 3860 3861```ts 3862audioStreamManager.off('audioRendererChange'); 3863``` 3864 3865### on('audioCapturerChange')<sup>9+</sup> 3866 3867on(type: 'audioCapturerChange', callback: Callback<AudioCapturerChangeInfoArray>): void 3868 3869监听音频采集器更改事件(当音频录制流状态变化、设备变化时触发),使用callback方式返回结果。 3870 3871**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3872 3873**参数:** 3874 3875| 参数名 | 类型 | 必填 | 说明 | 3876| -------- | ------- | --------- | ---------------------------------------------------------------------- | 3877| type | string | 是 | 监听事件,固定为:'audioCapturerChange'。 | 3878| callback | Callback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数,返回当前音频采集器信息。 | 3879 3880**错误码:** 3881 3882以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3883 3884| 错误码ID | 错误信息 | 3885| ------- | --------------------------------------------| 3886| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3887| 6800101 | Parameter verification failed. | 3888 3889**示例:** 3890 3891```ts 3892audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 3893 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 3894 console.info(`## CapChange on is called for element ${i} ##`); 3895 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 3896 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 3897 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 3898 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 3899 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 3900 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 3901 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 3902 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 3903 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 3904 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 3905 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 3906 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 3907 } 3908 } 3909}); 3910``` 3911 3912### off('audioCapturerChange')<sup>9+</sup> 3913 3914off(type: 'audioCapturerChange'): void 3915 3916取消监听音频采集器更改事件。 3917 3918**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3919 3920**参数:** 3921 3922| 参数名 | 类型 | 必填 | 说明 | 3923| -------- | -------- | --- | ------------------------------------------------------------- | 3924| type | string |是 | 监听事件,固定为:'audioCapturerChange'。 | 3925 3926**错误码:** 3927 3928以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3929 3930| 错误码ID | 错误信息 | 3931| ------- | --------------------------------------------| 3932| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3933| 6800101 | Parameter verification failed. | 3934 3935**示例:** 3936 3937```ts 3938audioStreamManager.off('audioCapturerChange'); 3939``` 3940 3941### isActive<sup>9+</sup> 3942 3943isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 3944 3945获取指定音频流是否为活跃状态,使用callback方式异步返回结果。 3946 3947**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3948 3949**参数:** 3950 3951| 参数名 | 类型 | 必填 | 说明 | 3952| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 3953| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 3954| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音频流是否为活跃状态成功,err为undefined,data为true为活跃,false为不活跃;否则为错误对象。 | 3955 3956**示例:** 3957 3958```ts 3959import { BusinessError } from '@kit.BasicServicesKit'; 3960 3961audioStreamManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 3962if (err) { 3963 console.error(`Failed to obtain the active status of the stream. ${err}`); 3964 return; 3965} 3966 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 3967}); 3968``` 3969 3970### isActive<sup>9+</sup> 3971 3972isActive(volumeType: AudioVolumeType): Promise<boolean> 3973 3974获取指定音频流是否为活跃状态,使用Promise方式异步返回结果。 3975 3976**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3977 3978**参数:** 3979 3980| 参数名 | 类型 | 必填 | 说明 | 3981| ---------- | ----------------------------------- | ---- | ------------ | 3982| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 3983 3984**返回值:** 3985 3986| 类型 | 说明 | 3987| ---------------------- | -------------------------------------------------------- | 3988| Promise<boolean> | Promise对象,返回流的活跃状态,true为活跃,false为不活跃。 | 3989 3990**示例:** 3991 3992```ts 3993audioStreamManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 3994 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 3995}); 3996``` 3997 3998### isActiveSync<sup>10+</sup> 3999 4000isActiveSync(volumeType: AudioVolumeType): boolean 4001 4002获取指定音频流是否为活跃状态,同步返回结果。 4003 4004**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4005 4006**参数:** 4007 4008| 参数名 | 类型 | 必填 | 说明 | 4009| ---------- | ----------------------------------- | ---- | ------------ | 4010| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4011 4012**返回值:** 4013 4014| 类型 | 说明 | 4015| ---------------------- | -------------------------------------------------------- | 4016| boolean | 返回流的活跃状态,true为活跃,false为不活跃。 | 4017 4018**错误码:** 4019 4020以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4021 4022| 错误码ID | 错误信息 | 4023| ------- | --------------------------------------------| 4024| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4025| 6800101 | Parameter verification failed. | 4026 4027**示例:** 4028 4029```ts 4030import { BusinessError } from '@kit.BasicServicesKit'; 4031 4032try { 4033 let value: boolean = audioStreamManager.isActiveSync(audio.AudioVolumeType.MEDIA); 4034 console.info(`Indicate that the active status of the stream is obtained ${value}.`); 4035} catch (err) { 4036 let error = err as BusinessError; 4037 console.error(`Failed to obtain the active status of the stream ${error}.`); 4038} 4039``` 4040 4041### getAudioEffectInfoArray<sup>10+</sup> 4042 4043getAudioEffectInfoArray(usage: StreamUsage, callback: AsyncCallback<AudioEffectInfoArray>): void 4044 4045获取当前音效模式的信息。使用callback异步回调。 4046 4047**系统能力**: SystemCapability.Multimedia.Audio.Renderer 4048 4049**参数:** 4050 4051| 参数名 | 类型 | 必填 | 说明 | 4052| -------- | ----------------------------------- | -------- | --------------------------- | 4053| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4054| callback | AsyncCallback<[AudioEffectInfoArray](#audioeffectinfoarray10)> | 是 | 回调函数。当获取当前音效模式的信息成功,err为undefined,data为获取到的当前音效模式的信息;否则为错误对象。| 4055 4056**错误码:** 4057 4058以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4059 4060| 错误码ID | 错误信息 | 4061| ------- | --------------------------------------------| 4062| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4063| 6800101 | Parameter verification failed. Return by callback.| 4064 4065**示例:** 4066 4067```ts 4068import { BusinessError } from '@kit.BasicServicesKit'; 4069 4070audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4071 console.info('getAudioEffectInfoArray **** Get Callback Called ****'); 4072 if (err) { 4073 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4074 return; 4075 } else { 4076 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4077 } 4078}); 4079``` 4080 4081### getAudioEffectInfoArray<sup>10+</sup> 4082 4083getAudioEffectInfoArray(usage: StreamUsage): Promise<AudioEffectInfoArray> 4084 4085获取当前音效模式的信息。使用Promise异步回调。 4086 4087**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4088 4089**参数:** 4090 4091| 参数名 | 类型 | 必填 | 说明 | 4092| -------- | ----------------------------------- | -------- | --------------------------- | 4093| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4094 4095**返回值:** 4096 4097| 类型 | 说明 | 4098| --------------------------------------------------------------------------| --------------------------------------- | 4099| Promise<[AudioEffectInfoArray](#audioeffectinfoarray10)> | Promise对象,返回当前音效模式的信息。 | 4100 4101**错误码:** 4102 4103以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4104 4105| 错误码ID | 错误信息 | 4106| ------- | --------------------------------------------| 4107| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4108| 6800101 | Parameter verification failed. Return by promise. | 4109 4110**示例:** 4111 4112```ts 4113import { BusinessError } from '@kit.BasicServicesKit'; 4114 4115audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC).then((audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4116 console.info('getAudioEffectInfoArray ######### Get Promise is called ##########'); 4117 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4118}).catch((err: BusinessError) => { 4119 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4120}); 4121``` 4122 4123### getAudioEffectInfoArraySync<sup>10+</sup> 4124 4125getAudioEffectInfoArraySync(usage: StreamUsage): AudioEffectInfoArray 4126 4127获取当前音效模式的信息,同步返回结果。 4128 4129**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4130 4131**参数:** 4132 4133| 参数名 | 类型 | 必填 | 说明 | 4134| -------- | ----------------------------------- | -------- | --------------------------- | 4135| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4136 4137**返回值:** 4138 4139| 类型 | 说明 | 4140| --------------------------------------------------------------------------| --------------------------------------- | 4141| [AudioEffectInfoArray](#audioeffectinfoarray10) | 返回当前音效模式的信息。 | 4142 4143**错误码:** 4144 4145以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4146 4147| 错误码ID | 错误信息 | 4148| ------- | --------------------------------------------| 4149| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4150| 6800101 | Parameter verification failed. | 4151 4152**示例:** 4153 4154```ts 4155import { BusinessError } from '@kit.BasicServicesKit'; 4156 4157try { 4158 let audioEffectInfoArray: audio.AudioEffectInfoArray = audioStreamManager.getAudioEffectInfoArraySync(audio.StreamUsage.STREAM_USAGE_MUSIC); 4159 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4160} catch (err) { 4161 let error = err as BusinessError; 4162 console.error(`getAudioEffectInfoArraySync ERROR: ${error}`); 4163} 4164``` 4165 4166## AudioRoutingManager<sup>9+</sup> 4167 4168音频路由管理。在使用AudioRoutingManager的接口前,需要使用[getRoutingManager](#getroutingmanager9)获取AudioRoutingManager实例。 4169 4170### getDevices<sup>9+</sup> 4171 4172getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 4173 4174获取音频设备列表,使用callback方式异步返回结果。 4175 4176**系统能力:** SystemCapability.Multimedia.Audio.Device 4177 4178**参数:** 4179 4180| 参数名 | 类型 | 必填 | 说明 | 4181| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 4182| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4183| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 4184 4185**示例:** 4186 4187```ts 4188import { BusinessError } from '@kit.BasicServicesKit'; 4189 4190audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 4191 if (err) { 4192 console.error(`Failed to obtain the device list. ${err}`); 4193 return; 4194 } 4195 console.info('Callback invoked to indicate that the device list is obtained.'); 4196}); 4197``` 4198 4199### getDevices<sup>9+</sup> 4200 4201getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 4202 4203获取音频设备列表,使用Promise方式异步返回结果。 4204 4205**系统能力:** SystemCapability.Multimedia.Audio.Device 4206 4207**参数:** 4208 4209| 参数名 | 类型 | 必填 | 说明 | 4210| ---------- | ------------------------- | ---- | ---------------- | 4211| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4212 4213**返回值:** 4214 4215| 类型 | 说明 | 4216| ------------------------------------------------------------ | ------------------------- | 4217| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 4218 4219**示例:** 4220 4221```ts 4222audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 4223 console.info('Promise returned to indicate that the device list is obtained.'); 4224}); 4225``` 4226 4227### getDevicesSync<sup>10+</sup> 4228 4229getDevicesSync(deviceFlag: DeviceFlag): AudioDeviceDescriptors 4230 4231获取音频设备列表,同步返回结果。 4232 4233**系统能力:** SystemCapability.Multimedia.Audio.Device 4234 4235**参数:** 4236 4237| 参数名 | 类型 | 必填 | 说明 | 4238| ---------- | ------------------------- | ---- | ---------------- | 4239| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4240 4241**返回值:** 4242 4243| 类型 | 说明 | 4244| ------------------------------------------------------------ | ------------------------- | 4245| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回设备列表。 | 4246 4247**错误码:** 4248 4249以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4250 4251| 错误码ID | 错误信息 | 4252| ------- | --------------------------------------------| 4253| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4254| 6800101 | Parameter verification failed. | 4255 4256**示例:** 4257 4258```ts 4259import { BusinessError } from '@kit.BasicServicesKit'; 4260 4261try { 4262 let data: audio.AudioDeviceDescriptors = audioRoutingManager.getDevicesSync(audio.DeviceFlag.OUTPUT_DEVICES_FLAG); 4263 console.info(`Indicate that the device list is obtained ${data}`); 4264} catch (err) { 4265 let error = err as BusinessError; 4266 console.error(`Failed to obtain the device list. ${error}`); 4267} 4268``` 4269 4270### isMicBlockDetectionSupported<sup>13+</sup> 4271 4272isMicBlockDetectionSupported(): Promise<boolean> 4273 4274获取当前设备是否支持麦克风状态检测,使用Promise方式异步返回结果。 4275 4276**系统能力:** SystemCapability.Multimedia.Audio.Device 4277 4278**返回值:** 4279 4280| 类型 | 说明 | 4281| ---------------------- | ------------------------------------------------------------ | 4282| Promise<boolean> | Promise对象,返回当前设备是否支持堵麦回调注册状态,true为支持,false为不支持。 | 4283 4284**示例:** 4285 4286```ts 4287audioRoutingManager.isMicBlockDetectionSupported().then((value: boolean) => { 4288 console.info(`Query whether microphone block detection is supported on current device result is ${value}.`); 4289}); 4290``` 4291 4292### on('micBlockStatusChanged')<sup>13+</sup> 4293 4294on(type: 'micBlockStatusChanged', callback: Callback<DeviceBlockStatusInfo\>): void 4295 4296监听音频麦克风是否被堵塞变化事件。在使用此功能之前,用户应查询当前设备是否支持检测,应用只有在使用麦克风录音时,并且所使用的麦克风的堵塞状态发生改变, 4297才会收到回调,目前此检测功能仅支持麦克风位于本地设备上,使用callback方式返回结果。 4298 4299**系统能力:** SystemCapability.Multimedia.Audio.Device 4300 4301**参数:** 4302 4303| 参数名 | 类型 | 必填 | 说明 | 4304| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 4305| type | string | 是 | 监听事件,固定为:'micBlockStatusChanged'。 | 4306| callback | Callback<[DeviceBlockStatusInfo](#deviceblockstatusinfo13)\> | 是 | 回调函数,返回设备麦克风是否被堵塞状态更新详情。 | 4307 4308**错误码:** 4309 4310以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4311 4312| 错误码ID | 错误信息 | 4313| ------- | --------------------------------------------| 4314| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4315| 6800101 | Parameter verification failed. | 4316 4317**示例:** 4318 4319```ts 4320// 在使用此功能之前,应先查询当前设备是否支持检测。 4321audioRoutingManager.isMicBlockDetectionSupported().then((value: boolean) => { 4322 console.info(`Query whether microphone block detection is supported on current device result is ${value}.`); 4323 if (value) { 4324 audioRoutingManager.on('micBlockStatusChanged', (micBlockStatusChanged: audio.DeviceBlockStatusInfo) => { 4325 console.info(`block status : ${micBlockStatusChanged.blockStatus} `); 4326 }); 4327 } 4328}); 4329``` 4330 4331### off('micBlockStatusChanged')<sup>13+</sup> 4332 4333off(type: 'micBlockStatusChanged', callback?: Callback<DeviceBlockStatusInfo\>): void 4334 4335取消监听音频麦克风是否被堵塞变化事件,使用callback方式返回结果。 4336 4337**系统能力:** SystemCapability.Multimedia.Audio.Device 4338 4339**参数:** 4340 4341| 参数名 | 类型 | 必填 | 说明 | 4342| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4343| type | string | 是 | 监听事件,固定为:'micBlockStatusChanged'。 | 4344| callback | Callback<[DeviceBlockStatusInfo](#deviceblockstatusinfo13)\> | 否 | 回调函数,返回设备麦克风是否被堵塞状态更新详情。| 4345 4346**错误码:** 4347 4348以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4349 4350| 错误码ID | 错误信息 | 4351| ------- | --------------------------------------------| 4352| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4353| 6800101 | Parameter verification failed. | 4354 4355**示例:** 4356 4357```ts 4358// 取消该事件的所有监听。 4359audioRoutingManager.off('micBlockStatusChanged'); 4360 4361// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4362let micBlockStatusCallback = (micBlockStatusChanged: audio.DeviceBlockStatusInfo) => { 4363 console.info(`block status : ${micBlockStatusChanged.blockStatus} `); 4364}; 4365 4366audioRoutingManager.on('micBlockStatusChanged', micBlockStatusCallback); 4367 4368audioRoutingManager.off('micBlockStatusChanged', micBlockStatusCallback); 4369``` 4370 4371### on('deviceChange')<sup>9+</sup> 4372 4373on(type: 'deviceChange', deviceFlag: DeviceFlag, callback: Callback<DeviceChangeAction\>): void 4374 4375监听音频设备连接变化事件(当音频设备连接状态发生变化时触发),使用callback方式返回结果。 4376 4377**系统能力:** SystemCapability.Multimedia.Audio.Device 4378 4379**参数:** 4380 4381| 参数名 | 类型 | 必填 | 说明 | 4382| :------- | :--------------------------------------------------- | :--- |:------------------------| 4383| type | string | 是 | 监听事件,固定为:'deviceChange'。 | 4384| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4385| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 4386 4387**错误码:** 4388 4389以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4390 4391| 错误码ID | 错误信息 | 4392| ------- | --------------------------------------------| 4393| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4394| 6800101 | Parameter verification failed. | 4395 4396**示例:** 4397 4398```ts 4399audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (deviceChanged: audio.DeviceChangeAction) => { 4400 console.info('device change type : ' + deviceChanged.type); 4401 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4402 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4403 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4404}); 4405``` 4406 4407### off('deviceChange')<sup>9+</sup> 4408 4409off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 4410 4411取消监听音频设备连接变化事件,使用callback方式返回结果。 4412 4413**系统能力:** SystemCapability.Multimedia.Audio.Device 4414 4415**参数:** 4416 4417| 参数名 | 类型 | 必填 | 说明 | 4418| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4419| type | string | 是 | 监听事件,固定为:'deviceChange'。 | 4420| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 4421 4422**错误码:** 4423 4424以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4425 4426| 错误码ID | 错误信息 | 4427| ------- | --------------------------------------------| 4428| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4429| 6800101 | Parameter verification failed. | 4430 4431**示例:** 4432 4433```ts 4434// 取消该事件的所有监听。 4435audioRoutingManager.off('deviceChange'); 4436 4437// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4438let deviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 4439 console.info('device change type : ' + deviceChanged.type); 4440 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4441 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4442 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4443}; 4444 4445audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, deviceChangeCallback); 4446 4447audioRoutingManager.off('deviceChange', deviceChangeCallback); 4448``` 4449 4450### setCommunicationDevice<sup>9+</sup> 4451 4452setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean, callback: AsyncCallback<void>): void 4453 4454设置通信设备激活状态,使用callback方式异步返回结果。 4455 4456该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 4457 4458推荐开发者使用AVSession提供的[设备切换组件](../../media/avsession/using-switch-call-devices.md),实现通话设备切换。 4459 4460**系统能力:** SystemCapability.Multimedia.Audio.Communication 4461 4462**参数:** 4463 4464| 参数名 | 类型 | 必填 | 说明 | 4465| ---------- | ------------------------------------- | ---- |-------------------------| 4466| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 音频设备类型。 | 4467| active | boolean | 是 | 设备激活状态,true激活,false未激活。 | 4468| callback | AsyncCallback<void> | 是 | 回调函数。当设置通信设备激活状态成功,err为undefined,否则为错误对象。 | 4469 4470**示例:** 4471 4472```ts 4473import { BusinessError } from '@kit.BasicServicesKit'; 4474 4475audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true, (err: BusinessError) => { 4476 if (err) { 4477 console.error(`Failed to set the active status of the device. ${err}`); 4478 return; 4479 } 4480 console.info('Callback invoked to indicate that the device is set to the active status.'); 4481}); 4482``` 4483 4484### getAvailableDevices<sup>12+</sup> 4485 4486getAvailableDevices(deviceUsage: DeviceUsage): AudioDeviceDescriptors 4487 4488获取音频可选设备列表,同步返回结果。 4489 4490**系统能力:** SystemCapability.Multimedia.Audio.Device 4491 4492**参数:** 4493 4494| 参数名 | 类型 | 必填 | 说明 | 4495| ---------- | ------------------------- | ---- | ---------------- | 4496| deviceUsage| [DeviceUsage](#deviceusage12) | 是 | 设备的usage。 | 4497 4498**返回值:** 4499 4500| 类型 | 说明 | 4501| ------------------------------------------------------------ | ------------------------- | 4502| [AudioDeviceDescriptors](js-apis-audio.md#audiodevicedescriptors) | 返回设备列表。 | 4503 4504**错误码:** 4505 4506以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4507 4508| 错误码ID | 错误信息 | 4509| ------- | --------------------------------------------| 4510| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4511| 6800101 | Parameter verification failed. | 4512 4513**示例:** 4514 4515```ts 4516import { BusinessError } from '@kit.BasicServicesKit'; 4517 4518try { 4519 let data: audio.AudioDeviceDescriptors = audioRoutingManager.getAvailableDevices(audio.DeviceUsage.MEDIA_OUTPUT_DEVICES); 4520 console.info(`Indicate that the device list is obtained ${data}`); 4521} catch (err) { 4522 let error = err as BusinessError; 4523 console.error(`Failed to obtain the device list. ${error}`); 4524} 4525``` 4526 4527### on('availableDeviceChange')<sup>12+</sup> 4528 4529on(type: 'availableDeviceChange', deviceUsage: DeviceUsage, callback: Callback<DeviceChangeAction\>): void 4530 4531监听音频可选设备连接变化事件(当音频可选设备连接状态发生变化时触发),使用callback方式返回结果。 4532 4533**系统能力:** SystemCapability.Multimedia.Audio.Device 4534 4535**参数:** 4536 4537| 参数名 | 类型 | 必填 | 说明 | 4538| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 4539| type | string | 是 | 监听事件,固定为:'availableDeviceChange'。 | 4540| deviceUsage | [DeviceUsage](#deviceusage12) | 是 | 设备的usage。 | 4541| callback | Callback<[DeviceChangeAction](js-apis-audio.md#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 4542 4543**错误码:** 4544 4545以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4546 4547| 错误码ID | 错误信息 | 4548| ------- | --------------------------------------------| 4549| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4550| 6800101 | Parameter verification failed. | 4551 4552**示例:** 4553 4554```ts 4555audioRoutingManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_OUTPUT_DEVICES, (deviceChanged: audio.DeviceChangeAction) => { 4556 console.info('device change type : ' + deviceChanged.type); 4557 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4558 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4559 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4560}); 4561``` 4562 4563### off('availableDeviceChange')<sup>12+</sup> 4564 4565off(type: 'availableDeviceChange', callback?: Callback<DeviceChangeAction\>): void 4566 4567取消监听音频可选设备连接变化事件,使用callback方式返回结果。 4568 4569**系统能力:** SystemCapability.Multimedia.Audio.Device 4570 4571**参数:** 4572 4573| 参数名 | 类型 | 必填 | 说明 | 4574| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4575| type | string | 是 | 监听事件,固定为:'availableDeviceChange'。 | 4576| callback | Callback<[DeviceChangeAction](js-apis-audio.md#devicechangeaction)> | 否 | 回调函数,返回可选设备更新详情。 | 4577 4578**错误码:** 4579 4580以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4581 4582| 错误码ID | 错误信息 | 4583| ------- | --------------------------------------------| 4584| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4585| 6800101 | Parameter verification failed. | 4586 4587**示例:** 4588 4589```ts 4590// 取消该事件的所有监听。 4591audioRoutingManager.off('availableDeviceChange'); 4592 4593// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4594let availableDeviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 4595 console.info('device change type : ' + deviceChanged.type); 4596 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4597 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4598 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4599}; 4600 4601audioRoutingManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_OUTPUT_DEVICES, availableDeviceChangeCallback); 4602 4603audioRoutingManager.off('availableDeviceChange', availableDeviceChangeCallback); 4604``` 4605 4606### setCommunicationDevice<sup>9+</sup> 4607 4608setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean): Promise<void> 4609 4610设置通信设备激活状态,使用Promise方式异步返回结果。 4611 4612该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 4613 4614推荐开发者使用AVSession提供的[设备切换组件](../../media/avsession/using-switch-call-devices.md),实现通话设备切换。 4615 4616**系统能力:** SystemCapability.Multimedia.Audio.Communication 4617 4618**参数:** 4619 4620| 参数名 | 类型 | 必填 | 说明 | 4621| ---------- | ----------------------------------------------------- | ---- | ------------------ | 4622| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4623| active | boolean | 是 | 设备激活状态,true激活,false未激活。 | 4624 4625**返回值:** 4626 4627| 类型 | 说明 | 4628| ------------------- | ------------------------------- | 4629| Promise<void> | Promise对象,无返回结果。 | 4630 4631**示例:** 4632 4633```ts 4634audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true).then(() => { 4635 console.info('Promise returned to indicate that the device is set to the active status.'); 4636}); 4637``` 4638 4639### isCommunicationDeviceActive<sup>9+</sup> 4640 4641isCommunicationDeviceActive(deviceType: CommunicationDeviceType, callback: AsyncCallback<boolean>): void 4642 4643获取指定通信设备的激活状态,使用callback方式异步返回结果。 4644 4645**系统能力:** SystemCapability.Multimedia.Audio.Communication 4646 4647**参数:** 4648 4649| 参数名 | 类型 | 必填 | 说明 | 4650| ---------- | ---------------------------------------------------- | ---- | ------------------------ | 4651| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4652| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定通信设备的激活状态成功,err为undefined,data为true为激活,false为未激活;否则为错误对象。 | 4653 4654**示例:** 4655 4656```ts 4657import { BusinessError } from '@kit.BasicServicesKit'; 4658 4659audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 4660 if (err) { 4661 console.error(`Failed to obtain the active status of the device. ${err}`); 4662 return; 4663 } 4664 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 4665}); 4666``` 4667 4668### isCommunicationDeviceActive<sup>9+</sup> 4669 4670isCommunicationDeviceActive(deviceType: CommunicationDeviceType): Promise<boolean> 4671 4672获取指定通信设备的激活状态,使用Promise方式异步返回结果。 4673 4674**系统能力:** SystemCapability.Multimedia.Audio.Communication 4675 4676**参数:** 4677 4678| 参数名 | 类型 | 必填 | 说明 | 4679| ---------- | ---------------------------------------------------- | ---- | ------------------ | 4680| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4681 4682**返回值:** 4683 4684| Type | Description | 4685| ---------------------- | ------------------------------- | 4686| Promise<boolean> | Promise对象,返回设备的激活状态,true激活,false未激活。 | 4687 4688**示例:** 4689 4690```ts 4691audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER).then((value: boolean) => { 4692 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 4693}); 4694``` 4695 4696### isCommunicationDeviceActiveSync<sup>10+</sup> 4697 4698isCommunicationDeviceActiveSync(deviceType: CommunicationDeviceType): boolean 4699 4700获取指定通信设备的激活状态,同步返回结果。 4701 4702**系统能力:** SystemCapability.Multimedia.Audio.Communication 4703 4704**参数:** 4705 4706| 参数名 | 类型 | 必填 | 说明 | 4707| ---------- | ---------------------------------------------------- | ---- | ------------------ | 4708| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4709 4710**返回值:** 4711 4712| Type | Description | 4713| ---------------------- | ------------------------------- | 4714| boolean | 返回设备的激活状态,true激活,false未激活。 | 4715 4716**错误码:** 4717 4718以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4719 4720| 错误码ID | 错误信息 | 4721| ------- | --------------------------------------------| 4722| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4723| 6800101 | Parameter verification failed. | 4724 4725**示例:** 4726 4727```ts 4728import { BusinessError } from '@kit.BasicServicesKit'; 4729 4730try { 4731 let value: boolean = audioRoutingManager.isCommunicationDeviceActiveSync(audio.CommunicationDeviceType.SPEAKER); 4732 console.info(`Indicate that the active status of the device is obtained ${value}.`); 4733} catch (err) { 4734 let error = err as BusinessError; 4735 console.error(`Failed to obtain the active status of the device ${error}.`); 4736} 4737``` 4738 4739### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 4740 4741getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 4742 4743根据音频信息,返回优先级最高的输出设备,使用callback方式异步返回结果。 4744 4745**系统能力:** SystemCapability.Multimedia.Audio.Device 4746 4747**参数:** 4748 4749| 参数名 | 类型 | 必填 | 说明 | 4750| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4751| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 4752| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输出设备成功,err为undefined,data为获取到的优先级最高的输出设备信息;否则为错误对象。 | 4753 4754**错误码:** 4755 4756以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4757 4758| 错误码ID | 错误信息 | 4759| ------- |--------------------------------------------------| 4760| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4761| 6800101 | Parameter verification failed. Return by callback. | 4762| 6800301 | System error. Return by callback. | 4763 4764**示例:** 4765```ts 4766import { audio } from '@kit.AudioKit'; 4767import { BusinessError } from '@kit.BasicServicesKit'; 4768 4769let rendererInfo: audio.AudioRendererInfo = { 4770 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 4771 rendererFlags: 0 // 音频渲染器标志。 4772}; 4773 4774async function getPreferOutputDevice() { 4775 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 4776 if (err) { 4777 console.error(`Result ERROR: ${err}`); 4778 } else { 4779 console.info(`device descriptor: ${desc}`); 4780 } 4781 }); 4782} 4783``` 4784 4785### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 4786getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo): Promise<AudioDeviceDescriptors> 4787 4788根据音频信息,返回优先级最高的输出设备,使用Promise方式异步返回结果。 4789 4790**系统能力:** SystemCapability.Multimedia.Audio.Device 4791 4792**参数:** 4793 4794| 参数名 | 类型 | 必填 | 说明 | 4795| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 4796| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 4797 4798**返回值:** 4799 4800| 类型 | 说明 | 4801| --------------------- | --------------------------- | 4802| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输出设备信息。 | 4803 4804**错误码:** 4805 4806以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4807 4808| 错误码ID | 错误信息 | 4809| ------- |-------------------------------------------------| 4810| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4811| 6800101 | Parameter verification failed. Return by promise. | 4812| 6800301 | System error. Return by promise. | 4813 4814**示例:** 4815 4816```ts 4817import { audio } from '@kit.AudioKit'; 4818import { BusinessError } from '@kit.BasicServicesKit'; 4819 4820let rendererInfo: audio.AudioRendererInfo = { 4821 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 4822 rendererFlags: 0 // 音频渲染器标志。 4823}; 4824 4825async function getPreferOutputDevice() { 4826 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => { 4827 console.info(`device descriptor: ${desc}`); 4828 }).catch((err: BusinessError) => { 4829 console.error(`Result ERROR: ${err}`); 4830 }) 4831} 4832``` 4833 4834### getPreferredOutputDeviceForRendererInfoSync<sup>10+</sup> 4835getPreferredOutputDeviceForRendererInfoSync(rendererInfo: AudioRendererInfo): AudioDeviceDescriptors 4836 4837根据音频信息,返回优先级最高的输出设备,同步返回结果。 4838 4839**系统能力:** SystemCapability.Multimedia.Audio.Device 4840 4841**参数:** 4842 4843| 参数名 | 类型 | 必填 | 说明 | 4844| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 4845| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 4846 4847**返回值:** 4848 4849| 类型 | 说明 | 4850| --------------------- | --------------------------- | 4851| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输出设备信息。 | 4852 4853**错误码:** 4854 4855以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4856 4857| 错误码ID | 错误信息 | 4858| ------- |--------------------------| 4859| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4860| 6800101 | Parameter verification failed. | 4861 4862**示例:** 4863 4864```ts 4865import { audio } from '@kit.AudioKit'; 4866import { BusinessError } from '@kit.BasicServicesKit'; 4867 4868let rendererInfo: audio.AudioRendererInfo = { 4869 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 4870 rendererFlags: 0 // 音频渲染器标志。 4871}; 4872 4873try { 4874 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredOutputDeviceForRendererInfoSync(rendererInfo); 4875 console.info(`device descriptor: ${desc}`); 4876} catch (err) { 4877 let error = err as BusinessError; 4878 console.error(`Result ERROR: ${error}`); 4879} 4880``` 4881 4882### on('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 4883 4884on(type: 'preferOutputDeviceChangeForRendererInfo', rendererInfo: AudioRendererInfo, callback: Callback<AudioDeviceDescriptors\>): void 4885 4886监听最高优先级输出设备变化事件(当最高优先级输出设备发生变化时触发),使用callback方式返回结果。 4887 4888**系统能力:** SystemCapability.Multimedia.Audio.Device 4889 4890**参数:** 4891 4892| 参数名 | 类型 | 必填 | 说明 | 4893| :------- | :--------------------------------------------------- | :--- |:--------------------------------------------------------| 4894| type | string | 是 | 监听事件,固定为:'preferOutputDeviceChangeForRendererInfo'。| 4895| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 4896| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输出设备信息。 | 4897 4898**错误码:** 4899 4900以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4901 4902| 错误码ID | 错误信息 | 4903| ------- | --------------------------------------------| 4904| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4905| 6800101 | Parameter verification failed. | 4906 4907**示例:** 4908 4909```ts 4910import { audio } from '@kit.AudioKit'; 4911 4912let rendererInfo: audio.AudioRendererInfo = { 4913 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 4914 rendererFlags: 0 // 音频渲染器标志。 4915}; 4916 4917audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => { 4918 console.info(`device descriptor: ${desc}`); 4919}); 4920``` 4921 4922### off('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 4923 4924off(type: 'preferOutputDeviceChangeForRendererInfo', callback?: Callback<AudioDeviceDescriptors\>): void 4925 4926取消监听最高优先级输出音频设备变化事件,使用callback方式返回结果。 4927 4928**系统能力:** SystemCapability.Multimedia.Audio.Device 4929 4930**参数:** 4931 4932| 参数名 | 类型 | 必填 | 说明 | 4933| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4934| type | string | 是 | 监听事件,固定为:'preferOutputDeviceChangeForRendererInfo'。 | 4935| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输出设备信息。 | 4936 4937**错误码:** 4938 4939以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4940 4941| 错误码ID | 错误信息 | 4942| ------- | --------------------------------------------| 4943| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4944| 6800101 | Parameter verification failed. | 4945 4946**示例:** 4947 4948```ts 4949// 取消该事件的所有监听。 4950audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo'); 4951 4952// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4953let preferOutputDeviceChangeForRendererInfoCallback = (desc: audio.AudioDeviceDescriptors) => { 4954 console.info(`device descriptor: ${desc}`); 4955}; 4956let rendererInfo: audio.AudioRendererInfo = { 4957 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 4958 rendererFlags: 0 // 音频渲染器标志。 4959}; 4960 4961audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, preferOutputDeviceChangeForRendererInfoCallback); 4962 4963audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo', preferOutputDeviceChangeForRendererInfoCallback); 4964``` 4965 4966### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 4967 4968getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 4969 4970根据音频信息,返回优先级最高的输入设备,使用callback方式异步返回结果。 4971 4972**系统能力:** SystemCapability.Multimedia.Audio.Device 4973 4974**参数:** 4975 4976| 参数名 | 类型 | 必填 | 说明 | 4977| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4978| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 4979| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输入设备成功,err为undefined,data为获取到的优先级最高的输入设备信息;否则为错误对象。 | 4980 4981**错误码:** 4982 4983以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4984 4985| 错误码ID | 错误信息 | 4986| ------- | --------------------------------------------| 4987| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4988| 6800101 | Parameter verification failed. Return by callback.| 4989| 6800301 | System error. Return by callback. | 4990 4991**示例:** 4992```ts 4993import { audio } from '@kit.AudioKit'; 4994import { BusinessError } from '@kit.BasicServicesKit'; 4995 4996let capturerInfo: audio.AudioCapturerInfo = { 4997 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 4998 capturerFlags: 0 // 音频采集器标志。 4999}; 5000 5001audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 5002 if (err) { 5003 console.error(`Result ERROR: ${err}`); 5004 } else { 5005 console.info(`device descriptor: ${desc}`); 5006 } 5007}); 5008``` 5009 5010### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 5011 5012getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo): Promise<AudioDeviceDescriptors> 5013 5014根据音频信息,返回优先级最高的输入设备,使用Promise方式异步返回结果。 5015 5016**系统能力:** SystemCapability.Multimedia.Audio.Device 5017 5018**参数:** 5019 5020| 参数名 | 类型 | 必填 | 说明 | 5021| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5022| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5023 5024**返回值:** 5025 5026| 类型 | 说明 | 5027| --------------------- | --------------------------- | 5028| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输入设备信息。 | 5029 5030**错误码:** 5031 5032以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5033 5034| 错误码ID | 错误信息 | 5035| ------- | --------------------------------------------| 5036| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5037| 6800101 | Parameter verification failed. Return by promise. | 5038| 6800301 | System error. Return by promise. | 5039 5040**示例:** 5041 5042```ts 5043import { audio } from '@kit.AudioKit'; 5044import { BusinessError } from '@kit.BasicServicesKit'; 5045 5046let capturerInfo: audio.AudioCapturerInfo = { 5047 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5048 capturerFlags: 0 // 音频采集器标志。 5049}; 5050 5051audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo).then((desc: audio.AudioDeviceDescriptors) => { 5052 console.info(`device descriptor: ${desc}`); 5053}).catch((err: BusinessError) => { 5054 console.error(`Result ERROR: ${err}`); 5055}); 5056``` 5057 5058### getPreferredInputDeviceForCapturerInfoSync<sup>10+</sup> 5059 5060getPreferredInputDeviceForCapturerInfoSync(capturerInfo: AudioCapturerInfo): AudioDeviceDescriptors 5061 5062根据音频信息,返回优先级最高的输入设备,同步返回结果。 5063 5064**系统能力:** SystemCapability.Multimedia.Audio.Device 5065 5066**参数:** 5067 5068| 参数名 | 类型 | 必填 | 说明 | 5069| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5070| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5071 5072**返回值:** 5073 5074| 类型 | 说明 | 5075| --------------------- | --------------------------- | 5076| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输入设备信息。 | 5077 5078**错误码:** 5079 5080以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5081 5082| 错误码ID | 错误信息 | 5083| ------- | --------------------------------------------| 5084| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5085| 6800101 | Parameter verification failed. | 5086 5087**示例:** 5088 5089```ts 5090import { audio } from '@kit.AudioKit'; 5091import { BusinessError } from '@kit.BasicServicesKit'; 5092 5093let capturerInfo: audio.AudioCapturerInfo = { 5094 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5095 capturerFlags: 0 // 音频采集器标志。 5096}; 5097 5098try { 5099 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredInputDeviceForCapturerInfoSync(capturerInfo); 5100 console.info(`device descriptor: ${desc}`); 5101} catch (err) { 5102 let error = err as BusinessError; 5103 console.error(`Result ERROR: ${error}`); 5104} 5105``` 5106 5107### on('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5108 5109on(type: 'preferredInputDeviceChangeForCapturerInfo', capturerInfo: AudioCapturerInfo, callback: Callback<AudioDeviceDescriptors\>): void 5110 5111监听最高优先级输入设备变化事件(当最高优先级输入设备发生变化时触发),使用callback方式返回结果。 5112 5113**系统能力:** SystemCapability.Multimedia.Audio.Device 5114 5115**参数:** 5116 5117| 参数名 | 类型 | 必填 | 说明 | 5118| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 5119| type | string | 是 | 监听事件,固定为:'preferredInputDeviceChangeForCapturerInfo' | 5120| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5121| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输入设备信息。 | 5122 5123**错误码:** 5124 5125以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5126 5127| 错误码ID | 错误信息 | 5128| ------- | --------------------------------------------| 5129| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5130| 6800101 | Parameter verification failed. | 5131 5132**示例:** 5133 5134```ts 5135import { audio } from '@kit.AudioKit'; 5136 5137let capturerInfo: audio.AudioCapturerInfo = { 5138 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5139 capturerFlags: 0 // 音频采集器标志。 5140}; 5141 5142audioRoutingManager.on('preferredInputDeviceChangeForCapturerInfo', capturerInfo, (desc: audio.AudioDeviceDescriptors) => { 5143 console.info(`device descriptor: ${desc}`); 5144}); 5145``` 5146 5147### off('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5148 5149off(type: 'preferredInputDeviceChangeForCapturerInfo', callback?: Callback<AudioDeviceDescriptors\>): void 5150 5151取消监听最高优先级输入音频设备变化事件,使用callback方式返回结果。 5152 5153**系统能力:** SystemCapability.Multimedia.Audio.Device 5154 5155**参数:** 5156 5157| 参数名 | 类型 | 必填 | 说明 | 5158| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5159| type | string | 是 | 监听事件,固定为:'preferredInputDeviceChangeForCapturerInfo' | 5160| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输入设备信息。 | 5161 5162**错误码:** 5163 5164以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5165 5166| 错误码ID | 错误信息 | 5167| ------- | --------------------------------------------| 5168| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5169| 6800101 | Parameter verification failed. | 5170 5171**示例:** 5172 5173```ts 5174// 取消该事件的所有监听。 5175audioRoutingManager.off('preferredInputDeviceChangeForCapturerInfo'); 5176 5177// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5178let preferredInputDeviceChangeForCapturerInfoCallback = (desc: audio.AudioDeviceDescriptors) => { 5179 console.info(`device descriptor: ${desc}`); 5180}; 5181let capturerInfo: audio.AudioCapturerInfo = { 5182 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5183 capturerFlags: 0 // 音频采集器标志。 5184}; 5185 5186audioRoutingManager.on('preferredInputDeviceChangeForCapturerInfo', capturerInfo, preferredInputDeviceChangeForCapturerInfoCallback); 5187 5188audioRoutingManager.off('preferredInputDeviceChangeForCapturerInfo', preferredInputDeviceChangeForCapturerInfoCallback); 5189``` 5190 5191## AudioSessionManager<sup>12+</sup> 5192 5193音频会话管理。在使用AudioSessionManager的接口前,需要使用[getSessionManager](#getsessionmanager12)获取AudioSessionManager实例。 5194 5195### activateAudioSession<sup>12+</sup> 5196 5197activateAudioSession(strategy: AudioSessionStrategy): Promise\<void> 5198 5199激活音频会话。使用Promise方式异步返回结果。 5200 5201**系统能力:** SystemCapability.Multimedia.Audio.Core 5202 5203**参数:** 5204 5205| 参数名 | 类型 | 必填 | 说明 | 5206| ------ |-------------------------------------------------| ---- | ------------ | 5207| strategy | [AudioSessionStrategy](#audiosessionstrategy12) | 是 | 音频会话策略。 | 5208 5209**返回值:** 5210 5211| 类型 | 说明 | 5212| -------------- | ------------------------- | 5213| Promise\<void> | Promise对象,无返回结果。 | 5214 5215**错误码:** 5216 5217以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5218 5219| 错误码ID | 错误信息 | 5220| ------- | ---------------------------------------------| 5221| 401 | Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types. | 5222| 6800101 | Parameter verification failed.| 5223| 6800301 | System error. Returned by promise. | 5224 5225**示例:** 5226 5227```ts 5228import { BusinessError } from '@kit.BasicServicesKit'; 5229 5230let strategy: audio.AudioSessionStrategy = { 5231 concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS 5232}; 5233 5234audioSessionManager.activateAudioSession(strategy).then(() => { 5235 console.info('activateAudioSession SUCCESS'); 5236}).catch((err: BusinessError) => { 5237 console.error(`ERROR: ${err}`); 5238}); 5239``` 5240 5241### deactivateAudioSession<sup>12+</sup> 5242 5243deactivateAudioSession(): Promise\<void> 5244 5245停用音频会话。使用Promise方式异步返回结果。 5246 5247**系统能力:** SystemCapability.Multimedia.Audio.Core 5248 5249**返回值:** 5250 5251| 类型 | 说明 | 5252| -------------- | ------------------------- | 5253| Promise\<void> | Promise对象,无返回结果。 | 5254 5255**错误码:** 5256 5257以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5258 5259| 错误码ID | 错误信息 | 5260| ------- | ---------------------------------------------| 5261| 6800301 | System error. Returned by promise. | 5262 5263**示例:** 5264 5265```ts 5266import { BusinessError } from '@kit.BasicServicesKit'; 5267 5268audioSessionManager.deactivateAudioSession().then(() => { 5269 console.info('deactivateAudioSession SUCCESS'); 5270}).catch((err: BusinessError) => { 5271 console.error(`ERROR: ${err}`); 5272}); 5273``` 5274 5275### isAudioSessionActivated<sup>12+</sup> 5276 5277isAudioSessionActivated(): boolean 5278 5279检查音频会话是否已激活。 5280 5281**系统能力:** SystemCapability.Multimedia.Audio.Core 5282 5283**返回值:** 5284 5285| 类型 | 说明 | 5286| ------------------------------------------------- |---------------------------------------| 5287| boolean | 返回当前pid应用程序的音频会话是否已激活,true表示已激活,false表示已停用。 | 5288 5289**示例:** 5290 5291```ts 5292let isActivated = audioSessionManager.isAudioSessionActivated(); 5293``` 5294 5295### on('audioSessionDeactivated')<sup>12+</sup> 5296 5297on(type: 'audioSessionDeactivated', callback: Callback\<AudioSessionDeactivatedEvent>): void 5298 5299监听音频会话停用事件(当音频会话停用时触发),使用callback方式返回结果。 5300 5301**系统能力:** SystemCapability.Multimedia.Audio.Core 5302 5303**参数:** 5304 5305| 参数名 | 类型 | 必填 | 说明 | 5306| -------- |---------------------------------------------------------------------------| ---- | ------------------------------------------------------------ | 5307| type | string | 是 | 监听事件,固定为:'audioSessionDeactivated'。 | 5308| callback | Callback<[AudioSessionDeactivatedEvent](#audiosessiondeactivatedevent12)> | 是 | 回调函数,返回音频会话停用原因。 | 5309 5310**错误码:** 5311 5312以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5313 5314| 错误码ID | 错误信息 | 5315| ------- | --------------------------------------------| 5316| 401 | Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types. | 5317| 6800101 | Parameter verification failed. | 5318 5319**示例:** 5320 5321```ts 5322audioSessionManager.on('audioSessionDeactivated', (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 5323 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 5324}); 5325``` 5326 5327### off('audioSessionDeactivated')<sup>12+</sup> 5328 5329off(type: 'audioSessionDeactivated', callback?: Callback\<AudioSessionDeactivatedEvent>): void 5330 5331取消监听音频会话停用事件,使用callback方式返回结果。 5332 5333**系统能力:** SystemCapability.Multimedia.Audio.Core 5334 5335**参数:** 5336 5337| 参数名 | 类型 | 必填 | 说明 | 5338| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 5339| type | string | 是 | 监听事件,固定为:'audioSessionDeactivated'。 | 5340| callback |Callback<[AudioSessionDeactivatedEvent](#audiosessiondeactivatedevent12)> | 否 | 回调函数,返回音频会话停用原因。 | 5341 5342**错误码:** 5343 5344以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5345 5346| 错误码ID | 错误信息 | 5347| ------- | --------------------------------------------| 5348| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5349| 6800101 | Parameter verification failed. | 5350 5351**示例:** 5352 5353```ts 5354// 取消该事件的所有监听。 5355audioSessionManager.off('audioSessionDeactivated'); 5356 5357// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5358let audioSessionDeactivatedCallback = (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 5359 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 5360}; 5361 5362audioSessionManager.on('audioSessionDeactivated', audioSessionDeactivatedCallback); 5363 5364audioSessionManager.off('audioSessionDeactivated', audioSessionDeactivatedCallback); 5365``` 5366 5367## AudioRendererChangeInfoArray<sup>9+</sup> 5368 5369type AudioRendererChangeInfoArray = Array<Readonly<AudioRendererChangeInfo>> 5370 5371数组类型,AudioRenderChangeInfo数组,只读。 5372 5373**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5374 5375| 类型 | 说明 | 5376|---------|---------------------------------------------------------------| 5377| Array<Readonly<AudioRendererChangeInfo>> | 数组类型,[AudioRenderChangeInfo](#audiorendererchangeinfo9)数组,只读。 | 5378 5379## AudioRendererChangeInfo<sup>9+</sup> 5380 5381描述音频渲染器更改信息。 5382 5383**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5384 5385| 名称 | 类型 | 可读 | 可写 | 说明 | 5386| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5387| streamId | number | 是 | 否 | 音频流唯一id。 | 5388| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 否 | 音频渲染器信息。 | 5389| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备描述。| 5390 5391**示例:** 5392 5393```ts 5394import { audio } from '@kit.AudioKit'; 5395 5396const audioManager = audio.getAudioManager(); 5397let audioStreamManager = audioManager.getStreamManager(); 5398 5399audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => { 5400 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 5401 console.info(`## RendererChange on is called for ${i} ##`); 5402 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfoArray[i].streamId}`); 5403 console.info(`Content for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.content}`); 5404 console.info(`Stream for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.usage}`); 5405 console.info(`Flag ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.rendererFlags}`); 5406 let devDescriptor = AudioRendererChangeInfoArray[i].deviceDescriptors; 5407 for (let j = 0; j < AudioRendererChangeInfoArray[i].deviceDescriptors.length; j++) { 5408 console.info(`Id: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].id}`); 5409 console.info(`Type: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 5410 console.info(`Role: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 5411 console.info(`Name: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].name}`); 5412 console.info(`Addr: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].address}`); 5413 console.info(`SR: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 5414 console.info(`C ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 5415 console.info(`CM: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 5416 } 5417 } 5418}); 5419``` 5420 5421 5422## AudioCapturerChangeInfoArray<sup>9+</sup> 5423 5424type AudioCapturerChangeInfoArray = Array<Readonly<AudioCapturerChangeInfo>> 5425 5426数组类型,AudioCapturerChangeInfo数组,只读。 5427 5428**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5429 5430| 类型 | 说明 | 5431|---------|-----------------------------------------------------------------| 5432| Array<Readonly<AudioCapturerChangeInfo>> | 数组类型,[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)数组,只读。 | 5433 5434## AudioCapturerChangeInfo<sup>9+</sup> 5435 5436描述音频采集器更改信息。 5437 5438**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5439 5440| 名称 | 类型 | 可读 | 可写 | 说明 | 5441| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5442| streamId | number | 是 | 否 | 音频流唯一id。 | 5443| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 否 | 音频采集器信息。 | 5444| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备描述。| 5445| muted<sup>11+</sup> | boolean | 是 | 否 | 音频采集器静音状态。true表示音频采集器为静音状态,false表示音频采集器为非静音状态。| 5446 5447**示例:** 5448 5449```ts 5450import { audio } from '@kit.AudioKit'; 5451 5452const audioManager = audio.getAudioManager(); 5453let audioStreamManager = audioManager.getStreamManager(); 5454 5455audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) => { 5456 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 5457 console.info(`## CapChange on is called for element ${i} ##`); 5458 console.info(`StrId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 5459 console.info(`Src for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 5460 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 5461 let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors; 5462 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 5463 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 5464 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 5465 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 5466 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 5467 console.info(`Addr: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 5468 console.info(`SR: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 5469 console.info(`C ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 5470 console.info(`CM ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 5471 } 5472 } 5473}); 5474``` 5475 5476## AudioEffectInfoArray<sup>10+</sup> 5477 5478type AudioEffectInfoArray = Array<Readonly<AudioEffectMode>> 5479 5480待查询ContentType和StreamUsage组合场景下的音效模式数组类型,[AudioEffectMode](#audioeffectmode10)数组,只读。 5481 5482**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5483 5484| 类型 | 说明 | 5485|---------|---------------------------------------------------------------| 5486| Array<Readonly<AudioEffectMode>> | 待查询ContentType和StreamUsage组合场景下的音效模式数组类型,[AudioEffectMode](#audioeffectmode10)数组,只读。 | 5487 5488## AudioDeviceDescriptors 5489 5490type AudioDeviceDescriptors = Array<Readonly<AudioDeviceDescriptor>> 5491 5492设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 5493 5494**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5495 5496**系统能力:** SystemCapability.Multimedia.Audio.Device 5497 5498| 类型 | 说明 | 5499|---------|---------------------------------------------------------------| 5500| Array<Readonly<AudioDeviceDescriptor>> | 设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 | 5501 5502## AudioDeviceDescriptor 5503 5504描述音频设备。 5505 5506**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5507 5508| 名称 | 类型 | 可读 | 可写 | 说明 | 5509| ----------------------------- | -------------------------- | ---- | ---- | ---------- | 5510| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5511| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5512| id<sup>9+</sup> | number | 是 | 否 | 设备id,唯一。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5513| name<sup>9+</sup> | string | 是 | 否 | 设备名称。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5514| address<sup>9+</sup> | string | 是 | 否 | 设备地址。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5515| sampleRates<sup>9+</sup> | Array<number> | 是 | 否 | 支持的采样率。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5516| channelCounts<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道数。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5517| channelMasks<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道掩码。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5518| displayName<sup>10+</sup> | string | 是 | 否 | 设备显示名。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device| 5519| encodingTypes<sup>11+</sup> | Array<[AudioEncodingType](#audioencodingtype8)> | 是 | 否 | 支持的编码类型。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Core| 5520 5521**示例:** 5522 5523```ts 5524import { audio } from '@kit.AudioKit'; 5525 5526function displayDeviceProp(value: audio.AudioDeviceDescriptor) { 5527 deviceRoleValue = value.deviceRole; 5528 deviceTypeValue = value.deviceType; 5529} 5530 5531let deviceRoleValue: audio.DeviceRole | undefined = undefined; 5532let deviceTypeValue: audio.DeviceType | undefined = undefined; 5533audio.getAudioManager().getRoutingManager().getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((value: audio.AudioDeviceDescriptors) => { 5534 console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG'); 5535 value.forEach(displayDeviceProp); 5536 if (deviceTypeValue != undefined && deviceRoleValue != undefined){ 5537 console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : PASS'); 5538 } else { 5539 console.error('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : FAIL'); 5540 } 5541}); 5542``` 5543## AudioDataCallbackResult<sup>12+</sup> 5544 5545枚举,表示音频数据回调的结果。 5546 5547**系统能力:** SystemCapability.Multimedia.Audio.Core 5548 5549| 名称 | 值 | 说明 | 5550| ---------------------| --------| ----------------- | 5551| INVALID | -1 | 表示该回调数据无效。 | 5552| VALID | 0 | 表示该回调数据有效。 | 5553 5554## AudioRendererWriteDataCallback<sup>12+</sup> 5555 5556type AudioRendererWriteDataCallback = (data: ArrayBuffer) => AudioDataCallbackResult | void 5557 5558回调函数类型,用于音频渲染器的数据写入,回调函数结束后,音频服务会把data指针数据放入队列里等待播放,因此请勿在回调外再次更改data指向的数据, 且务必保证往data填满待播放数据, 否则会导致音频服务播放杂音。 5559 5560**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5561 5562**参数:** 5563 5564| 参数名 | 类型 |必填 | 说明 | 5565| :--------------| :--------| :----- | :------------ | 5566| data | ArrayBuffer | 是 | 待写入缓冲区的数据。 | 5567 5568**返回值:** 5569 5570| 类型 | 说明 | 5571|--------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------| 5572| [AudioDataCallbackResult](#audiodatacallbackresult12) \| void | 如果返回 void 或 AudioDataCallbackResult.VALID:表示数据有效,将播放音频数据;如果返回 AudioDataCallbackResult.INVALID:表示数据无效,且音频数据不播放。 | 5573 5574## AudioRenderer<sup>8+</sup> 5575 5576提供音频渲染的相关接口。在调用AudioRenderer的接口前,需要先通过[createAudioRenderer](#audiocreateaudiorenderer8)创建实例。 5577 5578### 属性 5579 5580**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5581 5582| 名称 | 类型 | 可读 | 可写 | 说明 | 5583| ----- | -------------------------- | ---- | ---- | ------------------ | 5584| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频渲染器的状态。 | 5585 5586**示例:** 5587 5588```ts 5589import { audio } from '@kit.AudioKit'; 5590 5591let state: audio.AudioState = audioRenderer.state; 5592``` 5593 5594### getRendererInfo<sup>8+</sup> 5595 5596getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void 5597 5598获取当前被创建的音频渲染器的信息,使用callback方式异步返回结果。 5599 5600**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5601 5602**参数:** 5603 5604| 参数名 | 类型 | 必填 | 说明 | 5605| :------- | :------------------------------------------------------- | :--- | :--------------------- | 5606| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是 | 回调函数。当获取音频渲染器的信息成功,err为undefined,data为获取到的音频渲染器的信息;否则为错误对象。 | 5607 5608**示例:** 5609 5610```ts 5611import { BusinessError } from '@kit.BasicServicesKit'; 5612 5613audioRenderer.getRendererInfo((err: BusinessError, rendererInfo: audio.AudioRendererInfo) => { 5614 console.info('Renderer GetRendererInfo:'); 5615 console.info(`Renderer content: ${rendererInfo.content}`); 5616 console.info(`Renderer usage: ${rendererInfo.usage}`); 5617 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`); 5618}); 5619``` 5620 5621### getRendererInfo<sup>8+</sup> 5622 5623getRendererInfo(): Promise<AudioRendererInfo\> 5624 5625获取当前被创建的音频渲染器的信息,使用Promise方式异步返回结果。 5626 5627**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5628 5629**返回值:** 5630 5631| 类型 | 说明 | 5632| -------------------------------------------------- | ------------------------------- | 5633| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise对象,返回音频渲染器信息。 | 5634 5635**示例:** 5636 5637```ts 5638import { BusinessError } from '@kit.BasicServicesKit'; 5639 5640audioRenderer.getRendererInfo().then((rendererInfo: audio.AudioRendererInfo) => { 5641 console.info('Renderer GetRendererInfo:'); 5642 console.info(`Renderer content: ${rendererInfo.content}`); 5643 console.info(`Renderer usage: ${rendererInfo.usage}`); 5644 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 5645}).catch((err: BusinessError) => { 5646 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${err}`); 5647}); 5648``` 5649 5650### getRendererInfoSync<sup>10+</sup> 5651 5652getRendererInfoSync(): AudioRendererInfo 5653 5654获取当前被创建的音频渲染器的信息,同步返回结果。 5655 5656**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5657 5658**返回值:** 5659 5660| 类型 | 说明 | 5661| -------------------------------------------------- | ------------------------------- | 5662| [AudioRendererInfo](#audiorendererinfo8) | 返回音频渲染器信息。 | 5663 5664**示例:** 5665 5666```ts 5667import { BusinessError } from '@kit.BasicServicesKit'; 5668 5669try { 5670 let rendererInfo: audio.AudioRendererInfo = audioRenderer.getRendererInfoSync(); 5671 console.info(`Renderer content: ${rendererInfo.content}`); 5672 console.info(`Renderer usage: ${rendererInfo.usage}`); 5673 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 5674} catch (err) { 5675 let error = err as BusinessError; 5676 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${error}`); 5677} 5678``` 5679 5680### getStreamInfo<sup>8+</sup> 5681 5682getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 5683 5684获取音频流信息,使用callback方式异步返回结果。 5685 5686**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5687 5688**参数:** 5689 5690| 参数名 | 类型 | 必填 | 说明 | 5691| :------- | :--------------------------------------------------- | :--- | :------------------- | 5692| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取音频流信息成功,err为undefined,data为获取到的音频流信息;否则为错误对象。 | 5693 5694**示例:** 5695 5696```ts 5697import { BusinessError } from '@kit.BasicServicesKit'; 5698 5699audioRenderer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 5700 console.info('Renderer GetStreamInfo:'); 5701 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5702 console.info(`Renderer channel: ${streamInfo.channels}`); 5703 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5704 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5705}); 5706``` 5707 5708### getStreamInfo<sup>8+</sup> 5709 5710getStreamInfo(): Promise<AudioStreamInfo\> 5711 5712获取音频流信息,使用Promise方式异步返回结果。 5713 5714**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5715 5716**返回值:** 5717 5718| 类型 | 说明 | 5719| :--------------------------------------------- | :--------------------- | 5720| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回音频流信息。 | 5721 5722**示例:** 5723 5724```ts 5725import { BusinessError } from '@kit.BasicServicesKit'; 5726 5727audioRenderer.getStreamInfo().then((streamInfo: audio.AudioStreamInfo) => { 5728 console.info('Renderer GetStreamInfo:'); 5729 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5730 console.info(`Renderer channel: ${streamInfo.channels}`); 5731 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5732 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5733}).catch((err: BusinessError) => { 5734 console.error(`ERROR: ${err}`); 5735}); 5736``` 5737 5738### getStreamInfoSync<sup>10+</sup> 5739 5740getStreamInfoSync(): AudioStreamInfo 5741 5742获取音频流信息,同步返回结果。 5743 5744**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5745 5746**返回值:** 5747 5748| 类型 | 说明 | 5749| :--------------------------------------------- | :--------------------- | 5750| [AudioStreamInfo](#audiostreaminfo8) | 返回音频流信息。 | 5751 5752**示例:** 5753 5754```ts 5755import { BusinessError } from '@kit.BasicServicesKit'; 5756 5757try { 5758 let streamInfo: audio.AudioStreamInfo = audioRenderer.getStreamInfoSync(); 5759 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5760 console.info(`Renderer channel: ${streamInfo.channels}`); 5761 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5762 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5763} catch (err) { 5764 let error = err as BusinessError; 5765 console.error(`ERROR: ${error}`); 5766} 5767``` 5768 5769### getAudioStreamId<sup>9+</sup> 5770 5771getAudioStreamId(callback: AsyncCallback<number\>): void 5772 5773获取音频流id,使用callback方式异步返回结果。 5774 5775**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5776 5777**参数:** 5778 5779| 参数名 | 类型 | 必填 | 说明 | 5780| :------- | :--------------------------------------------------- | :--- | :------------------- | 5781| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 5782 5783**示例:** 5784 5785```ts 5786import { BusinessError } from '@kit.BasicServicesKit'; 5787 5788audioRenderer.getAudioStreamId((err: BusinessError, streamId: number) => { 5789 console.info(`Renderer GetStreamId: ${streamId}`); 5790}); 5791``` 5792 5793### getAudioStreamId<sup>9+</sup> 5794 5795getAudioStreamId(): Promise<number\> 5796 5797获取音频流id,使用Promise方式异步返回结果。 5798 5799**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5800 5801**返回值:** 5802 5803| 类型 | 说明 | 5804| :--------------------------------------------- | :--------------------- | 5805| Promise<number\> | Promise对象,返回音频流id。 | 5806 5807**示例:** 5808 5809```ts 5810import { BusinessError } from '@kit.BasicServicesKit'; 5811 5812audioRenderer.getAudioStreamId().then((streamId: number) => { 5813 console.info(`Renderer getAudioStreamId: ${streamId}`); 5814}).catch((err: BusinessError) => { 5815 console.error(`ERROR: ${err}`); 5816}); 5817``` 5818 5819### getAudioStreamIdSync<sup>10+</sup> 5820 5821getAudioStreamIdSync(): number 5822 5823获取音频流id,同步返回结果。 5824 5825**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5826 5827**返回值:** 5828 5829| 类型 | 说明 | 5830| :--------------------------------------------- | :--------------------- | 5831| number | 返回音频流id。 | 5832 5833**示例:** 5834 5835```ts 5836import { BusinessError } from '@kit.BasicServicesKit'; 5837 5838try { 5839 let streamId: number = audioRenderer.getAudioStreamIdSync(); 5840 console.info(`Renderer getAudioStreamIdSync: ${streamId}`); 5841} catch (err) { 5842 let error = err as BusinessError; 5843 console.error(`ERROR: ${error}`); 5844} 5845``` 5846 5847### setAudioEffectMode<sup>10+</sup> 5848 5849setAudioEffectMode(mode: AudioEffectMode, callback: AsyncCallback\<void>): void 5850 5851设置当前音效模式。使用callback方式异步返回结果。 5852 5853**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5854 5855**参数:** 5856 5857| 参数名 | 类型 | 必填 | 说明 | 5858| -------- | ---------------------------------------- | ---- | ------------------------ | 5859| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 5860| callback | AsyncCallback\<void> | 是 | 回调函数。当设置当前音效模式成功,err为undefined,否则为错误对象。 | 5861 5862**错误码:** 5863 5864以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5865 5866| 错误码ID | 错误信息 | 5867| ------- | ----------------------------------------------| 5868| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5869| 6800101 | Parameter verification failed. Return by callback. | 5870 5871**示例:** 5872 5873```ts 5874import { BusinessError } from '@kit.BasicServicesKit'; 5875 5876audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => { 5877 if (err) { 5878 console.error('Failed to set params'); 5879 } else { 5880 console.info('Callback invoked to indicate a successful audio effect mode setting.'); 5881 } 5882}); 5883``` 5884 5885### setAudioEffectMode<sup>10+</sup> 5886 5887setAudioEffectMode(mode: AudioEffectMode): Promise\<void> 5888 5889设置当前音效模式。使用Promise方式异步返回结果。 5890 5891**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5892 5893**参数:** 5894 5895| 参数名 | 类型 | 必填 | 说明 | 5896| ------ | ---------------------------------------- | ---- | ------------ | 5897| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 5898 5899**返回值:** 5900 5901| 类型 | 说明 | 5902| -------------- | ------------------------- | 5903| Promise\<void> | Promise对象,无返回结果。 | 5904 5905**错误码:** 5906 5907以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5908 5909| 错误码ID | 错误信息 | 5910| ------- | ---------------------------------------------| 5911| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5912| 6800101 | Parameter verification failed. Return by promise. | 5913 5914**示例:** 5915 5916```ts 5917import { BusinessError } from '@kit.BasicServicesKit'; 5918 5919audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT).then(() => { 5920 console.info('setAudioEffectMode SUCCESS'); 5921}).catch((err: BusinessError) => { 5922 console.error(`ERROR: ${err}`); 5923}); 5924``` 5925 5926### getAudioEffectMode<sup>10+</sup> 5927 5928getAudioEffectMode(callback: AsyncCallback\<AudioEffectMode>): void 5929 5930获取当前音效模式。使用callback方式异步返回结果。 5931 5932**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5933 5934**参数:** 5935 5936| 参数名 | 类型 | 必填 | 说明 | 5937| -------- | ------------------------------------------------------- | ---- | ------------------ | 5938| callback | AsyncCallback<[AudioEffectMode](#audioeffectmode10)> | 是 | 回调函数。当获取当前音效模式成功,err为undefined,data为获取到的当前音效模式;否则为错误对象。 | 5939 5940**示例:** 5941 5942```ts 5943import { BusinessError } from '@kit.BasicServicesKit'; 5944 5945audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => { 5946 if (err) { 5947 console.error('Failed to get params'); 5948 } else { 5949 console.info(`getAudioEffectMode: ${effectMode}`); 5950 } 5951}); 5952``` 5953 5954### getAudioEffectMode<sup>10+</sup> 5955 5956getAudioEffectMode(): Promise\<AudioEffectMode> 5957 5958获取当前音效模式。使用Promise方式异步返回结果。 5959 5960**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5961 5962**返回值:** 5963 5964| 类型 | 说明 | 5965| ------------------------------------------------- | ------------------------- | 5966| Promise<[AudioEffectMode](#audioeffectmode10)> | Promise对象,返回当前音效模式。 | 5967 5968**示例:** 5969 5970```ts 5971import { BusinessError } from '@kit.BasicServicesKit'; 5972 5973audioRenderer.getAudioEffectMode().then((effectMode: audio.AudioEffectMode) => { 5974 console.info(`getAudioEffectMode: ${effectMode}`); 5975}).catch((err: BusinessError) => { 5976 console.error(`ERROR: ${err}`); 5977}); 5978``` 5979 5980### start<sup>8+</sup> 5981 5982start(callback: AsyncCallback<void\>): void 5983 5984启动音频渲染器。使用callback方式异步返回结果。 5985 5986**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5987 5988**参数:** 5989 5990| 参数名 | 类型 | 必填 | 说明 | 5991| -------- | -------------------- | ---- | ---------- | 5992| callback | AsyncCallback\<void> | 是 | Callback对象,成功表示启动音频采集器成功,异常将返回error对象:<br>错误码6800301,表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 5993 5994**示例:** 5995 5996```ts 5997import { BusinessError } from '@kit.BasicServicesKit'; 5998 5999audioRenderer.start((err: BusinessError) => { 6000 if (err) { 6001 console.error('Renderer start failed.'); 6002 } else { 6003 console.info('Renderer start success.'); 6004 } 6005}); 6006``` 6007 6008### start<sup>8+</sup> 6009 6010start(): Promise<void\> 6011 6012启动音频渲染器。使用Promise方式异步返回结果。 6013 6014**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6015 6016**返回值:** 6017 6018| 类型 | 说明 | 6019| -------------- | ------------------------- | 6020| Promise\<void> | Promise对象,成功表示启动音频采集器成功,异常将返回error对象:<br>错误码6800301,表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 6021 6022**示例:** 6023 6024```ts 6025import { BusinessError } from '@kit.BasicServicesKit'; 6026 6027audioRenderer.start().then(() => { 6028 console.info('Renderer started'); 6029}).catch((err: BusinessError) => { 6030 console.error(`ERROR: ${err}`); 6031}); 6032``` 6033 6034### pause<sup>8+</sup> 6035 6036pause(callback: AsyncCallback\<void>): void 6037 6038暂停渲染。使用callback方式异步返回结果。 6039 6040**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6041 6042**参数:** 6043 6044| 参数名 | 类型 | 必填 | 说明 | 6045| -------- | -------------------- | ---- | ---------------- | 6046| callback | AsyncCallback\<void> | 是 | 回调函数。当暂停渲染成功,err为undefined,否则为错误对象。 | 6047 6048**示例:** 6049 6050```ts 6051import { BusinessError } from '@kit.BasicServicesKit'; 6052 6053audioRenderer.pause((err: BusinessError) => { 6054 if (err) { 6055 console.error('Renderer pause failed'); 6056 } else { 6057 console.info('Renderer paused.'); 6058 } 6059}); 6060``` 6061 6062### pause<sup>8+</sup> 6063 6064pause(): Promise\<void> 6065 6066暂停渲染。使用Promise方式异步返回结果。 6067 6068**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6069 6070**返回值:** 6071 6072| 类型 | 说明 | 6073| -------------- | ------------------------- | 6074| Promise\<void> | Promise对象,无返回结果。 | 6075 6076**示例:** 6077 6078```ts 6079import { BusinessError } from '@kit.BasicServicesKit'; 6080 6081audioRenderer.pause().then(() => { 6082 console.info('Renderer paused'); 6083}).catch((err: BusinessError) => { 6084 console.error(`ERROR: ${err}`); 6085}); 6086``` 6087 6088### drain<sup>8+</sup> 6089 6090drain(callback: AsyncCallback\<void>): void 6091 6092检查缓冲区是否已被耗尽。使用callback方式异步返回结果。 6093 6094**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6095 6096**参数:** 6097 6098| 参数名 | 类型 | 必填 | 说明 | 6099| -------- | -------------------- | ---- | ---------------- | 6100| callback | AsyncCallback\<void> | 是 | 回调函数。当检查缓冲区是否已被耗尽成功,err为undefined,否则为错误对象。 | 6101 6102**示例:** 6103 6104```ts 6105import { BusinessError } from '@kit.BasicServicesKit'; 6106 6107audioRenderer.drain((err: BusinessError) => { 6108 if (err) { 6109 console.error('Renderer drain failed'); 6110 } else { 6111 console.info('Renderer drained.'); 6112 } 6113}); 6114``` 6115 6116### drain<sup>8+</sup> 6117 6118drain(): Promise\<void> 6119 6120检查缓冲区是否已被耗尽。使用Promise方式异步返回结果。 6121 6122**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6123 6124**返回值:** 6125 6126| 类型 | 说明 | 6127| -------------- | ------------------------- | 6128| Promise\<void> | Promise对象,无返回结果。 | 6129 6130**示例:** 6131 6132```ts 6133import { BusinessError } from '@kit.BasicServicesKit'; 6134 6135audioRenderer.drain().then(() => { 6136 console.info('Renderer drained successfully'); 6137}).catch((err: BusinessError) => { 6138 console.error(`ERROR: ${err}`); 6139}); 6140``` 6141 6142### flush<sup>11+</sup> 6143 6144flush(): Promise\<void> 6145 6146清空缓冲区([AudioState](#audiostate8)为STATE_RUNNING、STATE_PAUSED、STATE_STOPPED状态下可用)。使用Promise方式异步返回结果。 6147 6148**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6149 6150**返回值:** 6151 6152| 类型 | 说明 | 6153| -------------- | ------------------------- | 6154| Promise\<void> | Promise对象,无返回结果。 | 6155 6156**错误码:** 6157 6158以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 6159 6160| 错误码ID | 错误信息 | 6161| ------- | --------------------------------------------| 6162| 6800103 | Operation not permit at current state. Return by promise. | 6163 6164**示例:** 6165 6166```ts 6167import { BusinessError } from '@kit.BasicServicesKit'; 6168 6169audioRenderer.flush().then(() => { 6170 console.info('Renderer flushed successfully'); 6171}).catch((err: BusinessError) => { 6172 console.error(`ERROR: ${err}`); 6173}); 6174``` 6175 6176### stop<sup>8+</sup> 6177 6178stop(callback: AsyncCallback\<void>): void 6179 6180停止渲染。使用callback方式异步返回结果。 6181 6182**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6183 6184**参数:** 6185 6186| 参数名 | 类型 | 必填 | 说明 | 6187| -------- | -------------------- | ---- | ---------------- | 6188| callback | AsyncCallback\<void> | 是 | 回调函数。当停止渲染成功,err为undefined,否则为错误对象。 | 6189 6190**示例:** 6191 6192```ts 6193import { BusinessError } from '@kit.BasicServicesKit'; 6194 6195audioRenderer.stop((err: BusinessError) => { 6196 if (err) { 6197 console.error('Renderer stop failed'); 6198 } else { 6199 console.info('Renderer stopped.'); 6200 } 6201}); 6202``` 6203 6204### stop<sup>8+</sup> 6205 6206stop(): Promise\<void> 6207 6208停止渲染。使用Promise方式异步返回结果。 6209 6210**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6211 6212**返回值:** 6213 6214| 类型 | 说明 | 6215| -------------- | ------------------------- | 6216| Promise\<void> | Promise对象,无返回结果。 | 6217 6218**示例:** 6219 6220```ts 6221import { BusinessError } from '@kit.BasicServicesKit'; 6222 6223audioRenderer.stop().then(() => { 6224 console.info('Renderer stopped successfully'); 6225}).catch((err: BusinessError) => { 6226 console.error(`ERROR: ${err}`); 6227}); 6228``` 6229 6230### release<sup>8+</sup> 6231 6232release(callback: AsyncCallback\<void>): void 6233 6234释放音频渲染器。使用callback方式异步返回结果。 6235 6236**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6237 6238**参数:** 6239 6240| 参数名 | 类型 | 必填 | 说明 | 6241| -------- | -------------------- | ---- | ---------------- | 6242| callback | AsyncCallback\<void> | 是 | 回调函数。当释放音频渲染器成功,err为undefined,否则为错误对象。 | 6243 6244**示例:** 6245 6246```ts 6247import { BusinessError } from '@kit.BasicServicesKit'; 6248 6249audioRenderer.release((err: BusinessError) => { 6250 if (err) { 6251 console.error('Renderer release failed'); 6252 } else { 6253 console.info('Renderer released.'); 6254 } 6255}); 6256``` 6257 6258### release<sup>8+</sup> 6259 6260release(): Promise\<void> 6261 6262释放渲染器。使用Promise方式异步返回结果。 6263 6264**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6265 6266**返回值:** 6267 6268| 类型 | 说明 | 6269| -------------- | ------------------------- | 6270| Promise\<void> | Promise对象,无返回结果。 | 6271 6272**示例:** 6273 6274```ts 6275import { BusinessError } from '@kit.BasicServicesKit'; 6276 6277audioRenderer.release().then(() => { 6278 console.info('Renderer released successfully'); 6279}).catch((err: BusinessError) => { 6280 console.error(`ERROR: ${err}`); 6281}); 6282``` 6283 6284### write<sup>8+(deprecated)</sup> 6285 6286write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void 6287 6288写入缓冲区。使用callback方式异步返回结果。 6289 6290> **说明:** 6291> 从 API version 8 开始支持,从 API version 11 开始废弃,建议使用AudioRenderer中的[on('writeData')](#onwritedata11)替代。 6292 6293**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6294 6295**参数:** 6296 6297| 参数名 | 类型 | 必填 | 说明 | 6298| -------- | ---------------------- | ---- | --------------------------------------------------- | 6299| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6300| callback | AsyncCallback\<number> | 是 | 回调函数。当写入缓冲区成功,err为undefined,data为获取到的写入的字节数;否则为错误对象。 | 6301 6302**示例:** 6303 6304```ts 6305import { BusinessError } from '@kit.BasicServicesKit'; 6306import { fileIo as fs } from '@kit.CoreFileKit'; 6307 6308let bufferSize: number; 6309class Options { 6310 offset?: number; 6311 length?: number; 6312} 6313audioRenderer.getBufferSize().then((data: number)=> { 6314 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6315 bufferSize = data; 6316 console.info(`Buffer size: ${bufferSize}`); 6317 let path = getContext().cacheDir; 6318 let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; 6319 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6320 fs.stat(filePath).then(async (stat: fs.Stat) => { 6321 let buf = new ArrayBuffer(bufferSize); 6322 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6323 for (let i = 0;i < len; i++) { 6324 let options: Options = { 6325 offset: i * bufferSize, 6326 length: bufferSize 6327 }; 6328 let readSize: number = await fs.read(file.fd, buf, options); 6329 let writeSize: number = await new Promise((resolve,reject)=>{ 6330 audioRenderer.write(buf,(err: BusinessError, writeSize: number)=>{ 6331 if(err){ 6332 reject(err) 6333 }else{ 6334 resolve(writeSize) 6335 } 6336 }) 6337 }) 6338 } 6339 }); 6340 }).catch((err: BusinessError) => { 6341 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6342}); 6343``` 6344 6345### write<sup>8+(deprecated)</sup> 6346 6347write(buffer: ArrayBuffer): Promise\<number> 6348 6349写入缓冲区。使用Promise方式异步返回结果。 6350 6351> **说明:** 6352> 从 API version 8 开始支持,从 API version 11 开始废弃,建议使用AudioRenderer中的[on('writeData')](#onwritedata11)替代。 6353 6354**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6355 6356**参数:** 6357 6358| 参数名 | 类型 | 必填 | 说明 | 6359| -------- | ---------------------- | ---- | --------------------------------------------------- | 6360| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6361 6362**返回值:** 6363 6364| 类型 | 说明 | 6365| ---------------- | ------------------------------------------------------------ | 6366| Promise\<number> | Promise对象,返回写入的字节数。 | 6367 6368**示例:** 6369 6370```ts 6371import { BusinessError } from '@kit.BasicServicesKit'; 6372import { fileIo as fs } from '@kit.CoreFileKit'; 6373 6374let bufferSize: number; 6375class Options { 6376 offset?: number; 6377 length?: number; 6378} 6379audioRenderer.getBufferSize().then((data: number) => { 6380 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6381 bufferSize = data; 6382 console.info(`BufferSize: ${bufferSize}`); 6383 let path = getContext().cacheDir; 6384 let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; 6385 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6386 fs.stat(filePath).then(async (stat: fs.Stat) => { 6387 let buf = new ArrayBuffer(bufferSize); 6388 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6389 for (let i = 0;i < len; i++) { 6390 let options: Options = { 6391 offset: i * bufferSize, 6392 length: bufferSize 6393 }; 6394 let readSize: number = await fs.read(file.fd, buf, options); 6395 try{ 6396 let writeSize: number = await audioRenderer.write(buf); 6397 } catch(err) { 6398 let error = err as BusinessError; 6399 console.error(`audioRenderer.write err: ${error}`); 6400 } 6401 } 6402 }); 6403}).catch((err: BusinessError) => { 6404 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6405}); 6406``` 6407 6408### getAudioTime<sup>8+</sup> 6409 6410getAudioTime(callback: AsyncCallback\<number>): void 6411 6412获取播放到当前位置时的时间戳(从 1970 年 1 月 1 日开始),单位为纳秒。使用callback方式异步返回结果。 6413 6414**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6415 6416**参数:** 6417 6418| 参数名 | 类型 | 必填 | 说明 | 6419| -------- | ---------------------- | ---- | ---------------- | 6420| callback | AsyncCallback\<number> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 6421 6422**示例:** 6423 6424```ts 6425import { BusinessError } from '@kit.BasicServicesKit'; 6426 6427audioRenderer.getAudioTime((err: BusinessError, timestamp: number) => { 6428 console.info(`Current timestamp: ${timestamp}`); 6429}); 6430``` 6431 6432### getAudioTime<sup>8+</sup> 6433 6434getAudioTime(): Promise\<number> 6435 6436获取播放到当前位置时的时间戳(从 1970 年 1 月 1 日开始),单位为纳秒。使用Promise方式异步返回结果。 6437 6438**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6439 6440**返回值:** 6441 6442| 类型 | 描述 | 6443| ---------------- | ----------------------- | 6444| Promise\<number> | Promise对象,返回时间戳。 | 6445 6446**示例:** 6447 6448```ts 6449import { BusinessError } from '@kit.BasicServicesKit'; 6450 6451audioRenderer.getAudioTime().then((timestamp: number) => { 6452 console.info(`Current timestamp: ${timestamp}`); 6453}).catch((err: BusinessError) => { 6454 console.error(`ERROR: ${err}`); 6455}); 6456``` 6457 6458### getAudioTimeSync<sup>10+</sup> 6459 6460getAudioTimeSync(): number 6461 6462获取播放到当前位置时的时间戳(从 1970 年 1 月 1 日开始),单位为纳秒。同步返回结果。 6463 6464**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6465 6466**返回值:** 6467 6468| 类型 | 描述 | 6469| ---------------- | ----------------------- | 6470| number | 返回时间戳。 | 6471 6472**示例:** 6473 6474```ts 6475import { BusinessError } from '@kit.BasicServicesKit'; 6476 6477try { 6478 let timestamp: number = audioRenderer.getAudioTimeSync(); 6479 console.info(`Current timestamp: ${timestamp}`); 6480} catch (err) { 6481 let error = err as BusinessError; 6482 console.error(`ERROR: ${error}`); 6483} 6484``` 6485 6486### getBufferSize<sup>8+</sup> 6487 6488getBufferSize(callback: AsyncCallback\<number>): void 6489 6490获取音频渲染器的最小缓冲区大小。使用callback方式异步返回结果。 6491 6492**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6493 6494**参数:** 6495 6496| 参数名 | 类型 | 必填 | 说明 | 6497| -------- | ---------------------- | ---- | -------------------- | 6498| callback | AsyncCallback\<number> | 是 | 回调函数。当获取音频渲染器的最小缓冲区大小成功,err为undefined,data为获取到的最小缓冲区大小;否则为错误对象。 | 6499 6500**示例:** 6501 6502```ts 6503import { BusinessError } from '@kit.BasicServicesKit'; 6504 6505let bufferSize: number; 6506 6507audioRenderer.getBufferSize((err: BusinessError, data: number) => { 6508 if (err) { 6509 console.error('getBufferSize error'); 6510 } else { 6511 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6512 bufferSize = data; 6513 } 6514}); 6515``` 6516 6517### getBufferSize<sup>8+</sup> 6518 6519getBufferSize(): Promise\<number> 6520 6521获取音频渲染器的最小缓冲区大小。使用Promise方式异步返回结果。 6522 6523**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6524 6525**返回值:** 6526 6527| 类型 | 说明 | 6528| ---------------- | --------------------------- | 6529| Promise\<number> | Promise对象,返回缓冲区大小。 | 6530 6531**示例:** 6532 6533```ts 6534import { BusinessError } from '@kit.BasicServicesKit'; 6535 6536let bufferSize: number; 6537 6538audioRenderer.getBufferSize().then((data: number) => { 6539 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6540 bufferSize = data; 6541}).catch((err: BusinessError) => { 6542 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6543}); 6544``` 6545 6546### getBufferSizeSync<sup>10+</sup> 6547 6548getBufferSizeSync(): number 6549 6550获取音频渲染器的最小缓冲区大小,同步返回结果。 6551 6552**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6553 6554**返回值:** 6555 6556| 类型 | 说明 | 6557| ---------------- | --------------------------- | 6558| number | 返回缓冲区大小。 | 6559 6560**示例:** 6561 6562```ts 6563import { BusinessError } from '@kit.BasicServicesKit'; 6564 6565let bufferSize: number = 0; 6566 6567try { 6568 bufferSize = audioRenderer.getBufferSizeSync(); 6569 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${bufferSize}`); 6570} catch (err) { 6571 let error = err as BusinessError; 6572 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${error}`); 6573} 6574``` 6575 6576### setRenderRate<sup>8+(deprecated)</sup> 6577 6578setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void 6579 6580设置音频渲染速率。使用callback方式异步返回结果。 6581 6582> **说明:** 6583> 从 API version 8 开始支持,从 API version 11 开始废弃。建议使用AudioRenderer中的[setSpeed](#setspeed11)替代。 6584 6585**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6586 6587**参数:** 6588 6589| 参数名 | 类型 | 必填 | 说明 | 6590| -------- | ---------------------------------------- | ---- | ------------------------ | 6591| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 6592| callback | AsyncCallback\<void> | 是 | 回调函数。当设置音频渲染速率成功,err为undefined,否则为错误对象。 | 6593 6594**示例:** 6595 6596```ts 6597import { BusinessError } from '@kit.BasicServicesKit'; 6598 6599audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err: BusinessError) => { 6600 if (err) { 6601 console.error('Failed to set params'); 6602 } else { 6603 console.info('Callback invoked to indicate a successful render rate setting.'); 6604 } 6605}); 6606``` 6607 6608### setRenderRate<sup>8+(deprecated)</sup> 6609 6610setRenderRate(rate: AudioRendererRate): Promise\<void> 6611 6612设置音频渲染速率。使用Promise方式异步返回结果。 6613 6614> **说明:** 6615> 从 API version 8 开始支持,从 API version 11 开始废弃。建议使用AudioRenderer中的[setSpeed](#setspeed11)替代。 6616 6617**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6618 6619**参数:** 6620 6621| 参数名 | 类型 | 必填 | 说明 | 6622| ------ | ---------------------------------------- | ---- | ------------ | 6623| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 6624 6625**返回值:** 6626 6627| 类型 | 说明 | 6628| -------------- | ------------------------- | 6629| Promise\<void> | Promise对象,无返回结果。 | 6630 6631**示例:** 6632 6633```ts 6634import { BusinessError } from '@kit.BasicServicesKit'; 6635 6636audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => { 6637 console.info('setRenderRate SUCCESS'); 6638}).catch((err: BusinessError) => { 6639 console.error(`ERROR: ${err}`); 6640}); 6641``` 6642 6643### setSpeed<sup>11+</sup> 6644 6645setSpeed(speed: number): void 6646 6647设置播放倍速。 6648 6649**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6650 6651**参数:** 6652 6653| 参数名 | 类型 | 必填 | 说明 | 6654| ------ | ---------------------------------------- | ---- |----------------------| 6655| speed | number | 是 | 设置播放的倍速值(倍速范围:0.125-4.0)。 | 6656 6657**错误码:** 6658 6659以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 6660 6661| 错误码ID | 错误信息 | 6662| ------- | --------------------------------------------| 6663| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6664| 6800101 | Parameter verification failed. | 6665 6666**示例:** 6667 6668```ts 6669audioRenderer.setSpeed(1.5); 6670``` 6671 6672### getRenderRate<sup>8+(deprecated)</sup> 6673 6674getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void 6675 6676获取当前渲染速率。使用callback方式异步返回结果。 6677 6678> **说明:** 6679> 从 API version 8 开始支持,从 API version 11 开始废弃。建议使用AudioRenderer中的[getSpeed](#getspeed11)替代。 6680 6681**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6682 6683**参数:** 6684 6685| 参数名 | 类型 | 必填 | 说明 | 6686| -------- | ------------------------------------------------------- | ---- | ------------------ | 6687| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是 | 回调函数。当获取当前渲染速率成功,err为undefined,data为获取到的当前渲染速率;否则为错误对象。 | 6688 6689**示例:** 6690 6691```ts 6692import { BusinessError } from '@kit.BasicServicesKit'; 6693 6694audioRenderer.getRenderRate((err: BusinessError, renderRate: audio.AudioRendererRate) => { 6695 console.info(`getRenderRate: ${renderRate}`); 6696}); 6697``` 6698 6699### getRenderRate<sup>8+(deprecated)</sup> 6700 6701getRenderRate(): Promise\<AudioRendererRate> 6702 6703获取当前渲染速率。使用Promise方式异步返回结果。 6704 6705> **说明:** 6706> 从 API version 8 开始支持,从 API version 11 开始废弃。建议使用AudioRenderer中的[getSpeed](#getspeed11)替代。 6707 6708**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6709 6710**返回值:** 6711 6712| 类型 | 说明 | 6713| ------------------------------------------------- | ------------------------- | 6714| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise对象,返回渲染速率。 | 6715 6716**示例:** 6717 6718```ts 6719import { BusinessError } from '@kit.BasicServicesKit'; 6720 6721audioRenderer.getRenderRate().then((renderRate: audio.AudioRendererRate) => { 6722 console.info(`getRenderRate: ${renderRate}`); 6723}).catch((err: BusinessError) => { 6724 console.error(`ERROR: ${err}`); 6725}); 6726``` 6727 6728### getRenderRateSync<sup>10+(deprecated)</sup> 6729 6730getRenderRateSync(): AudioRendererRate 6731 6732获取当前渲染速率,同步返回结果。 6733 6734> **说明:** 6735> 从 API version 10 开始支持,从 API version 11 开始废弃。建议使用AudioRenderer中的[getSpeed](#getspeed11)替代。 6736 6737**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6738 6739**返回值:** 6740 6741| 类型 | 说明 | 6742| ------------------------------------------------- | ------------------------- | 6743| [AudioRendererRate](#audiorendererrate8) | 返回渲染速率。 | 6744 6745**示例:** 6746 6747```ts 6748import { BusinessError } from '@kit.BasicServicesKit'; 6749 6750try { 6751 let renderRate: audio.AudioRendererRate = audioRenderer.getRenderRateSync(); 6752 console.info(`getRenderRate: ${renderRate}`); 6753} catch (err) { 6754 let error = err as BusinessError; 6755 console.error(`ERROR: ${error}`); 6756} 6757``` 6758 6759### getSpeed<sup>11+</sup> 6760 6761getSpeed(): number 6762 6763获取播放倍速。 6764 6765**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6766 6767**返回值:** 6768 6769| 类型 | 说明 | 6770| ------------------------------------------------- |-----------| 6771| number | 返回播放的倍速值。 | 6772 6773**示例:** 6774 6775```ts 6776let speed = audioRenderer.getSpeed(); 6777``` 6778 6779### setInterruptMode<sup>9+</sup> 6780 6781setInterruptMode(mode: InterruptMode): Promise<void> 6782 6783设置应用的焦点模型。使用Promise异步回调。 6784 6785**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6786 6787**参数:** 6788 6789| 参数名 | 类型 | 必填 | 说明 | 6790| ---------- | ---------------------------------- | ------ | ---------- | 6791| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 6792 6793**返回值:** 6794 6795| 类型 | 说明 | 6796| ------------------- | ----------------------------- | 6797| Promise<void> | Promise对象,无返回结果。 | 6798 6799**示例:** 6800 6801```ts 6802import { BusinessError } from '@kit.BasicServicesKit'; 6803 6804let mode = 0; 6805 6806audioRenderer.setInterruptMode(mode).then(() => { 6807 console.info('setInterruptMode Success!'); 6808}).catch((err: BusinessError) => { 6809 console.error(`setInterruptMode Fail: ${err}`); 6810}); 6811``` 6812### setInterruptMode<sup>9+</sup> 6813 6814setInterruptMode(mode: InterruptMode, callback: AsyncCallback\<void>): void 6815 6816设置应用的焦点模型。使用Callback回调返回执行结果。 6817 6818**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6819 6820**参数:** 6821 6822| 参数名 | 类型 | 必填 | 说明 | 6823| ------- | ----------------------------------- | ------ | -------------- | 6824|mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。| 6825|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的焦点模型成功,err为undefined,否则为错误对象。| 6826 6827**示例:** 6828 6829```ts 6830import { BusinessError } from '@kit.BasicServicesKit'; 6831 6832let mode = 1; 6833 6834audioRenderer.setInterruptMode(mode, (err: BusinessError) => { 6835 if(err){ 6836 console.error(`setInterruptMode Fail: ${err}`); 6837 } 6838 console.info('setInterruptMode Success!'); 6839}); 6840``` 6841 6842### setInterruptModeSync<sup>10+</sup> 6843 6844setInterruptModeSync(mode: InterruptMode): void 6845 6846设置应用的焦点模型,同步设置。 6847 6848**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6849 6850**参数:** 6851 6852| 参数名 | 类型 | 必填 | 说明 | 6853| ---------- | ---------------------------------- | ------ | ---------- | 6854| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 6855 6856**错误码:** 6857 6858以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 6859 6860| 错误码ID | 错误信息 | 6861| ------- | --------------------------------------------| 6862| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6863| 6800101 | Parameter verification failed. | 6864 6865**示例:** 6866 6867```ts 6868import { BusinessError } from '@kit.BasicServicesKit'; 6869 6870try { 6871 audioRenderer.setInterruptModeSync(0); 6872 console.info('setInterruptMode Success!'); 6873} catch (err) { 6874 let error = err as BusinessError; 6875 console.error(`setInterruptMode Fail: ${error}`); 6876} 6877``` 6878 6879### setVolume<sup>9+</sup> 6880 6881setVolume(volume: number): Promise<void> 6882 6883设置应用的音量。使用Promise异步回调。 6884 6885**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6886 6887**参数:** 6888 6889| 参数名 | 类型 | 必填 | 说明 | 6890| ---------- | ------- | ------ | ------------------- | 6891| volume | number | 是 | 音量值范围为0.0-1.0。 | 6892 6893**返回值:** 6894 6895| 类型 | 说明 | 6896| ------------------- | ----------------------------- | 6897| Promise<void> | Promise对象,无返回结果。 | 6898 6899**示例:** 6900 6901```ts 6902import { BusinessError } from '@kit.BasicServicesKit'; 6903 6904audioRenderer.setVolume(0.5).then(() => { 6905 console.info('setVolume Success!'); 6906}).catch((err: BusinessError) => { 6907 console.error(`setVolume Fail: ${err}`); 6908}); 6909``` 6910### setVolume<sup>9+</sup> 6911 6912setVolume(volume: number, callback: AsyncCallback\<void>): void 6913 6914设置应用的音量。使用Callback回调返回执行结果。 6915 6916**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6917 6918**参数:** 6919 6920| 参数名 | 类型 | 必填 | 说明 | 6921| ------- | -----------| ------ | ------------------- | 6922|volume | number | 是 | 音量值范围为0.0-1.0。 | 6923|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的音量成功,err为undefined,否则为错误对象。| 6924 6925**示例:** 6926 6927```ts 6928import { BusinessError } from '@kit.BasicServicesKit'; 6929 6930audioRenderer.setVolume(0.5, (err: BusinessError) => { 6931 if(err){ 6932 console.error(`setVolume Fail: ${err}`); 6933 return; 6934 } 6935 console.info('setVolume Success!'); 6936}); 6937``` 6938### getVolume<sup>12+</sup> 6939 6940getVolume(): number 6941 6942获取音频渲染器的当前音量值,同步返回结果。 6943 6944**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6945 6946**返回值:** 6947 6948| 类型 | 说明 | 6949| ---------------- | --------------------------- | 6950| number | 返回音量大小,音量范围[0.0-1.0]。 | 6951 6952**示例:** 6953 6954```ts 6955import { BusinessError } from '@kit.BasicServicesKit'; 6956 6957try { 6958 let value: number = audioRenderer.getVolume(); 6959 console.info(`Indicate that the volume is obtained ${value}.`); 6960} catch (err) { 6961 let error = err as BusinessError; 6962 console.error(`Failed to obtain the volume, error ${error}.`); 6963} 6964``` 6965 6966### getMinStreamVolume<sup>10+</sup> 6967 6968getMinStreamVolume(callback: AsyncCallback<number>): void 6969 6970获取应用基于音频流的最小音量。使用Callback回调返回。 6971 6972**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6973 6974**参数:** 6975 6976| 参数名 | 类型 | 必填 | 说明 | 6977| ------- | -----------| ------ | ------------------- | 6978|callback |AsyncCallback<number> | 是 |回调函数。当获取应用基于音频流的最小音量成功,err为undefined,data为获取到的应用基于音频流的最小音量(音量范围0-1);否则为错误对象。| 6979 6980**示例:** 6981 6982```ts 6983import { BusinessError } from '@kit.BasicServicesKit'; 6984 6985audioRenderer.getMinStreamVolume((err: BusinessError, minVolume: number) => { 6986 if (err) { 6987 console.error(`getMinStreamVolume error: ${err}`); 6988 } else { 6989 console.info(`getMinStreamVolume Success! ${minVolume}`); 6990 } 6991}); 6992``` 6993### getMinStreamVolume<sup>10+</sup> 6994 6995getMinStreamVolume(): Promise<number> 6996 6997获取应用基于音频流的最小音量。使用Promise异步回调。 6998 6999**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7000 7001**返回值:** 7002 7003| 类型 | 说明 | 7004| ------------------- | ----------------------------- | 7005| Promise<number>| Promise对象,返回音频流最小音量(音量范围0-1)。| 7006 7007**示例:** 7008 7009```ts 7010import { BusinessError } from '@kit.BasicServicesKit'; 7011 7012audioRenderer.getMinStreamVolume().then((value: number) => { 7013 console.info(`Get min stream volume Success! ${value}`); 7014}).catch((err: BusinessError) => { 7015 console.error(`Get min stream volume Fail: ${err}`); 7016}); 7017``` 7018 7019### getMinStreamVolumeSync<sup>10+</sup> 7020 7021getMinStreamVolumeSync(): number 7022 7023获取应用基于音频流的最小音量,同步返回结果。 7024 7025**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7026 7027**返回值:** 7028 7029| 类型 | 说明 | 7030| ------------------- | ----------------------------- | 7031| number| 返回音频流最小音量(音量范围0-1)。| 7032 7033**示例:** 7034 7035```ts 7036import { BusinessError } from '@kit.BasicServicesKit'; 7037 7038try { 7039 let value: number = audioRenderer.getMinStreamVolumeSync(); 7040 console.info(`Get min stream volume Success! ${value}`); 7041} catch (err) { 7042 let error = err as BusinessError; 7043 console.error(`Get min stream volume Fail: ${error}`); 7044} 7045``` 7046 7047### getMaxStreamVolume<sup>10+</sup> 7048 7049getMaxStreamVolume(callback: AsyncCallback<number>): void 7050 7051获取应用基于音频流的最大音量。使用Callback回调返回。 7052 7053**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7054 7055**参数:** 7056 7057| 参数名 | 类型 | 必填 | 说明 | 7058| ------- | -----------| ------ | ------------------- | 7059|callback | AsyncCallback<number> | 是 |回调函数。当获取应用基于音频流的最大音量成功,err为undefined,data为获取到的应用基于音频流的最大音量(音量范围0-1);否则为错误对象。| 7060 7061**示例:** 7062 7063```ts 7064import { BusinessError } from '@kit.BasicServicesKit'; 7065 7066audioRenderer.getMaxStreamVolume((err: BusinessError, maxVolume: number) => { 7067 if (err) { 7068 console.error(`getMaxStreamVolume Fail: ${err}`); 7069 } else { 7070 console.info(`getMaxStreamVolume Success! ${maxVolume}`); 7071 } 7072}); 7073``` 7074### getMaxStreamVolume<sup>10+</sup> 7075 7076getMaxStreamVolume(): Promise<number> 7077 7078获取应用基于音频流的最大音量。使用Promise异步回调。 7079 7080**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7081 7082**返回值:** 7083 7084| 类型 | 说明 | 7085| ------------------- | ----------------------------- | 7086| Promise<number>| Promise对象,返回音频流最大音量(音量范围0-1)。| 7087 7088**示例:** 7089 7090```ts 7091import { BusinessError } from '@kit.BasicServicesKit'; 7092 7093audioRenderer.getMaxStreamVolume().then((value: number) => { 7094 console.info(`Get max stream volume Success! ${value}`); 7095}).catch((err: BusinessError) => { 7096 console.error(`Get max stream volume Fail: ${err}`); 7097}); 7098``` 7099 7100### getMaxStreamVolumeSync<sup>10+</sup> 7101 7102getMaxStreamVolumeSync(): number 7103 7104获取应用基于音频流的最大音量,同步返回结果。 7105 7106**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7107 7108**返回值:** 7109 7110| 类型 | 说明 | 7111| ------------------- | ----------------------------- | 7112| number| 返回音频流最大音量(音量范围0-1)。| 7113 7114**示例:** 7115 7116```ts 7117import { BusinessError } from '@kit.BasicServicesKit'; 7118 7119try { 7120 let value: number = audioRenderer.getMaxStreamVolumeSync(); 7121 console.info(`Get max stream volume Success! ${value}`); 7122} catch (err) { 7123 let error = err as BusinessError; 7124 console.error(`Get max stream volume Fail: ${error}`); 7125} 7126``` 7127 7128### getUnderflowCount<sup>10+</sup> 7129 7130getUnderflowCount(callback: AsyncCallback<number>): void 7131 7132获取当前播放音频流的欠载音频帧数量。使用Callback回调返回。 7133 7134**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7135 7136**参数:** 7137 7138| 参数名 | 类型 | 必填 | 说明 | 7139| ------- | -----------| ------ | ------------------- | 7140|callback | AsyncCallback<number> | 是 |回调函数。当获取当前播放音频流的欠载音频帧数量成功,err为undefined,data为获取到的当前播放音频流的欠载音频帧数量;否则为错误对象。| 7141 7142**示例:** 7143 7144```ts 7145import { BusinessError } from '@kit.BasicServicesKit'; 7146 7147audioRenderer.getUnderflowCount((err: BusinessError, underflowCount: number) => { 7148 if (err) { 7149 console.error(`getUnderflowCount Fail: ${err}`); 7150 } else { 7151 console.info(`getUnderflowCount Success! ${underflowCount}`); 7152 } 7153}); 7154``` 7155### getUnderflowCount<sup>10+</sup> 7156 7157getUnderflowCount(): Promise<number> 7158 7159获取当前播放音频流的欠载音频帧数量。使用Promise异步回调。 7160 7161**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7162 7163**返回值:** 7164 7165| 类型 | 说明 | 7166| ------------------- | ----------------------------- | 7167| Promise<number>| Promise对象,返回音频流的欠载音频帧数量。| 7168 7169**示例:** 7170 7171```ts 7172import { BusinessError } from '@kit.BasicServicesKit'; 7173 7174audioRenderer.getUnderflowCount().then((value: number) => { 7175 console.info(`Get underflow count Success! ${value}`); 7176}).catch((err: BusinessError) => { 7177 console.error(`Get underflow count Fail: ${err}`); 7178}); 7179``` 7180 7181### getUnderflowCountSync<sup>10+</sup> 7182 7183getUnderflowCountSync(): number 7184 7185获取当前播放音频流的欠载音频帧数量,同步返回数据。 7186 7187**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7188 7189**返回值:** 7190 7191| 类型 | 说明 | 7192| ------------------- | ----------------------------- | 7193| number| 返回音频流的欠载音频帧数量。| 7194 7195**示例:** 7196 7197```ts 7198import { BusinessError } from '@kit.BasicServicesKit'; 7199 7200try { 7201 let value: number = audioRenderer.getUnderflowCountSync(); 7202 console.info(`Get underflow count Success! ${value}`); 7203} catch (err) { 7204 let error = err as BusinessError; 7205 console.error(`Get underflow count Fail: ${error}`); 7206} 7207``` 7208 7209### getCurrentOutputDevices<sup>10+</sup> 7210 7211getCurrentOutputDevices(callback: AsyncCallback<AudioDeviceDescriptors>): void 7212 7213获取音频流输出设备描述符。使用Callback回调返回。 7214 7215**系统能力:** SystemCapability.Multimedia.Audio.Device 7216 7217**参数:** 7218 7219| 参数名 | 类型 | 必填 | 说明 | 7220| ------- | -----------| ------ | ------------------- | 7221|callback | AsyncCallback\<[AudioDeviceDescriptors](#audiodevicedescriptors)>| 是 |回调函数。当获取音频流输出设备描述符成功,err为undefined,data为获取到的音频流输出设备描述符;否则为错误对象。| 7222 7223**示例:** 7224 7225```ts 7226import { BusinessError } from '@kit.BasicServicesKit'; 7227 7228audioRenderer.getCurrentOutputDevices((err: BusinessError, deviceInfo: audio.AudioDeviceDescriptors) => { 7229 if (err) { 7230 console.error(`getCurrentOutputDevices Fail: ${err}`); 7231 } else { 7232 for (let i = 0; i < deviceInfo.length; i++) { 7233 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7234 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7235 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7236 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7237 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7238 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7239 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7240 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7241 } 7242 } 7243}); 7244``` 7245### getCurrentOutputDevices<sup>10+</sup> 7246 7247getCurrentOutputDevices(): Promise<AudioDeviceDescriptors> 7248 7249获取音频流输出设备描述符。使用Promise异步回调。 7250 7251**系统能力:** SystemCapability.Multimedia.Audio.Device 7252 7253**返回值:** 7254 7255| 类型 | 说明 | 7256| ------------------- | ----------------------------- | 7257| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)>| Promise对象,返回音频流的输出设备描述信息 | 7258 7259**示例:** 7260 7261```ts 7262import { BusinessError } from '@kit.BasicServicesKit'; 7263 7264audioRenderer.getCurrentOutputDevices().then((deviceInfo: audio.AudioDeviceDescriptors) => { 7265 for (let i = 0; i < deviceInfo.length; i++) { 7266 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7267 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7268 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7269 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7270 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7271 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7272 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7273 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7274 } 7275}).catch((err: BusinessError) => { 7276 console.error(`Get current output devices Fail: ${err}`); 7277}); 7278``` 7279 7280### getCurrentOutputDevicesSync<sup>10+</sup> 7281 7282getCurrentOutputDevicesSync(): AudioDeviceDescriptors 7283 7284获取音频流输出设备描述符,同步返回结果。 7285 7286**系统能力:** SystemCapability.Multimedia.Audio.Device 7287 7288**返回值:** 7289 7290| 类型 | 说明 | 7291| ------------------- | ----------------------------- | 7292| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回音频流的输出设备描述信息 | 7293 7294**示例:** 7295 7296```ts 7297import { BusinessError } from '@kit.BasicServicesKit'; 7298 7299try { 7300 let deviceInfo: audio.AudioDeviceDescriptors = audioRenderer.getCurrentOutputDevicesSync(); 7301 for (let i = 0; i < deviceInfo.length; i++) { 7302 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7303 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7304 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7305 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7306 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7307 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7308 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7309 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7310 } 7311} catch (err) { 7312 let error = err as BusinessError; 7313 console.error(`Get current output devices Fail: ${error}`); 7314} 7315``` 7316### setChannelBlendMode<sup>11+</sup> 7317 7318setChannelBlendMode(mode: ChannelBlendMode): void 7319 7320设置单双声道混合模式。使用同步方式返回结果。 7321 7322**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7323 7324**参数:** 7325 7326| 参数名 | 类型 | 必填 | 说明 | 7327| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 7328| mode | [ChannelBlendMode](#channelblendmode11) | 是 | 声道混合模式类型。 | 7329 7330**错误码:** 7331 7332以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7333 7334| 错误码ID | 错误信息 | 7335| ------- | --------------------------------------------| 7336| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7337| 6800101 | Parameter verification failed. | 7338| 6800103 | Operation not permit at current state. | 7339 7340**示例:** 7341 7342```ts 7343let mode = audio.ChannelBlendMode.MODE_DEFAULT; 7344 7345audioRenderer.setChannelBlendMode(mode); 7346console.info(`BlendMode: ${mode}`); 7347``` 7348### setVolumeWithRamp<sup>11+</sup> 7349 7350setVolumeWithRamp(volume: number, duration: number): void 7351 7352设置音量渐变模式。使用同步方式返回结果。 7353 7354**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7355 7356**参数:** 7357 7358| 参数名 | 类型 | 必填 | 说明 | 7359| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 7360| volume | number | 是 | 渐变目标音量值,音量范围为[0.0, 1.0]。 | 7361| duration | number | 是 | 渐变持续时间,单位为ms。 | 7362 7363**错误码:** 7364 7365以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7366 7367| 错误码ID | 错误信息 | 7368| ------- | --------------------------------------------| 7369| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7370| 6800101 | Parameter verification failed. | 7371 7372**示例:** 7373 7374```ts 7375let volume = 0.5; 7376let duration = 1000; 7377 7378audioRenderer.setVolumeWithRamp(volume, duration); 7379console.info(`setVolumeWithRamp: ${volume}`); 7380``` 7381 7382### setSilentModeAndMixWithOthers<sup>12+</sup> 7383 7384setSilentModeAndMixWithOthers(on: boolean): void 7385 7386设置静音并发播放模式。 7387 7388当设置为true,打开静音并发播放模式,系统将让此音频流静音播放,并且不会打断其它音频流。设置为false,将关闭静音并发播放,音频流可根据系统焦点策略抢占焦点。 7389 7390**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7391 7392**参数:** 7393 7394| 参数名 | 类型 | 必填 | 说明 | 7395| ------ | ---------------------------------------- | ---- |----------------------| 7396| on | boolean | 是 | 打开/关闭静音并发播放模式,true打开,false关闭。 | 7397 7398**示例:** 7399 7400```ts 7401audioRenderer.setSilentModeAndMixWithOthers(true); 7402``` 7403 7404### getSilentModeAndMixWithOthers<sup>12+</sup> 7405 7406getSilentModeAndMixWithOthers(): boolean 7407 7408获取静音并发播放模式。 7409 7410**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7411 7412**返回值:** 7413 7414| 类型 | 说明 | 7415| ------------------------------------------------- |-----------| 7416| boolean | 返回静音并发播放模式状态,true打开,false关闭。 | 7417 7418**示例:** 7419 7420```ts 7421let on = audioRenderer.getSilentModeAndMixWithOthers(); 7422``` 7423 7424### setDefaultOutputDevice<sup>12+</sup> 7425 7426setDefaultOutputDevice(deviceType: DeviceType): Promise<void> 7427 7428设置默认本机内置发声设备。使用Promise方式异步返回结果。 7429 7430> **说明:** 7431> 7432> - 本接口仅适用于[音频流类型](#streamusage)为语音消息、VoIP语音通话或者VoIP视频通话的场景使用,可选的设备类型为听筒、扬声器和系统默认设备。 7433> 7434> - 本接口允许在AudioRenderer创建以后的任何时间被调用,系统会记录应用设置的默认本机内置发声设备。在应用启动播放时,若有外接设备如蓝牙耳机/有线耳机接入,系统优先从外接设备发声;否则系统遵循应用设置的默认本机内置发声设备发声。 7435> 7436> - 本接口优先级低于[AVCastPicker](../apis-avsession-kit/ohos-multimedia-avcastpicker.md#avcastpicker)。如果使用AVCastPicker切换过发声设备,再调用本接口切换设备不生效。 7437 7438**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7439 7440**参数:** 7441 7442| 参数名 | 类型 | 必填 | 说明 | 7443| ---------- |----------------| ------ |---------------------------------------------------------| 7444| deviceType | [DeviceType](#devicetype) | 是 | 设备类型。<br>只支持:EARPIECE(听筒)、SPEAKER(扬声器)和DEFAULT(系统默认设备)。 | 7445 7446**返回值:** 7447 7448| 类型 | 说明 | 7449| ------------------- | ----------------------------- | 7450| Promise<void> | Promise对象,无返回结果。 | 7451 7452**错误码:** 7453 7454以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7455 7456| 错误码ID | 错误信息 | 7457| ------- | --------------------------------------------| 7458| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7459| 6800101 | Parameter verification failed. | 7460| 6800103 | Operation not permit at current state. | 7461 7462**示例:** 7463 7464```ts 7465import { BusinessError } from '@kit.BasicServicesKit'; 7466 7467// 本接口允许在AudioRenderer创建以后的任何时间被调用。 7468// 未播放时调用,系统会记录应用设置的默认本机内置发声设备,当应用启动播放时从设置的默认本机内置发声设备发声。 7469// 正在播放时调用,在没有外接设备如蓝牙耳机/有线耳机,系统会立即切换到设置的默认本机内置发声设备发声;否则系统会先记录应用设置的默认本机内置发声设备,等外接设备移除后再切换到设置的默认本机内置发声设备发声。 7470audioRenderer.setDefaultOutputDevice(audio.DeviceType.SPEAKER).then(() => { 7471 console.info('setDefaultOutputDevice Success!'); 7472}).catch((err: BusinessError) => { 7473 console.error(`setDefaultOutputDevice Fail: ${err}`); 7474}); 7475``` 7476 7477### on('audioInterrupt')<sup>9+</sup> 7478 7479on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 7480 7481监听音频中断事件(当音频焦点发生变化时触发),使用callback方式返回结果。 7482 7483AudioRenderer对象在start事件发生时会主动获取焦点,在pause、stop等事件发生时会主动释放焦点,不需要开发者主动发起获取焦点或释放焦点的申请。 7484 7485调用此方法,在AudioRenderer对象获取焦点失败或发生中断事件(如被其他音频打断等)时,会收到[InterruptEvent](#interruptevent9)。建议应用可根据InterruptEvent的信息完成进一步处理,更多信息可参考文档[处理音频焦点事件](../../media/audio/audio-playback-concurrency.md)。 7486 7487**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 7488 7489**参数:** 7490 7491| 参数名 | 类型 | 必填 | 说明 | 7492| -------- | -------------------------------------------- | ---- | ----------------------------------------------------------- | 7493| type | string | 是 | 监听事件,固定为:'audioInterrupt'。 | 7494| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回播放中断时,应用接收的中断事件信息。 | 7495 7496**错误码:** 7497 7498以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7499 7500| 错误码ID | 错误信息 | 7501| ------- | --------------------------------------------| 7502| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7503| 6800101 | Parameter verification failed. | 7504 7505**示例:** 7506 7507```ts 7508import { audio } from '@kit.AudioKit'; 7509 7510let isPlaying: boolean; // 标识符,表示是否正在渲染。 7511let isDucked: boolean; // 标识符,表示是否被降低音量。 7512onAudioInterrupt(); 7513 7514async function onAudioInterrupt(){ 7515 audioRenderer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 7516 // 在发生音频打断事件时,audioRenderer收到interruptEvent回调,此处根据其内容做相应处理。 7517 // 1、可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 7518 // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 7519 // 2、必选:读取interruptEvent.hintType的类型,做出相应的处理。 7520 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 7521 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 7522 switch (interruptEvent.hintType) { 7523 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 7524 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 7525 console.info('Force paused. Update playing status and stop writing'); 7526 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 7527 break; 7528 case audio.InterruptHint.INTERRUPT_HINT_STOP: 7529 // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发。 7530 console.info('Force stopped. Update playing status and stop writing'); 7531 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 7532 break; 7533 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 7534 // 音频流已被降低音量渲染。 7535 console.info('Force ducked. Update volume status'); 7536 isDucked = true; // 简化处理,代表应用更新音量状态的若干操作。 7537 break; 7538 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 7539 // 音频流已被恢复正常音量渲染。 7540 console.info('Force ducked. Update volume status'); 7541 isDucked = false; // 简化处理,代表应用更新音量状态的若干操作。 7542 break; 7543 default: 7544 console.info('Invalid interruptEvent'); 7545 break; 7546 } 7547 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 7548 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 7549 switch (interruptEvent.hintType) { 7550 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 7551 // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染)。 7552 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 7553 console.info('Resume force paused renderer or ignore'); 7554 // 若选择继续渲染,需在此处主动执行开始渲染的若干操作。 7555 break; 7556 default: 7557 console.info('Invalid interruptEvent'); 7558 break; 7559 } 7560 } 7561 }); 7562} 7563``` 7564 7565### on('markReach')<sup>8+</sup> 7566 7567on(type: 'markReach', frame: number, callback: Callback<number>): void 7568 7569监听到达标记事件(当渲染的帧数到达frame参数的值时触发,仅调用一次),使用callback方式返回结果。 7570 7571举例说明,如果frame设置为100,当渲染帧数到达第100帧时,将上报信息。 7572 7573**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7574 7575**参数:** 7576 7577| 参数名 | 类型 | 必填 | 说明 | 7578| :------- | :----------------------- | :--- | :---------------------------------------- | 7579| type | string | 是 | 监听事件,固定为:'markReach'。 | 7580| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 7581| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 7582 7583**示例:** 7584 7585```ts 7586audioRenderer.on('markReach', 1000, (position: number) => { 7587 if (position == 1000) { 7588 console.info('ON Triggered successfully'); 7589 } 7590}); 7591``` 7592 7593 7594### off('markReach')<sup>8+</sup> 7595 7596off(type: 'markReach'): void 7597 7598取消监听到达标记事件。 7599 7600**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7601 7602**参数:** 7603 7604| 参数名 | 类型 | 必填 | 说明 | 7605| :----- | :----- | :--- | :------------------------------------------------ | 7606| type | string | 是 | 监听事件,固定为:'markReach'。 | 7607 7608**示例:** 7609 7610```ts 7611audioRenderer.off('markReach'); 7612``` 7613 7614### on('periodReach')<sup>8+</sup> 7615 7616on(type: 'periodReach', frame: number, callback: Callback<number>): void 7617 7618监听到达标记事件(每当渲染的帧数达到frame参数的值时触发,即按周期上报信息),使用callback方式返回结果。 7619 7620举例说明,如果frame设置为10,每当渲染10帧数据时将上报信息,例如在第10帧、20帧、30帧,均会上报信息。 7621 7622**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7623 7624**参数:** 7625 7626| 参数名 | 类型 | 必填 | 说明 | 7627| :------- | :----------------------- | :--- | :------------------------------------------ | 7628| type | string | 是 | 监听事件,固定为:'periodReach'。 | 7629| frame | number | 是 | 触发事件的帧数。该值必须大于 0。 | 7630| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 7631 7632**示例:** 7633 7634```ts 7635audioRenderer.on('periodReach', 1000, (position: number) => { 7636 if (position == 1000) { 7637 console.info('ON Triggered successfully'); 7638 } 7639}); 7640``` 7641 7642### off('periodReach')<sup>8+</sup> 7643 7644off(type: 'periodReach'): void 7645 7646取消监听到达标记事件。 7647 7648**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7649 7650**参数:** 7651 7652| 参数名 | 类型 | 必填 | 说明 | 7653| :----- | :----- | :--- | :-------------------------------------------------- | 7654| type | string | 是 | 监听事件,固定为:'periodReach'。 | 7655 7656**示例:** 7657 7658```ts 7659audioRenderer.off('periodReach'); 7660``` 7661 7662### on('stateChange')<sup>8+</sup> 7663 7664on(type: 'stateChange', callback: Callback<AudioState\>): void 7665 7666监听状态变化事件(当AudioRenderer的状态发生变化时触发),使用callback方式返回结果。 7667 7668**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7669 7670**参数:** 7671 7672| 参数名 | 类型 | 必填 | 说明 | 7673| :------- | :------------------------- | :--- | :------------------------------------------ | 7674| type | string | 是 | 监听事件,固定为:'stateChange'。 | 7675| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 7676 7677**示例:** 7678 7679```ts 7680audioRenderer.on('stateChange', (state: audio.AudioState) => { 7681 if (state == 1) { 7682 console.info('audio renderer state is: STATE_PREPARED'); 7683 } 7684 if (state == 2) { 7685 console.info('audio renderer state is: STATE_RUNNING'); 7686 } 7687}); 7688``` 7689 7690### on('outputDeviceChange')<sup>10+</sup> 7691 7692on(type: 'outputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 7693 7694监听音频输出设备变化事件(当音频输出设备发生变化时触发),使用callback方式返回结果。 7695 7696**系统能力:** SystemCapability.Multimedia.Audio.Device 7697 7698**参数:** 7699 7700| 参数名 | 类型 | 必填 | 说明 | 7701| :------- | :------------------------- | :--- | :------------------------------------------ | 7702| type | string | 是 | 监听事件,固定为:'outputDeviceChange'。 | 7703| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数,返回当前音频流的输出设备描述信息。 | 7704 7705**错误码:** 7706 7707以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7708 7709| 错误码ID | 错误信息 | 7710| ------- | --------------------------------------------| 7711| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7712| 6800101 | Parameter verification failed. | 7713 7714**示例:** 7715 7716```ts 7717audioRenderer.on('outputDeviceChange', (deviceInfo: audio.AudioDeviceDescriptors) => { 7718 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 7719 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 7720 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 7721}); 7722``` 7723 7724### off('outputDeviceChange')<sup>10+</sup> 7725 7726off(type: 'outputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 7727 7728取消监听音频输出设备变化事件,使用callback方式返回结果。 7729 7730**系统能力:** SystemCapability.Multimedia.Audio.Device 7731 7732**参数:** 7733 7734| 参数名 | 类型 | 必填 | 说明 | 7735| :------- | :------------------------- | :--- | :------------------------------------------ | 7736| type | string | 是 | 监听事件,固定为:'outputDeviceChange'。 | 7737| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回当前音频流的输出设备描述信息。 | 7738 7739**错误码:** 7740 7741以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7742 7743| 错误码ID | 错误信息 | 7744| ------- | --------------------------------------------| 7745| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7746| 6800101 | Parameter verification failed. | 7747 7748**示例:** 7749 7750```ts 7751// 取消该事件的所有监听。 7752audioRenderer.off('outputDeviceChange'); 7753 7754// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 7755let outputDeviceChangeCallback = (deviceInfo: audio.AudioDeviceDescriptors) => { 7756 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 7757 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 7758 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 7759}; 7760 7761audioRenderer.on('outputDeviceChange', outputDeviceChangeCallback); 7762 7763audioRenderer.off('outputDeviceChange', outputDeviceChangeCallback); 7764``` 7765 7766### on('outputDeviceChangeWithInfo')<sup>11+</sup> 7767 7768on(type: 'outputDeviceChangeWithInfo', callback: Callback\<AudioStreamDeviceChangeInfo>): void 7769 7770监听音频流输出设备变化及原因事件(当音频输出设备发生变化时触发),使用callback方式返回结果。 7771 7772**系统能力:** SystemCapability.Multimedia.Audio.Device 7773 7774**参数:** 7775 7776| 参数名 | 类型 | 必填 | 说明 | 7777| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 7778| type | string | 是 | 监听事件,固定为:'outputDeviceChangeWithInfo'。 | 7779| callback | Callback\<[AudioStreamDeviceChangeInfo](#audiostreamdevicechangeinfo11)> | 是 | 回调函数,返回当前音频流的输出设备描述信息及变化原因。 | 7780 7781**错误码:** 7782 7783以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7784 7785| 错误码ID | 错误信息 | 7786| ------- | --------------------------------------------| 7787| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7788| 6800101 | Parameter verification failed. | 7789 7790**示例:** 7791 7792```ts 7793audioRenderer.on('outputDeviceChangeWithInfo', (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 7794 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 7795 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 7796 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 7797 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 7798}); 7799``` 7800 7801### off('outputDeviceChangeWithInfo')<sup>11+</sup> 7802 7803off(type: 'outputDeviceChangeWithInfo', callback?: Callback\<AudioStreamDeviceChangeInfo>): void 7804 7805取消监听音频流输出设备变化及原因事件,使用callback方式返回结果。 7806 7807**系统能力:** SystemCapability.Multimedia.Audio.Device 7808 7809**参数:** 7810 7811| 参数名 | 类型 | 必填 | 说明 | 7812| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 7813| type | string | 是 | 监听事件,固定为:'outputDeviceChangeWithInfo'。 | 7814| callback | Callback\<[AudioStreamDeviceChangeInfo](#audiostreamdevicechangeinfo11)> | 否 | 回调函数,返回当前音频流的输出设备描述信息及变化原因。 | 7815 7816**错误码:** 7817 7818以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7819 7820| 错误码ID | 错误信息 | 7821| ------- | --------------------------------------------| 7822| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7823| 6800101 | Parameter verification failed. | 7824 7825**示例:** 7826 7827```ts 7828// 取消该事件的所有监听。 7829audioRenderer.off('outputDeviceChangeWithInfo'); 7830 7831// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 7832let outputDeviceChangeWithInfoCallback = (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 7833 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 7834 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 7835 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 7836 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 7837}; 7838 7839audioRenderer.on('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 7840 7841audioRenderer.off('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 7842``` 7843 7844### on('writeData')<sup>11+</sup> 7845 7846on(type: 'writeData', callback: AudioRendererWriteDataCallback): void 7847 7848监听音频数据写入回调事件(当需要写入音频数据时触发),使用 callback 方式返回结果。 7849 7850回调函数仅用来写入音频数据,请勿在回调函数中调用AudioRenderer相关接口。 7851 7852**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7853 7854**参数:** 7855 7856| 参数名 | 类型 | 必填 | 说明 | 7857| :------- |:--------------------------------| :--- |:--------------------------------------| 7858| type | string | 是 | 监听事件,固定为:'writeData'。 | 7859| callback | [AudioRendererWriteDataCallback](#audiorendererwritedatacallback12) | 是 | 回调函数,入参代表应用接收待写入的数据缓冲区。<br>API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果[AudioDataCallbackResult](#audiodatacallbackresult12)。 | 7860 7861**错误码:** 7862 7863以下错误码的详细介绍请参见 [Audio错误码](errorcode-audio.md)。 7864 7865| 错误码ID | 错误信息 | 7866| ------- | --------------------------------------------| 7867| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7868| 6800101 | Parameter verification failed. | 7869 7870**示例:** 7871 7872```ts 7873import { BusinessError } from '@kit.BasicServicesKit'; 7874import {fileIo as fs} from '@kit.CoreFileKit'; 7875 7876class Options { 7877 offset?: number; 7878 length?: number; 7879} 7880 7881let bufferSize: number = 0; 7882let path = getContext().cacheDir; 7883// 确保该沙箱路径下存在该资源。 7884let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; 7885let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 7886let writeDataCallback = (buffer: ArrayBuffer) => { 7887 let options: Options = { 7888 offset: bufferSize, 7889 length: buffer.byteLength 7890 }; 7891 7892 try { 7893 fs.readSync(file.fd, buffer, options); 7894 bufferSize += buffer.byteLength; 7895 // API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果。 7896 return audio.AudioDataCallbackResult.VALID; 7897 } catch (error) { 7898 console.error('Error reading file:', error); 7899 // API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果。 7900 return audio.AudioDataCallbackResult.INVALID; 7901 } 7902}; 7903 7904audioRenderer.on('writeData', writeDataCallback); 7905audioRenderer.start().then(() => { 7906 console.info('Renderer started'); 7907}).catch((err: BusinessError) => { 7908 console.error(`ERROR: ${err}`); 7909}); 7910``` 7911 7912### off('writeData')<sup>11+</sup> 7913 7914off(type: 'writeData', callback?: AudioRendererWriteDataCallback): void 7915 7916取消监听音频数据写入回调事件,使用 callback 方式返回结果。 7917 7918**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7919 7920**参数:** 7921 7922| 参数名 | 类型 | 必填 | 说明 | 7923| :------- |:--------------------------------| :--- |:--------------------------------------| 7924| type | string | 是 | 监听事件,固定为:'writeData'。 | 7925| callback | [AudioRendererWriteDataCallback](#audiorendererwritedatacallback12) | 否 | 回调函数,入参代表应用接收待写入的数据缓冲区。<br>API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果[AudioDataCallbackResult](#audiodatacallbackresult12)。 | 7926 7927**错误码:** 7928 7929以下错误码的详细介绍请参见 [Audio错误码](errorcode-audio.md)。 7930 7931| 错误码ID | 错误信息 | 7932| ------- | --------------------------------------------| 7933| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7934| 6800101 | Parameter verification failed. | 7935 7936**示例:** 7937 7938```ts 7939// 取消该事件的所有监听。 7940audioRenderer.off('writeData'); 7941 7942// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 7943let writeDataCallback = (data: ArrayBuffer) => { 7944 console.info(`write data: ${data}`); 7945}; 7946 7947audioRenderer.on('writeData', writeDataCallback); 7948 7949audioRenderer.off('writeData', writeDataCallback); 7950``` 7951 7952## AudioCapturer<sup>8+</sup> 7953 7954提供音频采集的相关接口。在调用AudioCapturer的接口前,需要先通过[createAudioCapturer](#audiocreateaudiocapturer8)创建实例。 7955 7956### 属性 7957 7958**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7959 7960| 名称 | 类型 | 可读 | 可写 | 说明 | 7961| :---- | :------------------------- | :--- | :--- | :--------------- | 7962| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频采集器状态。 | 7963 7964**示例:** 7965 7966```ts 7967import { audio } from '@kit.AudioKit'; 7968 7969let state: audio.AudioState = audioCapturer.state; 7970``` 7971 7972### getCapturerInfo<sup>8+</sup> 7973 7974getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void 7975 7976获取采集器信息。使用callback方式异步返回结果。 7977 7978**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7979 7980**参数:** 7981 7982| 参数名 | 类型 | 必填 | 说明 | 7983| :------- | :-------------------------------- | :--- | :----------------------------------- | 7984| callback | AsyncCallback<[AudioCapturerInfo](#audiocapturerinfo8)\> | 是 | 回调函数。当获取采集器信息成功,err为undefined,data为获取到的采集器信息;否则为错误对象。 | 7985 7986**示例:** 7987 7988```ts 7989import { BusinessError } from '@kit.BasicServicesKit'; 7990 7991audioCapturer.getCapturerInfo((err: BusinessError, capturerInfo: audio.AudioCapturerInfo) => { 7992 if (err) { 7993 console.error('Failed to get capture info'); 7994 } else { 7995 console.info('Capturer getCapturerInfo:'); 7996 console.info(`Capturer source: ${capturerInfo.source}`); 7997 console.info(`Capturer flags: ${capturerInfo.capturerFlags}`); 7998 } 7999}); 8000``` 8001 8002 8003### getCapturerInfo<sup>8+</sup> 8004 8005getCapturerInfo(): Promise<AudioCapturerInfo\> 8006 8007获取采集器信息。使用Promise方式异步返回结果。 8008 8009**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8010 8011**返回值:** 8012 8013| 类型 | 说明 | 8014| :------------------------------------------------ | :---------------------------------- | 8015| Promise<[AudioCapturerInfo](#audiocapturerinfo8)\> | Promise对象,返回采集器信息。 | 8016 8017**示例:** 8018 8019```ts 8020import { BusinessError } from '@kit.BasicServicesKit'; 8021 8022audioCapturer.getCapturerInfo().then((audioParamsGet: audio.AudioCapturerInfo) => { 8023 if (audioParamsGet != undefined) { 8024 console.info('AudioFrameworkRecLog: Capturer CapturerInfo:'); 8025 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 8026 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 8027 } else { 8028 console.info(`AudioFrameworkRecLog: audioParamsGet is : ${audioParamsGet}`); 8029 console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect'); 8030 } 8031}).catch((err: BusinessError) => { 8032 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${err}`); 8033}) 8034``` 8035 8036### getCapturerInfoSync<sup>10+</sup> 8037 8038getCapturerInfoSync(): AudioCapturerInfo 8039 8040获取采集器信息,同步返回结果。 8041 8042**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8043 8044**返回值:** 8045 8046| 类型 | 说明 | 8047| :------------------------------------------------ | :---------------------------------- | 8048| [AudioCapturerInfo](#audiocapturerinfo8) | 返回采集器信息。 | 8049 8050**示例:** 8051 8052```ts 8053import { BusinessError } from '@kit.BasicServicesKit'; 8054 8055try { 8056 let audioParamsGet: audio.AudioCapturerInfo = audioCapturer.getCapturerInfoSync(); 8057 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 8058 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 8059} catch (err) { 8060 let error = err as BusinessError; 8061 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${error}`); 8062} 8063``` 8064 8065### getStreamInfo<sup>8+</sup> 8066 8067getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 8068 8069获取采集器流信息。使用callback方式异步返回结果。 8070 8071**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8072 8073**参数:** 8074 8075| 参数名 | 类型 | 必填 | 说明 | 8076| :------- | :--------------------------------------------------- | :--- | :------------------------------- | 8077| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取采集器流信息成功,err为undefined,data为获取到的采集器流信息;否则为错误对象。 | 8078 8079**示例:** 8080 8081```ts 8082import { BusinessError } from '@kit.BasicServicesKit'; 8083 8084audioCapturer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 8085 if (err) { 8086 console.error('Failed to get stream info'); 8087 } else { 8088 console.info('Capturer GetStreamInfo:'); 8089 console.info(`Capturer sampling rate: ${streamInfo.samplingRate}`); 8090 console.info(`Capturer channel: ${streamInfo.channels}`); 8091 console.info(`Capturer format: ${streamInfo.sampleFormat}`); 8092 console.info(`Capturer encoding type: ${streamInfo.encodingType}`); 8093 } 8094}); 8095``` 8096 8097### getStreamInfo<sup>8+</sup> 8098 8099getStreamInfo(): Promise<AudioStreamInfo\> 8100 8101获取采集器流信息。使用Promise方式异步返回结果。 8102 8103**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8104 8105**返回值:** 8106 8107| 类型 | 说明 | 8108| :--------------------------------------------- | :------------------------------ | 8109| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回流信息。 | 8110 8111**示例:** 8112 8113```ts 8114import { BusinessError } from '@kit.BasicServicesKit'; 8115 8116audioCapturer.getStreamInfo().then((audioParamsGet: audio.AudioStreamInfo) => { 8117 console.info('getStreamInfo:'); 8118 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 8119 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 8120 console.info(`channels: ${audioParamsGet.channels}`); 8121 console.info(`encodingType: ${audioParamsGet.encodingType}`); 8122}).catch((err: BusinessError) => { 8123 console.error(`getStreamInfo :ERROR: ${err}`); 8124}); 8125``` 8126 8127### getStreamInfoSync<sup>10+</sup> 8128 8129getStreamInfoSync(): AudioStreamInfo 8130 8131获取采集器流信息,同步返回结果。 8132 8133**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8134 8135**返回值:** 8136 8137| 类型 | 说明 | 8138| :--------------------------------------------- | :------------------------------ | 8139| [AudioStreamInfo](#audiostreaminfo8) | 返回流信息。 | 8140 8141**示例:** 8142 8143```ts 8144import { BusinessError } from '@kit.BasicServicesKit'; 8145 8146try { 8147 let audioParamsGet: audio.AudioStreamInfo = audioCapturer.getStreamInfoSync(); 8148 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 8149 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 8150 console.info(`channels: ${audioParamsGet.channels}`); 8151 console.info(`encodingType: ${audioParamsGet.encodingType}`); 8152} catch (err) { 8153 let error = err as BusinessError; 8154 console.error(`getStreamInfo :ERROR: ${error}`); 8155} 8156``` 8157 8158### getAudioStreamId<sup>9+</sup> 8159 8160getAudioStreamId(callback: AsyncCallback<number\>): void 8161 8162获取音频流id,使用callback方式异步返回结果。 8163 8164**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8165 8166**参数:** 8167 8168| 参数名 | 类型 | 必填 | 说明 | 8169| :------- | :--------------------------------------------------- | :--- | :------------------- | 8170| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 8171 8172**示例:** 8173 8174```ts 8175import { BusinessError } from '@kit.BasicServicesKit'; 8176 8177audioCapturer.getAudioStreamId((err: BusinessError, streamId: number) => { 8178 console.info(`audioCapturer GetStreamId: ${streamId}`); 8179}); 8180``` 8181 8182### getAudioStreamId<sup>9+</sup> 8183 8184getAudioStreamId(): Promise<number\> 8185 8186获取音频流id,使用Promise方式异步返回结果。 8187 8188**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8189 8190**返回值:** 8191 8192| 类型 | 说明 | 8193| :----------------| :--------------------- | 8194| Promise<number\> | Promise对象,返回音频流id。 | 8195 8196**示例:** 8197 8198```ts 8199import { BusinessError } from '@kit.BasicServicesKit'; 8200 8201audioCapturer.getAudioStreamId().then((streamId: number) => { 8202 console.info(`audioCapturer getAudioStreamId: ${streamId}`); 8203}).catch((err: BusinessError) => { 8204 console.error(`ERROR: ${err}`); 8205}); 8206``` 8207 8208### getAudioStreamIdSync<sup>10+</sup> 8209 8210getAudioStreamIdSync(): number 8211 8212获取音频流id,同步返回结果。 8213 8214**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8215 8216**返回值:** 8217 8218| 类型 | 说明 | 8219| :----------------| :--------------------- | 8220| number | 返回音频流id。 | 8221 8222**示例:** 8223 8224```ts 8225import { BusinessError } from '@kit.BasicServicesKit'; 8226 8227try { 8228 let streamId: number = audioCapturer.getAudioStreamIdSync(); 8229 console.info(`audioCapturer getAudioStreamIdSync: ${streamId}`); 8230} catch (err) { 8231 let error = err as BusinessError; 8232 console.error(`ERROR: ${error}`); 8233} 8234``` 8235 8236### start<sup>8+</sup> 8237 8238start(callback: AsyncCallback<void\>): void 8239 8240启动音频采集器。使用callback方式异步返回结果。 8241 8242**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8243 8244**参数:** 8245 8246| 参数名 | 类型 | 必填 | 说明 | 8247| :------- | :------------------- | :--- | :----------------------------- | 8248| callback | AsyncCallback<void\> | 是 | Callback对象,成功表示启动音频采集器成功,异常将返回error对象:<br>错误码6800301,表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 8249 8250**示例:** 8251 8252```ts 8253import { BusinessError } from '@kit.BasicServicesKit'; 8254 8255audioCapturer.start((err: BusinessError) => { 8256 if (err) { 8257 console.error('Capturer start failed.'); 8258 } else { 8259 console.info('Capturer start success.'); 8260 } 8261}); 8262``` 8263 8264 8265### start<sup>8+</sup> 8266 8267start(): Promise<void\> 8268 8269启动音频采集器。使用Promise方式异步返回结果。 8270 8271**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8272 8273**返回值:** 8274 8275| 类型 | 说明 | 8276| :------------- | :---------------------------- | 8277| Promise<void\> | Promise对象,成功表示启动音频采集器成功,异常将返回error对象:<br>错误码6800301,表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 8278 8279**示例:** 8280 8281```ts 8282import { BusinessError } from '@kit.BasicServicesKit'; 8283 8284audioCapturer.start().then(() => { 8285 console.info('AudioFrameworkRecLog: ---------START---------'); 8286 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 8287 console.info(`AudioFrameworkRecLog: AudioCapturer: STATE: ${audioCapturer.state}`); 8288 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 8289 if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) { 8290 console.info('AudioFrameworkRecLog: AudioCapturer is in Running State'); 8291 } 8292}).catch((err: BusinessError) => { 8293 console.error(`AudioFrameworkRecLog: Capturer start :ERROR : ${err}`); 8294}); 8295``` 8296 8297### stop<sup>8+</sup> 8298 8299stop(callback: AsyncCallback<void\>): void 8300 8301停止采集。使用callback方式异步返回结果。 8302 8303**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8304 8305**参数:** 8306 8307| 参数名 | 类型 | 必填 | 说明 | 8308| :------- | :------------------- | :--- | :----------------------------- | 8309| callback | AsyncCallback<void\> | 是 | 回调函数。当停止采集成功,err为undefined,否则为错误对象。 | 8310 8311**示例:** 8312 8313```ts 8314import { BusinessError } from '@kit.BasicServicesKit'; 8315 8316audioCapturer.stop((err: BusinessError) => { 8317 if (err) { 8318 console.error('Capturer stop failed'); 8319 } else { 8320 console.info('Capturer stopped.'); 8321 } 8322}); 8323``` 8324 8325 8326### stop<sup>8+</sup> 8327 8328stop(): Promise<void\> 8329 8330停止采集。使用Promise方式异步返回结果。 8331 8332**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8333 8334**返回值:** 8335 8336| 类型 | 说明 | 8337| :------------- | :---------------------------- | 8338| Promise<void\> | Promise对象,无返回结果。 | 8339 8340**示例:** 8341 8342```ts 8343import { BusinessError } from '@kit.BasicServicesKit'; 8344 8345audioCapturer.stop().then(() => { 8346 console.info('AudioFrameworkRecLog: ---------STOP RECORD---------'); 8347 console.info('AudioFrameworkRecLog: Capturer stopped: SUCCESS'); 8348 if ((audioCapturer.state == audio.AudioState.STATE_STOPPED)){ 8349 console.info('AudioFrameworkRecLog: State is Stopped:'); 8350 } 8351}).catch((err: BusinessError) => { 8352 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 8353}); 8354``` 8355 8356### release<sup>8+</sup> 8357 8358release(callback: AsyncCallback<void\>): void 8359 8360释放采集器。使用callback方式异步返回结果。 8361 8362**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8363 8364**参数:** 8365 8366| 参数名 | 类型 | 必填 | 说明 | 8367| :------- | :------------------- | :--- | :---------------------------------- | 8368| callback | AsyncCallback<void\> | 是 | 回调函数。当释放采集器成功,err为undefined,否则为错误对象。 | 8369 8370**示例:** 8371 8372```ts 8373import { BusinessError } from '@kit.BasicServicesKit'; 8374 8375audioCapturer.release((err: BusinessError) => { 8376 if (err) { 8377 console.error('capturer release failed'); 8378 } else { 8379 console.info('capturer released.'); 8380 } 8381}); 8382``` 8383 8384 8385### release<sup>8+</sup> 8386 8387release(): Promise<void\> 8388 8389释放采集器。使用Promise方式异步返回结果。 8390 8391**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8392 8393**返回值:** 8394 8395| 类型 | 说明 | 8396| :------------- | :---------------------------- | 8397| Promise<void\> | Promise对象,无返回结果。 | 8398 8399**示例:** 8400 8401```ts 8402import { BusinessError } from '@kit.BasicServicesKit'; 8403 8404audioCapturer.release().then(() => { 8405 console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------'); 8406 console.info('AudioFrameworkRecLog: Capturer release : SUCCESS'); 8407 console.info(`AudioFrameworkRecLog: AudioCapturer : STATE : ${audioCapturer.state}`); 8408}).catch((err: BusinessError) => { 8409 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 8410}); 8411``` 8412 8413### read<sup>8+(deprecated)</sup> 8414 8415read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void 8416 8417读入缓冲区。使用callback方式异步返回结果。 8418 8419> **说明:** 8420> 从 API version 8 开始支持,从 API version 11 开始废弃,建议使用AudioCapturer中的[on('readData')](#onreaddata11)替代。 8421 8422**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8423 8424**参数:** 8425 8426| 参数名 | 类型 | 必填 | 说明 | 8427| :------------- | :-------------------------- | :--- | :------------------------------- | 8428| size | number | 是 | 读入的字节数。 | 8429| isBlockingRead | boolean | 是 | 是否阻塞读操作 ,true阻塞,false不阻塞。 | 8430| callback | AsyncCallback<ArrayBuffer\> | 是 | 回调函数。当读入缓冲区成功,err为undefined,data为获取到的缓冲区;否则为错误对象。 | 8431 8432**示例:** 8433 8434```ts 8435import { BusinessError } from '@kit.BasicServicesKit'; 8436 8437let bufferSize: number = 0; 8438 8439audioCapturer.getBufferSize().then((data: number) => { 8440 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 8441 bufferSize = data; 8442}).catch((err: BusinessError) => { 8443 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR: ${err}`); 8444}); 8445 8446audioCapturer.read(bufferSize, true, (err: BusinessError, buffer: ArrayBuffer) => { 8447 if (!err) { 8448 console.info('Success in reading the buffer data'); 8449 } 8450}); 8451``` 8452 8453### read<sup>8+(deprecated)</sup> 8454 8455read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\> 8456 8457读入缓冲区。使用Promise方式异步返回结果。 8458 8459> **说明:** 8460> 从 API version 8 开始支持,从 API version 11 开始废弃,建议使用AudioCapturer中的[on('readData')](#onreaddata11)替代。 8461 8462**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8463 8464**参数:** 8465 8466| 参数名 | 类型 | 必填 | 说明 | 8467| :------------- | :------ | :--- | :--------------- | 8468| size | number | 是 | 读入的字节数。 | 8469| isBlockingRead | boolean | 是 | 是否阻塞读操作 ,true阻塞,false不阻塞。 | 8470 8471**返回值:** 8472 8473| 类型 | 说明 | 8474| :-------------------- | :----------------------------------------------------- | 8475| Promise<ArrayBuffer\> | Promise对象,返回读取的缓冲区数据。 | 8476 8477**示例:** 8478 8479```ts 8480import { BusinessError } from '@kit.BasicServicesKit'; 8481 8482let bufferSize: number = 0; 8483 8484audioCapturer.getBufferSize().then((data: number) => { 8485 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 8486 bufferSize = data; 8487}).catch((err: BusinessError) => { 8488 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR ${err}`); 8489}); 8490console.info(`Buffer size: ${bufferSize}`); 8491 8492audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 8493 console.info('buffer read successfully'); 8494}).catch((err: BusinessError) => { 8495 console.error(`ERROR : ${err}`); 8496}); 8497``` 8498 8499### getAudioTime<sup>8+</sup> 8500 8501getAudioTime(callback: AsyncCallback<number\>): void 8502 8503获取录制到当前位置时的时间戳(从1970年1月1日开始),单位为纳秒。使用callback方式异步返回结果。 8504 8505**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8506 8507**参数:** 8508 8509| 参数名 | 类型 | 必填 | 说明 | 8510| :------- | :--------------------- | :--- | :----------------------------- | 8511| callback | AsyncCallback<number\> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 8512 8513**示例:** 8514 8515```ts 8516import { BusinessError } from '@kit.BasicServicesKit'; 8517 8518audioCapturer.getAudioTime((err: BusinessError, timestamp: number) => { 8519 console.info(`Current timestamp: ${timestamp}`); 8520}); 8521``` 8522 8523### getAudioTime<sup>8+</sup> 8524 8525getAudioTime(): Promise<number\> 8526 8527获取录制到当前位置时的时间戳(从1970年1月1日开始),单位为纳秒。使用Promise方式异步返回结果。 8528 8529**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8530 8531**返回值:** 8532 8533| 类型 | 说明 | 8534| :--------------- | :---------------------------- | 8535| Promise<number\> | Promise对象,返回时间戳(从1970年1月1日开始),单位为纳秒。 | 8536 8537**示例:** 8538 8539```ts 8540import { BusinessError } from '@kit.BasicServicesKit'; 8541 8542audioCapturer.getAudioTime().then((audioTime: number) => { 8543 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTime : Success ${audioTime}`); 8544}).catch((err: BusinessError) => { 8545 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 8546}); 8547``` 8548 8549### getAudioTimeSync<sup>10+</sup> 8550 8551getAudioTimeSync(): number 8552 8553获取录制到当前位置时的时间戳(从1970年1月1日开始),单位为纳秒。同步返回结果。 8554 8555**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8556 8557**返回值:** 8558 8559| 类型 | 说明 | 8560| :--------------- | :---------------------------- | 8561| number | 返回时间戳。 | 8562 8563**示例:** 8564 8565```ts 8566import { BusinessError } from '@kit.BasicServicesKit'; 8567 8568try { 8569 let audioTime: number = audioCapturer.getAudioTimeSync(); 8570 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : Success ${audioTime}`); 8571} catch (err) { 8572 let error = err as BusinessError; 8573 console.error(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : ERROR : ${error}`); 8574} 8575``` 8576 8577### getBufferSize<sup>8+</sup> 8578 8579getBufferSize(callback: AsyncCallback<number\>): void 8580 8581获取采集器合理的最小缓冲区大小。使用callback方式异步返回结果。 8582 8583**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8584 8585**参数:** 8586 8587| 参数名 | 类型 | 必填 | 说明 | 8588| :------- | :--------------------- | :--- | :----------------------------------- | 8589| callback | AsyncCallback<number\> | 是 | 回调函数。当获取采集器合理的最小缓冲区大小成功,err为undefined,data为获取到的采集器合理的最小缓冲区大小;否则为错误对象。 | 8590 8591**示例:** 8592 8593```ts 8594import { BusinessError } from '@kit.BasicServicesKit'; 8595 8596audioCapturer.getBufferSize((err: BusinessError, bufferSize: number) => { 8597 if (!err) { 8598 console.info(`BufferSize : ${bufferSize}`); 8599 audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 8600 console.info(`Buffer read is ${buffer.byteLength}`); 8601 }).catch((err: BusinessError) => { 8602 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 8603 }); 8604 } 8605}); 8606``` 8607 8608### getBufferSize<sup>8+</sup> 8609 8610getBufferSize(): Promise<number\> 8611 8612获取采集器合理的最小缓冲区大小。使用Promise方式异步返回结果。 8613 8614**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8615 8616**返回值:** 8617 8618| 类型 | 说明 | 8619| :--------------- | :---------------------------------- | 8620| Promise<number\> | Promise对象,返回缓冲区大小。 | 8621 8622**示例:** 8623 8624```ts 8625import { BusinessError } from '@kit.BasicServicesKit'; 8626 8627let bufferSize: number = 0; 8628 8629audioCapturer.getBufferSize().then((data: number) => { 8630 console.info(`AudioFrameworkRecLog: getBufferSize :SUCCESS ${data}`); 8631 bufferSize = data; 8632}).catch((err: BusinessError) => { 8633 console.error(`AudioFrameworkRecLog: getBufferSize :ERROR : ${err}`); 8634}); 8635``` 8636 8637### getBufferSizeSync<sup>10+</sup> 8638 8639getBufferSizeSync(): number 8640 8641获取采集器合理的最小缓冲区大小,同步返回结果。 8642 8643**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8644 8645**返回值:** 8646 8647| 类型 | 说明 | 8648| :--------------- | :---------------------------------- | 8649| number | 返回缓冲区大小。 | 8650 8651**示例:** 8652 8653```ts 8654import { BusinessError } from '@kit.BasicServicesKit'; 8655 8656let bufferSize: number = 0; 8657 8658try { 8659 bufferSize = audioCapturer.getBufferSizeSync(); 8660 console.info(`AudioFrameworkRecLog: getBufferSizeSync :SUCCESS ${bufferSize}`); 8661} catch (err) { 8662 let error = err as BusinessError; 8663 console.error(`AudioFrameworkRecLog: getBufferSizeSync :ERROR : ${error}`); 8664} 8665``` 8666 8667### getCurrentInputDevices<sup>11+</sup> 8668 8669getCurrentInputDevices(): AudioDeviceDescriptors 8670 8671获取录音流输入设备描述符。使用同步方式返回结果。 8672 8673**系统能力:** SystemCapability.Multimedia.Audio.Device 8674 8675**返回值:** 8676 8677| 类型 | 说明 | 8678| ---------------------- | ------------------------------------------------------ | 8679| [AudioDeviceDescriptors](#audiodevicedescriptors) | 同步接口,返回设备属性数组类型数据。 | 8680 8681**示例:** 8682 8683```ts 8684let deviceDescriptors: audio.AudioDeviceDescriptors = audioCapturer.getCurrentInputDevices(); 8685console.info(`Device id: ${deviceDescriptors[0].id}`); 8686console.info(`Device type: ${deviceDescriptors[0].deviceType}`); 8687console.info(`Device role: ${deviceDescriptors[0].deviceRole}`); 8688console.info(`Device name: ${deviceDescriptors[0].name}`); 8689console.info(`Device address: ${deviceDescriptors[0].address}`); 8690console.info(`Device samplerates: ${deviceDescriptors[0].sampleRates[0]}`); 8691console.info(`Device channelcounts: ${deviceDescriptors[0].channelCounts[0]}`); 8692console.info(`Device channelmask: ${deviceDescriptors[0].channelMasks[0]}`); 8693if (deviceDescriptors[0].encodingTypes) { 8694 console.info(`Device encodingTypes: ${deviceDescriptors[0].encodingTypes[0]}`); 8695} 8696``` 8697 8698### getCurrentAudioCapturerChangeInfo<sup>11+</sup> 8699 8700getCurrentAudioCapturerChangeInfo(): AudioCapturerChangeInfo 8701 8702获取录音流配置。使用同步方式返回结果。 8703 8704**系统能力:** SystemCapability.Multimedia.Audio.Device 8705 8706**返回值:** 8707 8708| 类型 | 说明 | 8709| :--------------- | :---------------------------------- | 8710| [AudioCapturerChangeInfo](#audiocapturerchangeinfo9) | 同步接口,返回描述音频采集器更改信息。 | 8711 8712**示例:** 8713 8714```ts 8715let info: audio.AudioCapturerChangeInfo = audioCapturer.getCurrentAudioCapturerChangeInfo(); 8716console.info(`Info streamId: ${info.streamId}`); 8717console.info(`Info source: ${info.capturerInfo.source}`); 8718console.info(`Info capturerFlags: ${info.capturerInfo.capturerFlags}`); 8719console.info(`Info muted: ${info.muted}`); 8720console.info(`Info type: ${info.deviceDescriptors[0].deviceType}`); 8721console.info(`Info role: ${info.deviceDescriptors[0].deviceRole}`); 8722console.info(`Info name: ${info.deviceDescriptors[0].name}`); 8723console.info(`Info address: ${info.deviceDescriptors[0].address}`); 8724console.info(`Info samplerates: ${info.deviceDescriptors[0].sampleRates[0]}`); 8725console.info(`Info channelcounts: ${info.deviceDescriptors[0].channelCounts[0]}`); 8726console.info(`Info channelmask: ${info.deviceDescriptors[0].channelMasks[0]}`); 8727if (info.deviceDescriptors[0].encodingTypes) { 8728 console.info(`Device encodingTypes: ${info.deviceDescriptors[0].encodingTypes[0]}`); 8729} 8730``` 8731 8732### on('audioInterrupt')<sup>10+</sup> 8733 8734on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 8735 8736监听音频中断事件(当音频焦点发生变化时触发),使用callback方式返回结果。 8737 8738AudioCapturer对象在start事件发生时会主动获取焦点,在pause、stop等事件发生时会主动释放焦点,不需要开发者主动发起获取焦点或释放焦点的申请。 8739 8740调用此方法,在AudioCapturer对象获取焦点失败或发生中断事件(如被其他音频打断等)时,会收到[InterruptEvent](#interruptevent9)。建议应用可根据InterruptEvent的信息完成进一步处理,更多信息可参考文档[处理音频焦点事件](../../media/audio/audio-playback-concurrency.md)。 8741 8742**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8743 8744**参数:** 8745 8746| 参数名 | 类型 | 必填 | 说明 | 8747| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 8748| type | string | 是 | 监听事件,固定为:'audioInterrupt'。 | 8749| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回录制中断时,应用接收的中断事件信息。 | 8750 8751**错误码:** 8752 8753以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8754 8755| 错误码ID | 错误信息 | 8756| ------- | --------------------------------------------| 8757| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8758| 6800101 | Parameter verification failed. | 8759 8760**示例:** 8761 8762```ts 8763import { audio } from '@kit.AudioKit'; 8764 8765let isCapturing: boolean; // 标识符,表示是否正在采集。 8766onAudioInterrupt(); 8767 8768async function onAudioInterrupt(){ 8769 audioCapturer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 8770 // 在发生音频打断事件时,audioCapturer收到interruptEvent回调,此处根据其内容做相应处理。 8771 // 1、可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 8772 // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 8773 // 2、必选:读取interruptEvent.hintType的类型,做出相应的处理。 8774 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 8775 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 8776 switch (interruptEvent.hintType) { 8777 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 8778 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 8779 console.info('Force paused. Update capturing status and stop reading'); 8780 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8781 break; 8782 case audio.InterruptHint.INTERRUPT_HINT_STOP: 8783 // 音频流已被停止,永久失去焦点,若想恢复采集,需用户主动触发。 8784 console.info('Force stopped. Update capturing status and stop reading'); 8785 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8786 break; 8787 default: 8788 console.info('Invalid interruptEvent'); 8789 break; 8790 } 8791 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 8792 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 8793 switch (interruptEvent.hintType) { 8794 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 8795 // 建议应用继续采集(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复采集)。 8796 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 8797 console.info('Resume force paused renderer or ignore'); 8798 // 若选择继续采集,需在此处主动执行开始采集的若干操作。 8799 break; 8800 default: 8801 console.info('Invalid interruptEvent'); 8802 break; 8803 } 8804 } 8805 }); 8806} 8807``` 8808 8809### off('audioInterrupt')<sup>10+</sup> 8810 8811off(type: 'audioInterrupt'): void 8812 8813取消监听音频中断事件。 8814 8815**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8816 8817**参数:** 8818 8819| 参数名 | 类型 | 必填 | 说明 | 8820| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 8821| type | string | 是 | 监听事件,固定为:'audioInterrupt'。 | 8822 8823**错误码:** 8824 8825以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8826 8827| 错误码ID | 错误信息 | 8828| ------- | --------------------------------------------| 8829| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8830| 6800101 | Parameter verification failed. | 8831 8832**示例:** 8833 8834```ts 8835audioCapturer.off('audioInterrupt'); 8836``` 8837 8838### on('inputDeviceChange')<sup>11+</sup> 8839 8840on(type: 'inputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 8841 8842监听音频输入设备变化事件(当音频输入设备发生变化时触发),使用callback方式返回结果。 8843 8844**系统能力:** SystemCapability.Multimedia.Audio.Device 8845 8846**参数:** 8847 8848| 参数名 | 类型 | 必填 | 说明 | 8849| :------- | :------------------------- | :--- | :------------------------------------------ | 8850| type | string | 是 | 监听事件,固定为:'inputDeviceChange'。 | 8851| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数,返回监听的音频输入设备变化(返回数据为切换后的设备信息)。 | 8852 8853**错误码:** 8854 8855以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8856 8857| 错误码ID | 错误信息 | 8858| ------- | --------------------------------------------| 8859| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8860| 6800101 | Parameter verification failed. | 8861 8862**示例:** 8863 8864```ts 8865audioCapturer.on('inputDeviceChange', (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 8866 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 8867 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 8868 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 8869}); 8870``` 8871### off('inputDeviceChange')<sup>11+</sup> 8872 8873off(type: 'inputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 8874 8875取消监听音频输入设备更改事件,使用callback方式返回结果。 8876 8877**系统能力:** SystemCapability.Multimedia.Audio.Device 8878 8879**参数:** 8880 8881| 参数名 | 类型 | 必填 | 说明 | 8882| :------- | :------------------------- | :--- |:-----------------------------------------| 8883| type | string | 是 | 监听事件,固定为:'inputDeviceChange'。 | 8884| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回监听的音频输入设备信息。 | 8885 8886**错误码:** 8887 8888以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8889 8890| 错误码ID | 错误信息 | 8891| ------- | --------------------------------------------| 8892| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8893| 6800101 | Parameter verification failed. | 8894 8895**示例:** 8896 8897```ts 8898// 取消该事件的所有监听。 8899audioCapturer.off('inputDeviceChange'); 8900 8901// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8902let inputDeviceChangeCallback = (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 8903 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 8904 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 8905 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 8906}; 8907 8908audioCapturer.on('inputDeviceChange', inputDeviceChangeCallback); 8909 8910audioCapturer.off('inputDeviceChange', inputDeviceChangeCallback); 8911``` 8912 8913### on('audioCapturerChange')<sup>11+</sup> 8914 8915on(type: 'audioCapturerChange', callback: Callback\<AudioCapturerChangeInfo>): void 8916 8917监听录音流配置变化事件(当音频录制流状态变化、设备变化时触发),使用callback方式返回结果。订阅内部是异步实现,是非精确回调,在录音流配置变化的同时注册回调,收到的返回结果存在变化可能性。 8918 8919**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8920 8921**参数:** 8922 8923| 参数名 | 类型 | 必填 | 说明 | 8924| :------- | :------------------------- | :--- | :------------------------------------------ | 8925| type | string | 是 | 监听事件,固定为:'audioCapturerChange'。 | 8926| callback | Callback\<[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)> | 是 | 回调函数,录音流配置或状态变化时返回监听的录音流当前配置和状态信息。 | 8927 8928**错误码:** 8929 8930以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8931 8932| 错误码ID | 错误信息 | 8933| ------- | --------------------------------------------| 8934| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8935| 6800101 | Parameter verification failed. | 8936 8937**示例:** 8938 8939```ts 8940audioCapturer.on('audioCapturerChange', (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 8941 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 8942 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 8943 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 8944}); 8945``` 8946 8947### off('audioCapturerChange')<sup>11+</sup> 8948 8949off(type: 'audioCapturerChange', callback?: Callback\<AudioCapturerChangeInfo>): void 8950 8951取消监听录音流配置变化事件,使用callback方式返回结果。 8952 8953**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8954 8955**参数:** 8956 8957| 参数名 | 类型 | 必填 | 说明 | 8958| :------- | :------------------------- | :--- | :------------------------------------------ | 8959| type | string | 是 | 监听事件,固定为:'audioCapturerChange'。 | 8960| callback | Callback\<[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)> | 否 | 回调函数,返回取消监听的录音流配置或状态变化。 | 8961 8962**错误码:** 8963 8964以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8965 8966| 错误码ID | 错误信息 | 8967| ------- | --------------------------------------------| 8968| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8969| 6800101 | Parameter verification failed. | 8970 8971**示例:** 8972 8973```ts 8974// 取消该事件的所有监听。 8975audioCapturer.off('audioCapturerChange'); 8976 8977// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8978let audioCapturerChangeCallback = (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 8979 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 8980 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 8981 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 8982}; 8983 8984audioCapturer.on('audioCapturerChange', audioCapturerChangeCallback); 8985 8986audioCapturer.off('audioCapturerChange', audioCapturerChangeCallback); 8987``` 8988 8989### on('markReach')<sup>8+</sup> 8990 8991on(type: 'markReach', frame: number, callback: Callback<number>): void 8992 8993监听标记到达事件(当采集的帧数达到frame参数的值时触发,仅调用一次),使用callback方式返回结果。 8994 8995举例说明,如果frame设置为100,当采集帧数到达第100帧时,将上报信息。 8996 8997**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8998 8999**参数:** 9000 9001| 参数名 | 类型 | 必填 | 说明 | 9002| :------- | :---------------------- | :--- | :----------------------------------------- | 9003| type | string | 是 | 监听事件,固定为:'markReach'。 | 9004| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 9005| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 9006 9007**示例:** 9008 9009```ts 9010audioCapturer.on('markReach', 1000, (position: number) => { 9011 if (position == 1000) { 9012 console.info('ON Triggered successfully'); 9013 } 9014}); 9015``` 9016 9017### off('markReach')<sup>8+</sup> 9018 9019off(type: 'markReach'): void 9020 9021取消监听标记到达事件。 9022 9023**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9024 9025**参数:** 9026 9027| 参数名 | 类型 | 必填 | 说明 | 9028| :----- | :----- | :--- | :-------------------------------------------- | 9029| type | string | 是 | 监听事件,固定为:'markReach'。 | 9030 9031**示例:** 9032 9033```ts 9034audioCapturer.off('markReach'); 9035``` 9036 9037### on('periodReach')<sup>8+</sup> 9038 9039on(type: 'periodReach', frame: number, callback: Callback<number>): void 9040 9041监听到达标记事件(当采集的帧数达到frame参数的值时触发,即按周期上报信息),使用callback方式返回结果。 9042 9043举例说明,如果frame设置为10,每当采集10帧数据时将上报信息,例如在第10帧、20帧、30帧,均会上报信息。 9044 9045**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9046 9047**参数:** 9048 9049| 参数名 | 类型 | 必填 | 说明 | 9050| :------- | :----------------------- | :--- | :------------------------------------------ | 9051| type | string | 是 | 监听事件,固定为:'periodReach'。 | 9052| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 9053| callback | Callback\<number> | 是 |回调函数,返回frame参数的值。 | 9054 9055**示例:** 9056 9057```ts 9058audioCapturer.on('periodReach', 1000, (position: number) => { 9059 if (position == 1000) { 9060 console.info('ON Triggered successfully'); 9061 } 9062}); 9063``` 9064 9065### off('periodReach')<sup>8+</sup> 9066 9067off(type: 'periodReach'): void 9068 9069取消监听标记到达事件。 9070 9071**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9072 9073**参数:** 9074 9075| 参数名 | 类型 | 必填 | 说明 | 9076| :----- | :----- | :--- | :---------------------------------------------- | 9077| type | string | 是 | 监听事件,固定为:'periodReach'。 | 9078 9079**示例:** 9080 9081```ts 9082audioCapturer.off('periodReach'); 9083``` 9084 9085### on('stateChange')<sup>8+</sup> 9086 9087on(type: 'stateChange', callback: Callback<AudioState\>): void 9088 9089监听状态变化事件(当AudioCapturer状态发生变化时触发),使用callback方式返回结果。 9090 9091**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9092 9093**参数:** 9094 9095| 参数名 | 类型 | 必填 | 说明 | 9096| :------- | :------------------------- | :--- | :------------------------------------------ | 9097| type | string | 是 | 监听事件,固定为:'stateChange'。 | 9098| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 9099 9100**示例:** 9101 9102```ts 9103audioCapturer.on('stateChange', (state: audio.AudioState) => { 9104 if (state == 1) { 9105 console.info('audio capturer state is: STATE_PREPARED'); 9106 } 9107 if (state == 2) { 9108 console.info('audio capturer state is: STATE_RUNNING'); 9109 } 9110}); 9111``` 9112 9113### on('readData')<sup>11+</sup> 9114 9115on(type: 'readData', callback: Callback\<ArrayBuffer>): void 9116 9117监听音频数据读取回调事件(当需要读取音频流数据时触发),使用callback方式返回结果。 9118 9119回调函数仅用来读取音频数据,请勿在回调函数中调用AudioCapturer相关接口。 9120 9121**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9122 9123**参数:** 9124 9125| 参数名 | 类型 | 必填 | 说明 | 9126| :------- |:-----------------------| :--- |:--------------------------| 9127| type | string | 是 | 监听事件,固定为:'readData'。 | 9128| callback | Callback\<ArrayBuffer> | 是 | 回调函数,返回读到的数据缓冲区。 | 9129 9130**错误码:** 9131 9132以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 9133 9134| 错误码ID | 错误信息 | 9135| ------- | --------------------------------------------| 9136| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9137| 6800101 | Parameter verification failed. | 9138 9139**示例:** 9140 9141```ts 9142import { BusinessError } from '@kit.BasicServicesKit'; 9143import { fileIo as fs } from '@kit.CoreFileKit'; 9144 9145class Options { 9146 offset?: number; 9147 length?: number; 9148} 9149 9150let bufferSize: number = 0; 9151let path = getContext().cacheDir; 9152// 确保该沙箱路径下存在该资源。 9153let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 9154let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 9155let readDataCallback = (buffer: ArrayBuffer) => { 9156 let options: Options = { 9157 offset: bufferSize, 9158 length: buffer.byteLength 9159 }; 9160 fs.writeSync(file.fd, buffer, options); 9161 bufferSize += buffer.byteLength; 9162} 9163 9164audioCapturer.on('readData', readDataCallback); 9165 9166audioCapturer.start((err: BusinessError) => { 9167 if (err) { 9168 console.error('Capturer start failed.'); 9169 } else { 9170 console.info('Capturer start success.'); 9171 } 9172}); 9173``` 9174 9175### off('readData')<sup>11+</sup> 9176 9177off(type: 'readData', callback?: Callback\<ArrayBuffer>): void 9178 9179取消监听音频数据读取回调事件,使用callback方式返回结果。 9180 9181**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9182 9183**参数:** 9184 9185| 参数名 | 类型 | 必填 | 说明 | 9186| :------- |:-----------------------| :--- |:-------------------------------------------| 9187| type | string | 是 | 监听事件,固定为:'readData'。 | 9188| callback | Callback\<ArrayBuffer> | 否 | 回调函数,返回读到的数据缓冲区。 | 9189 9190**错误码:** 9191 9192以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 9193 9194| 错误码ID | 错误信息 | 9195| ------- | --------------------------------------------| 9196| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9197| 6800101 | Parameter verification failed. | 9198 9199**示例:** 9200 9201```ts 9202// 取消该事件的所有监听。 9203audioCapturer.off('readData'); 9204 9205// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 9206let readDataCallback = (data: ArrayBuffer) => { 9207 console.info(`read data: ${data}`); 9208}; 9209 9210audioCapturer.on('readData', readDataCallback); 9211 9212audioCapturer.off('readData', readDataCallback); 9213``` 9214 9215### getOverflowCount<sup>12+</sup> 9216 9217getOverflowCount(): Promise<number> 9218 9219获取当前录制音频流的过载音频帧数量。使用Promise异步回调。 9220 9221**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9222 9223**返回值:** 9224 9225| 类型 | 说明 | 9226| ------------------- | ----------------------------- | 9227| Promise<number>| Promise对象,返回音频流的过载音频帧数量。| 9228 9229**示例:** 9230 9231```ts 9232import { BusinessError } from '@kit.BasicServicesKit'; 9233 9234audioCapturer.getOverflowCount().then((value: number) => { 9235 console.info(`Get overflow count Success! ${value}`); 9236}).catch((err: BusinessError) => { 9237 console.error(`Get overflow count Fail: ${err}`); 9238}); 9239``` 9240 9241### getOverflowCountSync<sup>12+</sup> 9242 9243getOverflowCountSync(): number 9244 9245获取当前录制音频流的过载音频帧数量,同步返回数据。 9246 9247**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9248 9249**返回值:** 9250 9251| 类型 | 说明 | 9252| ------------------- | ----------------------------- | 9253| number| 返回音频流的过载音频帧数量。| 9254 9255**示例:** 9256 9257```ts 9258import { BusinessError } from '@kit.BasicServicesKit'; 9259 9260try { 9261 let value: number = audioCapturer.getOverflowCountSync(); 9262 console.info(`Get overflow count Success! ${value}`); 9263} catch (err) { 9264 let error = err as BusinessError; 9265 console.error(`Get overflow count Fail: ${error}`); 9266} 9267``` 9268 9269## ActiveDeviceType<sup>(deprecated)</sup> 9270 9271枚举,活跃设备类型。 9272 9273> **说明:** 9274> 9275> 从 API version 9 开始废弃,建议使用[CommunicationDeviceType](#communicationdevicetype9)替代。 9276 9277**系统能力:** SystemCapability.Multimedia.Audio.Device 9278 9279| 名称 | 值 | 说明 | 9280| ------------- | ------ | ---------------------------------------------------- | 9281| SPEAKER | 2 | 扬声器。 | 9282| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 9283 9284## InterruptActionType<sup>(deprecated)</sup> 9285 9286枚举,中断事件返回类型。 9287 9288> **说明:** 9289> 9290> 从 API version 7 开始支持,从 API version 9 开始废弃。无替代接口,与中断事件配套使用。 9291 9292**系统能力:** SystemCapability.Multimedia.Audio.Renderer 9293 9294| 名称 | 值 | 说明 | 9295| -------------- | ------ | ------------------ | 9296| TYPE_ACTIVATED | 0 | 表示触发焦点事件。 | 9297| TYPE_INTERRUPT | 1 | 表示音频打断事件。 | 9298 9299## AudioInterrupt<sup>(deprecated)</sup> 9300 9301音频监听事件传入的参数。 9302 9303> **说明:** 9304> 9305> 从 API version 7 开始支持,从 API version 9 开始废弃。无替代接口,与中断事件配套使用。 9306 9307**系统能力:** SystemCapability.Multimedia.Audio.Renderer 9308 9309| 名称 | 类型 | 必填 | 说明 | 9310| --------------- | --------------------------- | ----| ------------------------------------------------------------ | 9311| streamUsage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 9312| contentType | [ContentType](#contenttypedeprecated) | 是 | 音频打断媒体类型。 | 9313| pauseWhenDucked | boolean | 是 | 音频打断时是否可以暂停音频播放(true表示音频播放可以在音频打断期间暂停,false表示相反)。 | 9314 9315## InterruptAction<sup>(deprecated)</sup> 9316 9317音频打断/获取焦点事件的回调方法。 9318 9319> **说明:** 9320> 9321> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用[InterruptEvent](#interruptevent9)替代。 9322 9323**系统能力:** SystemCapability.Multimedia.Audio.Renderer 9324 9325| 名称 | 类型 | 必填 | 说明 | 9326| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 9327| actionType | [InterruptActionType](#interruptactiontypedeprecated) | 是 | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。 | 9328| type | [InterruptType](#interrupttype) | 否 | 打断事件类型。 | 9329| hint | [InterruptHint](#interrupthint) | 否 | 打断事件提示。 | 9330| activated | boolean | 否 | 获得/释放焦点。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 | 9331