1# @ohos.multimedia.audio (音频管理) 2<!--Kit: Audio Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @songshenke--> 5<!--SE: @caixuejiang; @hao-liangfei; @zhanganxiang--> 6<!--TSE: @Filger--> 7 8音频管理提供基础的音频控制能力,包括音量调节、设备管理、数据采集及渲染。 9 10该模块提供以下音频相关的常用功能: 11 12- [AudioManager](#audiomanager):音频管理器。 13- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。 14- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM音频数据。 15 16> **说明:** 17> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 18 19## 导入模块 20 21```ts 22import { audio } from '@kit.AudioKit'; 23``` 24 25## 常量 26 27| 名称 | 类型 | 可读 | 可写 | 说明 | 28| --------------------------------------- | ----------| ---- | ---- | ------------------ | 29| DEFAULT_VOLUME_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音量组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Volume | 30| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音频中断组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Interrupt | 31 32**示例:** 33 34```ts 35import { audio } from '@kit.AudioKit'; 36 37const defaultVolumeGroupId = audio.DEFAULT_VOLUME_GROUP_ID; 38const defaultInterruptGroupId = audio.DEFAULT_INTERRUPT_GROUP_ID; 39``` 40 41## audio.getAudioManager 42 43getAudioManager(): AudioManager 44 45获取音频管理器。 46 47**系统能力:** SystemCapability.Multimedia.Audio.Core 48 49**返回值:** 50 51| 类型 | 说明 | 52| ----------------------------- | ------------ | 53| [AudioManager](#audiomanager) | 音频管理器对象。 | 54 55**示例:** 56```ts 57import { audio } from '@kit.AudioKit'; 58 59let audioManager = audio.getAudioManager(); 60``` 61 62## audio.createAudioRenderer<sup>8+</sup> 63 64createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void 65 66获取音频渲染器。使用callback异步回调。 67 68**系统能力:** SystemCapability.Multimedia.Audio.Renderer 69 70**参数:** 71 72| 参数名 | 类型 | 必填 | 说明 | 73| -------- | ----------------------------------------------- | ---- | ---------------- | 74| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 75| callback | AsyncCallback<[AudioRenderer](#audiorenderer8)> | 是 | 回调函数。当获取音频渲染器成功,err为undefined,data为获取到的音频渲染器对象;否则为错误对象。 | 76 77**示例:** 78 79```ts 80import { audio } from '@kit.AudioKit'; 81 82let audioStreamInfo: audio.AudioStreamInfo = { 83 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 84 channels: audio.AudioChannel.CHANNEL_2, // 通道。 85 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 86 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 87}; 88 89let audioRendererInfo: audio.AudioRendererInfo = { 90 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 91 rendererFlags: 0 // 音频渲染器标志。 92}; 93 94let audioRendererOptions: audio.AudioRendererOptions = { 95 streamInfo: audioStreamInfo, 96 rendererInfo: audioRendererInfo 97}; 98 99audio.createAudioRenderer(audioRendererOptions,(err, data) => { 100 if (err) { 101 console.error(`AudioRenderer Created: Error: ${err}`); 102 } else { 103 console.info('AudioRenderer Created: Success: SUCCESS'); 104 let audioRenderer = data; 105 } 106}); 107``` 108 109## audio.createAudioRenderer<sup>8+</sup> 110 111createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\> 112 113获取音频渲染器。使用Promise异步回调。 114 115**系统能力:** SystemCapability.Multimedia.Audio.Renderer 116 117**参数:** 118 119| 参数名 | 类型 | 必填 | 说明 | 120| :------ | :--------------------------------------------- | :--- | :----------- | 121| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 122 123**返回值:** 124 125| 类型 | 说明 | 126| ----------------------------------------- | ---------------- | 127| Promise<[AudioRenderer](#audiorenderer8)> | Promise对象,返回音频渲染器对象。 | 128 129**示例:** 130 131```ts 132import { audio } from '@kit.AudioKit'; 133import { BusinessError } from '@kit.BasicServicesKit'; 134 135let audioStreamInfo: audio.AudioStreamInfo = { 136 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 137 channels: audio.AudioChannel.CHANNEL_2, // 通道。 138 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 139 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 140}; 141 142let audioRendererInfo: audio.AudioRendererInfo = { 143 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 144 rendererFlags: 0 // 音频渲染器标志。 145}; 146 147let audioRendererOptions: audio.AudioRendererOptions = { 148 streamInfo: audioStreamInfo, 149 rendererInfo: audioRendererInfo 150}; 151 152let audioRenderer: audio.AudioRenderer; 153 154audio.createAudioRenderer(audioRendererOptions).then((data) => { 155 audioRenderer = data; 156 console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS'); 157}).catch((err: BusinessError) => { 158 console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`); 159}); 160``` 161 162## audio.createAudioCapturer<sup>8+</sup> 163 164createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void 165 166获取音频采集器。使用callback异步回调。 167 168**系统能力:** SystemCapability.Multimedia.Audio.Capturer 169 170**需要权限:** ohos.permission.MICROPHONE 171 172当设置Mic音频源(即[SourceType](#sourcetype8)为SOURCE_TYPE_MIC、SOURCE_TYPE_VOICE_RECOGNITION、SOURCE_TYPE_VOICE_COMMUNICATION、SOURCE_TYPE_VOICE_MESSAGE、SOURCE_TYPE_CAMCORDER)时需要该权限。 173 174**参数:** 175 176| 参数名 | 类型 | 必填 | 说明 | 177| :------- | :---------------------------------------------- | :--- | :--------------- | 178| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 179| callback | AsyncCallback<[AudioCapturer](#audiocapturer8)> | 是 | 回调函数。当获取音频采集器成功,err为undefined,data为获取到的音频采集器对象;否则为错误对象。异常将返回error对象:<br>错误码6800301:表示参数校验异常、权限校验异常或系统处理异常(具体错误查看系统日志)。<br>错误码6800101:表示必选参数为空或参数类型错误。 | 180 181**示例:** 182 183```ts 184import { audio } from '@kit.AudioKit'; 185 186let audioStreamInfo: audio.AudioStreamInfo = { 187 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 188 channels: audio.AudioChannel.CHANNEL_2, // 通道。 189 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 190 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 191}; 192 193let audioCapturerInfo: audio.AudioCapturerInfo = { 194 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 195 capturerFlags: 0 // 音频采集器标志。 196}; 197 198let audioCapturerOptions: audio.AudioCapturerOptions = { 199 streamInfo: audioStreamInfo, 200 capturerInfo: audioCapturerInfo 201}; 202 203audio.createAudioCapturer(audioCapturerOptions, (err, data) => { 204 if (err) { 205 console.error(`AudioCapturer Created : Error: ${err}`); 206 } else { 207 console.info('AudioCapturer Created : Success : SUCCESS'); 208 let audioCapturer = data; 209 } 210}); 211``` 212 213## audio.createAudioCapturer<sup>8+</sup> 214 215createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\> 216 217获取音频采集器。使用Promise异步回调。 218 219**系统能力:** SystemCapability.Multimedia.Audio.Capturer 220 221**需要权限:** ohos.permission.MICROPHONE 222 223当设置Mic音频源(即[SourceType](#sourcetype8)为SOURCE_TYPE_MIC、SOURCE_TYPE_VOICE_RECOGNITION、SOURCE_TYPE_VOICE_COMMUNICATION、SOURCE_TYPE_VOICE_MESSAGE、SOURCE_TYPE_CAMCORDER)时需要该权限。 224 225**参数:** 226 227| 参数名 | 类型 | 必填 | 说明 | 228| :------ | :--------------------------------------------- | :--- | :--------------- | 229| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 230 231**返回值:** 232 233| 类型 | 说明 | 234| ----------------------------------------- |----------------------| 235| Promise<[AudioCapturer](#audiocapturer8)> | Promise对象,成功将返回音频采集器对象,异常将返回error对象:<br>错误码6800301:表示参数校验异常、权限校验异常或系统处理异常(具体错误查看系统日志)。<br>错误码6800101:表示必选参数为空或参数类型错误。 | 236 237**示例:** 238 239```ts 240import { audio } from '@kit.AudioKit'; 241import { BusinessError } from '@kit.BasicServicesKit'; 242 243let audioStreamInfo: audio.AudioStreamInfo = { 244 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率。 245 channels: audio.AudioChannel.CHANNEL_2, // 通道。 246 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式。 247 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式。 248}; 249 250let audioCapturerInfo: audio.AudioCapturerInfo = { 251 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 252 capturerFlags: 0 // 音频采集器标志。 253}; 254 255let audioCapturerOptions:audio.AudioCapturerOptions = { 256 streamInfo: audioStreamInfo, 257 capturerInfo: audioCapturerInfo 258}; 259 260let audioCapturer: audio.AudioCapturer; 261 262audio.createAudioCapturer(audioCapturerOptions).then((data) => { 263 audioCapturer = data; 264 console.info('AudioCapturer Created : Success : Stream Type: SUCCESS'); 265}).catch((err: BusinessError) => { 266 console.error(`AudioCapturer Created : ERROR : ${err}`); 267}); 268``` 269 270## AudioVolumeType 271 272表示音频流类型的枚举。 273 274**系统能力:** SystemCapability.Multimedia.Audio.Volume 275 276| 名称 | 值 | 说明 | 277| ---------------------------- | ------ | ---------- | 278| VOICE_CALL<sup>8+</sup> | 0 | 语音电话。 | 279| RINGTONE | 2 | 铃声。 | 280| MEDIA | 3 | 媒体。 | 281| ALARM<sup>10+</sup> | 4 | 闹钟。 | 282| ACCESSIBILITY<sup>10+</sup> | 5 | 无障碍。 | 283| VOICE_ASSISTANT<sup>8+</sup> | 9 | 语音助手。 | 284 285## InterruptMode<sup>9+</sup> 286 287表示焦点模型的枚举。 288 289**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 290 291**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 292 293| 名称 | 值 | 说明 | 294| ---------------------------- | ------ | ---------- | 295| SHARE_MODE | 0 | 共享焦点模式。 | 296| INDEPENDENT_MODE | 1 | 独立焦点模式。 | 297 298## DeviceFlag 299 300表示音频设备类型的枚举。 301 302**系统能力:** SystemCapability.Multimedia.Audio.Device 303 304| 名称 | 值 | 说明 | 305| ------------------------------- | ------ |---------------------------| 306| OUTPUT_DEVICES_FLAG | 1 | 输出设备。 | 307| INPUT_DEVICES_FLAG | 2 | 输入设备。 | 308| ALL_DEVICES_FLAG | 3 | 所有设备。 | 309 310## DeviceUsage<sup>12+</sup> 311 312表示音频设备类型的枚举(根据用途分类)。 313 314**系统能力:** SystemCapability.Multimedia.Audio.Device 315 316| 名称 | 值 | 说明 | 317| ------------------------------- | ------ |---------------------------| 318| MEDIA_OUTPUT_DEVICES | 1 | 媒体输出设备。| 319| MEDIA_INPUT_DEVICES | 2 | 媒体输入设备。| 320| ALL_MEDIA_DEVICES | 3 | 所有媒体设备。| 321| CALL_OUTPUT_DEVICES | 4 | 通话输出设备。| 322| CALL_INPUT_DEVICES | 8 | 通话输入设备。| 323| ALL_CALL_DEVICES | 12 | 所有通话设备。| 324 325## DeviceRole 326 327表示设备角色的枚举。 328 329**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 330 331**系统能力:** SystemCapability.Multimedia.Audio.Device 332 333| 名称 | 值 | 说明 | 334| ------------- | ------ | -------------- | 335| INPUT_DEVICE | 1 | 输入设备角色。 | 336| OUTPUT_DEVICE | 2 | 输出设备角色。 | 337 338## DeviceType 339 340表示设备类型的枚举。 341 342**系统能力:** SystemCapability.Multimedia.Audio.Device 343 344| 名称 | 值 | 说明 | 345| ---------------------| ------ | --------------------------------------------------------- | 346| INVALID | 0 | 无效设备。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 347| EARPIECE | 1 | 听筒。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 348| SPEAKER | 2 | 扬声器。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 349| WIRED_HEADSET | 3 | 有线耳机,带麦克风。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 350| WIRED_HEADPHONES | 4 | 有线耳机,不带麦克风。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 351| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 352| BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 353| MIC | 15 | 麦克风。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 354| USB_HEADSET | 22 | USB耳机,带麦克风。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 355| DISPLAY_PORT<sup>12+</sup> | 23 | DisplayPort(显示接口,简称DP),用于外接扩展设备。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 356| REMOTE_CAST<sup>12+</sup> | 24 | 音频被系统应用投送到其他的远程设备。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 357| USB_DEVICE<sup>18+</sup> | 25 | USB设备(不包含USB耳机)。 | 358| HDMI<sup>19+</sup> | 27 | HDMI设备(例如HDMI、ARC、eARC等)。 | 359| LINE_DIGITAL<sup>19+</sup> | 28 | 有线数字设备(例如S/PDIF等)。 | 360| REMOTE_DAUDIO<sup>18+</sup> | 29 | 分布式设备。<br/>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 | 361| DEFAULT<sup>9+</sup> | 1000 | 默认设备类型。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 362 363## CommunicationDeviceType<sup>9+</sup> 364 365表示用于通信的可用设备类型的枚举。 366 367**系统能力:** SystemCapability.Multimedia.Audio.Communication 368 369| 名称 | 值 | 说明 | 370| ------------- | ------ | -------------| 371| SPEAKER | 2 | 扬声器。 | 372 373## AudioRingMode 374 375表示铃声模式的枚举。 376 377**系统能力:** SystemCapability.Multimedia.Audio.Communication 378 379| 名称 | 值 | 说明 | 380| ------------------- | ------ | ---------- | 381| RINGER_MODE_SILENT | 0 | 静音模式。 | 382| RINGER_MODE_VIBRATE | 1 | 震动模式。 | 383| RINGER_MODE_NORMAL | 2 | 响铃模式。 | 384 385## AudioSampleFormat<sup>8+</sup> 386 387表示音频采样格式的枚举。 388 389**系统能力:** SystemCapability.Multimedia.Audio.Core 390 391| 名称 | 值 | 说明 | 392| ---------------------------------- | ------ | -------------------------- | 393| SAMPLE_FORMAT_INVALID | -1 | 无效格式。 | 394| SAMPLE_FORMAT_U8 | 0 | 无符号8位整数。 | 395| SAMPLE_FORMAT_S16LE | 1 | 带符号的16位整数,小尾数。 | 396| SAMPLE_FORMAT_S24LE | 2 | 带符号的24位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 397| SAMPLE_FORMAT_S32LE | 3 | 带符号的32位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 398| SAMPLE_FORMAT_F32LE<sup>9+</sup> | 4 | 带符号的32位浮点数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 399 400## AudioErrors<sup>9+</sup> 401 402表示音频错误码的枚举。 403 404**系统能力:** SystemCapability.Multimedia.Audio.Core 405 406| 名称 | 值 | 说明 | 407| ---------------------| --------| ----------------- | 408| ERROR_INVALID_PARAM | 6800101 | 无效入参。 | 409| ERROR_NO_MEMORY | 6800102 | 分配内存失败。 | 410| ERROR_ILLEGAL_STATE | 6800103 | 状态不支持。 | 411| ERROR_UNSUPPORTED | 6800104 | 参数选项不支持。 | 412| ERROR_TIMEOUT | 6800105 | 处理超时。 | 413| ERROR_STREAM_LIMIT | 6800201 | 音频流数量达到限制。| 414| ERROR_SYSTEM | 6800301 | 系统处理异常。 | 415 416## AudioChannel<sup>8+</sup> 417 418表示音频声道的枚举。 419 420**系统能力:** SystemCapability.Multimedia.Audio.Core 421 422| 名称 | 值 | 说明 | 423| --------- | -------- |------| 424| CHANNEL_1 | 1 | 单声道。 | 425| CHANNEL_2 | 2 | 双声道。 | 426| CHANNEL_3<sup>11+</sup> | 3 | 三声道。 | 427| CHANNEL_4<sup>11+</sup> | 4 | 四声道。 | 428| CHANNEL_5<sup>11+</sup> | 5 | 五声道。 | 429| CHANNEL_6<sup>11+</sup> | 6 | 六声道。 | 430| CHANNEL_7<sup>11+</sup> | 7 | 七声道。 | 431| CHANNEL_8<sup>11+</sup> | 8 | 八声道。 | 432| CHANNEL_9<sup>11+</sup> | 9 | 九声道。 | 433| CHANNEL_10<sup>11+</sup> | 10 | 十声道。 | 434| CHANNEL_12<sup>11+</sup> | 12 | 十二声道。 | 435| CHANNEL_14<sup>11+</sup> | 14 | 十四声道。 | 436| CHANNEL_16<sup>11+</sup> | 16 | 十六声道。 | 437 438## AudioSamplingRate<sup>8+</sup> 439 440表示音频采样率的枚举(具体设备支持的采样率规格会存在差异)。 441 442**系统能力:** SystemCapability.Multimedia.Audio.Core 443 444| 名称 | 值 | 说明 | 445| ----------------- | ------ | --------------- | 446| SAMPLE_RATE_8000 | 8000 | 采样率为8000。 | 447| SAMPLE_RATE_11025 | 11025 | 采样率为11025。 | 448| SAMPLE_RATE_12000 | 12000 | 采样率为12000。 | 449| SAMPLE_RATE_16000 | 16000 | 采样率为16000。 | 450| SAMPLE_RATE_22050 | 22050 | 采样率为22050。 | 451| SAMPLE_RATE_24000 | 24000 | 采样率为24000。 | 452| SAMPLE_RATE_32000 | 32000 | 采样率为32000。 | 453| SAMPLE_RATE_44100 | 44100 | 采样率为44100。 | 454| SAMPLE_RATE_48000 | 48000 | 采样率为48000。 | 455| SAMPLE_RATE_64000 | 64000 | 采样率为64000。 | 456| SAMPLE_RATE_88200<sup>12+</sup> | 88200 | 采样率为88200。 | 457| SAMPLE_RATE_96000 | 96000 | 采样率为96000。 | 458| SAMPLE_RATE_176400<sup>12+</sup> | 176400 | 采样率为176400。 | 459| SAMPLE_RATE_192000<sup>12+</sup> | 192000 | 采样率为192000。 | 460 461## AudioEncodingType<sup>8+</sup> 462 463表示音频编码类型的枚举。 464 465**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 466 467**系统能力:** SystemCapability.Multimedia.Audio.Core 468 469| 名称 | 值 | 说明 | 470| --------------------- | ------ | --------- | 471| ENCODING_TYPE_INVALID | -1 | 无效。 | 472| ENCODING_TYPE_RAW | 0 | PCM编码。 | 473 474## AudioChannelLayout<sup>11+</sup> 475 476表示音频文件声道布局类型的枚举。 477 478**系统能力:** SystemCapability.Multimedia.Audio.Core 479 480| 名称 | 值 | 说明 | 481| ------------------------------ | ---------------- | --------------------------------------------- | 482| CH_LAYOUT_UNKNOWN | 0x0 | 未知声道布局。 | 483| CH_LAYOUT_MONO | 0x4 | 声道布局为MONO。 | 484| CH_LAYOUT_STEREO | 0x3 | 声道布局为STEREO。 | 485| CH_LAYOUT_STEREO_DOWNMIX | 0x60000000 | 声道布局为STEREO-DOWNMIX。 | 486| CH_LAYOUT_2POINT1 | 0xB | 声道布局为2.1。 | 487| CH_LAYOUT_3POINT0 | 0x103 | 声道布局为3.0。 | 488| CH_LAYOUT_SURROUND | 0x7 | 声道布局为SURROUND。 | 489| CH_LAYOUT_3POINT1 | 0xF | 声道布局为3.1。 | 490| CH_LAYOUT_4POINT0 | 0x107 | 声道布局为4.0。 | 491| CH_LAYOUT_QUAD | 0x33 | 声道布局为QUAD。 | 492| CH_LAYOUT_QUAD_SIDE | 0x603 | 声道布局为QUAD-SIDE。 | 493| CH_LAYOUT_2POINT0POINT2 | 0x3000000003 | 声道布局为2.0.2。 | 494| CH_LAYOUT_AMB_ORDER1_ACN_N3D | 0x100000000001 | 声道排序为ACN_N3D(根据ITU标准)的一阶FOA文件。 | 495| CH_LAYOUT_AMB_ORDER1_ACN_SN3D | 0x100000001001 | 声道排序为ACN_SN3D(根据ITU标准)的一阶FOA文件。 | 496| CH_LAYOUT_AMB_ORDER1_FUMA | 0x100000000101 | 声道排序为FUMA(根据ITU标准)的一阶FOA文件。 | 497| CH_LAYOUT_4POINT1 | 0x10F | 声道布局为4.1 | 498| CH_LAYOUT_5POINT0 | 0x607 | 声道布局为5.0。 | 499| CH_LAYOUT_5POINT0_BACK | 0x37 | 声道布局为5.0-BACK。 | 500| CH_LAYOUT_2POINT1POINT2 | 0x300000000B | 声道布局为2.1.2。 | 501| CH_LAYOUT_3POINT0POINT2 | 0x3000000007 | 声道布局为3.0.2。 | 502| CH_LAYOUT_5POINT1 | 0x60F | 声道布局为5.1。 | 503| CH_LAYOUT_5POINT1_BACK | 0x3F | 声道布局为5.1-BACK。 | 504| CH_LAYOUT_6POINT0 | 0x707 | 声道布局为6.0。 | 505| CH_LAYOUT_HEXAGONAL | 0x137 | 声道布局为HEXAGONAL。 | 506| CH_LAYOUT_3POINT1POINT2 | 0x500F | 声道布局为3.1.2。 | 507| CH_LAYOUT_6POINT0_FRONT | 0x6C3 | 声道布局为6.0-FRONT。 | 508| CH_LAYOUT_6POINT1 | 0x70F | 声道布局为6.1。 | 509| CH_LAYOUT_6POINT1_BACK | 0x13F | 声道布局为6.1-BACK。 | 510| CH_LAYOUT_6POINT1_FRONT | 0x6CB | 声道布局为6.1-FRONT。 | 511| CH_LAYOUT_7POINT0 | 0x637 | 声道布局为7.0。 | 512| CH_LAYOUT_7POINT0_FRONT | 0x6C7 | 声道布局为7.0-FRONT。 | 513| CH_LAYOUT_7POINT1 | 0x63F | 声道布局为7.1。 | 514| CH_LAYOUT_OCTAGONAL | 0x737 | 声道布局为OCTAGONAL。 | 515| CH_LAYOUT_5POINT1POINT2 | 0x300000060F | 声道布局为5.1.2。 | 516| CH_LAYOUT_7POINT1_WIDE | 0x6CF | 声道布局为7.1-WIDE。 | 517| CH_LAYOUT_7POINT1_WIDE_BACK | 0xFF | 声道布局为7.1-WIDE-BACK。 | 518| CH_LAYOUT_AMB_ORDER2_ACN_N3D | 0x100000000002 | 声道排序为ACN_N3D(根据ITU标准)的二阶HOA文件。 | 519| CH_LAYOUT_AMB_ORDER2_ACN_SN3D | 0x100000001002 | 声道排序为ACN_SN3D(根据ITU标准)的二阶HOA文件。 | 520| CH_LAYOUT_AMB_ORDER2_FUMA | 0x100000000102 | 声道排序为FUMA(根据ITU标准)的二阶HOA文件。 | 521| CH_LAYOUT_5POINT1POINT4 | 0x2D60F | 声道布局为5.1.4。 | 522| CH_LAYOUT_7POINT1POINT2 | 0x300000063F | 声道布局为7.1.2。 | 523| CH_LAYOUT_7POINT1POINT4 | 0x2D63F | 声道布局为7.1.4。 | 524| CH_LAYOUT_10POINT2 | 0x180005737 | 声道布局为10.2。 | 525| CH_LAYOUT_9POINT1POINT4 | 0x18002D63F | 声道布局为9.1.4。 | 526| CH_LAYOUT_9POINT1POINT6 | 0x318002D63F | 声道布局为9.1.6。 | 527| CH_LAYOUT_HEXADECAGONAL | 0x18003F737 | 声道布局为HEXADECAGONAL。 | 528| CH_LAYOUT_AMB_ORDER3_ACN_N3D | 0x100000000003 | 声道排序为ACN_N3D(根据ITU标准)的三阶HOA文件。 | 529| CH_LAYOUT_AMB_ORDER3_ACN_SN3D | 0x100000001003 | 声道排序为ACN_SN3D(根据ITU标准)的三阶HOA文件。 | 530| CH_LAYOUT_AMB_ORDER3_FUMA | 0x100000000103 | 声道排序为FUMA(根据ITU标准)的三阶HOA文件。 | 531 532## ContentType<sup>(deprecated)</sup> 533 534表示音频内容类型的枚举。 535 536> **说明:** 537> 从API version 7开始支持,从API version 10开始废弃,建议使用[StreamUsage](#streamusage)替代。 538 539**系统能力:** SystemCapability.Multimedia.Audio.Core 540 541| 名称 | 值 | 说明 | 542| ---------------------------------- | ------ | ---------- | 543| CONTENT_TYPE_UNKNOWN | 0 | 未知类型。 | 544| CONTENT_TYPE_SPEECH | 1 | 语音。 | 545| CONTENT_TYPE_MUSIC | 2 | 音乐。 | 546| CONTENT_TYPE_MOVIE | 3 | 电影。 | 547| CONTENT_TYPE_SONIFICATION | 4 | 通知音。 | 548| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5 | 铃声。 | 549 550## StreamUsage 551 552表示音频流使用类型的枚举。 553 554**系统能力:** SystemCapability.Multimedia.Audio.Core 555 556| 名称 | 值 | 说明 | 557| ------------------------------------------| ------ |--------------------------------------------------------------------------------| 558| STREAM_USAGE_UNKNOWN | 0 | 未知类型。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 559| 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替代。 | 560| STREAM_USAGE_MUSIC<sup>10+</sup> | 1 | 音乐。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 561| STREAM_USAGE_VOICE_COMMUNICATION | 2 | VoIP语音通话。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 562| STREAM_USAGE_VOICE_ASSISTANT<sup>9+</sup> | 3 | 语音播报。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 563| STREAM_USAGE_ALARM<sup>10+</sup> | 4 | 闹钟。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 564| STREAM_USAGE_VOICE_MESSAGE<sup>10+</sup> | 5 | 语音消息。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 565| STREAM_USAGE_NOTIFICATION_RINGTONE<sup>(deprecated)</sup> | 6 | 通知铃声。<br/> 从API version 7开始支持,从API version 10开始废弃,建议使用该枚举中的STREAM_USAGE_RINGTONE替代。 | 566| STREAM_USAGE_RINGTONE<sup>10+</sup> | 6 | 铃声。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 567| STREAM_USAGE_NOTIFICATION<sup>10+</sup> | 7 | 通知音。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 568| STREAM_USAGE_ACCESSIBILITY<sup>10+</sup> | 8 | 无障碍。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 569| STREAM_USAGE_MOVIE<sup>10+</sup> | 10 | 电影或视频。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 570| STREAM_USAGE_GAME<sup>10+</sup> | 11 | 游戏。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 571| STREAM_USAGE_AUDIOBOOK<sup>10+</sup> | 12 | 有声读物(包括听书、相声、评书)、听新闻、播客等。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 572| STREAM_USAGE_NAVIGATION<sup>10+</sup> | 13 | 导航。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 573| STREAM_USAGE_VIDEO_COMMUNICATION<sup>12+</sup> | 17 | VoIP视频通话。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 574 575## AudioState<sup>8+</sup> 576 577表示音频状态的枚举。 578 579**系统能力:** SystemCapability.Multimedia.Audio.Core 580 581| 名称 | 值 | 说明 | 582| -------------- | ------ | ---------------- | 583| STATE_INVALID | -1 | 无效状态。 | 584| STATE_NEW | 0 | 创建新实例状态。 | 585| STATE_PREPARED | 1 | 准备状态。 | 586| STATE_RUNNING | 2 | 运行状态。 | 587| STATE_STOPPED | 3 | 停止状态。 | 588| STATE_RELEASED | 4 | 释放状态。 | 589| STATE_PAUSED | 5 | 暂停状态。 | 590 591## AudioEffectMode<sup>10+</sup> 592 593表示音效模式的枚举。 594 595**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 596 597**系统能力:** SystemCapability.Multimedia.Audio.Renderer 598 599| 名称 | 值 | 说明 | 600| ------------------ | ------ | ---------- | 601| EFFECT_NONE | 0 | 关闭音效。 | 602| EFFECT_DEFAULT | 1 | 默认音效。 | 603 604## AudioRendererRate<sup>8+</sup> 605 606表示音频渲染速度的枚举。 607 608**系统能力:** SystemCapability.Multimedia.Audio.Renderer 609 610| 名称 | 值 | 说明 | 611| ------------------ | ------ | ---------- | 612| RENDER_RATE_NORMAL | 0 | 正常速度。 | 613| RENDER_RATE_DOUBLE | 1 | 2倍速。 | 614| RENDER_RATE_HALF | 2 | 0.5倍速。 | 615 616## InterruptType 617 618表示中断类型的枚举。 619 620**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 621 622**系统能力:** SystemCapability.Multimedia.Audio.Renderer 623 624| 名称 | 值 | 说明 | 625| -------------------- | ------ | ---------------------- | 626| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断事件开始。 | 627| INTERRUPT_TYPE_END | 2 | 音频播放中断事件结束。 | 628 629## InterruptForceType<sup>9+</sup> 630 631表示音频打断类型的枚举。 632 633当用户监听到音频中断(即收到[InterruptEvent](#interruptevent9)事件)时,获取此信息。 634 635此类型表示音频打断是否已由系统强制执行,具体操作信息(如音频暂停、停止等)可通过[InterruptHint](#interrupthint)获取。关于音频打断策略的详细说明可参考文档[音频焦点和音频会话介绍](../../media/audio/audio-playback-concurrency.md)。 636 637**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 638 639**系统能力:** SystemCapability.Multimedia.Audio.Renderer 640 641| 名称 | 值 | 说明 | 642| --------------- | ------ | ------------------------------------ | 643| INTERRUPT_FORCE | 0 | 强制打断类型,即具体操作已由系统强制执行。 | 644| INTERRUPT_SHARE | 1 | 共享打断类型,即系统不执行具体操作,通过[InterruptHint](#interrupthint)建议并提示应用操作,应用可自行决策下一步处理方式。 | 645 646## InterruptHint 647 648表示中断提示的枚举。 649 650当用户监听到音频中断事件(即收到[InterruptEvent](#interruptevent9)事件)时,获取此信息。 651 652此类型表示根据焦点策略,对音频流执行的具体操作(如暂停、调整音量等)。 653 654可以结合InterruptEvent中的[InterruptForceType](#interruptforcetype9)信息,判断该操作是否已由系统强制执行。详情请参阅文档[音频焦点和音频会话介绍](../../media/audio/audio-playback-concurrency.md)。 655 656**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 657 658**系统能力:** SystemCapability.Multimedia.Audio.Renderer 659 660| 名称 | 值 | 说明 | 661| ---------------------------------- | ------ | -------------------------------------------- | 662| INTERRUPT_HINT_NONE<sup>8+</sup> | 0 | 无提示。 | 663| INTERRUPT_HINT_RESUME | 1 | 提示音频恢复,应用可主动触发开始渲染或开始采集的相关操作。<br>此操作无法由系统强制执行,其对应的[InterruptForceType](#interruptforcetype9)一定为INTERRUPT_SHARE类型。 | 664| INTERRUPT_HINT_PAUSE | 2 | 提示音频暂停,暂时失去音频焦点。<br>待焦点可用时,会收到INTERRUPT_HINT_RESUME事件。 | 665| INTERRUPT_HINT_STOP | 3 | 提示音频停止,彻底失去音频焦点。 | 666| INTERRUPT_HINT_DUCK | 4 | 提示音频躲避开始,降低音量播放。 | 667| INTERRUPT_HINT_UNDUCK<sup>8+</sup> | 5 | 提示音频躲避结束,恢复音量播放。 | 668 669## AudioVolumeMode<sup>19+</sup> 670 671表示音量模式的枚举。 672 673**系统能力:** SystemCapability.Multimedia.Audio.Volume 674 675| 名称 | 值 | 说明 | 676| -------------- | ------ |--------------| 677| SYSTEM_GLOBAL | 0 | 系统级音量(默认模式)。 | 678| APP_INDIVIDUAL | 1 | 应用级音量。 | 679 680## AudioStreamInfo<sup>8+</sup> 681 682音频流信息。 683 684**系统能力:** SystemCapability.Multimedia.Audio.Core 685 686| 名称 | 类型 | 必填 | 说明 | 687| ------------ | ------------------------------------------------- | ---- | ------------------ | 688| samplingRate | [AudioSamplingRate](#audiosamplingrate8) | 是 | 音频文件的采样率。 | 689| channels | [AudioChannel](#audiochannel8) | 是 | 音频文件的通道数。 | 690| sampleFormat | [AudioSampleFormat](#audiosampleformat8) | 是 | 音频采样格式。 | 691| encodingType | [AudioEncodingType](#audioencodingtype8) | 是 | 音频编码格式。 | 692| channelLayout<sup>11+</sup> | [AudioChannelLayout](#audiochannellayout11) | 否 | 音频声道布局,默认值为0x0。 | 693 694## AudioRendererInfo<sup>8+</sup> 695 696音频渲染器信息。 697 698**系统能力:** SystemCapability.Multimedia.Audio.Core 699 700| 名称 | 类型 | 必填 | 说明 | 701| ------------- | --------------------------- | ---- | --------------- | 702| content | [ContentType](#contenttypedeprecated) | 否 | 音频内容类型。<br>API version 8、9为必填参数,从API version 10开始为可选参数,默认值为CONTENT_TYPE_UNKNOWN。<br>从API version 7开始支持,从API version 10开始废弃,建议使用[StreamUsage](#streamusage)替代。 | 703| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 704| rendererFlags | number | 是 | 音频渲染器标志。<br>0代表音频渲染器。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 705| volumeMode<sup>19+</sup> | [AudioVolumeMode](#audiovolumemode19) | 否 | 音频的音量模式。默认值为SYSTEM_GLOBAL。| 706 707## AudioRendererOptions<sup>8+</sup> 708 709音频渲染器选项信息。 710 711| 名称 | 类型 | 必填 | 说明 | 712| ------------ | ---------------------------------------- | ---- | ---------------- | 713| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 音频流信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 714| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 音频渲染器信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 715| privacyType<sup>10+</sup> | [AudioPrivacyType](#audioprivacytype10) | 否 | 表示音频流是否可以被其他应用录制,默认值为0。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture | 716 717## AudioPrivacyType<sup>10+</sup> 718 719表示对应播放音频流是否支持被其他应用录制的枚举。 720 721**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 722 723| 名称 | 值 | 说明 | 724| -------------------- | ---- | -------------------------------- | 725| PRIVACY_TYPE_PUBLIC | 0 | 表示音频流可以被其他应用录制。 | 726| PRIVACY_TYPE_PRIVATE | 1 | 表示音频流不可以被其他应用录制。 | 727 728## InterruptEvent<sup>9+</sup> 729 730音频中断时,应用接收的中断事件。 731 732**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 733 734**系统能力:** SystemCapability.Multimedia.Audio.Renderer 735 736| 名称 | 类型 |必填 | 说明 | 737| --------- | ------------------------------------------ | ---- | ------------------------------------ | 738| eventType | [InterruptType](#interrupttype) | 是 | 音频中断事件类型,开始或是结束。 | 739| forceType | [InterruptForceType](#interruptforcetype9) | 是 | 操作是由系统强制执行或是由应用程序执行。 | 740| hintType | [InterruptHint](#interrupthint) | 是 | 中断提示,用于提供中断事件的相关信息。 | 741 742## VolumeEvent<sup>9+</sup> 743 744音量改变时,应用接收到的事件。 745 746**系统能力:** SystemCapability.Multimedia.Audio.Volume 747 748| 名称 | 类型 | 必填 | 说明 | 749| ---------- | ----------------------------------- | ---- |-------------------------------------------| 750| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 751| volume | number | 是 |音量等级,可设置范围通过调用getMinVolume和getMaxVolume方法获取。 | 752| updateUi | boolean | 是 | 是否在UI中显示音量变化。true表示显示,false表示不显示。 | 753| volumeMode<sup>19+</sup> | [AudioVolumeMode](#audiovolumemode19) | 否 | 音频的音量模式。默认值为SYSTEM_GLOBAL。| 754 755## MicStateChangeEvent<sup>9+</sup> 756 757麦克风状态变化时,应用接收到的事件。 758 759**系统能力:** SystemCapability.Multimedia.Audio.Device 760 761| 名称 | 类型 | 必填 | 说明 | 762| ---------- | ----------------------------------- | ---- |-------------------------------------------------------- | 763| mute | boolean | 是 | 系统麦克风是否为静音状态。true表示静音,false表示非静音。 | 764 765## DeviceChangeAction 766 767描述设备连接状态变化和设备信息。 768 769**系统能力:** SystemCapability.Multimedia.Audio.Device 770 771| 名称 | 类型 | 必填 | 说明 | 772| :---------------- | :------------------------------------------------ | :--- | :----------------- | 773| type | [DeviceChangeType](#devicechangetype) | 是 | 设备连接状态变化。 | 774| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 775 776## DeviceBlockStatusInfo<sup>13+</sup> 777 778描述音频设备被堵塞状态和设备信息。 779 780**系统能力:** SystemCapability.Multimedia.Audio.Device 781 782| 名称 | 类型 | 必填 | 说明 | 783| :---------------- | :------------------------------------------------ | :--- | :----------------- | 784| blockStatus | [DeviceBlockStatus](#deviceblockstatus13) | 是 | 音频设备堵塞状态。 | 785| devices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 786 787## ChannelBlendMode<sup>11+</sup> 788 789表示声道混合模式类型的枚举。 790 791**系统能力:** SystemCapability.Multimedia.Audio.Core 792 793| 名称 | 值 | 说明 | 794| :------------------------------------------- | :----- | :--------------------- | 795| MODE_DEFAULT | 0 | 无声道混合。 | 796| MODE_BLEND_LR | 1 | 混合左右声道。 | 797| MODE_ALL_LEFT | 2 | 从左声道覆盖到右声道混合。 | 798| MODE_ALL_RIGHT | 3 | 从右声道覆盖到左声道混合。 | 799 800## AudioStreamDeviceChangeReason<sup>11+</sup> 801 802表示流设备变更原因的枚举。 803 804**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 805 806**系统能力:** SystemCapability.Multimedia.Audio.Device 807 808| 名称 | 值 | 说明 | 809|:------------------------------------------| :----- |:----------------| 810| REASON_UNKNOWN | 0 | 未知原因。 | 811| REASON_NEW_DEVICE_AVAILABLE | 1 | 新设备可用。 | 812| REASON_OLD_DEVICE_UNAVAILABLE | 2 | 旧设备不可用。报告此原因时,应考虑暂停音频播放。 | 813| REASON_OVERRODE | 3 | 强选。 | 814 815## AudioStreamDeviceChangeInfo<sup>11+</sup> 816 817流设备变更时,应用接收到的事件。 818 819**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 820 821**系统能力:** SystemCapability.Multimedia.Audio.Device 822 823| 名称 | 类型 | 必填 | 说明 | 824| :---------------- |:------------------------------------------------------------------| :--- | :----------------- | 825| devices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 826| changeReason | [AudioStreamDeviceChangeReason](#audiostreamdevicechangereason11) | 是 | 流设备变更原因。 | 827 828## DeviceChangeType 829 830表示设备连接状态变化的枚举。 831 832**系统能力:** SystemCapability.Multimedia.Audio.Device 833 834| 名称 | 值 | 说明 | 835| :--------- | :--- | :------------- | 836| CONNECT | 0 | 设备连接。 | 837| DISCONNECT | 1 | 断开设备连接。 | 838 839## DeviceBlockStatus<sup>13+</sup> 840 841表示音频设备是否被堵塞的枚举。 842 843**系统能力:** SystemCapability.Multimedia.Audio.Device 844 845| 名称 | 值 | 说明 | 846| :--------- | :--- | :------------- | 847| UNBLOCKED | 0 | 音频设备正常。 | 848| BLOCKED | 1 | 音频设备被堵塞。 | 849 850## AudioCapturerOptions<sup>8+</sup> 851 852音频采集器选项信息。 853 854| 名称 | 类型 | 必填 | 说明 | 855| ----------------------------------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 856| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 音频流信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 857| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 音频采集器信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 858| playbackCaptureConfig<sup>(deprecated)</sup> | [AudioPlaybackCaptureConfig](#audioplaybackcaptureconfigdeprecated) | 否 | 音频内录的配置信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture。<br/> 从API version 10开始支持,从API version 12开始废弃,建议使用[录屏接口AVScreenCapture](../apis-media-kit/capi-avscreencapture.md)替代。 | 859 860## AudioCapturerInfo<sup>8+</sup> 861 862描述音频采集器信息。 863 864**系统能力:** SystemCapability.Multimedia.Audio.Core 865 866| 名称 | 类型 | 必填 | 说明 | 867| :------------ | :------------------------ | :--- | :--------------- | 868| source | [SourceType](#sourcetype8) | 是 | 音源类型。 | 869| capturerFlags | number | 是 | 音频采集器标志。<br>0代表音频采集器。 | 870 871## SourceType<sup>8+</sup> 872 873表示音源类型的枚举。 874 875| 名称 | 值 | 说明 | 876| :------------------------------------------- | :----- | :--------------------- | 877| SOURCE_TYPE_INVALID | -1 | 无效的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 878| SOURCE_TYPE_MIC | 0 | Mic音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 879| SOURCE_TYPE_VOICE_RECOGNITION<sup>9+</sup> | 1 | 语音识别源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 880| SOURCE_TYPE_PLAYBACK_CAPTURE<sup>(deprecated)</sup> | 2 | 播放音频流(内录)录制音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture。<br/> 从API version 10开始支持,从API version 12开始废弃,建议使用[录屏接口AVScreenCapture](../apis-media-kit/capi-avscreencapture.md)替代。 | 881| SOURCE_TYPE_VOICE_COMMUNICATION | 7 | 语音通话场景的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 882| SOURCE_TYPE_VOICE_MESSAGE<sup>12+</sup> | 10 | 短语音消息的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 883| SOURCE_TYPE_CAMCORDER<sup>13+</sup> | 13 | 录像的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 884| SOURCE_TYPE_UNPROCESSED<sup>14+</sup> | 14 | 麦克风纯净录音的音频源(系统不做任何算法处理)。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 885| SOURCE_TYPE_LIVE<sup>20+</sup> | 17 | 直播场景的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 886 887## AudioPlaybackCaptureConfig<sup>(deprecated)</sup> 888 889音频内录的配置信息。 890 891> **说明:** 892> 从API version 10开始支持,从API version 12开始废弃,建议使用[录屏接口AVScreenCapture](../apis-media-kit/capi-avscreencapture.md)替代。 893 894**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 895 896| 名称 | 类型 | 必填 | 说明 | 897| ------------- | --------------------------------------------- | ---- | -------------------------------- | 898| filterOptions | [CaptureFilterOptions](#capturefilteroptionsdeprecated) | 是 | 需要录制的播放音频流的筛选信息。 | 899 900## CaptureFilterOptions<sup>(deprecated)</sup> 901 902待录制的播放音频流的筛选信息。 903 904> **说明:** 905> 从API version 10开始支持,从API version 12开始废弃,建议使用[录屏接口AVScreenCapture](../apis-media-kit/capi-avscreencapture.md)替代。 906 907**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 908 909| 名称 | 类型 | 必填 | 说明 | 910| ------ | ---------------------------------- | ---- | ------------------------------------------------------------ | 911| 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,所以当前接口不再涉及此权限。 | 912 913## AudioScene<sup>8+</sup> 914 915表示音频场景的枚举。 916 917**系统能力:** SystemCapability.Multimedia.Audio.Communication 918 919| 名称 | 值 | 说明 | 920| :--------------------- | :----- | :-------------------------------------------- | 921| AUDIO_SCENE_DEFAULT | 0 | 默认音频场景。 | 922| AUDIO_SCENE_RINGING<sup>12+</sup> | 1 | 响铃模式。 | 923| AUDIO_SCENE_PHONE_CALL<sup>12+</sup> | 2 | 电话模式。 | 924| AUDIO_SCENE_VOICE_CHAT | 3 | 语音聊天模式。 | 925 926## AudioConcurrencyMode<sup>12+</sup> 927 928表示音频并发模式的枚举。 929 930**系统能力:** SystemCapability.Multimedia.Audio.Core 931 932| 名称 | 值 | 说明 | 933| :--------------------- |:--|:--------| 934| CONCURRENCY_DEFAULT | 0 | 默认使用系统策略。 | 935| CONCURRENCY_MIX_WITH_OTHERS | 1 | 和其他音频并发。 | 936| CONCURRENCY_DUCK_OTHERS | 2 | 压低其他音频的音量。 | 937| CONCURRENCY_PAUSE_OTHERS | 3 | 暂停其他音频。 | 938 939## AudioSessionDeactivatedReason<sup>12+</sup> 940 941表示音频会话停用原因的枚举。 942 943**系统能力:** SystemCapability.Multimedia.Audio.Core 944 945| 名称 | 值 | 说明 | 946| :--------------------- |:--|:-------| 947| DEACTIVATED_LOWER_PRIORITY | 0 | 应用焦点被抢占。 | 948| DEACTIVATED_TIMEOUT | 1 | 音频会话等待超时。 | 949 950## AudioSessionStrategy<sup>12+</sup> 951 952音频会话策略。 953 954**系统能力:** SystemCapability.Multimedia.Audio.Core 955 956| 名称 | 类型 | 必填 | 说明 | 957| :------------ |:------------------------------------------------| :--- | :--------------- | 958| concurrencyMode | [AudioConcurrencyMode](#audioconcurrencymode12) | 是 | 音频并发模式。 | 959 960## AudioSessionDeactivatedEvent<sup>12+</sup> 961 962音频会话停用事件。 963 964**系统能力:** SystemCapability.Multimedia.Audio.Core 965 966| 名称 | 类型 | 必填 | 说明 | 967| :------------ |:------------------------------------------------------------------| :--- | :--------------- | 968| reason | [AudioSessionDeactivatedReason](#audiosessiondeactivatedreason12) | 是 | 音频会话停用原因。 | 969 970## AudioManager 971 972管理音频音量和设备。 973 974在使用AudioManager的接口之前,需先通过[getAudioManager](#audiogetaudiomanager)获取AudioManager实例。 975 976### setAudioParameter<sup>(deprecated)</sup> 977 978setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void 979 980音频参数设置。使用callback异步回调。 981 982接口根据硬件设备的支持能力扩展音频配置。支持的参数与产品和设备强相关,非通用参数,示例代码内使用样例参数。 983 984> **说明:** 985> 从API version 7开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 986 987**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 988 989**系统能力:** SystemCapability.Multimedia.Audio.Core 990 991**参数:** 992 993| 参数名 | 类型 | 必填 | 说明 | 994| -------- | ------------------------- | ---- | ------------------------ | 995| key | string | 是 | 被设置的音频参数的键。 | 996| value | string | 是 | 被设置的音频参数的值。 | 997| callback | AsyncCallback<void> | 是 | 回调函数。当音频参数设置成功,err为undefined,否则为错误对象。 | 998 999**示例:** 1000 1001```ts 1002import { BusinessError } from '@kit.BasicServicesKit'; 1003 1004audioManager.setAudioParameter('key_example', 'value_example', (err: BusinessError) => { 1005 if (err) { 1006 console.error(`Failed to set the audio parameter. ${err}`); 1007 return; 1008 } 1009 console.info('Callback invoked to indicate a successful setting of the audio parameter.'); 1010}); 1011``` 1012 1013### setAudioParameter<sup>(deprecated)</sup> 1014 1015setAudioParameter(key: string, value: string): Promise<void> 1016 1017音频参数设置。使用Promise异步回调。 1018 1019接口根据硬件设备的支持能力扩展音频配置。支持的参数与产品和设备强相关,非通用参数,示例代码内使用样例参数。 1020 1021> **说明:** 1022> 从API version 7开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 1023 1024**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 1025 1026**系统能力:** SystemCapability.Multimedia.Audio.Core 1027 1028**参数:** 1029 1030| 参数名 | 类型 | 必填 | 说明 | 1031| ------ | ------ | ---- | ---------------------- | 1032| key | string | 是 | 被设置的音频参数的键。 | 1033| value | string | 是 | 被设置的音频参数的值。 | 1034 1035**返回值:** 1036 1037| 类型 | 说明 | 1038| ------------------- | ------------------------------- | 1039| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1040 1041**示例:** 1042 1043```ts 1044audioManager.setAudioParameter('key_example', 'value_example').then(() => { 1045 console.info('Promise returned to indicate a successful setting of the audio parameter.'); 1046}); 1047``` 1048 1049### getAudioParameter<sup>(deprecated)</sup> 1050 1051getAudioParameter(key: string, callback: AsyncCallback<string>): void 1052 1053获取指定音频参数值。使用callback异步回调。 1054 1055本接口的使用场景为:根据硬件设备的支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1056 1057> **说明:** 1058> 从API version 7开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 1059 1060**系统能力:** SystemCapability.Multimedia.Audio.Core 1061 1062**参数:** 1063 1064| 参数名 | 类型 | 必填 | 说明 | 1065| -------- | --------------------------- | ---- | ---------------------------- | 1066| key | string | 是 | 待获取的音频参数的键。 | 1067| callback | AsyncCallback<string> | 是 | 回调函数。当获取指定音频参数值成功,err为undefined,data为获取到的指定音频参数值;否则为错误对象。 | 1068 1069**示例:** 1070 1071```ts 1072import { BusinessError } from '@kit.BasicServicesKit'; 1073 1074audioManager.getAudioParameter('key_example', (err: BusinessError, value: string) => { 1075 if (err) { 1076 console.error(`Failed to obtain the value of the audio parameter. ${err}`); 1077 return; 1078 } 1079 console.info(`Callback invoked to indicate that the value of the audio parameter is obtained ${value}.`); 1080}); 1081``` 1082 1083### getAudioParameter<sup>(deprecated)</sup> 1084 1085getAudioParameter(key: string): Promise<string> 1086 1087获取指定音频参数值。使用Promise异步回调。 1088 1089本接口的使用场景为:根据硬件设备的支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1090 1091> **说明:** 1092> 从API version 7开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 1093 1094**系统能力:** SystemCapability.Multimedia.Audio.Core 1095 1096**参数:** 1097 1098| 参数名 | 类型 | 必填 | 说明 | 1099| ------ | ------ | ---- | ---------------------- | 1100| key | string | 是 | 待获取的音频参数的键。 | 1101 1102**返回值:** 1103 1104| 类型 | 说明 | 1105| --------------------- | ----------------------------------- | 1106| Promise<string> | Promise对象,返回获取的音频参数值。 | 1107 1108**示例:** 1109 1110```ts 1111audioManager.getAudioParameter('key_example').then((value: string) => { 1112 console.info(`Promise returned to indicate that the value of the audio parameter is obtained ${value}.`); 1113}); 1114``` 1115 1116### getAudioScene<sup>8+</sup> 1117 1118getAudioScene\(callback: AsyncCallback<AudioScene\>\): void 1119 1120获取音频场景模式。使用callback异步回调。 1121 1122**系统能力:** SystemCapability.Multimedia.Audio.Communication 1123 1124**参数:** 1125 1126| 参数名 | 类型 | 必填 | 说明 | 1127| :------- | :-------------------------------------------------- | :--- | :--------------------------- | 1128| callback | AsyncCallback<[AudioScene](#audioscene8)> | 是 | 回调函数。当获取音频场景模式成功,err为undefined,data为获取到的音频场景模式;否则为错误对象。 | 1129 1130**示例:** 1131 1132```ts 1133import { BusinessError } from '@kit.BasicServicesKit'; 1134 1135audioManager.getAudioScene((err: BusinessError, value: audio.AudioScene) => { 1136 if (err) { 1137 console.error(`Failed to obtain the audio scene mode. ${err}`); 1138 return; 1139 } 1140 console.info(`Callback invoked to indicate that the audio scene mode is obtained ${value}.`); 1141}); 1142``` 1143 1144### getAudioScene<sup>8+</sup> 1145 1146getAudioScene\(\): Promise<AudioScene\> 1147 1148获取音频场景模式。使用Promise异步回调。 1149 1150**系统能力:** SystemCapability.Multimedia.Audio.Communication 1151 1152**返回值:** 1153 1154| 类型 | 说明 | 1155| :-------------------------------------------- | :--------------------------- | 1156| Promise<[AudioScene](#audioscene8)> | Promise对象,返回音频场景模式。 | 1157 1158**示例:** 1159 1160```ts 1161import { BusinessError } from '@kit.BasicServicesKit'; 1162 1163audioManager.getAudioScene().then((value: audio.AudioScene) => { 1164 console.info(`Promise returned to indicate that the audio scene mode is obtained ${value}.`); 1165}).catch ((err: BusinessError) => { 1166 console.error(`Failed to obtain the audio scene mode ${err}`); 1167}); 1168``` 1169 1170### getAudioSceneSync<sup>10+</sup> 1171 1172getAudioSceneSync\(\): AudioScene 1173 1174获取音频场景模式。同步返回结果。 1175 1176**系统能力:** SystemCapability.Multimedia.Audio.Communication 1177 1178**返回值:** 1179 1180| 类型 | 说明 | 1181| :-------------------------------------------- | :--------------------------- | 1182| [AudioScene](#audioscene8) | 音频场景模式。 | 1183 1184**示例:** 1185 1186```ts 1187import { BusinessError } from '@kit.BasicServicesKit'; 1188 1189try { 1190 let value: audio.AudioScene = audioManager.getAudioSceneSync(); 1191 console.info(`indicate that the audio scene mode is obtained ${value}.`); 1192} catch (err) { 1193 let error = err as BusinessError; 1194 console.error(`Failed to obtain the audio scene mode ${error}`); 1195} 1196``` 1197 1198### on('audioSceneChange')<sup>20+</sup> 1199 1200on(type: 'audioSceneChange', callback: Callback\<AudioScene\>): void 1201 1202监听音频场景变化事件。使用callback异步回调。 1203 1204**系统能力:** SystemCapability.Multimedia.Audio.Communication 1205 1206**参数:** 1207 1208| 参数名 | 类型 | 必填 | 说明 | 1209| :------- | :------------------------- | :--- | :------------------------------------------ | 1210| type | string | 是 | 事件回调类型,支持的事件为'audioSceneChange',当音频场景模式发生变化时,触发该事件。 | 1211| callback | Callback\<[AudioScene](#audioscene8)> | 是 | 回调函数,返回当前音频场景模式。 | 1212 1213**示例:** 1214 1215```ts 1216audioManager.on('audioSceneChange', (audioScene: audio.AudioScene) => { 1217 console.info(`audio scene : ${audioScene}.`); 1218}); 1219``` 1220 1221### off('audioSceneChange')<sup>20+</sup> 1222 1223off(type: 'audioSceneChange', callback?: Callback\<AudioScene\>): void 1224 1225取消监听音频场景变化事件。使用callback异步回调。 1226 1227**系统能力:** SystemCapability.Multimedia.Audio.Communication 1228 1229**参数:** 1230 1231| 参数名 | 类型 | 必填 | 说明 | 1232| :------- | :------------------------- | :--- | :------------------------------------------ | 1233| type | string | 是 | 事件回调类型,支持的事件为'audioSceneChange',当取消监听当前音频场景变化事件时,触发该事件。 | 1234| callback | Callback\<[AudioScene](#audioscene8)> | 是 | 回调函数,返回当前音频场景模式。 | 1235 1236**示例:** 1237 1238```ts 1239// 取消该事件的所有监听。 1240audioManager.off('audioSceneChange'); 1241 1242// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1243let audioSceneChangeCallback = (audioScene: audio.AudioScene) => { 1244 console.info(`audio scene : ${audioScene}.`); 1245}; 1246 1247audioManager.on('audioSceneChange', audioSceneChangeCallback); 1248 1249audioManager.off('audioSceneChange', audioSceneChangeCallback); 1250``` 1251 1252### getVolumeManager<sup>9+</sup> 1253 1254getVolumeManager(): AudioVolumeManager 1255 1256获取音频音量管理器。 1257 1258**系统能力:** SystemCapability.Multimedia.Audio.Volume 1259 1260**返回值:** 1261 1262| 类型 | 说明 | 1263|-----------------------------------------| ----------------------------- | 1264| [AudioVolumeManager](#audiovolumemanager9) | AudioVolumeManager实例。 | 1265 1266**示例:** 1267 1268```ts 1269import { audio } from '@kit.AudioKit'; 1270 1271let audioVolumeManager: audio.AudioVolumeManager = audioManager.getVolumeManager(); 1272``` 1273 1274### getStreamManager<sup>9+</sup> 1275 1276getStreamManager(): AudioStreamManager 1277 1278获取音频流管理器。 1279 1280**系统能力:** SystemCapability.Multimedia.Audio.Core 1281 1282**返回值:** 1283 1284| 类型 | 说明 | 1285|--------------------------------------------| ----------------------------- | 1286| [AudioStreamManager](#audiostreammanager9) | AudioStreamManager实例。 | 1287 1288**示例:** 1289 1290```ts 1291import { audio } from '@kit.AudioKit'; 1292 1293let audioStreamManager: audio.AudioStreamManager = audioManager.getStreamManager(); 1294``` 1295 1296### getRoutingManager<sup>9+</sup> 1297 1298getRoutingManager(): AudioRoutingManager 1299 1300获取音频路由管理器。 1301 1302**系统能力:** SystemCapability.Multimedia.Audio.Device 1303 1304**返回值:** 1305 1306| 类型 | 说明 | 1307|------------------------------------------| ----------------------------- | 1308| [AudioRoutingManager](#audioroutingmanager9) | AudioRoutingManager实例。 | 1309 1310**示例:** 1311 1312```ts 1313import { audio } from '@kit.AudioKit'; 1314 1315let audioRoutingManager: audio.AudioRoutingManager = audioManager.getRoutingManager(); 1316``` 1317 1318### getSessionManager<sup>12+</sup> 1319 1320getSessionManager(): AudioSessionManager 1321 1322获取音频会话管理器。 1323 1324**系统能力:** SystemCapability.Multimedia.Audio.Core 1325 1326**返回值:** 1327 1328| 类型 | 说明 | 1329|----------------------------------------------| ----------------------------- | 1330| [AudioSessionManager](#audiosessionmanager12) | AudioSessionManager实例。 | 1331 1332**示例:** 1333 1334```ts 1335import { audio } from '@kit.AudioKit'; 1336 1337let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager(); 1338``` 1339 1340### getSpatializationManager<sup>18+</sup> 1341 1342getSpatializationManager(): AudioSpatializationManager 1343 1344获取空间音频管理器。 1345 1346**系统能力:** SystemCapability.Multimedia.Audio.Spatialization 1347 1348**返回值:** 1349 1350| 类型 | 说明 | 1351|------------------------------------------| ----------------------------- | 1352| [AudioSpatializationManager](#audiospatializationmanager18) | AudioSpatializationManager实例。 | 1353 1354**示例:** 1355 1356```ts 1357import { audio } from '@kit.AudioKit'; 1358let audioSpatializationManager: audio.AudioSpatializationManager = audioManager.getSpatializationManager(); 1359``` 1360 1361### setVolume<sup>(deprecated)</sup> 1362 1363setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void 1364 1365设置指定流的音量。使用callback异步回调。 1366 1367> **说明:** 1368> 1369> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1370> 1371> 应用无法直接调节系统音量,建议通过系统音量面板组件调节音量。具体样例和介绍请查看[AVVolumePanel参考文档](ohos-multimedia-avvolumepanel.md)。 1372 1373**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1374 1375仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1376 1377**系统能力:** SystemCapability.Multimedia.Audio.Volume 1378 1379**参数:** 1380 1381| 参数名 | 类型 | 必填 | 说明 | 1382| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1383| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1384| volume | number | 是 | 音量等级,可设置范围通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1385| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定流的音量成功,err为undefined,否则为错误对象。 | 1386 1387**示例:** 1388 1389```ts 1390import { BusinessError } from '@kit.BasicServicesKit'; 1391 1392audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err: BusinessError) => { 1393 if (err) { 1394 console.error(`Failed to set the volume. ${err}`); 1395 return; 1396 } 1397 console.info('Callback invoked to indicate a successful volume setting.'); 1398}); 1399``` 1400 1401### setVolume<sup>(deprecated)</sup> 1402 1403setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> 1404 1405设置指定流的音量。使用Promise异步回调。 1406 1407> **说明:** 1408> 1409> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1410> 1411> 应用无法直接调节系统音量,建议通过系统音量面板组件调节音量。具体样例和介绍请查看[AVVolumePanel参考文档](ohos-multimedia-avvolumepanel.md)。 1412 1413**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1414 1415仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1416 1417**系统能力:** SystemCapability.Multimedia.Audio.Volume 1418 1419**参数:** 1420 1421| 参数名 | 类型 | 必填 | 说明 | 1422| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1423| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1424| volume | number | 是 | 音量等级,可设置范围通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1425 1426**返回值:** 1427 1428| 类型 | 说明 | 1429| ------------------- | ----------------------------- | 1430| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1431 1432**示例:** 1433 1434```ts 1435audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => { 1436 console.info('Promise returned to indicate a successful volume setting.'); 1437}); 1438``` 1439 1440### getVolume<sup>(deprecated)</sup> 1441 1442getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1443 1444获取指定流的音量。使用callback异步回调。 1445 1446> **说明:** 1447> 从API version 7开始支持,从API version 9开始废弃,建议使用[getVolume](#getvolume9)替代。 1448 1449**系统能力:** SystemCapability.Multimedia.Audio.Volume 1450 1451**参数:** 1452 1453| 参数名 | 类型 | 必填 | 说明 | 1454| ---------- | ----------------------------------- | ---- | ------------------ | 1455| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1456| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。指定流的音量等级范围可通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1457 1458**示例:** 1459 1460```ts 1461import { BusinessError } from '@kit.BasicServicesKit'; 1462 1463audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1464 if (err) { 1465 console.error(`Failed to obtain the volume. ${err}`); 1466 return; 1467 } 1468 console.info('Callback invoked to indicate that the volume is obtained.'); 1469}); 1470``` 1471 1472### getVolume<sup>(deprecated)</sup> 1473 1474getVolume(volumeType: AudioVolumeType): Promise<number> 1475 1476获取指定流的音量。使用Promise异步回调。 1477 1478> **说明:** 1479> 从API version 7开始支持,从API version 9开始废弃,建议使用[getVolume](#getvolume9)替代。 1480 1481**系统能力:** SystemCapability.Multimedia.Audio.Volume 1482 1483**参数:** 1484 1485| 参数名 | 类型 | 必填 | 说明 | 1486| ---------- | ----------------------------------- | ---- | ------------ | 1487| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1488 1489**返回值:** 1490 1491| 类型 | 说明 | 1492| --------------------- | ------------------------- | 1493| Promise<number> | Promise对象,返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolumedeprecated)和[getMaxVolume](#getmaxvolumedeprecated)获取。 | 1494 1495**示例:** 1496 1497```ts 1498audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1499 console.info(`Promise returned to indicate that the volume is obtained ${value} .`); 1500}); 1501``` 1502 1503### getMinVolume<sup>(deprecated)</sup> 1504 1505getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1506 1507获取指定流的最小音量。使用callback异步回调。 1508 1509> **说明:** 1510> 从API version 7开始支持,从API version 9开始废弃,建议使用[getMinVolume](#getminvolume9)替代。 1511 1512**系统能力:** SystemCapability.Multimedia.Audio.Volume 1513 1514**参数:** 1515 1516| 参数名 | 类型 | 必填 | 说明 | 1517| ---------- | ----------------------------------- | ---- | ------------------ | 1518| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1519| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 1520 1521**示例:** 1522 1523```ts 1524import { BusinessError } from '@kit.BasicServicesKit'; 1525 1526audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1527 if (err) { 1528 console.error(`Failed to obtain the minimum volume. ${err}`); 1529 return; 1530 } 1531 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 1532}); 1533``` 1534 1535### getMinVolume<sup>(deprecated)</sup> 1536 1537getMinVolume(volumeType: AudioVolumeType): Promise<number> 1538 1539获取指定流的最小音量。使用Promise异步回调。 1540 1541> **说明:** 1542> 从API version 7开始支持,从API version 9开始废弃,建议使用[getMinVolume](#getminvolume9)替代。 1543 1544**系统能力:** SystemCapability.Multimedia.Audio.Volume 1545 1546**参数:** 1547 1548| 参数名 | 类型 | 必填 | 说明 | 1549| ---------- | ----------------------------------- | ---- | ------------ | 1550| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1551 1552**返回值:** 1553 1554| 类型 | 说明 | 1555| --------------------- | ------------------------- | 1556| Promise<number> | Promise对象,返回最小音量。 | 1557 1558**示例:** 1559 1560```ts 1561audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1562 console.info(`Promised returned to indicate that the minimum volume is obtained. ${value}`); 1563}); 1564``` 1565 1566### getMaxVolume<sup>(deprecated)</sup> 1567 1568getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1569 1570获取指定流的最大音量。使用callback异步回调。 1571 1572> **说明:** 1573> 从API version 7开始支持,从API version 9开始废弃,建议使用[getMaxVolume](#getmaxvolume9)替代。 1574 1575**系统能力:** SystemCapability.Multimedia.Audio.Volume 1576 1577**参数:** 1578 1579| 参数名 | 类型 | 必填 | 说明 | 1580| ---------- | ----------------------------------- | ---- | ---------------------- | 1581| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1582| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 1583 1584**示例:** 1585 1586```ts 1587import { BusinessError } from '@kit.BasicServicesKit'; 1588 1589audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1590 if (err) { 1591 console.error(`Failed to obtain the maximum volume. ${err}`); 1592 return; 1593 } 1594 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 1595}); 1596``` 1597 1598### getMaxVolume<sup>(deprecated)</sup> 1599 1600getMaxVolume(volumeType: AudioVolumeType): Promise<number> 1601 1602获取指定流的最大音量。使用Promise异步回调。 1603 1604> **说明:** 1605> 从API version 7开始支持,从API version 9开始废弃,建议使用[getMaxVolume](#getmaxvolume9)替代。 1606 1607**系统能力:** SystemCapability.Multimedia.Audio.Volume 1608 1609**参数:** 1610 1611| 参数名 | 类型 | 必填 | 说明 | 1612| ---------- | ----------------------------------- | ---- | ------------ | 1613| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1614 1615**返回值:** 1616 1617| 类型 | 说明 | 1618| --------------------- | ----------------------------- | 1619| Promise<number> | Promise对象,返回最大音量。 | 1620 1621**示例:** 1622 1623```ts 1624audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 1625 console.info('Promised returned to indicate that the maximum volume is obtained.'); 1626}); 1627``` 1628 1629### mute<sup>(deprecated)</sup> 1630 1631mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void 1632 1633设置指定音量流静音。使用callback异步回调。 1634 1635当该音量流可设置的最小音量不能为0时,不支持静音操作。例如:闹钟和通话。 1636 1637> **说明:** 1638> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1639 1640**系统能力:** SystemCapability.Multimedia.Audio.Volume 1641 1642**参数:** 1643 1644| 参数名 | 类型 | 必填 | 说明 | 1645| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1646| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1647| mute | boolean | 是 | 是否设置指定音量流为静音状态。true表示静音,false表示非静音。 | 1648| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定音量流静音成功,err为undefined,否则为错误对象。 | 1649 1650**示例:** 1651 1652```ts 1653import { BusinessError } from '@kit.BasicServicesKit'; 1654 1655audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err: BusinessError) => { 1656 if (err) { 1657 console.error(`Failed to mute the stream. ${err}`); 1658 return; 1659 } 1660 console.info('Callback invoked to indicate that the stream is muted.'); 1661}); 1662``` 1663 1664### mute<sup>(deprecated)</sup> 1665 1666mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> 1667 1668设置指定音量流静音。使用Promise异步回调。 1669 1670当该音量流可设置的最小音量不能为0时,不支持静音操作。例如:闹钟和通话。 1671 1672> **说明:** 1673> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1674 1675**系统能力:** SystemCapability.Multimedia.Audio.Volume 1676 1677**参数:** 1678 1679| 参数名 | 类型 | 必填 | 说明 | 1680| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1681| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1682| mute | boolean | 是 | 是否设置指定音量流为静音状态。true表示静音,false表示非静音。 | 1683 1684**返回值:** 1685 1686| 类型 | 说明 | 1687| ------------------- | ----------------------------- | 1688| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1689 1690**示例:** 1691 1692 1693```ts 1694audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => { 1695 console.info('Promise returned to indicate that the stream is muted.'); 1696}); 1697``` 1698 1699### isMute<sup>(deprecated)</sup> 1700 1701isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1702 1703获取指定音量流的静音状态。使用callback异步回调。 1704 1705> **说明:** 1706> 从API version 7开始支持,从API version 9开始废弃,建议使用[isMute](#ismute9)替代。 1707 1708**系统能力:** SystemCapability.Multimedia.Audio.Volume 1709 1710**参数:** 1711 1712| 参数名 | 类型 | 必填 | 说明 | 1713| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 1714| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1715| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流的静音状态成功,err为undefined,data为true表示静音,false表示非静音;否则为错误对象。 | 1716 1717**示例:** 1718 1719```ts 1720import { BusinessError } from '@kit.BasicServicesKit'; 1721 1722audioManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1723 if (err) { 1724 console.error(`Failed to obtain the mute status. ${err}`); 1725 return; 1726 } 1727 console.info(`Callback invoked to indicate that the mute status of the stream is obtained. ${value}`); 1728}); 1729``` 1730 1731### isMute<sup>(deprecated)</sup> 1732 1733isMute(volumeType: AudioVolumeType): Promise<boolean> 1734 1735获取指定音量流的静音状态。使用Promise异步回调。 1736 1737> **说明:** 1738> 从API version 7开始支持,从API version 9开始废弃,建议使用[isMute](#ismute9)替代。 1739 1740**系统能力:** SystemCapability.Multimedia.Audio.Volume 1741 1742**参数:** 1743 1744| 参数名 | 类型 | 必填 | 说明 | 1745| ---------- | ----------------------------------- | ---- | ------------ | 1746| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1747 1748**返回值:** 1749 1750| 类型 | 说明 | 1751| ---------------------- | ------------------------------------------------------ | 1752| Promise<boolean> | Promise对象。返回true表示静音;返回false表示非静音。 | 1753 1754**示例:** 1755 1756```ts 1757audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1758 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 1759}); 1760``` 1761 1762### isActive<sup>(deprecated)</sup> 1763 1764isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1765 1766获取指定音量流的活跃状态。使用callback异步回调。 1767 1768> **说明:** 1769> 从API version 7开始支持,从API version 9开始废弃,建议使用[isActive](#isactive9)替代。 1770 1771**系统能力:** SystemCapability.Multimedia.Audio.Volume 1772 1773**参数:** 1774 1775| 参数名 | 类型 | 必填 | 说明 | 1776| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 1777| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1778| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流的活跃状态成功,err为undefined,data为true表示活跃,false表示不活跃;否则为错误对象。 | 1779 1780**示例:** 1781 1782```ts 1783import { BusinessError } from '@kit.BasicServicesKit'; 1784 1785audioManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1786 if (err) { 1787 console.error(`Failed to obtain the active status of the stream. ${err}`); 1788 return; 1789 } 1790 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 1791}); 1792``` 1793 1794### isActive<sup>(deprecated)</sup> 1795 1796isActive(volumeType: AudioVolumeType): Promise<boolean> 1797 1798获取指定音量流的活跃状态。使用Promise异步回调。 1799 1800> **说明:** 1801> 从API version 7开始支持,从API version 9开始废弃,建议使用[isActive](#isactive9)替代。 1802 1803**系统能力:** SystemCapability.Multimedia.Audio.Volume 1804 1805**参数:** 1806 1807| 参数名 | 类型 | 必填 | 说明 | 1808| ---------- | ----------------------------------- | ---- | ------------ | 1809| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1810 1811**返回值:** 1812 1813| 类型 | 说明 | 1814| ---------------------- | -------------------------------------------------------- | 1815| Promise<boolean> | Promise对象。返回true表示流状态为活跃;返回false表示流状态不活跃。 | 1816 1817**示例:** 1818 1819```ts 1820audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1821 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 1822}); 1823``` 1824 1825### setRingerMode<sup>(deprecated)</sup> 1826 1827setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void 1828 1829设置铃声模式。使用callback异步回调。 1830 1831> **说明:** 1832> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1833 1834**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1835 1836仅在静音和非静音状态切换时需要该权限。 1837 1838**系统能力:** SystemCapability.Multimedia.Audio.Communication 1839 1840**参数:** 1841 1842| 参数名 | 类型 | 必填 | 说明 | 1843| -------- | ------------------------------- | ---- | ------------------------ | 1844| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1845| callback | AsyncCallback<void> | 是 | 回调函数。当设置铃声模式成功,err为undefined,否则为错误对象。 | 1846 1847**示例:** 1848 1849```ts 1850import { BusinessError } from '@kit.BasicServicesKit'; 1851 1852audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err: BusinessError) => { 1853 if (err) { 1854 console.error(`Failed to set the ringer mode. ${err}`); 1855 return; 1856 } 1857 console.info('Callback invoked to indicate a successful setting of the ringer mode.'); 1858}); 1859``` 1860 1861### setRingerMode<sup>(deprecated)</sup> 1862 1863setRingerMode(mode: AudioRingMode): Promise<void> 1864 1865设置铃声模式。使用Promise异步回调。 1866 1867> **说明:** 1868> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 1869 1870 1871**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1872 1873仅在静音和非静音状态切换时需要该权限。 1874 1875**系统能力:** SystemCapability.Multimedia.Audio.Communication 1876 1877**参数:** 1878 1879| 参数名 | 类型 | 必填 | 说明 | 1880| ------ | ------------------------------- | ---- | -------------- | 1881| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1882 1883**返回值:** 1884 1885| 类型 | 说明 | 1886| ------------------- | ------------------------------- | 1887| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1888 1889**示例:** 1890 1891```ts 1892audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => { 1893 console.info('Promise returned to indicate a successful setting of the ringer mode.'); 1894}); 1895``` 1896 1897### getRingerMode<sup>(deprecated)</sup> 1898 1899getRingerMode(callback: AsyncCallback<AudioRingMode>): void 1900 1901获取铃声模式。使用callback异步回调。 1902 1903> **说明:** 1904> 从API version 7开始支持,从API version 9开始废弃,建议使用[getRingerMode](#getringermode9)替代。 1905 1906**系统能力:** SystemCapability.Multimedia.Audio.Communication 1907 1908**参数:** 1909 1910| 参数名 | 类型 | 必填 | 说明 | 1911| -------- | ---------------------------------------------------- | ---- | ------------------------ | 1912| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 1913 1914**示例:** 1915 1916```ts 1917import { BusinessError } from '@kit.BasicServicesKit'; 1918 1919audioManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 1920 if (err) { 1921 console.error(`Failed to obtain the ringer mode. ${err}`); 1922 return; 1923 } 1924 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 1925}); 1926``` 1927 1928### getRingerMode<sup>(deprecated)</sup> 1929 1930getRingerMode(): Promise<AudioRingMode> 1931 1932获取铃声模式。使用Promise异步回调。 1933 1934> **说明:** 1935> 从API version 7开始支持,从API version 9开始废弃,建议使用[getRingerMode](#getringermode9)替代。 1936 1937**系统能力:** SystemCapability.Multimedia.Audio.Communication 1938 1939**返回值:** 1940 1941| 类型 | 说明 | 1942| ---------------------------------------------- | ------------------------------- | 1943| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 1944 1945**示例:** 1946 1947```ts 1948audioManager.getRingerMode().then((value: audio.AudioRingMode) => { 1949 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 1950}); 1951``` 1952 1953### getDevices<sup>(deprecated)</sup> 1954 1955getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 1956 1957获取音频设备列表。使用callback异步回调。 1958 1959> **说明:** 1960> 从API version 7开始支持,从API version 9开始废弃,建议使用[getDevices](#getdevices9)替代。 1961 1962**系统能力:** SystemCapability.Multimedia.Audio.Device 1963 1964**参数:** 1965 1966| 参数名 | 类型 | 必填 | 说明 | 1967| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 1968| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 1969| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 1970 1971**示例:** 1972```ts 1973import { BusinessError } from '@kit.BasicServicesKit'; 1974 1975audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 1976 if (err) { 1977 console.error(`Failed to obtain the device list. ${err}`); 1978 return; 1979 } 1980 console.info('Callback invoked to indicate that the device list is obtained.'); 1981}); 1982``` 1983 1984### getDevices<sup>(deprecated)</sup> 1985 1986getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 1987 1988获取音频设备列表。使用Promise异步回调。 1989 1990> **说明:** 1991> 从API version 7开始支持,从API version 9开始废弃,建议使用[getDevices](#getdevices9)替代。 1992 1993**系统能力:** SystemCapability.Multimedia.Audio.Device 1994 1995**参数:** 1996 1997| 参数名 | 类型 | 必填 | 说明 | 1998| ---------- | ------------------------- | ---- | ---------------- | 1999| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 2000 2001**返回值:** 2002 2003| 类型 | 说明 | 2004| ------------------------------------------------------------ | ------------------------- | 2005| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 2006 2007**示例:** 2008 2009```ts 2010audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 2011 console.info('Promise returned to indicate that the device list is obtained.'); 2012}); 2013``` 2014 2015### setDeviceActive<sup>(deprecated)</sup> 2016 2017setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void 2018 2019设置设备激活状态。使用callback异步回调。 2020 2021> **说明:** 2022> 从API version 7开始支持,从API version 9开始废弃,建议使用[setCommunicationDevice](#setcommunicationdevice9)替代。 2023 2024**系统能力:** SystemCapability.Multimedia.Audio.Device 2025 2026**参数:** 2027 2028| 参数名 | 类型 | 必填 | 说明 | 2029| ---------- | ------------------------------------- | ---- |-------------| 2030| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 2031| active | boolean | 是 | 是否设置设备为激活状态。true表示已激活,false表示未激活。 | 2032| callback | AsyncCallback<void> | 是 | 回调函数。当设置设备激活状态成功,err为undefined,否则为错误对象。 | 2033 2034**示例:** 2035 2036```ts 2037import { BusinessError } from '@kit.BasicServicesKit'; 2038 2039audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err: BusinessError) => { 2040 if (err) { 2041 console.error(`Failed to set the active status of the device. ${err}`); 2042 return; 2043 } 2044 console.info('Callback invoked to indicate that the device is set to the active status.'); 2045}); 2046``` 2047 2048### setDeviceActive<sup>(deprecated)</sup> 2049 2050setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void> 2051 2052设置设备激活状态。使用Promise异步回调。 2053 2054> **说明:** 2055> 从API version 7开始支持,从API version 9开始废弃,建议使用[setCommunicationDevice](#setcommunicationdevice9)替代。 2056 2057**系统能力:** SystemCapability.Multimedia.Audio.Device 2058 2059**参数:** 2060 2061| 参数名 | 类型 | 必填 | 说明 | 2062| ---------- | ------------------------------------- | ---- | ------------------ | 2063| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 2064| active | boolean | 是 | 是否设置设备为激活状态。true表示已激活,false表示未激活。 | 2065 2066**返回值:** 2067 2068| 类型 | 说明 | 2069| ------------------- | ------------------------------- | 2070| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2071 2072**示例:** 2073 2074 2075```ts 2076audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => { 2077 console.info('Promise returned to indicate that the device is set to the active status.'); 2078}); 2079``` 2080 2081### isDeviceActive<sup>(deprecated)</sup> 2082 2083isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void 2084 2085获取指定设备的激活状态。使用callback异步回调。 2086 2087> **说明:** 2088> 从API version 7开始支持,从API version 9开始废弃,建议使用[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 2089 2090**系统能力:** SystemCapability.Multimedia.Audio.Device 2091 2092**参数:** 2093 2094| 参数名 | 类型 | 必填 | 说明 | 2095| ---------- | ------------------------------------- | ---- | ------------------------ | 2096| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 2097| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定设备的激活状态成功,err为undefined,data为true表示激活,false表示未激活;否则为错误对象。 | 2098 2099**示例:** 2100 2101```ts 2102import { BusinessError } from '@kit.BasicServicesKit'; 2103 2104audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 2105 if (err) { 2106 console.error(`Failed to obtain the active status of the device. ${err}`); 2107 return; 2108 } 2109 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 2110}); 2111``` 2112 2113### isDeviceActive<sup>(deprecated)</sup> 2114 2115isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean> 2116 2117获取指定设备的激活状态。使用Promise异步回调。 2118 2119> **说明:** 2120> 从API version 7开始支持,从API version 9开始废弃,建议使用[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 2121 2122**系统能力:** SystemCapability.Multimedia.Audio.Device 2123 2124**参数:** 2125 2126| 参数名 | 类型 | 必填 | 说明 | 2127| ---------- | ------------------------------------- | ---- | ------------------ | 2128| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 2129 2130**返回值:** 2131 2132| Type | Description | 2133| ---------------------- |---------------------------------------| 2134| Promise<boolean> | Promise对象。返回true表示设备已激活;返回false表示设备未激活。 | 2135 2136**示例:** 2137 2138```ts 2139audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value: boolean) => { 2140 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 2141}); 2142``` 2143 2144### setMicrophoneMute<sup>(deprecated)</sup> 2145 2146setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 2147 2148设置麦克风静音状态。使用callback异步回调。 2149 2150> **说明:** 2151> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 2152 2153**需要权限:** ohos.permission.MICROPHONE 2154 2155**系统能力:** SystemCapability.Multimedia.Audio.Device 2156 2157**参数:** 2158 2159| 参数名 | 类型 | 必填 | 说明 | 2160| -------- | ------------------------- | ---- | --------------------------------------------- | 2161| mute | boolean | 是 | 是否设置麦克风为静音状态。true表示静音,false表示非静音。 | 2162| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 2163 2164**示例:** 2165 2166```ts 2167import { BusinessError } from '@kit.BasicServicesKit'; 2168 2169audioManager.setMicrophoneMute(true, (err: BusinessError) => { 2170 if (err) { 2171 console.error(`Failed to mute the microphone. ${err}`); 2172 return; 2173 } 2174 console.info('Callback invoked to indicate that the microphone is muted.'); 2175}); 2176``` 2177 2178### setMicrophoneMute<sup>(deprecated)</sup> 2179 2180setMicrophoneMute(mute: boolean): Promise<void> 2181 2182设置麦克风静音状态。使用Promise异步回调。 2183 2184> **说明:** 2185> 从API version 7开始支持,从API version 9开始废弃,替代接口仅面向系统应用开放。 2186 2187**需要权限:** ohos.permission.MICROPHONE 2188 2189**系统能力:** SystemCapability.Multimedia.Audio.Device 2190 2191**参数:** 2192 2193| 参数名 | 类型 | 必填 | 说明 | 2194| ------ | ------- | ---- | --------------------------------------------- | 2195| mute | boolean | 是 | 是否设置麦克风为静音状态。true表示静音,false表示非静音。 | 2196 2197**返回值:** 2198 2199| 类型 | 说明 | 2200| ------------------- | ------------------------------- | 2201| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2202 2203**示例:** 2204 2205```ts 2206audioManager.setMicrophoneMute(true).then(() => { 2207 console.info('Promise returned to indicate that the microphone is muted.'); 2208}); 2209``` 2210 2211### isMicrophoneMute<sup>(deprecated)</sup> 2212 2213isMicrophoneMute(callback: AsyncCallback<boolean>): void 2214 2215获取麦克风静音状态。使用callback异步回调。 2216 2217> **说明:** 2218> 从API version 7开始支持,从API version 9开始废弃,建议使用[isMicrophoneMute](#ismicrophonemute9)替代。 2219 2220**需要权限:** ohos.permission.MICROPHONE 2221 2222**系统能力:** SystemCapability.Multimedia.Audio.Device 2223 2224**参数:** 2225 2226| 参数名 | 类型 | 必填 | 说明 | 2227| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 2228| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true表示静音,false表示非静音;否则为错误对象。 | 2229 2230**示例:** 2231 2232```ts 2233import { BusinessError } from '@kit.BasicServicesKit'; 2234 2235audioManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 2236 if (err) { 2237 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 2238 return; 2239 } 2240 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 2241}); 2242``` 2243 2244### isMicrophoneMute<sup>(deprecated)</sup> 2245 2246isMicrophoneMute(): Promise<boolean> 2247 2248获取麦克风静音状态。使用Promise异步回调。 2249 2250> **说明:** 2251> 从API version 7开始支持,从API version 9开始废弃,建议使用[isMicrophoneMute](#ismicrophonemute9)替代。 2252 2253**需要权限:** ohos.permission.MICROPHONE 2254 2255**系统能力:** SystemCapability.Multimedia.Audio.Device 2256 2257**返回值:** 2258 2259| 类型 | 说明 | 2260| ---------------------- | ------------------------------------------------------------ | 2261| Promise<boolean> | Promise对象。返回true表示麦克风被静音;返回false表示麦克风未被静音。 | 2262 2263**示例:** 2264 2265```ts 2266audioManager.isMicrophoneMute().then((value: boolean) => { 2267 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 2268}); 2269``` 2270 2271### on('deviceChange')<sup>(deprecated)</sup> 2272 2273on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void 2274 2275监听音频设备连接变化事件(当音频设备连接状态发生变化时触发)。使用callback异步回调。 2276 2277> **说明:** 2278> 从API version 7开始支持,从API version 9开始废弃,建议使用[on('deviceChange')](#ondevicechange9)替代。 2279 2280**系统能力:** SystemCapability.Multimedia.Audio.Device 2281 2282**参数:** 2283 2284| 参数名 | 类型 | 必填 | 说明 | 2285| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 2286| type | string | 是 | 事件回调类型,支持的事件为'deviceChange',当音频设备连接状态发生变化时,触发该事件。 | 2287| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 2288 2289**示例:** 2290 2291```ts 2292audioManager.on('deviceChange', (deviceChanged: audio.DeviceChangeAction) => { 2293 console.info(`device change type : ${deviceChanged.type} `); 2294 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `); 2295 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `); 2296 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `); 2297}); 2298``` 2299 2300### off('deviceChange')<sup>(deprecated)</sup> 2301 2302off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 2303 2304取消监听音频设备连接变化事件。使用callback异步回调。 2305 2306> **说明:** 2307> 从API version 7开始支持,从API version 9开始废弃,建议使用[off('deviceChange')](#offdevicechange9)替代。 2308 2309**系统能力:** SystemCapability.Multimedia.Audio.Device 2310 2311**参数:** 2312 2313| 参数名 | 类型 | 必填 | 说明 | 2314| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 2315| type | string | 是 | 事件回调类型,支持的事件为'deviceChange',当取消监听音频设备连接变化事件时,触发该事件。 | 2316| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 2317 2318**示例:** 2319 2320```ts 2321// 取消该事件的所有监听。 2322audioManager.off('deviceChange'); 2323 2324// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2325let deviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 2326 console.info(`device change type : ${deviceChanged.type} `); 2327 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `); 2328 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `); 2329 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `); 2330}; 2331 2332audioManager.on('deviceChange', deviceChangeCallback); 2333 2334audioManager.off('deviceChange', deviceChangeCallback); 2335``` 2336 2337### on('interrupt')<sup>(deprecated)</sup> 2338 2339on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback\<InterruptAction>): void 2340 2341监听音频打断事件(当音频焦点发生变化时触发)。使用callback异步回调。 2342 2343与[on('audioInterrupt')](#onaudiointerrupt9)作用一致,均用于监听焦点变化。为无音频流的场景(未曾创建AudioRenderer对象),比如FM、语音唤醒等提供焦点变化监听功能。 2344 2345> **说明:** 2346> 从API version 7开始支持,从API version 11开始废弃,建议使用[on('audioInterrupt')](#onaudiointerrupt10)替代。 2347 2348**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2349 2350**参数:** 2351 2352| 参数名 | 类型 | 必填 | 说明 | 2353| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2354| type | string | 是 | 事件回调类型,支持的事件为'interrupt',当音频焦点状态发生变化时,触发该事件。 | 2355| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2356| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 是 | 回调函数,返回打断事件信息。 | 2357 2358**示例:** 2359 2360```ts 2361import { audio } from '@kit.AudioKit'; 2362 2363let interAudioInterrupt: audio.AudioInterrupt = { 2364 streamUsage:2, 2365 contentType:0, 2366 pauseWhenDucked:true 2367}; 2368 2369audioManager.on('interrupt', interAudioInterrupt, (interruptAction: audio.InterruptAction) => { 2370 if (interruptAction.actionType === 0) { 2371 console.info('An event to gain the audio focus starts.'); 2372 console.info(`Focus gain event: ${interruptAction} `); 2373 } 2374 if (interruptAction.actionType === 1) { 2375 console.info('An audio interruption event starts.'); 2376 console.info(`Audio interruption event: ${interruptAction} `); 2377 } 2378}); 2379``` 2380 2381### off('interrupt')<sup>(deprecated)</sup> 2382 2383off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback\<InterruptAction>): void 2384 2385取消监听音频打断事件。使用callback异步回调。 2386 2387> **说明:** 2388> 从API version 7开始支持,从API version 11开始废弃,建议使用[off('audioInterrupt')](#offaudiointerrupt10)替代。 2389 2390**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2391 2392**参数:** 2393 2394| 参数名 | 类型 | 必填 | 说明 | 2395| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2396| type | string | 是 | 事件回调类型,支持的事件为'interrupt',当取消监听音频打断事件时,触发该事件。 | 2397| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2398| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 否 | 回调函数,返回打断事件信息。 | 2399 2400**示例:** 2401 2402```ts 2403import { audio } from '@kit.AudioKit'; 2404 2405let interAudioInterrupt: audio.AudioInterrupt = { 2406 streamUsage:2, 2407 contentType:0, 2408 pauseWhenDucked:true 2409}; 2410 2411// 取消该事件的所有监听。 2412audioManager.off('interrupt', interAudioInterrupt); 2413 2414// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2415let interruptCallback = (interruptAction: audio.InterruptAction) => { 2416 if (interruptAction.actionType === 0) { 2417 console.info('An event to gain the audio focus starts.'); 2418 console.info(`Focus gain event: ${interruptAction} `); 2419 } 2420 if (interruptAction.actionType === 1) { 2421 console.info('An audio interruption event starts.'); 2422 console.info(`Audio interruption event: ${interruptAction} `); 2423 } 2424}; 2425 2426audioManager.on('interrupt', interAudioInterrupt, interruptCallback); 2427 2428audioManager.off('interrupt', interAudioInterrupt, interruptCallback); 2429``` 2430 2431## AudioVolumeManager<sup>9+</sup> 2432 2433音量管理。 2434 2435在使用AudioVolumeManager的接口之前,需先通过[getVolumeManager](#getvolumemanager9)获取AudioVolumeManager实例。 2436 2437### getVolumeGroupManager<sup>9+</sup> 2438 2439getVolumeGroupManager(groupId: number, callback: AsyncCallback<AudioVolumeGroupManager\>\): void 2440 2441获取音频组管理器。使用callback异步回调。 2442 2443**系统能力:** SystemCapability.Multimedia.Audio.Volume 2444 2445**参数:** 2446 2447| 参数名 | 类型 | 必填 | 说明 | 2448| ---------- | ------------------------------------------------------------ | ---- |-----------------------------------------------------------| 2449| groupId | number | 是 | 音量组id,默认使用DEFAULT_VOLUME_GROUP_ID。 | 2450| callback | AsyncCallback<[AudioVolumeGroupManager](#audiovolumegroupmanager9)> | 是 | 回调函数。当获取音频组管理器成功,err为undefined,data为获取到的音频组管理器对象;否则为错误对象。 | 2451 2452**示例:** 2453 2454```ts 2455import { BusinessError } from '@kit.BasicServicesKit'; 2456 2457let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2458 2459audioVolumeManager.getVolumeGroupManager(groupId, (err: BusinessError, value: audio.AudioVolumeGroupManager) => { 2460 if (err) { 2461 console.error(`Failed to obtain the volume group infos list. ${err}`); 2462 return; 2463 } 2464 console.info('Callback invoked to indicate that the volume group infos list is obtained.'); 2465}); 2466 2467``` 2468 2469### getVolumeGroupManager<sup>9+</sup> 2470 2471getVolumeGroupManager(groupId: number\): Promise<AudioVolumeGroupManager\> 2472 2473获取音频组管理器。使用Promise异步回调。 2474 2475**系统能力:** SystemCapability.Multimedia.Audio.Volume 2476 2477**参数:** 2478 2479| 参数名 | 类型 | 必填 | 说明 | 2480| ---------- | ---------------------------------------- | ---- |----------------------------------| 2481| groupId | number | 是 | 音量组id,默认使用DEFAULT_VOLUME_GROUP_ID。 | 2482 2483**返回值:** 2484 2485| 类型 | 说明 | 2486| ------------------- | ----------------------------- | 2487| Promise< [AudioVolumeGroupManager](#audiovolumegroupmanager9) > | Promise对象,返回音量组实例。 | 2488 2489**示例:** 2490 2491```ts 2492import { audio } from '@kit.AudioKit'; 2493 2494let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2495let audioVolumeGroupManager: audio.AudioVolumeGroupManager | undefined = undefined; 2496 2497async function getVolumeGroupManager(){ 2498 audioVolumeGroupManager = await audioVolumeManager.getVolumeGroupManager(groupId); 2499 console.info('Promise returned to indicate that the volume group infos list is obtained.'); 2500} 2501``` 2502 2503### getVolumeGroupManagerSync<sup>10+</sup> 2504 2505getVolumeGroupManagerSync(groupId: number\): AudioVolumeGroupManager 2506 2507获取音频组管理器。同步返回结果。 2508 2509**系统能力:** SystemCapability.Multimedia.Audio.Volume 2510 2511**参数:** 2512 2513| 参数名 | 类型 | 必填 | 说明 | 2514| ---------- | ---------------------------------------- | ---- |----------------------------------| 2515| groupId | number | 是 | 音量组id,默认使用DEFAULT_VOLUME_GROUP_ID。 | 2516 2517**返回值:** 2518 2519| 类型 | 说明 | 2520| ------------------- | ----------------------------- | 2521| [AudioVolumeGroupManager](#audiovolumegroupmanager9) | 音量组实例。 | 2522 2523**错误码:** 2524 2525以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2526 2527| 错误码ID | 错误信息 | 2528| ------- | --------------------------------------------| 2529| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2530| 6800101 | Parameter verification failed. | 2531 2532**示例:** 2533 2534```ts 2535import { BusinessError } from '@kit.BasicServicesKit'; 2536 2537try { 2538 let audioVolumeGroupManager: audio.AudioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(audio.DEFAULT_VOLUME_GROUP_ID); 2539 console.info(`Get audioVolumeGroupManager success.`); 2540} catch (err) { 2541 let error = err as BusinessError; 2542 console.error(`Failed to get audioVolumeGroupManager, error: ${error}`); 2543} 2544``` 2545 2546### getAppVolumePercentage<sup>19+</sup> 2547 2548getAppVolumePercentage(): Promise<number\> 2549 2550获取应用的音量(范围为0到100)。使用Promise异步回调。 2551 2552**系统能力:** SystemCapability.Multimedia.Audio.Volume 2553 2554**返回值:** 2555 2556| 类型 | 说明 | 2557| ------------------- |--------------------| 2558| Promise<number> | Promise对象,返回应用的音量。 | 2559 2560**示例:** 2561 2562```ts 2563import { audio } from '@kit.AudioKit'; 2564 2565audioVolumeManager.getAppVolumePercentage().then((value: number) => { 2566 console.info(`app volume is ${value}.`); 2567}); 2568``` 2569 2570### setAppVolumePercentage<sup>19+</sup> 2571 2572setAppVolumePercentage(volume: number\): Promise<void\> 2573 2574设置应用的音量(范围为0到100)。使用Promise异步回调。 2575 2576**系统能力:** SystemCapability.Multimedia.Audio.Volume 2577 2578**参数:** 2579 2580| 参数名 | 类型 | 必填 | 说明 | 2581| ---------- | ---------------------------------------- | ---- |----------| 2582| volume | number | 是 | 要设置的音量值。 | 2583 2584**返回值:** 2585 2586| 类型 | 说明 | 2587| ------------------- | ------------------------------- | 2588| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2589 2590**错误码:** 2591 2592以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2593 2594| 错误码ID | 错误信息 | 2595| ------- | --------------------------------------------| 2596| 6800101 | Parameter verification failed.| 2597| 6800301 | Crash or blocking occurs in system process. | 2598 2599**示例:** 2600 2601```ts 2602import { audio } from '@kit.AudioKit'; 2603 2604audioVolumeManager.setAppVolumePercentage(20).then(() => { 2605 console.info(`set app volume success.`); 2606}); 2607``` 2608 2609### on('volumeChange')<sup>9+</sup> 2610 2611on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void 2612 2613监听系统音量变化事件(当系统音量发生变化时触发)。使用callback异步回调。 2614 2615**系统能力:** SystemCapability.Multimedia.Audio.Volume 2616 2617**参数:** 2618 2619| 参数名 | 类型 | 必填 | 说明 | 2620| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2621| type | string | 是 | 事件回调类型,支持的事件为'volumeChange',当系统音量发生变化时,触发该事件。 | 2622| callback | Callback<[VolumeEvent](#volumeevent9)> | 是 | 回调函数,返回变化后的音量信息。 | 2623 2624**错误码:** 2625 2626以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2627 2628| 错误码ID | 错误信息 | 2629| ------- | --------------------------------------------| 2630| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2631| 6800101 | Parameter verification failed. | 2632 2633**示例:** 2634 2635```ts 2636audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => { 2637 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2638 console.info(`Volume level: ${volumeEvent.volume} `); 2639 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2640}); 2641``` 2642 2643### off('volumeChange')<sup>12+</sup> 2644 2645off(type: 'volumeChange', callback?: Callback\<VolumeEvent>): void 2646 2647取消监听系统音量变化事件。使用callback异步回调。 2648 2649**系统能力:** SystemCapability.Multimedia.Audio.Volume 2650 2651**参数:** 2652 2653| 参数名 | 类型 | 必填 | 说明 | 2654| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2655| type | string | 是 | 事件回调类型,支持的事件为'volumeChange',当取消监听系统音量变化事件时,触发该事件。 | 2656| callback | Callback<[VolumeEvent](#volumeevent9)> | 否 | 回调函数,返回变化后的音量信息。 | 2657 2658**错误码:** 2659 2660以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2661 2662| 错误码ID | 错误信息 | 2663| ------- | --------------------------------------------| 2664| 401 | Parameter error. Possible causes: 1.Mandatory parameters missing; 2.Incorrect parameter types. | 2665| 6800101 | Parameter verification failed. | 2666 2667**示例:** 2668 2669```ts 2670// 取消该事件的所有监听。 2671audioVolumeManager.off('volumeChange'); 2672 2673// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2674let volumeChangeCallback = (volumeEvent: audio.VolumeEvent) => { 2675 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2676 console.info(`Volume level: ${volumeEvent.volume} `); 2677 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2678}; 2679 2680audioVolumeManager.on('volumeChange', volumeChangeCallback); 2681 2682audioVolumeManager.off('volumeChange', volumeChangeCallback); 2683``` 2684 2685### on('appVolumeChange')<sup>19+</sup> 2686 2687on(type: 'appVolumeChange', callback: Callback\<VolumeEvent>): void 2688 2689监听当前应用应用级音量变化事件(当应用级音量发生变化时触发)。使用callback异步回调。 2690 2691**系统能力:** SystemCapability.Multimedia.Audio.Volume 2692 2693**参数:** 2694 2695| 参数名 | 类型 | 必填 | 说明 | 2696| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2697| type | string | 是 | 事件回调类型,支持的事件为'appVolumeChange',当应用级音量发生变化时,触发该事件。 | 2698| callback | Callback<[VolumeEvent](#volumeevent9)> | 是 | 回调函数,返回变化后的音量信息。 | 2699 2700**错误码:** 2701 2702以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2703 2704| 错误码ID | 错误信息 | 2705| ------- | --------------------------------------------| 2706| 6800101 | Parameter verification failed. | 2707 2708**示例:** 2709 2710```ts 2711audioVolumeManager.on('appVolumeChange', (volumeEvent: audio.VolumeEvent) => { 2712 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2713 console.info(`Volume level: ${volumeEvent.volume} `); 2714 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2715}); 2716``` 2717 2718### off('appVolumeChange')<sup>19+</sup> 2719 2720off(type: 'appVolumeChange', callback?: Callback\<VolumeEvent>): void 2721 2722取消监听当前应用应用级音量变化事件。使用callback异步回调。 2723 2724**系统能力:** SystemCapability.Multimedia.Audio.Volume 2725 2726**参数:** 2727 2728| 参数名 | 类型 | 必填 | 说明 | 2729| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2730| type | string | 是 | 事件回调类型,支持的事件为'appVolumeChange',当取消监听当前应用应用级音量变化事件时,触发该事件。 | 2731| callback | Callback<[VolumeEvent](#volumeevent9)> | 否 | 回调函数,返回变化后的音量信息。 | 2732 2733**错误码:** 2734 2735以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 2736 2737| 错误码ID | 错误信息 | 2738| ------- | --------------------------------------------| 2739| 6800101 | Parameter verification failed. | 2740 2741**示例:** 2742 2743```ts 2744// 取消该事件的所有监听。 2745audioVolumeManager.off('appVolumeChange'); 2746 2747// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2748let appVolumeChangeCallback = (volumeEvent: audio.VolumeEvent) => { 2749 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2750 console.info(`Volume level: ${volumeEvent.volume} `); 2751 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2752}; 2753 2754audioVolumeManager.on('appVolumeChange', appVolumeChangeCallback); 2755 2756audioVolumeManager.off('appVolumeChange', appVolumeChangeCallback); 2757``` 2758 2759### on('activeVolumeTypeChange')<sup>20+</sup> 2760 2761on(type: 'activeVolumeTypeChange', callback: Callback\<AudioVolumeType>): void 2762 2763监听当前活跃流变化事件(当活跃流发生变化时触发)。使用callback异步回调。 2764 2765**系统能力:** SystemCapability.Multimedia.Audio.Volume 2766 2767**参数:** 2768 2769| 参数名 | 类型 | 必填 | 说明 | 2770| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2771| type | string | 是 | 事件回调类型,支持的事件为'activeVolumeTypeChange',当活跃流发生变化时,触发该事件。 | 2772| callback | Callback\<[AudioVolumeType](#audiovolumetype)> | 是 | 回调函数,返回变化后的活跃流类型。 | 2773 2774**错误码:** 2775 2776以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2777 2778| 错误码ID | 错误信息 | 2779| ------- | --------------------------------------------| 2780| 202 | Not system App. | 2781| 6800101 | Parameter verification failed. | 2782 2783**示例:** 2784 2785```ts 2786audioVolumeManager.on('activeVolumeTypeChange', (volumeType: audio.AudioVolumeType) => { 2787 console.info(`VolumeType of stream: ${volumeType} `); 2788}); 2789``` 2790 2791### off('activeVolumeTypeChange')<sup>20+</sup> 2792 2793off(type: 'activeVolumeTypeChange', callback?: Callback\<AudioVolumeType>): void 2794 2795取消监听当前活跃流变化事件。使用callback异步回调。 2796 2797**系统能力:** SystemCapability.Multimedia.Audio.Volume 2798 2799**参数:** 2800 2801| 参数名 | 类型 | 必填 | 说明 | 2802| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2803| type | string | 是 | 事件回调类型,支持的事件为'activeVolumeTypeChange',当取消监听当前活跃流变化事件时,触发该事件。 | 2804| callback | Callback\<[AudioVolumeType](#audiovolumetype)> | 否 | 回调函数,返回变化后的活跃流类型。 | 2805 2806**错误码:** 2807 2808以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2809 2810| 错误码ID | 错误信息 | 2811| ------- | --------------------------------------------| 2812| 202 | Not system App. | 2813| 6800101 | Parameter verification failed. | 2814 2815**示例:** 2816 2817```ts 2818// 取消该事件的所有监听。 2819audioVolumeManager.off('activeVolumeTypeChange'); 2820 2821// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 2822let activeVolumeTypeChangeCallback = (volumeType: audio.AudioVolumeType) => { 2823 console.info(`VolumeType of stream: ${volumeType} `); 2824}; 2825 2826audioVolumeManager.on('activeVolumeTypeChange', activeVolumeTypeChangeCallback); 2827 2828audioVolumeManager.off('activeVolumeTypeChange', activeVolumeTypeChangeCallback); 2829``` 2830 2831## AudioVolumeGroupManager<sup>9+</sup> 2832 2833管理音频组音量。 2834 2835在使用AudioVolumeGroupManager的接口之前,需先通过[getVolumeGroupManager](#getvolumegroupmanager9)获取AudioVolumeGroupManager实例。 2836 2837### getVolume<sup>9+</sup> 2838 2839getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2840 2841获取指定流的音量。使用callback异步回调。 2842 2843**系统能力:** SystemCapability.Multimedia.Audio.Volume 2844 2845**参数:** 2846 2847| 参数名 | 类型 | 必填 | 说明 | 2848| ---------- | ----------------------------------- | ---- | ------------------ | 2849| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2850| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2851 2852**示例:** 2853 2854```ts 2855import { BusinessError } from '@kit.BasicServicesKit'; 2856 2857audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2858 if (err) { 2859 console.error(`Failed to obtain the volume. ${err}`); 2860 return; 2861 } 2862 console.info('Callback invoked to indicate that the volume is obtained.'); 2863}); 2864``` 2865 2866### getVolume<sup>9+</sup> 2867 2868getVolume(volumeType: AudioVolumeType): Promise<number> 2869 2870获取指定流的音量。使用Promise异步回调。 2871 2872**系统能力:** SystemCapability.Multimedia.Audio.Volume 2873 2874**参数:** 2875 2876| 参数名 | 类型 | 必填 | 说明 | 2877| ---------- | ----------------------------------- | ---- | ------------ | 2878| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2879 2880**返回值:** 2881 2882| 类型 | 说明 | 2883| --------------------- | ------------------------- | 2884| Promise<number> | Promise对象,返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2885 2886**示例:** 2887 2888```ts 2889audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2890 console.info(`Promise returned to indicate that the volume is obtained ${value}.`); 2891}); 2892``` 2893 2894### getVolumeSync<sup>10+</sup> 2895 2896getVolumeSync(volumeType: AudioVolumeType): number 2897 2898获取指定流的音量。同步返回结果。 2899 2900**系统能力:** SystemCapability.Multimedia.Audio.Volume 2901 2902**参数:** 2903 2904| 参数名 | 类型 | 必填 | 说明 | 2905| ---------- | ----------------------------------- | ---- | ------------ | 2906| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2907 2908**返回值:** 2909 2910| 类型 | 说明 | 2911| --------------------- | ------------------------- | 2912| number | 返回指定流的音量。指定流的音量等级范围可通过[getMinVolume](#getminvolume9)和[getMaxVolume](#getmaxvolume9)获取。 | 2913 2914**错误码:** 2915 2916以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 2917 2918| 错误码ID | 错误信息 | 2919| ------- | --------------------------------------------| 2920| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2921| 6800101 | Parameter verification failed. | 2922 2923**示例:** 2924 2925```ts 2926import { BusinessError } from '@kit.BasicServicesKit'; 2927 2928try { 2929 let value: number = audioVolumeGroupManager.getVolumeSync(audio.AudioVolumeType.MEDIA); 2930 console.info(`Indicate that the volume is obtained ${value}.`); 2931} catch (err) { 2932 let error = err as BusinessError; 2933 console.error(`Failed to obtain the volume, error ${error}.`); 2934} 2935``` 2936 2937### getMinVolume<sup>9+</sup> 2938 2939getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2940 2941获取指定流的最小音量。使用callback异步回调。 2942 2943**系统能力:** SystemCapability.Multimedia.Audio.Volume 2944 2945**参数:** 2946 2947| 参数名 | 类型 | 必填 | 说明 | 2948| ---------- | ----------------------------------- | ---- | ------------------ | 2949| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2950| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 2951 2952**示例:** 2953 2954```ts 2955import { BusinessError } from '@kit.BasicServicesKit'; 2956 2957audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2958 if (err) { 2959 console.error(`Failed to obtain the minimum volume. ${err}`); 2960 return; 2961 } 2962 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 2963}); 2964``` 2965 2966### getMinVolume<sup>9+</sup> 2967 2968getMinVolume(volumeType: AudioVolumeType): Promise<number> 2969 2970获取指定流的最小音量。使用Promise异步回调。 2971 2972**系统能力:** SystemCapability.Multimedia.Audio.Volume 2973 2974**参数:** 2975 2976| 参数名 | 类型 | 必填 | 说明 | 2977| ---------- | ----------------------------------- | ---- | ------------ | 2978| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2979 2980**返回值:** 2981 2982| 类型 | 说明 | 2983| --------------------- | ------------------------- | 2984| Promise<number> | Promise对象,返回最小音量。 | 2985 2986**示例:** 2987 2988```ts 2989audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2990 console.info(`Promised returned to indicate that the minimum volume is obtained ${value}.`); 2991}); 2992``` 2993 2994### getMinVolumeSync<sup>10+</sup> 2995 2996getMinVolumeSync(volumeType: AudioVolumeType): number 2997 2998获取指定流的最小音量。同步返回结果。 2999 3000**系统能力:** SystemCapability.Multimedia.Audio.Volume 3001 3002**参数:** 3003 3004| 参数名 | 类型 | 必填 | 说明 | 3005| ---------- | ----------------------------------- | ---- | ------------ | 3006| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3007 3008**返回值:** 3009 3010| 类型 | 说明 | 3011| --------------------- | ------------------------- | 3012| number | 返回最小音量。 | 3013 3014**错误码:** 3015 3016以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3017 3018| 错误码ID | 错误信息 | 3019| ------- | --------------------------------------------| 3020| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3021| 6800101 | Parameter verification failed. | 3022 3023**示例:** 3024 3025```ts 3026import { BusinessError } from '@kit.BasicServicesKit'; 3027 3028try { 3029 let value: number = audioVolumeGroupManager.getMinVolumeSync(audio.AudioVolumeType.MEDIA); 3030 console.info(`Indicate that the minimum volume is obtained ${value}.`); 3031} catch (err) { 3032 let error = err as BusinessError; 3033 console.error(`Failed to obtain the minimum volume, error ${error}.`); 3034} 3035``` 3036 3037### getMaxVolume<sup>9+</sup> 3038 3039getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 3040 3041获取指定流的最大音量。使用callback异步回调。 3042 3043**系统能力:** SystemCapability.Multimedia.Audio.Volume 3044 3045**参数:** 3046 3047| 参数名 | 类型 | 必填 | 说明 | 3048| ---------- | ----------------------------------- | ---- | ---------------------- | 3049| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3050| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 3051 3052**示例:** 3053 3054```ts 3055import { BusinessError } from '@kit.BasicServicesKit'; 3056 3057audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 3058 if (err) { 3059 console.error(`Failed to obtain the maximum volume. ${err}`); 3060 return; 3061 } 3062 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 3063}); 3064``` 3065 3066### getMaxVolume<sup>9+</sup> 3067 3068getMaxVolume(volumeType: AudioVolumeType): Promise<number> 3069 3070获取指定流的最大音量。使用Promise异步回调。 3071 3072**系统能力:** SystemCapability.Multimedia.Audio.Volume 3073 3074**参数:** 3075 3076| 参数名 | 类型 | 必填 | 说明 | 3077| ---------- | ----------------------------------- | ---- | ------------ | 3078| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3079 3080**返回值:** 3081 3082| 类型 | 说明 | 3083| --------------------- | ----------------------------- | 3084| Promise<number> | Promise对象,返回最大音量大小。 | 3085 3086**示例:** 3087 3088```ts 3089audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 3090 console.info('Promised returned to indicate that the maximum volume is obtained.'); 3091}); 3092``` 3093 3094### getMaxVolumeSync<sup>10+</sup> 3095 3096getMaxVolumeSync(volumeType: AudioVolumeType): number 3097 3098获取指定流的最大音量。同步返回结果。 3099 3100**系统能力:** SystemCapability.Multimedia.Audio.Volume 3101 3102**参数:** 3103 3104| 参数名 | 类型 | 必填 | 说明 | 3105| ---------- | ----------------------------------- | ---- | ------------ | 3106| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3107 3108**返回值:** 3109 3110| 类型 | 说明 | 3111| --------------------- | ----------------------------- | 3112| number | 返回最大音量大小。 | 3113 3114**错误码:** 3115 3116以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3117 3118| 错误码ID | 错误信息 | 3119| ------- | --------------------------------------------| 3120| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3121| 6800101 | Parameter verification failed. | 3122 3123**示例:** 3124 3125```ts 3126import { BusinessError } from '@kit.BasicServicesKit'; 3127 3128try { 3129 let value: number = audioVolumeGroupManager.getMaxVolumeSync(audio.AudioVolumeType.MEDIA); 3130 console.info(`Indicate that the maximum volume is obtained. ${value}`); 3131} catch (err) { 3132 let error = err as BusinessError; 3133 console.error(`Failed to obtain the maximum volume, error ${error}.`); 3134} 3135``` 3136 3137### isMute<sup>9+</sup> 3138 3139isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 3140 3141获取指定音量流静音状态。使用callback异步回调。 3142 3143**系统能力:** SystemCapability.Multimedia.Audio.Volume 3144 3145**参数:** 3146 3147| 参数名 | 类型 | 必填 | 说明 | 3148| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 3149| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3150| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流静音状态成功,err为undefined,data为true表示静音,false表示非静音;否则为错误对象。 | 3151 3152**示例:** 3153 3154```ts 3155import { BusinessError } from '@kit.BasicServicesKit'; 3156 3157audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 3158 if (err) { 3159 console.error(`Failed to obtain the mute status. ${err}`); 3160 return; 3161 } 3162 console.info(`Callback invoked to indicate that the mute status of the stream is obtained ${value}.`); 3163}); 3164``` 3165 3166### isMute<sup>9+</sup> 3167 3168isMute(volumeType: AudioVolumeType): Promise<boolean> 3169 3170获取指定音量流是否被静音。使用Promise异步回调。 3171 3172**系统能力:** SystemCapability.Multimedia.Audio.Volume 3173 3174**参数:** 3175 3176| 参数名 | 类型 | 必填 | 说明 | 3177| ---------- | ----------------------------------- | ---- | ------------ | 3178| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3179 3180**返回值:** 3181 3182| 类型 | 说明 | 3183| ---------------------- | ------------------------------------------------------ | 3184| Promise<boolean> | Promise对象。返回true表示静音;返回false表示非静音。 | 3185 3186**示例:** 3187 3188```ts 3189audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 3190 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 3191}); 3192``` 3193 3194### isMuteSync<sup>10+</sup> 3195 3196isMuteSync(volumeType: AudioVolumeType): boolean 3197 3198获取指定音量流是否被静音。同步返回结果。 3199 3200**系统能力:** SystemCapability.Multimedia.Audio.Volume 3201 3202**参数:** 3203 3204| 参数名 | 类型 | 必填 | 说明 | 3205| ---------- | ----------------------------------- | ---- | ------------ | 3206| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3207 3208**返回值:** 3209 3210| 类型 | 说明 | 3211| ---------------------- | ------------------------------------------------------ | 3212| boolean | 流静音状态。返回true表示静音,返回false表示非静音。 | 3213 3214**错误码:** 3215 3216以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3217 3218| 错误码ID | 错误信息 | 3219| ------- | --------------------------------------------| 3220| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3221| 6800101 | Parameter verification failed. | 3222 3223**示例:** 3224 3225```ts 3226import { BusinessError } from '@kit.BasicServicesKit'; 3227 3228try { 3229 let value: boolean = audioVolumeGroupManager.isMuteSync(audio.AudioVolumeType.MEDIA); 3230 console.info(`Indicate that the mute status of the stream is obtained ${value}.`); 3231} catch (err) { 3232 let error = err as BusinessError; 3233 console.error(`Failed to obtain the mute status of the stream, error ${error}.`); 3234} 3235``` 3236 3237### getRingerMode<sup>9+</sup> 3238 3239getRingerMode(callback: AsyncCallback<AudioRingMode>): void 3240 3241获取铃声模式。使用callback异步回调。 3242 3243**系统能力:** SystemCapability.Multimedia.Audio.Volume 3244 3245**参数:** 3246 3247| 参数名 | 类型 | 必填 | 说明 | 3248| -------- | ---------------------------------------------------- | ---- | ------------------------ | 3249| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 3250 3251**示例:** 3252 3253```ts 3254import { BusinessError } from '@kit.BasicServicesKit'; 3255 3256audioVolumeGroupManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 3257 if (err) { 3258 console.error(`Failed to obtain the ringer mode. ${err}`); 3259 return; 3260 } 3261 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 3262}); 3263``` 3264 3265### getRingerMode<sup>9+</sup> 3266 3267getRingerMode(): Promise<AudioRingMode> 3268 3269获取铃声模式。使用Promise异步回调。 3270 3271**系统能力:** SystemCapability.Multimedia.Audio.Volume 3272 3273**返回值:** 3274 3275| 类型 | 说明 | 3276| ---------------------------------------------- | ------------------------------- | 3277| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 3278 3279**示例:** 3280 3281```ts 3282audioVolumeGroupManager.getRingerMode().then((value: audio.AudioRingMode) => { 3283 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 3284}); 3285``` 3286 3287### getRingerModeSync<sup>10+</sup> 3288 3289getRingerModeSync(): AudioRingMode 3290 3291获取铃声模式。同步返回结果。 3292 3293**系统能力:** SystemCapability.Multimedia.Audio.Volume 3294 3295**返回值:** 3296 3297| 类型 | 说明 | 3298| ---------------------------------------------- | ------------------------------- | 3299| [AudioRingMode](#audioringmode) | 返回系统的铃声模式。 | 3300 3301**示例:** 3302 3303```ts 3304import { BusinessError } from '@kit.BasicServicesKit'; 3305 3306try { 3307 let value: audio.AudioRingMode = audioVolumeGroupManager.getRingerModeSync(); 3308 console.info(`Indicate that the ringer mode is obtained ${value}.`); 3309} catch (err) { 3310 let error = err as BusinessError; 3311 console.error(`Failed to obtain the ringer mode, error ${error}.`); 3312} 3313``` 3314 3315### on('ringerModeChange')<sup>9+</sup> 3316 3317on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void 3318 3319监听铃声模式变化事件(当[铃声模式](#audioringmode)发生变化时触发)。使用callback异步回调。 3320 3321**系统能力:** SystemCapability.Multimedia.Audio.Volume 3322 3323**参数:** 3324 3325| 参数名 | 类型 | 必填 | 说明 | 3326| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 3327| type | string | 是 | 事件回调类型,支持的事件为'ringerModeChange',当铃声模式发生变化时,触发该事件。 | 3328| callback | Callback<[AudioRingMode](#audioringmode)> | 是 | 回调函数,返回变化后的铃音模式。 | 3329 3330**错误码:** 3331 3332以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3333 3334| 错误码ID | 错误信息 | 3335| ------- | --------------------------------------------| 3336| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3337| 6800101 | Parameter verification failed. | 3338 3339**示例:** 3340 3341```ts 3342audioVolumeGroupManager.on('ringerModeChange', (ringerMode: audio.AudioRingMode) => { 3343 console.info(`Updated ringermode: ${ringerMode}`); 3344}); 3345``` 3346 3347### off('ringerModeChange')<sup>18+</sup> 3348 3349off(type: 'ringerModeChange', callback?: Callback<AudioRingMode>): void 3350 3351取消监听铃声模式变化事件。使用callback异步回调。 3352 3353**系统能力:** SystemCapability.Multimedia.Audio.Volume 3354 3355**参数:** 3356 3357| 参数名 | 类型 | 必填 | 说明 | 3358| -------- | -------------------------------------- |----| ------------------------------------------------------------ | 3359| type | string | 是 | 事件回调类型,支持的事件为'ringerModeChange',当取消监听铃声模式变化事件时,触发该事件。 | 3360| callback |Callback<[AudioRingMode](#audioringmode)> | 否 | 回调函数,返回变化后的铃音模式。 | 3361 3362**错误码:** 3363 3364以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 3365 3366| 错误码ID | 错误信息 | 3367| ------- | --------------------------------------------| 3368| 6800101 | Parameter verification failed. | 3369 3370**示例:** 3371 3372```ts 3373// 取消该事件的所有监听。 3374audioVolumeGroupManager.off('ringerModeChange'); 3375 3376// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 3377let ringerModeChangeCallback = (ringerMode: audio.AudioRingMode) => { 3378 console.info(`Updated ringermode: ${ringerMode}`); 3379}; 3380 3381audioVolumeGroupManager.on('ringerModeChange', ringerModeChangeCallback); 3382 3383audioVolumeGroupManager.off('ringerModeChange', ringerModeChangeCallback); 3384``` 3385 3386### setMicrophoneMute<sup>(deprecated)</sup> 3387 3388setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 3389 3390设置麦克风静音状态。使用callback异步回调。 3391 3392> **说明:** 3393> 3394> 从API version 9开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 3395 3396**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG,该权限仅系统应用可申请。 3397 3398**系统能力:** SystemCapability.Multimedia.Audio.Volume 3399 3400**参数:** 3401 3402| 参数名 | 类型 | 必填 | 说明 | 3403| -------- | ------------------------- | ---- | --------------------------------------------- | 3404| mute | boolean | 是 | 是否设置麦克风为静音状态。true表示静音,false表示非静音。 | 3405| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 3406 3407**示例:** 3408 3409```ts 3410import { BusinessError } from '@kit.BasicServicesKit'; 3411 3412audioVolumeGroupManager.setMicrophoneMute(true, (err: BusinessError) => { 3413 if (err) { 3414 console.error(`Failed to mute the microphone. ${err}`); 3415 return; 3416 } 3417 console.info('Callback invoked to indicate that the microphone is muted.'); 3418}); 3419``` 3420 3421### setMicrophoneMute<sup>(deprecated)</sup> 3422 3423setMicrophoneMute(mute: boolean): Promise<void> 3424 3425设置麦克风静音状态。使用Promise异步回调。 3426 3427> **说明:** 3428> 3429> 从API version 9开始支持,从API version 11开始废弃,替代接口仅面向系统应用开放。 3430 3431**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG,该权限仅系统应用可申请。 3432 3433**系统能力:** SystemCapability.Multimedia.Audio.Volume 3434 3435**参数:** 3436 3437| 参数名 | 类型 | 必填 | 说明 | 3438| ------ | ------- | ---- | --------------------------------------------- | 3439| mute | boolean | 是 | 是否设置麦克风为静音状态。true表示静音,false表示非静音。 | 3440 3441**返回值:** 3442 3443| 类型 | 说明 | 3444| ------------------- | ------------------------------- | 3445| Promise<void> | Promise对象。无返回结果的Promise对象。 | 3446 3447**示例:** 3448 3449```ts 3450audioVolumeGroupManager.setMicrophoneMute(true).then(() => { 3451 console.info('Promise returned to indicate that the microphone is muted.'); 3452}); 3453``` 3454 3455### isMicrophoneMute<sup>9+</sup> 3456 3457isMicrophoneMute(callback: AsyncCallback<boolean>): void 3458 3459获取麦克风静音状态。使用callback异步回调。 3460 3461**系统能力:** SystemCapability.Multimedia.Audio.Volume 3462 3463**参数:** 3464 3465| 参数名 | 类型 | 必填 | 说明 | 3466| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 3467| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true表示静音,false表示非静音;否则为错误对象。 | 3468 3469**示例:** 3470 3471```ts 3472import { BusinessError } from '@kit.BasicServicesKit'; 3473 3474audioVolumeGroupManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 3475 if (err) { 3476 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 3477 return; 3478 } 3479 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 3480}); 3481``` 3482 3483### isMicrophoneMute<sup>9+</sup> 3484 3485isMicrophoneMute(): Promise<boolean> 3486 3487获取麦克风静音状态。使用Promise异步回调。 3488 3489**系统能力:** SystemCapability.Multimedia.Audio.Volume 3490 3491**返回值:** 3492 3493| 类型 | 说明 | 3494| ---------------------- | ------------------------------------------------------------ | 3495| Promise<boolean> | Promise对象。返回true表示麦克风被静音;返回false表示麦克风未被静音。 | 3496 3497**示例:** 3498 3499```ts 3500audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => { 3501 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 3502}); 3503``` 3504 3505### isMicrophoneMuteSync<sup>10+</sup> 3506 3507isMicrophoneMuteSync(): boolean 3508 3509获取麦克风静音状态。同步返回结果。 3510 3511**系统能力:** SystemCapability.Multimedia.Audio.Volume 3512 3513**返回值:** 3514 3515| 类型 | 说明 | 3516| ---------------------- | ------------------------------------------------------------ | 3517| boolean | 系统麦克风静音状态。返回true表示静音,返回false表示非静音。 | 3518 3519**示例:** 3520 3521```ts 3522import { BusinessError } from '@kit.BasicServicesKit'; 3523 3524try { 3525 let value: boolean = audioVolumeGroupManager.isMicrophoneMuteSync(); 3526 console.info(`Indicate that the mute status of the microphone is obtained ${value}.`); 3527} catch (err) { 3528 let error = err as BusinessError; 3529 console.error(`Failed to obtain the mute status of the microphone, error ${error}.`); 3530} 3531``` 3532 3533### on('micStateChange')<sup>9+</sup> 3534 3535on(type: 'micStateChange', callback: Callback<MicStateChangeEvent>): void 3536 3537监听系统麦克风状态更改事件(当检测到系统麦克风状态发生改变时触发)。使用callback异步回调。 3538 3539目前此订阅接口在单进程多AudioManager实例的使用场景下,仅最后一个实例的订阅生效,其他实例的订阅会被覆盖(即使最后一个实例没有进行订阅。因此,推荐使用单一AudioManager实例进行开发。 3540 3541**系统能力:** SystemCapability.Multimedia.Audio.Volume 3542 3543**参数:** 3544 3545| 参数名 | 类型 | 必填 | 说明 | 3546| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 3547| type | string | 是 | 事件回调类型,支持的事件为'micStateChange',当检测到系统麦克风状态发生改变时,触发该事件。 | 3548| callback | Callback<[MicStateChangeEvent](#micstatechangeevent9)> | 是 | 回调函数,返回变更后的麦克风状态。 | 3549 3550**错误码:** 3551 3552以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3553 3554| 错误码ID | 错误信息 | 3555| ------- | --------------------------------------------| 3556| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3557| 6800101 | Parameter verification failed. | 3558 3559**示例:** 3560 3561```ts 3562audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => { 3563 console.info(`Current microphone status is: ${micStateChange.mute} `); 3564}); 3565``` 3566 3567### off('micStateChange')<sup>12+</sup> 3568 3569off(type: 'micStateChange', callback?: Callback<MicStateChangeEvent>): void 3570 3571取消监听系统麦克风状态更改事件。使用callback异步回调。 3572 3573**系统能力:** SystemCapability.Multimedia.Audio.Volume 3574 3575**参数:** 3576 3577| 参数名 | 类型 | 必填 | 说明 | 3578| -------- | -------------------------------------- |----| ------------------------------------------------------------ | 3579| type | string | 是 | 事件回调类型,支持的事件为'micStateChange',当取消监听系统麦克风状态更改事件时,触发该事件。 | 3580| callback | Callback<[MicStateChangeEvent](#micstatechangeevent9)> | 否 | 回调函数,返回变更后的麦克风状态。 | 3581 3582**错误码:** 3583 3584以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3585 3586| 错误码ID | 错误信息 | 3587| ------- | --------------------------------------------| 3588| 401 | Parameter error. Possible causes: 1.Mandatory parameters missing; 2.Incorrect parameter types. | 3589| 6800101 | Parameter verification failed. | 3590 3591**示例:** 3592 3593```ts 3594// 取消该事件的所有监听。 3595audioVolumeGroupManager.off('micStateChange'); 3596 3597// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 3598let micStateChangeCallback = (micStateChange: audio.MicStateChangeEvent) => { 3599 console.info(`Current microphone status is: ${micStateChange.mute} `); 3600}; 3601 3602audioVolumeGroupManager.on('micStateChange', micStateChangeCallback); 3603 3604audioVolumeGroupManager.off('micStateChange', micStateChangeCallback); 3605``` 3606 3607### isVolumeUnadjustable<sup>10+</sup> 3608 3609isVolumeUnadjustable(): boolean 3610 3611获取固定音量模式开关状态,打开时进入固定音量模式,此时音量固定无法被调节。同步返回结果。 3612 3613**系统能力:** SystemCapability.Multimedia.Audio.Volume 3614 3615**返回值:** 3616 3617| 类型 | 说明 | 3618| ---------------------- | ------------------------------------------------------ | 3619| boolean | 固定音量模式开关状态。返回true表示固定音量模式,返回false表示非固定音量模式。 | 3620 3621**示例:** 3622 3623```ts 3624let volumeAdjustSwitch: boolean = audioVolumeGroupManager.isVolumeUnadjustable(); 3625console.info(`Whether it is volume unadjustable: ${volumeAdjustSwitch}.`); 3626``` 3627 3628### getSystemVolumeInDb<sup>10+</sup> 3629 3630getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType, callback: AsyncCallback<number>): void 3631 3632获取音量增益dB值。使用callback异步回调。 3633 3634**系统能力:** SystemCapability.Multimedia.Audio.Volume 3635 3636**参数:** 3637 3638| 参数名 | 类型 | 必填 | 说明 | 3639| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3640| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3641| volumeLevel | number | 是 | 音量等级。 | 3642| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3643| callback | AsyncCallback<number> | 是 | 回调函数。当获取音量增益dB值成功,err为undefined,data为获取到的音量增益dB值;否则为错误对象。 | 3644 3645**错误码:** 3646 3647以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3648 3649| 错误码ID | 错误信息 | 3650| ------- | --------------------------------------------| 3651| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3652| 6800101 | Parameter verification failed. Return by callback. | 3653| 6800301 | System error. Return by callback. | 3654 3655**示例:** 3656 3657```ts 3658import { BusinessError } from '@kit.BasicServicesKit'; 3659 3660audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER, (err: BusinessError, dB: number) => { 3661 if (err) { 3662 console.error(`Failed to get the volume DB. ${err}`); 3663 } else { 3664 console.info(`Success to get the volume DB. ${dB}`); 3665 } 3666}); 3667``` 3668 3669### getSystemVolumeInDb<sup>10+</sup> 3670 3671getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): Promise<number> 3672 3673获取音量增益dB值。使用Promise异步回调。 3674 3675**系统能力:** SystemCapability.Multimedia.Audio.Volume 3676 3677**参数:** 3678 3679| 参数名 | 类型 | 必填 | 说明 | 3680| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3681| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3682| volumeLevel | number | 是 | 音量等级。 | 3683| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3684 3685**返回值:** 3686 3687| 类型 | 说明 | 3688| --------------------- | ---------------------------------- | 3689| Promise<number> | Promise对象,返回对应的音量增益dB值。 | 3690 3691**错误码:** 3692 3693以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3694 3695| 错误码ID | 错误信息 | 3696| ------- | --------------------------------------------| 3697| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3698| 6800101 | Parameter verification failed. Return by promise. | 3699| 6800301 | System error. Return by promise. | 3700 3701**示例:** 3702 3703```ts 3704import { BusinessError } from '@kit.BasicServicesKit'; 3705 3706audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER).then((value: number) => { 3707 console.info(`Success to get the volume DB. ${value}`); 3708}).catch((error: BusinessError) => { 3709 console.error(`Fail to adjust the system volume by step. ${error}`); 3710}); 3711``` 3712 3713### getSystemVolumeInDbSync<sup>10+</sup> 3714 3715getSystemVolumeInDbSync(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): number 3716 3717获取音量增益dB值。同步返回结果。 3718 3719**系统能力:** SystemCapability.Multimedia.Audio.Volume 3720 3721**参数:** 3722 3723| 参数名 | 类型 | 必填 | 说明 | 3724| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3725| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3726| volumeLevel | number | 是 | 音量等级。 | 3727| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3728 3729**返回值:** 3730 3731| 类型 | 说明 | 3732| --------------------- | ---------------------------------- | 3733| number | 返回对应的音量增益dB值。 | 3734 3735**错误码:** 3736 3737以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3738 3739| 错误码ID | 错误信息 | 3740| ------- | --------------------------------------------| 3741| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3742| 6800101 | Parameter verification failed. | 3743 3744**示例:** 3745 3746```ts 3747import { BusinessError } from '@kit.BasicServicesKit'; 3748 3749try { 3750 let value: number = audioVolumeGroupManager.getSystemVolumeInDbSync(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER); 3751 console.info(`Success to get the volume DB. ${value}`); 3752} catch (err) { 3753 let error = err as BusinessError; 3754 console.error(`Fail to adjust the system volume by step. ${error}`); 3755} 3756``` 3757 3758### getMaxAmplitudeForInputDevice<sup>12+</sup> 3759 3760getMaxAmplitudeForInputDevice(inputDevice: AudioDeviceDescriptor): Promise<number> 3761 3762获取输入设备音频流的最大电平值,取值范围为[0, 1]。使用Promise异步回调。 3763 3764**系统能力:** SystemCapability.Multimedia.Audio.Volume 3765 3766**参数:** 3767 3768| 参数名 | 类型 | 必填 | 说明 | 3769| ----------- | ------------------------------------- | ---- | --------------------------------------------------- | 3770| inputDevice | [AudioDeviceDescriptor](#audiodevicedescriptor) | 是 | 获取最大电平值的设备信息。 | 3771 3772**返回值:** 3773 3774| 类型 | 说明 | 3775| --------------------- | ---------------------------------- | 3776| Promise<number> | Promise对象,返回对应设备的电平值,大小在[0, 1]之间。 | 3777 3778**错误码:** 3779 3780以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3781 3782| 错误码ID | 错误信息 | 3783| ------- | --------------------------------------------| 3784| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3785| 6800101 | Parameter verification failed. Return by promise. | 3786| 6800301 | System error. Return by promise. | 3787 3788**示例:** 3789 3790```ts 3791import { BusinessError } from '@kit.BasicServicesKit'; 3792 3793let capturerInfo: audio.AudioCapturerInfo = { 3794 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 3795 capturerFlags: 0 // 音频采集器标志。 3796}; 3797 3798audio.getAudioManager().getRoutingManager().getPreferredInputDeviceForCapturerInfo(capturerInfo).then((data) => { 3799 audioVolumeGroupManager.getMaxAmplitudeForInputDevice(data[0]).then((value) => { 3800 console.info(`mic volatileume amplitude is: ${value}`); 3801 }).catch((err: BusinessError) => { 3802 console.error("getMaxAmplitudeForInputDevice error" + JSON.stringify(err)); 3803 }) 3804}).catch((err: BusinessError) => { 3805 console.error("get outputDeviceId error" + JSON.stringify(err)); 3806}) 3807``` 3808 3809### getMaxAmplitudeForOutputDevice<sup>12+</sup> 3810 3811getMaxAmplitudeForOutputDevice(outputDevice: AudioDeviceDescriptor): Promise<number> 3812 3813获取输出设备音频流的最大电平值,取值范围为[0, 1]。使用Promise异步回调。 3814 3815**系统能力:** SystemCapability.Multimedia.Audio.Volume 3816 3817**参数:** 3818 3819| 参数名 | 类型 | 必填 | 说明 | 3820| ------------ | --------------------------------------- | ---- | -------------------------------------------------------- | 3821| outputDevice | [AudioDeviceDescriptor](#audiodevicedescriptor) | 是 | 获取最大电平值的设备信息。 | 3822 3823**返回值:** 3824 3825| 类型 | 说明 | 3826| --------------------- | ---------------------------------- | 3827| Promise<number> | Promise对象,返回对应设备的电平值,大小在[0, 1]之间。 | 3828 3829**错误码:** 3830 3831以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 3832 3833| 错误码ID | 错误信息 | 3834| ------- | --------------------------------------------| 3835| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 3836| 6800101 | Parameter verification failed. Return by promise. | 3837| 6800301 | System error. Return by promise. | 3838 3839**示例:** 3840 3841```ts 3842import { BusinessError } from '@kit.BasicServicesKit'; 3843 3844let rendererInfo: audio.AudioRendererInfo = { 3845 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 3846 rendererFlags: 0 // 音频渲染器标志。 3847}; 3848 3849audio.getAudioManager().getRoutingManager().getPreferOutputDeviceForRendererInfo(rendererInfo).then((data) => { 3850 audioVolumeGroupManager.getMaxAmplitudeForOutputDevice(data[0]).then((value) => { 3851 console.info(`mic volatileume amplitude is: ${value}`); 3852 }).catch((err: BusinessError) => { 3853 console.error("getMaxAmplitudeForOutputDevice error" + JSON.stringify(err)); 3854 }) 3855}).catch((err: BusinessError) => { 3856 console.error("getPreferOutputDeviceForRendererInfo error" + JSON.stringify(err)); 3857}) 3858``` 3859 3860## AudioStreamManager<sup>9+</sup> 3861 3862管理音频流。 3863 3864在使用AudioStreamManager的接口之前,需先通过[getStreamManager](#getstreammanager9)获取AudioStreamManager实例。 3865 3866### getCurrentAudioRendererInfoArray<sup>9+</sup> 3867 3868getCurrentAudioRendererInfoArray(callback: AsyncCallback<AudioRendererChangeInfoArray>): void 3869 3870获取当前音频渲染器的信息。使用callback异步回调。 3871 3872**系统能力**: SystemCapability.Multimedia.Audio.Renderer 3873 3874**参数:** 3875 3876| 参数名 | 类型 | 必填 | 说明 | 3877| -------- | ----------------------------------- | -------- | --------------------------- | 3878| callback | AsyncCallback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数。当获取当前音频渲染器的信息成功,err为undefined,data为获取到的当前音频渲染器的信息;否则为错误对象。 | 3879 3880**示例:** 3881 3882```ts 3883import { BusinessError } from '@kit.BasicServicesKit'; 3884 3885audioStreamManager.getCurrentAudioRendererInfoArray(async (err: BusinessError, AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3886 console.info('getCurrentAudioRendererInfoArray **** Get Callback Called ****'); 3887 if (err) { 3888 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3889 } else { 3890 if (AudioRendererChangeInfoArray != null) { 3891 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3892 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3893 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3894 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3895 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3896 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3897 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3898 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3899 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3900 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3901 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3902 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3903 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3904 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3905 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3906 } 3907 } 3908 } 3909 } 3910}); 3911``` 3912 3913### getCurrentAudioRendererInfoArray<sup>9+</sup> 3914 3915getCurrentAudioRendererInfoArray(): Promise<AudioRendererChangeInfoArray> 3916 3917获取当前音频渲染器的信息。使用Promise异步回调。 3918 3919**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3920 3921**返回值:** 3922 3923| 类型 | 说明 | 3924| ---------------------------------------------------------------------------------| --------------------------------------- | 3925| Promise<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | Promise对象,返回当前音频渲染器信息。 | 3926 3927**示例:** 3928 3929```ts 3930import { BusinessError } from '@kit.BasicServicesKit'; 3931 3932async function getCurrentAudioRendererInfoArray(){ 3933 await audioStreamManager.getCurrentAudioRendererInfoArray().then((AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3934 console.info(`getCurrentAudioRendererInfoArray ######### Get Promise is called ##########`); 3935 if (AudioRendererChangeInfoArray != null) { 3936 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3937 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3938 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3939 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3940 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3941 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3942 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3943 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3944 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3945 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3946 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3947 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3948 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3949 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3950 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3951 } 3952 } 3953 } 3954 }).catch((err: BusinessError) => { 3955 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3956 }); 3957} 3958``` 3959### getCurrentAudioRendererInfoArraySync<sup>10+</sup> 3960 3961getCurrentAudioRendererInfoArraySync(): AudioRendererChangeInfoArray 3962 3963获取当前音频渲染器的信息。同步返回结果。 3964 3965**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3966 3967**返回值:** 3968 3969| 类型 | 说明 | 3970| ---------------------------------------------------------------------------------| --------------------------------------- | 3971| [AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9) | 返回当前音频渲染器信息。 | 3972 3973**示例:** 3974 3975```ts 3976import { BusinessError } from '@kit.BasicServicesKit'; 3977 3978try { 3979 let audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray = audioStreamManager.getCurrentAudioRendererInfoArraySync(); 3980 console.info(`getCurrentAudioRendererInfoArraySync success.`); 3981 if (audioRendererChangeInfoArray != null) { 3982 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 3983 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 3984 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3985 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3986 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3987 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3988 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3989 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3990 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3991 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3992 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3993 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3994 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3995 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3996 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3997 } 3998 } 3999 } 4000} catch (err) { 4001 let error = err as BusinessError; 4002 console.error(`getCurrentAudioRendererInfoArraySync :ERROR: ${error}`); 4003} 4004``` 4005 4006### getCurrentAudioCapturerInfoArray<sup>9+</sup> 4007 4008getCurrentAudioCapturerInfoArray(callback: AsyncCallback<AudioCapturerChangeInfoArray>): void 4009 4010获取当前音频采集器的信息。使用callback异步回调。 4011 4012**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4013 4014**参数:** 4015 4016| 参数名 | 类型 | 必填 | 说明 | 4017| ---------- | ----------------------------------- | --------- | -------------------------------------------------------- | 4018| callback | AsyncCallback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数。当获取当前音频采集器的信息成功,err为undefined,data为获取到的当前音频采集器的信息;否则为错误对象。 | 4019 4020**示例:** 4021 4022```ts 4023import { BusinessError } from '@kit.BasicServicesKit'; 4024 4025audioStreamManager.getCurrentAudioCapturerInfoArray(async (err: BusinessError, AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4026 console.info('getCurrentAudioCapturerInfoArray **** Get Callback Called ****'); 4027 if (err) { 4028 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 4029 } else { 4030 if (AudioCapturerChangeInfoArray != null) { 4031 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4032 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4033 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4034 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4035 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4036 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4037 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4038 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4039 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4040 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4041 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4042 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4043 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4044 } 4045 } 4046 } 4047 } 4048}); 4049``` 4050 4051### getCurrentAudioCapturerInfoArray<sup>9+</sup> 4052 4053getCurrentAudioCapturerInfoArray(): Promise<AudioCapturerChangeInfoArray> 4054 4055获取当前音频采集器的信息。使用Promise异步回调。 4056 4057**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4058 4059**返回值:** 4060 4061| 类型 | 说明 | 4062| -----------------------------------------------------------------------------| ----------------------------------- | 4063| Promise<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | Promise对象,返回当前音频采集器信息。 | 4064 4065**示例:** 4066 4067```ts 4068import { BusinessError } from '@kit.BasicServicesKit'; 4069 4070async function getCurrentAudioCapturerInfoArray(){ 4071 await audioStreamManager.getCurrentAudioCapturerInfoArray().then((AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4072 console.info('getCurrentAudioCapturerInfoArray **** Get Promise Called ****'); 4073 if (AudioCapturerChangeInfoArray != null) { 4074 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4075 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4076 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4077 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4078 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4079 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4080 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4081 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4082 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4083 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4084 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4085 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4086 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4087 } 4088 } 4089 } 4090 }).catch((err: BusinessError) => { 4091 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 4092 }); 4093} 4094``` 4095### getCurrentAudioCapturerInfoArraySync<sup>10+</sup> 4096 4097getCurrentAudioCapturerInfoArraySync(): AudioCapturerChangeInfoArray 4098 4099获取当前音频采集器的信息。同步返回结果。 4100 4101**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4102 4103**返回值:** 4104 4105| 类型 | 说明 | 4106| -----------------------------------------------------------------------------| ----------------------------------- | 4107| [AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9) | 返回当前音频采集器信息。 | 4108 4109**示例:** 4110 4111```ts 4112import { BusinessError } from '@kit.BasicServicesKit'; 4113 4114try { 4115 let audioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray = audioStreamManager.getCurrentAudioCapturerInfoArraySync(); 4116 console.info('getCurrentAudioCapturerInfoArraySync success.'); 4117 if (audioCapturerChangeInfoArray != null) { 4118 for (let i = 0; i < audioCapturerChangeInfoArray.length; i++) { 4119 console.info(`StreamId for ${i} is: ${audioCapturerChangeInfoArray[i].streamId}`); 4120 console.info(`Source for ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.source}`); 4121 console.info(`Flag ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4122 for (let j = 0; j < audioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4123 console.info(`Id: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4124 console.info(`Type: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4125 console.info(`Role: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4126 console.info(`Name: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4127 console.info(`Address: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4128 console.info(`SampleRate: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4129 console.info(`ChannelCount: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4130 console.info(`ChannelMask: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4131 } 4132 } 4133 } 4134} catch (err) { 4135 let error = err as BusinessError; 4136 console.error(`getCurrentAudioCapturerInfoArraySync ERROR: ${error}`); 4137} 4138``` 4139 4140### on('audioRendererChange')<sup>9+</sup> 4141 4142on(type: 'audioRendererChange', callback: Callback<AudioRendererChangeInfoArray>): void 4143 4144监听音频渲染器更改事件(当音频播放流状态变化或设备变化时触发)。使用callback异步回调。 4145 4146**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4147 4148**参数:** 4149 4150| 参数名 | 类型 | 必填 | 说明 | 4151| -------- | ---------- | --------- | ------------------------------------------------------------------------ | 4152| type | string | 是 | 事件回调类型,支持的事件为'audioRendererChange',当音频播放流状态变化或设备变化时,触发该事件。 | 4153| callback | Callback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数,返回当前音频渲染器信息。 | 4154 4155**错误码:** 4156 4157以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4158 4159| 错误码ID | 错误信息 | 4160| ------- | --------------------------------------------| 4161| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4162| 6800101 | Parameter verification failed. | 4163 4164**示例:** 4165 4166```ts 4167audioStreamManager.on('audioRendererChange', (audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 4168 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 4169 let audioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 4170 console.info(`## RendererChange on is called for ${i} ##`); 4171 console.info(`StreamId for ${i} is: ${audioRendererChangeInfo.streamId}`); 4172 console.info(`Content ${i} is: ${audioRendererChangeInfo.rendererInfo.content}`); 4173 console.info(`Stream ${i} is: ${audioRendererChangeInfo.rendererInfo.usage}`); 4174 console.info(`Flag ${i} is: ${audioRendererChangeInfo.rendererInfo.rendererFlags}`); 4175 for (let j = 0;j < audioRendererChangeInfo.deviceDescriptors.length; j++) { 4176 console.info(`Id: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].id}`); 4177 console.info(`Type: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 4178 console.info(`Role: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 4179 console.info(`Name: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].name}`); 4180 console.info(`Address: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].address}`); 4181 console.info(`SampleRate: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 4182 console.info(`ChannelCount: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 4183 console.info(`ChannelMask: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 4184 } 4185 } 4186}); 4187``` 4188 4189### off('audioRendererChange')<sup>9+</sup> 4190 4191off(type: 'audioRendererChange', callback?: Callback<AudioRendererChangeInfoArray>): void 4192 4193取消监听音频渲染器更改事件。使用callback异步回调。 4194 4195**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4196 4197**参数:** 4198 4199| 参数名 | 类型 | 必填 | 说明 | 4200| -------- | ------- |----| ---------------- | 4201| type | string | 是 | 事件回调类型,支持的事件为'audioRendererChange',当取消监听音频渲染器更改事件时,触发该事件。 | 4202| callback<sup>18+</sup> | Callback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 否 | 回调函数,返回当前音频渲染器信息。 | 4203 4204**错误码:** 4205 4206以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4207 4208| 错误码ID | 错误信息 | 4209| ------- |--------------------------| 4210| 6800101 | Parameter verification failed. | 4211 4212**示例:** 4213 4214```ts 4215// 取消该事件的所有监听。 4216audioStreamManager.off('audioRendererChange'); 4217 4218// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4219let audioRendererChangeCallback = (audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 4220 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 4221 let audioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 4222 console.info(`## RendererChange on is called for ${i} ##`); 4223 console.info(`StreamId for ${i} is: ${audioRendererChangeInfo.streamId}`); 4224 console.info(`Content ${i} is: ${audioRendererChangeInfo.rendererInfo.content}`); 4225 console.info(`Stream ${i} is: ${audioRendererChangeInfo.rendererInfo.usage}`); 4226 console.info(`Flag ${i} is: ${audioRendererChangeInfo.rendererInfo.rendererFlags}`); 4227 for (let j = 0;j < audioRendererChangeInfo.deviceDescriptors.length; j++) { 4228 console.info(`Id: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].id}`); 4229 console.info(`Type: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 4230 console.info(`Role: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 4231 console.info(`Name: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].name}`); 4232 console.info(`Address: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].address}`); 4233 console.info(`SampleRate: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 4234 console.info(`ChannelCount: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 4235 console.info(`ChannelMask: ${i} : ${audioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 4236 } 4237 } 4238}; 4239 4240audioStreamManager.on('audioRendererChange', audioRendererChangeCallback); 4241 4242audioStreamManager.off('audioRendererChange', audioRendererChangeCallback); 4243``` 4244 4245### on('audioCapturerChange')<sup>9+</sup> 4246 4247on(type: 'audioCapturerChange', callback: Callback<AudioCapturerChangeInfoArray>): void 4248 4249监听音频采集器更改事件(当音频录制流状态变化或设备变化时触发)。使用callback异步回调。 4250 4251**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4252 4253**参数:** 4254 4255| 参数名 | 类型 | 必填 | 说明 | 4256| -------- | ------- | --------- | ---------------------------------------------------------------------- | 4257| type | string | 是 | 事件回调类型,支持的事件为'audioCapturerChange',当音频录制流状态变化或设备变化时,触发该事件。 | 4258| callback | Callback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数,返回当前音频采集器信息。 | 4259 4260**错误码:** 4261 4262以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4263 4264| 错误码ID | 错误信息 | 4265| ------- | --------------------------------------------| 4266| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4267| 6800101 | Parameter verification failed. | 4268 4269**示例:** 4270 4271```ts 4272audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4273 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4274 console.info(`## CapChange on is called for element ${i} ##`); 4275 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4276 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4277 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4278 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4279 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4280 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4281 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4282 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4283 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4284 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4285 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4286 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4287 } 4288 } 4289}); 4290``` 4291 4292### off('audioCapturerChange')<sup>9+</sup> 4293 4294off(type: 'audioCapturerChange', callback?: Callback<AudioCapturerChangeInfoArray>): void 4295 4296取消监听音频采集器更改事件。使用callback异步回调。 4297 4298**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4299 4300**参数:** 4301 4302| 参数名 | 类型 | 必填 | 说明 | 4303| -------- | -------- | --- | ------------------------------------------------------------- | 4304| type | string |是 | 事件回调类型,支持的事件为'audioCapturerChange',当取消监听音频采集器更改事件时,触发该事件。 | 4305| callback<sup>18+</sup> | Callback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 否 | 回调函数,返回当前音频采集器信息。 | 4306 4307**错误码:** 4308 4309以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4310 4311| 错误码ID | 错误信息 | 4312| ------- | --------------------------------------------| 4313| 6800101 | Parameter verification failed. | 4314 4315**示例:** 4316 4317```ts 4318audioStreamManager.off('audioCapturerChange'); 4319// 取消该事件的所有监听。 4320audioStreamManager.off('audioCapturerChange'); 4321 4322// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4323let audioCapturerChangeCallback = (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4324 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4325 console.info(`## CapChange on is called for element ${i} ##`); 4326 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4327 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4328 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4329 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4330 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4331 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4332 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4333 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4334 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4335 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4336 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4337 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4338 } 4339 } 4340}; 4341 4342audioStreamManager.on('audioCapturerChange', audioCapturerChangeCallback); 4343 4344audioStreamManager.off('audioCapturerChange', audioCapturerChangeCallback); 4345``` 4346 4347### isActive<sup>9+</sup> 4348 4349isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 4350 4351获取指定音频流活跃状态。使用callback异步回调。 4352 4353**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4354 4355**参数:** 4356 4357| 参数名 | 类型 | 必填 | 说明 | 4358| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 4359| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4360| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音频流活跃状态成功,err为undefined,data为true表示活跃,false表示不活跃;否则为错误对象。 | 4361 4362**示例:** 4363 4364```ts 4365import { BusinessError } from '@kit.BasicServicesKit'; 4366 4367audioStreamManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 4368if (err) { 4369 console.error(`Failed to obtain the active status of the stream. ${err}`); 4370 return; 4371} 4372 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 4373}); 4374``` 4375 4376### isActive<sup>9+</sup> 4377 4378isActive(volumeType: AudioVolumeType): Promise<boolean> 4379 4380获取指定音频流是否为活跃状态。使用Promise异步回调。 4381 4382**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4383 4384**参数:** 4385 4386| 参数名 | 类型 | 必填 | 说明 | 4387| ---------- | ----------------------------------- | ---- | ------------ | 4388| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4389 4390**返回值:** 4391 4392| 类型 | 说明 | 4393| ---------------------- | -------------------------------------------------------- | 4394| Promise<boolean> | Promise对象。返回true表示流状态为活跃;返回false表示流状态不活跃。 | 4395 4396**示例:** 4397 4398```ts 4399audioStreamManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 4400 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 4401}); 4402``` 4403 4404### isActiveSync<sup>10+</sup> 4405 4406isActiveSync(volumeType: AudioVolumeType): boolean 4407 4408获取指定音频流是否为活跃状态。同步返回结果。 4409 4410**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4411 4412**参数:** 4413 4414| 参数名 | 类型 | 必填 | 说明 | 4415| ---------- | ----------------------------------- | ---- | ------------ | 4416| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4417 4418**返回值:** 4419 4420| 类型 | 说明 | 4421| ---------------------- | -------------------------------------------------------- | 4422| boolean | 流的活跃状态。返回true表示活跃,返回false表示不活跃。 | 4423 4424**错误码:** 4425 4426以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4427 4428| 错误码ID | 错误信息 | 4429| ------- | --------------------------------------------| 4430| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4431| 6800101 | Parameter verification failed. | 4432 4433**示例:** 4434 4435```ts 4436import { BusinessError } from '@kit.BasicServicesKit'; 4437 4438try { 4439 let value: boolean = audioStreamManager.isActiveSync(audio.AudioVolumeType.MEDIA); 4440 console.info(`Indicate that the active status of the stream is obtained ${value}.`); 4441} catch (err) { 4442 let error = err as BusinessError; 4443 console.error(`Failed to obtain the active status of the stream ${error}.`); 4444} 4445``` 4446 4447### getAudioEffectInfoArray<sup>10+</sup> 4448 4449getAudioEffectInfoArray(usage: StreamUsage, callback: AsyncCallback<AudioEffectInfoArray>): void 4450 4451获取当前音效模式的信息。使用callback异步回调。 4452 4453**系统能力**: SystemCapability.Multimedia.Audio.Renderer 4454 4455**参数:** 4456 4457| 参数名 | 类型 | 必填 | 说明 | 4458| -------- | ----------------------------------- | -------- | --------------------------- | 4459| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4460| callback | AsyncCallback<[AudioEffectInfoArray](#audioeffectinfoarray10)> | 是 | 回调函数。当获取当前音效模式的信息成功,err为undefined,data为获取到的当前音效模式的信息;否则为错误对象。| 4461 4462**错误码:** 4463 4464以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4465 4466| 错误码ID | 错误信息 | 4467| ------- | --------------------------------------------| 4468| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4469| 6800101 | Parameter verification failed. Return by callback.| 4470 4471**示例:** 4472 4473```ts 4474import { BusinessError } from '@kit.BasicServicesKit'; 4475 4476audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4477 console.info('getAudioEffectInfoArray **** Get Callback Called ****'); 4478 if (err) { 4479 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4480 return; 4481 } else { 4482 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4483 } 4484}); 4485``` 4486 4487### getAudioEffectInfoArray<sup>10+</sup> 4488 4489getAudioEffectInfoArray(usage: StreamUsage): Promise<AudioEffectInfoArray> 4490 4491获取当前音效模式的信息。使用Promise异步回调。 4492 4493**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4494 4495**参数:** 4496 4497| 参数名 | 类型 | 必填 | 说明 | 4498| -------- | ----------------------------------- | -------- | --------------------------- | 4499| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4500 4501**返回值:** 4502 4503| 类型 | 说明 | 4504| --------------------------------------------------------------------------| --------------------------------------- | 4505| Promise<[AudioEffectInfoArray](#audioeffectinfoarray10)> | Promise对象,返回当前音效模式的信息。 | 4506 4507**错误码:** 4508 4509以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4510 4511| 错误码ID | 错误信息 | 4512| ------- | --------------------------------------------| 4513| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4514| 6800101 | Parameter verification failed. Return by promise. | 4515 4516**示例:** 4517 4518```ts 4519import { BusinessError } from '@kit.BasicServicesKit'; 4520 4521audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC).then((audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4522 console.info('getAudioEffectInfoArray ######### Get Promise is called ##########'); 4523 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4524}).catch((err: BusinessError) => { 4525 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4526}); 4527``` 4528 4529### getAudioEffectInfoArraySync<sup>10+</sup> 4530 4531getAudioEffectInfoArraySync(usage: StreamUsage): AudioEffectInfoArray 4532 4533获取当前音效模式的信息。同步返回结果。 4534 4535**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4536 4537**参数:** 4538 4539| 参数名 | 类型 | 必填 | 说明 | 4540| -------- | ----------------------------------- | -------- | --------------------------- | 4541| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4542 4543**返回值:** 4544 4545| 类型 | 说明 | 4546| --------------------------------------------------------------------------| --------------------------------------- | 4547| [AudioEffectInfoArray](#audioeffectinfoarray10) | 返回当前音效模式的信息。 | 4548 4549**错误码:** 4550 4551以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4552 4553| 错误码ID | 错误信息 | 4554| ------- | --------------------------------------------| 4555| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4556| 6800101 | Parameter verification failed. | 4557 4558**示例:** 4559 4560```ts 4561import { BusinessError } from '@kit.BasicServicesKit'; 4562 4563try { 4564 let audioEffectInfoArray: audio.AudioEffectInfoArray = audioStreamManager.getAudioEffectInfoArraySync(audio.StreamUsage.STREAM_USAGE_MUSIC); 4565 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4566} catch (err) { 4567 let error = err as BusinessError; 4568 console.error(`getAudioEffectInfoArraySync ERROR: ${error}`); 4569} 4570``` 4571 4572### isAcousticEchoCancelerSupported<sup>20+</sup> 4573 4574isAcousticEchoCancelerSupported(sourceType: SourceType): boolean 4575 4576查询指定的source type是否支持回声消除。 4577 4578**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4579 4580**参数:** 4581 4582| 参数名 | 类型 | 必填 | 说明 | 4583| -------- | ----------------------------------- | -------- | --------------------------- | 4584| sourceType | [SourceType](#sourcetype8) | 是 | 音源类型。 | 4585 4586**返回值:** 4587 4588| 类型 | 说明 | 4589| --------------------------------------------------------------------------| --------------------------------------- | 4590| boolean | 是否支持回声消除。true表示支持回声消除,false表示不支持回声消除。 | 4591 4592**错误码:** 4593 4594以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 4595 4596| 错误码ID | 错误信息 | 4597| ------- | --------------------------------------------| 4598| 6800101 | Parameter verification failed. | 4599 4600**示例:** 4601 4602```ts 4603import { BusinessError } from '@kit.BasicServicesKit'; 4604 4605try { 4606 let isSupportAEC = audioStreamManager.isAcousticEchoCancelerSupported(audio.SourceType.SOURCE_TYPE_LIVE); 4607 console.info(`[AEC Support] SourceType: ${audio.SourceType.SOURCE_TYPE_LIVE}, Status: ${isSupportAEC}`); 4608} catch (err) { 4609 let error = err as BusinessError; 4610 console.error(`isAcousticEchoCancelerSupported ERROR: ${error}`); 4611} 4612``` 4613 4614## AudioRoutingManager<sup>9+</sup> 4615 4616音频路由管理。 4617 4618在使用AudioRoutingManager的接口之前,需先通过[getRoutingManager](#getroutingmanager9)获取AudioRoutingManager实例。 4619 4620### getDevices<sup>9+</sup> 4621 4622getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 4623 4624获取音频设备列表。使用callback异步回调。 4625 4626**系统能力:** SystemCapability.Multimedia.Audio.Device 4627 4628**参数:** 4629 4630| 参数名 | 类型 | 必填 | 说明 | 4631| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 4632| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 4633| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 4634 4635**示例:** 4636 4637```ts 4638import { BusinessError } from '@kit.BasicServicesKit'; 4639 4640audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 4641 if (err) { 4642 console.error(`Failed to obtain the device list. ${err}`); 4643 return; 4644 } 4645 console.info('Callback invoked to indicate that the device list is obtained.'); 4646}); 4647``` 4648 4649### getDevices<sup>9+</sup> 4650 4651getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 4652 4653获取音频设备列表。使用Promise异步回调。 4654 4655**系统能力:** SystemCapability.Multimedia.Audio.Device 4656 4657**参数:** 4658 4659| 参数名 | 类型 | 必填 | 说明 | 4660| ---------- | ------------------------- | ---- | ---------------- | 4661| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 4662 4663**返回值:** 4664 4665| 类型 | 说明 | 4666| ------------------------------------------------------------ | ------------------------- | 4667| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 4668 4669**示例:** 4670 4671```ts 4672audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 4673 console.info('Promise returned to indicate that the device list is obtained.'); 4674}); 4675``` 4676 4677### getDevicesSync<sup>10+</sup> 4678 4679getDevicesSync(deviceFlag: DeviceFlag): AudioDeviceDescriptors 4680 4681获取音频设备列表。同步返回结果。 4682 4683**系统能力:** SystemCapability.Multimedia.Audio.Device 4684 4685**参数:** 4686 4687| 参数名 | 类型 | 必填 | 说明 | 4688| ---------- | ------------------------- | ---- | ---------------- | 4689| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 4690 4691**返回值:** 4692 4693| 类型 | 说明 | 4694| ------------------------------------------------------------ | ------------------------- | 4695| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回设备列表。 | 4696 4697**错误码:** 4698 4699以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4700 4701| 错误码ID | 错误信息 | 4702| ------- | --------------------------------------------| 4703| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4704| 6800101 | Parameter verification failed. | 4705 4706**示例:** 4707 4708```ts 4709import { BusinessError } from '@kit.BasicServicesKit'; 4710 4711try { 4712 let data: audio.AudioDeviceDescriptors = audioRoutingManager.getDevicesSync(audio.DeviceFlag.OUTPUT_DEVICES_FLAG); 4713 console.info(`Indicate that the device list is obtained ${data}`); 4714} catch (err) { 4715 let error = err as BusinessError; 4716 console.error(`Failed to obtain the device list. ${error}`); 4717} 4718``` 4719 4720### isMicBlockDetectionSupported<sup>13+</sup> 4721 4722isMicBlockDetectionSupported(): Promise<boolean> 4723 4724获取当前设备是否支持麦克风状态检测。使用Promise异步回调。 4725 4726**系统能力:** SystemCapability.Multimedia.Audio.Device 4727 4728**返回值:** 4729 4730| 类型 | 说明 | 4731| ---------------------- | ------------------------------------------------------------ | 4732| Promise<boolean> | Promise对象。返回true表示支持;返回false表示不支持。 | 4733 4734**示例:** 4735 4736```ts 4737audioRoutingManager.isMicBlockDetectionSupported().then((value: boolean) => { 4738 console.info(`Query whether microphone block detection is supported on current device result is ${value}.`); 4739}); 4740``` 4741 4742### on('micBlockStatusChanged')<sup>13+</sup> 4743 4744on(type: 'micBlockStatusChanged', callback: Callback<DeviceBlockStatusInfo\>): void 4745 4746监听麦克风堵塞状态变化事件。使用callback异步回调。 4747 4748使用此功能前,请查询设备是否支持检测。应用在使用麦克风录音时,若麦克风堵塞状态发生变化,将触发该事件。目前此检测功能仅支持麦克风位于本地设备上。 4749 4750**系统能力:** SystemCapability.Multimedia.Audio.Device 4751 4752**参数:** 4753 4754| 参数名 | 类型 | 必填 | 说明 | 4755| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 4756| type | string | 是 | 事件回调类型,支持的事件为'micBlockStatusChanged',当麦克风堵塞状态发生变化时,触发该事件。 | 4757| callback | Callback<[DeviceBlockStatusInfo](#deviceblockstatusinfo13)\> | 是 | 回调函数,返回麦克风被堵塞状态和设备信息。 | 4758 4759**错误码:** 4760 4761以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4762 4763| 错误码ID | 错误信息 | 4764| ------- | --------------------------------------------| 4765| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4766| 6800101 | Parameter verification failed. | 4767 4768**示例:** 4769 4770```ts 4771// 在使用此功能之前,应先查询当前设备是否支持检测。 4772audioRoutingManager.isMicBlockDetectionSupported().then((value: boolean) => { 4773 console.info(`Query whether microphone block detection is supported on current device result is ${value}.`); 4774 if (value) { 4775 audioRoutingManager.on('micBlockStatusChanged', (micBlockStatusChanged: audio.DeviceBlockStatusInfo) => { 4776 console.info(`block status : ${micBlockStatusChanged.blockStatus} `); 4777 }); 4778 } 4779}); 4780``` 4781 4782### off('micBlockStatusChanged')<sup>13+</sup> 4783 4784off(type: 'micBlockStatusChanged', callback?: Callback<DeviceBlockStatusInfo\>): void 4785 4786取消监听麦克风堵塞状态变化事件。使用callback异步回调。 4787 4788**系统能力:** SystemCapability.Multimedia.Audio.Device 4789 4790**参数:** 4791 4792| 参数名 | 类型 | 必填 | 说明 | 4793| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4794| type | string | 是 | 事件回调类型,支持的事件为'micBlockStatusChanged',当取消监听音频麦克风是否被堵塞变化事件时,触发该事件。 | 4795| callback | Callback<[DeviceBlockStatusInfo](#deviceblockstatusinfo13)\> | 否 | 回调函数,返回麦克风被堵塞状态和设备信息。| 4796 4797**错误码:** 4798 4799以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4800 4801| 错误码ID | 错误信息 | 4802| ------- | --------------------------------------------| 4803| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4804| 6800101 | Parameter verification failed. | 4805 4806**示例:** 4807 4808```ts 4809// 取消该事件的所有监听。 4810audioRoutingManager.off('micBlockStatusChanged'); 4811 4812// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4813let micBlockStatusCallback = (micBlockStatusChanged: audio.DeviceBlockStatusInfo) => { 4814 console.info(`block status : ${micBlockStatusChanged.blockStatus} `); 4815}; 4816 4817audioRoutingManager.on('micBlockStatusChanged', micBlockStatusCallback); 4818 4819audioRoutingManager.off('micBlockStatusChanged', micBlockStatusCallback); 4820``` 4821 4822### on('deviceChange')<sup>9+</sup> 4823 4824on(type: 'deviceChange', deviceFlag: DeviceFlag, callback: Callback<DeviceChangeAction\>): void 4825 4826监听音频设备连接状态变化事件(当音频设备连接状态发生变化时触发)。使用callback异步回调。 4827 4828**系统能力:** SystemCapability.Multimedia.Audio.Device 4829 4830**参数:** 4831 4832| 参数名 | 类型 | 必填 | 说明 | 4833| :------- | :--------------------------------------------------- | :--- |:------------------------| 4834| type | string | 是 | 事件回调类型,支持的事件为'deviceChange',当音频设备连接状态发生变化时,触发该事件。 | 4835| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 音频设备类型。 | 4836| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 4837 4838**错误码:** 4839 4840以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4841 4842| 错误码ID | 错误信息 | 4843| ------- | --------------------------------------------| 4844| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4845| 6800101 | Parameter verification failed. | 4846 4847**示例:** 4848 4849```ts 4850audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (deviceChanged: audio.DeviceChangeAction) => { 4851 console.info('device change type : ' + deviceChanged.type); 4852 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4853 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4854 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4855}); 4856``` 4857 4858### off('deviceChange')<sup>9+</sup> 4859 4860off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 4861 4862取消监听音频设备连接状态变化事件。使用callback异步回调。 4863 4864**系统能力:** SystemCapability.Multimedia.Audio.Device 4865 4866**参数:** 4867 4868| 参数名 | 类型 | 必填 | 说明 | 4869| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4870| type | string | 是 | 事件回调类型,支持的事件为'deviceChange',当取消监听音频设备连接变化事件时,触发该事件。 | 4871| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 4872 4873**错误码:** 4874 4875以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4876 4877| 错误码ID | 错误信息 | 4878| ------- | --------------------------------------------| 4879| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4880| 6800101 | Parameter verification failed. | 4881 4882**示例:** 4883 4884```ts 4885// 取消该事件的所有监听。 4886audioRoutingManager.off('deviceChange'); 4887 4888// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 4889let deviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 4890 console.info('device change type : ' + deviceChanged.type); 4891 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4892 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4893 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4894}; 4895 4896audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, deviceChangeCallback); 4897 4898audioRoutingManager.off('deviceChange', deviceChangeCallback); 4899``` 4900 4901### setCommunicationDevice<sup>9+</sup> 4902 4903setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean, callback: AsyncCallback<void>): void 4904 4905设置通信设备激活状态。使用callback异步回调。 4906 4907该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 4908 4909推荐使用AVSession提供的[设备切换组件](../../media/avsession/using-switch-call-devices.md),实现通话设备切换。 4910 4911**系统能力:** SystemCapability.Multimedia.Audio.Communication 4912 4913**参数:** 4914 4915| 参数名 | 类型 | 必填 | 说明 | 4916| ---------- | ------------------------------------- | ---- |-------------------------| 4917| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 音频设备类型。 | 4918| active | boolean | 是 | 是否设置设备为激活状态。true表示激活,false表示未激活。 | 4919| callback | AsyncCallback<void> | 是 | 回调函数。当设置通信设备激活状态成功,err为undefined,否则为错误对象。 | 4920 4921**示例:** 4922 4923```ts 4924import { BusinessError } from '@kit.BasicServicesKit'; 4925 4926audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true, (err: BusinessError) => { 4927 if (err) { 4928 console.error(`Failed to set the active status of the device. ${err}`); 4929 return; 4930 } 4931 console.info('Callback invoked to indicate that the device is set to the active status.'); 4932}); 4933``` 4934 4935### getAvailableDevices<sup>12+</sup> 4936 4937getAvailableDevices(deviceUsage: DeviceUsage): AudioDeviceDescriptors 4938 4939获取音频可选设备列表。同步返回结果。 4940 4941**系统能力:** SystemCapability.Multimedia.Audio.Device 4942 4943**参数:** 4944 4945| 参数名 | 类型 | 必填 | 说明 | 4946| ---------- | ------------------------- | ---- | ---------------- | 4947| deviceUsage| [DeviceUsage](#deviceusage12) | 是 | 音频设备类型(根据用途分类)。 | 4948 4949**返回值:** 4950 4951| 类型 | 说明 | 4952| ------------------------------------------------------------ | ------------------------- | 4953| [AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors) | 返回设备列表。 | 4954 4955**错误码:** 4956 4957以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4958 4959| 错误码ID | 错误信息 | 4960| ------- | --------------------------------------------| 4961| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 4962| 6800101 | Parameter verification failed. | 4963 4964**示例:** 4965 4966```ts 4967import { BusinessError } from '@kit.BasicServicesKit'; 4968 4969try { 4970 let data: audio.AudioDeviceDescriptors = audioRoutingManager.getAvailableDevices(audio.DeviceUsage.MEDIA_OUTPUT_DEVICES); 4971 console.info(`Indicate that the device list is obtained ${data}`); 4972} catch (err) { 4973 let error = err as BusinessError; 4974 console.error(`Failed to obtain the device list. ${error}`); 4975} 4976``` 4977 4978### on('availableDeviceChange')<sup>12+</sup> 4979 4980on(type: 'availableDeviceChange', deviceUsage: DeviceUsage, callback: Callback<DeviceChangeAction\>): void 4981 4982监听音频可选设备连接状态变化事件(当音频可选设备连接状态发生变化时触发)。使用callback异步回调。 4983 4984**系统能力:** SystemCapability.Multimedia.Audio.Device 4985 4986**参数:** 4987 4988| 参数名 | 类型 | 必填 | 说明 | 4989| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 4990| type | string | 是 | 事件回调类型,支持的事件为'availableDeviceChange',当音频可选设备连接状态发生变化时,触发该事件。 | 4991| deviceUsage | [DeviceUsage](#deviceusage12) | 是 | 音频设备类型(根据用途分类)。 | 4992| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 4993 4994**错误码:** 4995 4996以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 4997 4998| 错误码ID | 错误信息 | 4999| ------- | --------------------------------------------| 5000| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5001| 6800101 | Parameter verification failed. | 5002 5003**示例:** 5004 5005```ts 5006audioRoutingManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_OUTPUT_DEVICES, (deviceChanged: audio.DeviceChangeAction) => { 5007 console.info('device change type : ' + deviceChanged.type); 5008 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 5009 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 5010 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 5011}); 5012``` 5013 5014### off('availableDeviceChange')<sup>12+</sup> 5015 5016off(type: 'availableDeviceChange', callback?: Callback<DeviceChangeAction\>): void 5017 5018取消监听音频可选设备连接状态变化事件。使用callback异步回调。 5019 5020**系统能力:** SystemCapability.Multimedia.Audio.Device 5021 5022**参数:** 5023 5024| 参数名 | 类型 | 必填 | 说明 | 5025| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5026| type | string | 是 | 事件回调类型,支持的事件为'availableDeviceChange',当取消监听音频可选设备连接变化事件时,触发该事件。 | 5027| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回可选设备更新详情。 | 5028 5029**错误码:** 5030 5031以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5032 5033| 错误码ID | 错误信息 | 5034| ------- | --------------------------------------------| 5035| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5036| 6800101 | Parameter verification failed. | 5037 5038**示例:** 5039 5040```ts 5041// 取消该事件的所有监听。 5042audioRoutingManager.off('availableDeviceChange'); 5043 5044// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5045let availableDeviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => { 5046 console.info('device change type : ' + deviceChanged.type); 5047 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 5048 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 5049 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 5050}; 5051 5052audioRoutingManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_OUTPUT_DEVICES, availableDeviceChangeCallback); 5053 5054audioRoutingManager.off('availableDeviceChange', availableDeviceChangeCallback); 5055``` 5056 5057### setCommunicationDevice<sup>9+</sup> 5058 5059setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean): Promise<void> 5060 5061设置通信设备激活状态。使用Promise异步回调。 5062 5063该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 5064 5065推荐开发者使用AVSession提供的[设备切换组件](../../media/avsession/using-switch-call-devices.md),实现通话设备切换。 5066 5067**系统能力:** SystemCapability.Multimedia.Audio.Communication 5068 5069**参数:** 5070 5071| 参数名 | 类型 | 必填 | 说明 | 5072| ---------- | ----------------------------------------------------- | ---- | ------------------ | 5073| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 5074| active | boolean | 是 | 是否设置设备为激活状态。true表示激活,false表示未激活。 | 5075 5076**返回值:** 5077 5078| 类型 | 说明 | 5079| ------------------- | ------------------------------- | 5080| Promise<void> | Promise对象。无返回结果的Promise对象。 | 5081 5082**示例:** 5083 5084```ts 5085audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true).then(() => { 5086 console.info('Promise returned to indicate that the device is set to the active status.'); 5087}); 5088``` 5089 5090### isCommunicationDeviceActive<sup>9+</sup> 5091 5092isCommunicationDeviceActive(deviceType: CommunicationDeviceType, callback: AsyncCallback<boolean>): void 5093 5094获取指定通信设备的激活状态。使用callback异步回调。 5095 5096**系统能力:** SystemCapability.Multimedia.Audio.Communication 5097 5098**参数:** 5099 5100| 参数名 | 类型 | 必填 | 说明 | 5101| ---------- | ---------------------------------------------------- | ---- | ------------------------ | 5102| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 5103| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定通信设备的激活状态成功,err为undefined,data为true表示激活,false表示未激活;否则为错误对象。 | 5104 5105**示例:** 5106 5107```ts 5108import { BusinessError } from '@kit.BasicServicesKit'; 5109 5110audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 5111 if (err) { 5112 console.error(`Failed to obtain the active status of the device. ${err}`); 5113 return; 5114 } 5115 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 5116}); 5117``` 5118 5119### isCommunicationDeviceActive<sup>9+</sup> 5120 5121isCommunicationDeviceActive(deviceType: CommunicationDeviceType): Promise<boolean> 5122 5123获取指定通信设备的激活状态。使用Promise异步回调。 5124 5125**系统能力:** SystemCapability.Multimedia.Audio.Communication 5126 5127**参数:** 5128 5129| 参数名 | 类型 | 必填 | 说明 | 5130| ---------- | ---------------------------------------------------- | ---- | ------------------ | 5131| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 5132 5133**返回值:** 5134 5135| Type | Description | 5136| ---------------------- | ------------------------------- | 5137| Promise<boolean> | Promise对象。返回true表示设备已激活;返回false表示设备未激活。 | 5138 5139**示例:** 5140 5141```ts 5142audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER).then((value: boolean) => { 5143 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 5144}); 5145``` 5146 5147### isCommunicationDeviceActiveSync<sup>10+</sup> 5148 5149isCommunicationDeviceActiveSync(deviceType: CommunicationDeviceType): boolean 5150 5151获取指定通信设备的激活状态。同步返回结果。 5152 5153**系统能力:** SystemCapability.Multimedia.Audio.Communication 5154 5155**参数:** 5156 5157| 参数名 | 类型 | 必填 | 说明 | 5158| ---------- | ---------------------------------------------------- | ---- | ------------------ | 5159| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 5160 5161**返回值:** 5162 5163| Type | Description | 5164| ---------------------- | ------------------------------- | 5165| boolean | 设备是否处于激活状态。true表示处于激活状态,false表示处于未激活状态。 | 5166 5167**错误码:** 5168 5169以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5170 5171| 错误码ID | 错误信息 | 5172| ------- | --------------------------------------------| 5173| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5174| 6800101 | Parameter verification failed. | 5175 5176**示例:** 5177 5178```ts 5179import { BusinessError } from '@kit.BasicServicesKit'; 5180 5181try { 5182 let value: boolean = audioRoutingManager.isCommunicationDeviceActiveSync(audio.CommunicationDeviceType.SPEAKER); 5183 console.info(`Indicate that the active status of the device is obtained ${value}.`); 5184} catch (err) { 5185 let error = err as BusinessError; 5186 console.error(`Failed to obtain the active status of the device ${error}.`); 5187} 5188``` 5189 5190### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 5191 5192getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 5193 5194根据音频信息,返回优先级最高的输出设备。使用callback异步回调。 5195 5196**系统能力:** SystemCapability.Multimedia.Audio.Device 5197 5198**参数:** 5199 5200| 参数名 | 类型 | 必填 | 说明 | 5201| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 5202| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 音频渲染器信息。 | 5203| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输出设备成功,err为undefined,data为获取到的优先级最高的输出设备信息;否则为错误对象。 | 5204 5205**错误码:** 5206 5207以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5208 5209| 错误码ID | 错误信息 | 5210| ------- |--------------------------------------------------| 5211| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5212| 6800101 | Parameter verification failed. Return by callback. | 5213| 6800301 | System error. Return by callback. | 5214 5215**示例:** 5216```ts 5217import { audio } from '@kit.AudioKit'; 5218import { BusinessError } from '@kit.BasicServicesKit'; 5219 5220let rendererInfo: audio.AudioRendererInfo = { 5221 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 5222 rendererFlags: 0 // 音频渲染器标志。 5223}; 5224 5225async function getPreferOutputDevice() { 5226 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 5227 if (err) { 5228 console.error(`Result ERROR: ${err}`); 5229 } else { 5230 console.info(`device descriptor: ${desc}`); 5231 } 5232 }); 5233} 5234``` 5235 5236### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 5237getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo): Promise<AudioDeviceDescriptors> 5238 5239根据音频信息,返回优先级最高的输出设备。使用Promise异步回调。 5240 5241**系统能力:** SystemCapability.Multimedia.Audio.Device 5242 5243**参数:** 5244 5245| 参数名 | 类型 | 必填 | 说明 | 5246| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5247| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 音频渲染器信息。 | 5248 5249**返回值:** 5250 5251| 类型 | 说明 | 5252| --------------------- | --------------------------- | 5253| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输出设备信息。 | 5254 5255**错误码:** 5256 5257以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5258 5259| 错误码ID | 错误信息 | 5260| ------- |-------------------------------------------------| 5261| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5262| 6800101 | Parameter verification failed. Return by promise. | 5263| 6800301 | System error. Return by promise. | 5264 5265**示例:** 5266 5267```ts 5268import { audio } from '@kit.AudioKit'; 5269import { BusinessError } from '@kit.BasicServicesKit'; 5270 5271let rendererInfo: audio.AudioRendererInfo = { 5272 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 5273 rendererFlags: 0 // 音频渲染器标志。 5274}; 5275 5276async function getPreferOutputDevice() { 5277 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => { 5278 console.info(`device descriptor: ${desc}`); 5279 }).catch((err: BusinessError) => { 5280 console.error(`Result ERROR: ${err}`); 5281 }) 5282} 5283``` 5284 5285### getPreferredOutputDeviceForRendererInfoSync<sup>10+</sup> 5286getPreferredOutputDeviceForRendererInfoSync(rendererInfo: AudioRendererInfo): AudioDeviceDescriptors 5287 5288根据音频信息,返回优先级最高的输出设备。同步返回结果。 5289 5290**系统能力:** SystemCapability.Multimedia.Audio.Device 5291 5292**参数:** 5293 5294| 参数名 | 类型 | 必填 | 说明 | 5295| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5296| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 音频渲染器信息。 | 5297 5298**返回值:** 5299 5300| 类型 | 说明 | 5301| --------------------- | --------------------------- | 5302| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输出设备信息。 | 5303 5304**错误码:** 5305 5306以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5307 5308| 错误码ID | 错误信息 | 5309| ------- |--------------------------| 5310| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5311| 6800101 | Parameter verification failed. | 5312 5313**示例:** 5314 5315```ts 5316import { audio } from '@kit.AudioKit'; 5317import { BusinessError } from '@kit.BasicServicesKit'; 5318 5319let rendererInfo: audio.AudioRendererInfo = { 5320 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 5321 rendererFlags: 0 // 音频渲染器标志。 5322}; 5323 5324try { 5325 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredOutputDeviceForRendererInfoSync(rendererInfo); 5326 console.info(`device descriptor: ${desc}`); 5327} catch (err) { 5328 let error = err as BusinessError; 5329 console.error(`Result ERROR: ${error}`); 5330} 5331``` 5332 5333### on('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 5334 5335on(type: 'preferOutputDeviceChangeForRendererInfo', rendererInfo: AudioRendererInfo, callback: Callback<AudioDeviceDescriptors\>): void 5336 5337监听最高优先级输出设备变化事件(当最高优先级输出设备发生变化时触发)。使用callback异步回调。 5338 5339**系统能力:** SystemCapability.Multimedia.Audio.Device 5340 5341**参数:** 5342 5343| 参数名 | 类型 | 必填 | 说明 | 5344| :------- | :--------------------------------------------------- | :--- |:--------------------------------------------------------| 5345| type | string | 是 | 事件回调类型,支持的事件为'preferOutputDeviceChangeForRendererInfo',当最高优先级输出设备发生变化时,触发该事件。| 5346| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 音频渲染器信息。 | 5347| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输出设备信息。 | 5348 5349**错误码:** 5350 5351以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5352 5353| 错误码ID | 错误信息 | 5354| ------- | --------------------------------------------| 5355| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5356| 6800101 | Parameter verification failed. | 5357 5358**示例:** 5359 5360```ts 5361import { audio } from '@kit.AudioKit'; 5362 5363let rendererInfo: audio.AudioRendererInfo = { 5364 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 5365 rendererFlags: 0 // 音频渲染器标志。 5366}; 5367 5368audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => { 5369 console.info(`device descriptor: ${desc}`); 5370}); 5371``` 5372 5373### off('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 5374 5375off(type: 'preferOutputDeviceChangeForRendererInfo', callback?: Callback<AudioDeviceDescriptors\>): void 5376 5377取消监听最高优先级输出音频设备变化事件。使用callback异步回调。 5378 5379**系统能力:** SystemCapability.Multimedia.Audio.Device 5380 5381**参数:** 5382 5383| 参数名 | 类型 | 必填 | 说明 | 5384| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5385| type | string | 是 | 事件回调类型,支持的事件为'preferOutputDeviceChangeForRendererInfo',当取消监听最高优先级输出音频设备变化事件时,触发该事件。 | 5386| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输出设备信息。 | 5387 5388**错误码:** 5389 5390以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5391 5392| 错误码ID | 错误信息 | 5393| ------- | --------------------------------------------| 5394| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5395| 6800101 | Parameter verification failed. | 5396 5397**示例:** 5398 5399```ts 5400// 取消该事件的所有监听。 5401audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo'); 5402 5403// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5404let preferOutputDeviceChangeForRendererInfoCallback = (desc: audio.AudioDeviceDescriptors) => { 5405 console.info(`device descriptor: ${desc}`); 5406}; 5407let rendererInfo: audio.AudioRendererInfo = { 5408 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // 音频流使用类型:音乐。根据业务场景配置,参考StreamUsage。 5409 rendererFlags: 0 // 音频渲染器标志。 5410}; 5411 5412audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, preferOutputDeviceChangeForRendererInfoCallback); 5413 5414audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo', preferOutputDeviceChangeForRendererInfoCallback); 5415``` 5416 5417### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 5418 5419getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 5420 5421根据音频信息,返回优先级最高的输入设备。使用callback异步回调。 5422 5423**系统能力:** SystemCapability.Multimedia.Audio.Device 5424 5425**参数:** 5426 5427| 参数名 | 类型 | 必填 | 说明 | 5428| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 5429| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 音频采集器信息。 | 5430| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输入设备成功,err为undefined,data为获取到的优先级最高的输入设备信息;否则为错误对象。 | 5431 5432**错误码:** 5433 5434以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5435 5436| 错误码ID | 错误信息 | 5437| ------- | --------------------------------------------| 5438| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5439| 6800101 | Parameter verification failed. Return by callback.| 5440| 6800301 | System error. Return by callback. | 5441 5442**示例:** 5443```ts 5444import { audio } from '@kit.AudioKit'; 5445import { BusinessError } from '@kit.BasicServicesKit'; 5446 5447let capturerInfo: audio.AudioCapturerInfo = { 5448 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5449 capturerFlags: 0 // 音频采集器标志。 5450}; 5451 5452audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 5453 if (err) { 5454 console.error(`Result ERROR: ${err}`); 5455 } else { 5456 console.info(`device descriptor: ${desc}`); 5457 } 5458}); 5459``` 5460 5461### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 5462 5463getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo): Promise<AudioDeviceDescriptors> 5464 5465根据音频信息,返回优先级最高的输入设备。使用Promise异步回调。 5466 5467**系统能力:** SystemCapability.Multimedia.Audio.Device 5468 5469**参数:** 5470 5471| 参数名 | 类型 | 必填 | 说明 | 5472| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5473| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 音频采集器信息。 | 5474 5475**返回值:** 5476 5477| 类型 | 说明 | 5478| --------------------- | --------------------------- | 5479| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输入设备信息。 | 5480 5481**错误码:** 5482 5483以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5484 5485| 错误码ID | 错误信息 | 5486| ------- | --------------------------------------------| 5487| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5488| 6800101 | Parameter verification failed. Return by promise. | 5489| 6800301 | System error. Return by promise. | 5490 5491**示例:** 5492 5493```ts 5494import { audio } from '@kit.AudioKit'; 5495import { BusinessError } from '@kit.BasicServicesKit'; 5496 5497let capturerInfo: audio.AudioCapturerInfo = { 5498 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5499 capturerFlags: 0 // 音频采集器标志。 5500}; 5501 5502audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo).then((desc: audio.AudioDeviceDescriptors) => { 5503 console.info(`device descriptor: ${desc}`); 5504}).catch((err: BusinessError) => { 5505 console.error(`Result ERROR: ${err}`); 5506}); 5507``` 5508 5509### getPreferredInputDeviceForCapturerInfoSync<sup>10+</sup> 5510 5511getPreferredInputDeviceForCapturerInfoSync(capturerInfo: AudioCapturerInfo): AudioDeviceDescriptors 5512 5513根据音频信息,返回优先级最高的输入设备。同步返回结果。 5514 5515**系统能力:** SystemCapability.Multimedia.Audio.Device 5516 5517**参数:** 5518 5519| 参数名 | 类型 | 必填 | 说明 | 5520| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5521| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 音频采集器信息。 | 5522 5523**返回值:** 5524 5525| 类型 | 说明 | 5526| --------------------- | --------------------------- | 5527| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输入设备信息。 | 5528 5529**错误码:** 5530 5531以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5532 5533| 错误码ID | 错误信息 | 5534| ------- | --------------------------------------------| 5535| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5536| 6800101 | Parameter verification failed. | 5537 5538**示例:** 5539 5540```ts 5541import { audio } from '@kit.AudioKit'; 5542import { BusinessError } from '@kit.BasicServicesKit'; 5543 5544let capturerInfo: audio.AudioCapturerInfo = { 5545 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5546 capturerFlags: 0 // 音频采集器标志。 5547}; 5548 5549try { 5550 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredInputDeviceForCapturerInfoSync(capturerInfo); 5551 console.info(`device descriptor: ${desc}`); 5552} catch (err) { 5553 let error = err as BusinessError; 5554 console.error(`Result ERROR: ${error}`); 5555} 5556``` 5557 5558### on('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5559 5560on(type: 'preferredInputDeviceChangeForCapturerInfo', capturerInfo: AudioCapturerInfo, callback: Callback<AudioDeviceDescriptors\>): void 5561 5562监听最高优先级输入设备变化事件(当最高优先级输入设备发生变化时触发)。使用callback异步回调。 5563 5564**系统能力:** SystemCapability.Multimedia.Audio.Device 5565 5566**参数:** 5567 5568| 参数名 | 类型 | 必填 | 说明 | 5569| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 5570| type | string | 是 | 事件回调类型,支持的事件为'preferredInputDeviceChangeForCapturerInfo',当最高优先级输入设备发生变化时,触发该事件。 | 5571| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 音频采集器信息。 | 5572| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输入设备信息。 | 5573 5574**错误码:** 5575 5576以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5577 5578| 错误码ID | 错误信息 | 5579| ------- | --------------------------------------------| 5580| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5581| 6800101 | Parameter verification failed. | 5582 5583**示例:** 5584 5585```ts 5586import { audio } from '@kit.AudioKit'; 5587 5588let capturerInfo: audio.AudioCapturerInfo = { 5589 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5590 capturerFlags: 0 // 音频采集器标志。 5591}; 5592 5593audioRoutingManager.on('preferredInputDeviceChangeForCapturerInfo', capturerInfo, (desc: audio.AudioDeviceDescriptors) => { 5594 console.info(`device descriptor: ${desc}`); 5595}); 5596``` 5597 5598### off('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5599 5600off(type: 'preferredInputDeviceChangeForCapturerInfo', callback?: Callback<AudioDeviceDescriptors\>): void 5601 5602取消监听最高优先级输入音频设备变化事件。使用callback异步回调。 5603 5604**系统能力:** SystemCapability.Multimedia.Audio.Device 5605 5606**参数:** 5607 5608| 参数名 | 类型 | 必填 | 说明 | 5609| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5610| type | string | 是 | 事件回调类型,支持的事件为'preferredInputDeviceChangeForCapturerInfo',当取消监听最高优先级输入音频设备变化事件时,触发该事件。 | 5611| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输入设备信息。 | 5612 5613**错误码:** 5614 5615以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5616 5617| 错误码ID | 错误信息 | 5618| ------- | --------------------------------------------| 5619| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5620| 6800101 | Parameter verification failed. | 5621 5622**示例:** 5623 5624```ts 5625// 取消该事件的所有监听。 5626audioRoutingManager.off('preferredInputDeviceChangeForCapturerInfo'); 5627 5628// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5629let preferredInputDeviceChangeForCapturerInfoCallback = (desc: audio.AudioDeviceDescriptors) => { 5630 console.info(`device descriptor: ${desc}`); 5631}; 5632let capturerInfo: audio.AudioCapturerInfo = { 5633 source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型:Mic音频源。根据业务场景配置,参考SourceType。 5634 capturerFlags: 0 // 音频采集器标志。 5635}; 5636 5637audioRoutingManager.on('preferredInputDeviceChangeForCapturerInfo', capturerInfo, preferredInputDeviceChangeForCapturerInfoCallback); 5638 5639audioRoutingManager.off('preferredInputDeviceChangeForCapturerInfo', preferredInputDeviceChangeForCapturerInfoCallback); 5640``` 5641 5642## AudioSessionManager<sup>12+</sup> 5643 5644音频会话管理。 5645 5646在使用AudioSessionManager的接口之前,需先通过[getSessionManager](#getsessionmanager12)获取AudioSessionManager实例。 5647 5648### activateAudioSession<sup>12+</sup> 5649 5650activateAudioSession(strategy: AudioSessionStrategy): Promise\<void> 5651 5652激活音频会话。使用Promise异步回调。 5653 5654**系统能力:** SystemCapability.Multimedia.Audio.Core 5655 5656**参数:** 5657 5658| 参数名 | 类型 | 必填 | 说明 | 5659| ------ |-------------------------------------------------| ---- | ------------ | 5660| strategy | [AudioSessionStrategy](#audiosessionstrategy12) | 是 | 音频会话策略。 | 5661 5662**返回值:** 5663 5664| 类型 | 说明 | 5665| -------------- | ------------------------- | 5666| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 5667 5668**错误码:** 5669 5670以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5671 5672| 错误码ID | 错误信息 | 5673| ------- | ---------------------------------------------| 5674| 401 | Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types. | 5675| 6800101 | Parameter verification failed.| 5676| 6800301 | System error. Returned by promise. | 5677 5678**示例:** 5679 5680```ts 5681import { BusinessError } from '@kit.BasicServicesKit'; 5682 5683let strategy: audio.AudioSessionStrategy = { 5684 concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS 5685}; 5686 5687audioSessionManager.activateAudioSession(strategy).then(() => { 5688 console.info('activateAudioSession SUCCESS'); 5689}).catch((err: BusinessError) => { 5690 console.error(`ERROR: ${err}`); 5691}); 5692``` 5693 5694### deactivateAudioSession<sup>12+</sup> 5695 5696deactivateAudioSession(): Promise\<void> 5697 5698停用音频会话。使用Promise异步回调。 5699 5700**系统能力:** SystemCapability.Multimedia.Audio.Core 5701 5702**返回值:** 5703 5704| 类型 | 说明 | 5705| -------------- | ------------------------- | 5706| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 5707 5708**错误码:** 5709 5710以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5711 5712| 错误码ID | 错误信息 | 5713| ------- | ---------------------------------------------| 5714| 6800301 | System error. Returned by promise. | 5715 5716**示例:** 5717 5718```ts 5719import { BusinessError } from '@kit.BasicServicesKit'; 5720 5721audioSessionManager.deactivateAudioSession().then(() => { 5722 console.info('deactivateAudioSession SUCCESS'); 5723}).catch((err: BusinessError) => { 5724 console.error(`ERROR: ${err}`); 5725}); 5726``` 5727 5728### isAudioSessionActivated<sup>12+</sup> 5729 5730isAudioSessionActivated(): boolean 5731 5732检查音频会话是否已激活。 5733 5734**系统能力:** SystemCapability.Multimedia.Audio.Core 5735 5736**返回值:** 5737 5738| 类型 | 说明 | 5739| ------------------------------------------------- |---------------------------------------| 5740| boolean | 音频会话是否处于激活状态。true表示已激活,false表示已停用。 | 5741 5742**示例:** 5743 5744```ts 5745let isActivated = audioSessionManager.isAudioSessionActivated(); 5746``` 5747 5748### on('audioSessionDeactivated')<sup>12+</sup> 5749 5750on(type: 'audioSessionDeactivated', callback: Callback\<AudioSessionDeactivatedEvent>): void 5751 5752监听音频会话停用事件(当音频会话停用时触发)。使用callback异步回调。 5753 5754**系统能力:** SystemCapability.Multimedia.Audio.Core 5755 5756**参数:** 5757 5758| 参数名 | 类型 | 必填 | 说明 | 5759| -------- |---------------------------------------------------------------------------| ---- | ------------------------------------------------------------ | 5760| type | string | 是 | 事件回调类型,支持的事件为'audioSessionDeactivated',当音频会话停用时,触发该事件。 | 5761| callback | Callback<[AudioSessionDeactivatedEvent](#audiosessiondeactivatedevent12)> | 是 | 回调函数,返回音频会话停用原因。 | 5762 5763**错误码:** 5764 5765以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5766 5767| 错误码ID | 错误信息 | 5768| ------- | --------------------------------------------| 5769| 401 | Parameter error. Possible causes: 1.Mandatory parameters unspecified. 2.Incorrect parameter types. | 5770| 6800101 | Parameter verification failed. | 5771 5772**示例:** 5773 5774```ts 5775audioSessionManager.on('audioSessionDeactivated', (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 5776 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 5777}); 5778``` 5779 5780### off('audioSessionDeactivated')<sup>12+</sup> 5781 5782off(type: 'audioSessionDeactivated', callback?: Callback\<AudioSessionDeactivatedEvent>): void 5783 5784取消监听音频会话停用事件。使用callback异步回调。 5785 5786**系统能力:** SystemCapability.Multimedia.Audio.Core 5787 5788**参数:** 5789 5790| 参数名 | 类型 | 必填 | 说明 | 5791| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 5792| type | string | 是 | 事件回调类型,支持的事件为'audioSessionDeactivated',当取消监听音频会话停用事件时,触发该事件。 | 5793| callback |Callback<[AudioSessionDeactivatedEvent](#audiosessiondeactivatedevent12)> | 否 | 回调函数,返回音频会话停用原因。 | 5794 5795**错误码:** 5796 5797以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 5798 5799| 错误码ID | 错误信息 | 5800| ------- | --------------------------------------------| 5801| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 5802| 6800101 | Parameter verification failed. | 5803 5804**示例:** 5805 5806```ts 5807// 取消该事件的所有监听。 5808audioSessionManager.off('audioSessionDeactivated'); 5809 5810// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 5811let audioSessionDeactivatedCallback = (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 5812 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 5813}; 5814 5815audioSessionManager.on('audioSessionDeactivated', audioSessionDeactivatedCallback); 5816 5817audioSessionManager.off('audioSessionDeactivated', audioSessionDeactivatedCallback); 5818``` 5819 5820## AudioSpatializationManager<sup>18+</sup> 5821 5822空间音频管理。 5823 5824在使用AudioSpatializationManager的接口之前,需先通过[getSpatializationManager](#getspatializationmanager18)获取AudioSpatializationManager实例。 5825 5826### isSpatializationEnabledForCurrentDevice<sup>18+</sup> 5827 5828isSpatializationEnabledForCurrentDevice(): boolean 5829 5830获取当前设备空间音频渲染是否开启。同步返回结果。 5831 5832**系统能力:** SystemCapability.Multimedia.Audio.Spatialization 5833 5834**返回值:** 5835 5836| 类型 | 说明 | 5837| ---------------------- | ------------------------------------------------------------ | 5838| boolean | 当前设备空间音频渲染是否开启。true表示开启,false表示未开启。 | 5839 5840**示例:** 5841 5842```ts 5843import { audio } from '@kit.AudioKit'; 5844 5845let isSpatializationEnabledForCurrentDevice: boolean = audioSpatializationManager.isSpatializationEnabledForCurrentDevice(); 5846console.info(`AudioSpatializationManager isSpatializationEnabledForCurrentDevice: ${isSpatializationEnabledForCurrentDevice}`); 5847``` 5848 5849### on('spatializationEnabledChangeForCurrentDevice')<sup>18+</sup> 5850 5851on(type: 'spatializationEnabledChangeForCurrentDevice', callback: Callback<boolean\>): void 5852 5853监听当前设备空间音频渲染开关状态变化事件。使用callback异步回调。 5854 5855**系统能力:** SystemCapability.Multimedia.Audio.Spatialization 5856 5857**参数:** 5858 5859| 参数名 | 类型 | 必填 | 说明 | 5860| :------- | :--------------------------------------------------- | :--- |:---------------------------------------------| 5861| type | string | 是 | 事件回调类型,支持的事件为'spatializationEnabledChangeForCurrentDevice',当空间音频渲染开关状态变化时,触发该事件。 | 5862| callback | Callback<boolean\> | 是 | 回调函数。返回true表示打开空间音频渲染状态;返回false表示关闭空间音频渲染状态。 | 5863 5864**错误码:** 5865 5866以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5867 5868| 错误码ID | 错误信息 | 5869| ------- | --------------------------------------------| 5870| 6800101 | Parameter verification failed. | 5871 5872**示例:** 5873 5874```ts 5875import { audio } from '@kit.AudioKit'; 5876 5877audioSpatializationManager.on('spatializationEnabledChangeForCurrentDevice', (isSpatializationEnabledForCurrentDevice: boolean) => { 5878 console.info(`isSpatializationEnabledForCurrentDevice: ${isSpatializationEnabledForCurrentDevice}`); 5879}); 5880``` 5881 5882### off('spatializationEnabledChangeForCurrentDevice')<sup>18+</sup> 5883 5884off(type: 'spatializationEnabledChangeForCurrentDevice', callback?: Callback<boolean\>): void 5885 5886取消监听当前设备空间音频渲染开关状态变化事件。使用callback异步回调。 5887 5888**系统能力:** SystemCapability.Multimedia.Audio.Spatialization 5889 5890**参数:** 5891 5892| 参数名 | 类型 | 必填 | 说明 | 5893| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5894| type | string | 是 | 事件回调类型,支持的事件为'spatializationEnabledChangeForCurrentDevice',当取消订阅当前设备空间音频渲染开关状态变化事件时,触发该事件。 | 5895| callback | Callback<boolean\> | 否 | 回调函数。返回true表示打开空间音频渲染状态;返回false表示关闭空间音频渲染状态。 | 5896 5897**错误码:** 5898 5899以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 5900 5901| 错误码ID | 错误信息 | 5902| ------- | --------------------------------------------| 5903| 6800101 | Parameter verification failed. | 5904 5905**示例:** 5906 5907```ts 5908import { audio } from '@kit.AudioKit'; 5909audioSpatializationManager.off('spatializationEnabledChangeForCurrentDevice'); 5910``` 5911 5912## AudioRendererChangeInfoArray<sup>9+</sup> 5913 5914type AudioRendererChangeInfoArray = Array<Readonly<AudioRendererChangeInfo>> 5915 5916数组类型,AudioRenderChangeInfo数组,只读。 5917 5918**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5919 5920| 类型 | 说明 | 5921|---------|---------------------------------------------------------------| 5922| Array<Readonly<AudioRendererChangeInfo>> | 数组类型,[AudioRenderChangeInfo](#audiorendererchangeinfo9)数组,只读。 | 5923 5924## AudioRendererChangeInfo<sup>9+</sup> 5925 5926描述音频渲染器更改信息。 5927 5928**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5929 5930| 名称 | 类型 | 可读 | 可写 | 说明 | 5931| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5932| streamId | number | 是 | 否 | 音频流唯一id。 | 5933| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 否 | 音频渲染器信息。 | 5934| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备描述。| 5935 5936**示例:** 5937 5938```ts 5939import { audio } from '@kit.AudioKit'; 5940 5941const audioManager = audio.getAudioManager(); 5942let audioStreamManager = audioManager.getStreamManager(); 5943 5944audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => { 5945 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 5946 console.info(`## RendererChange on is called for ${i} ##`); 5947 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfoArray[i].streamId}`); 5948 console.info(`Content for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.content}`); 5949 console.info(`Stream for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.usage}`); 5950 console.info(`Flag ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.rendererFlags}`); 5951 let devDescriptor = AudioRendererChangeInfoArray[i].deviceDescriptors; 5952 for (let j = 0; j < AudioRendererChangeInfoArray[i].deviceDescriptors.length; j++) { 5953 console.info(`Id: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].id}`); 5954 console.info(`Type: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 5955 console.info(`Role: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 5956 console.info(`Name: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].name}`); 5957 console.info(`Addr: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].address}`); 5958 console.info(`SR: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 5959 console.info(`C ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 5960 console.info(`CM: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 5961 } 5962 } 5963}); 5964``` 5965 5966 5967## AudioCapturerChangeInfoArray<sup>9+</sup> 5968 5969type AudioCapturerChangeInfoArray = Array<Readonly<AudioCapturerChangeInfo>> 5970 5971数组类型,AudioCapturerChangeInfo数组,只读。 5972 5973**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5974 5975| 类型 | 说明 | 5976|---------|-----------------------------------------------------------------| 5977| Array<Readonly<AudioCapturerChangeInfo>> | 数组类型,[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)数组,只读。 | 5978 5979## AudioCapturerChangeInfo<sup>9+</sup> 5980 5981描述音频采集器更改信息。 5982 5983**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5984 5985| 名称 | 类型 | 可读 | 可写 | 说明 | 5986| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5987| streamId | number | 是 | 否 | 音频流唯一id。 | 5988| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 否 | 音频采集器信息。 | 5989| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备信息。| 5990| muted<sup>11+</sup> | boolean | 是 | 否 | 音频采集器是否处于静音状态。true表示静音,false表示非静音。| 5991 5992**示例:** 5993 5994```ts 5995import { audio } from '@kit.AudioKit'; 5996 5997const audioManager = audio.getAudioManager(); 5998let audioStreamManager = audioManager.getStreamManager(); 5999 6000audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) => { 6001 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 6002 console.info(`## CapChange on is called for element ${i} ##`); 6003 console.info(`StrId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 6004 console.info(`Src for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 6005 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 6006 let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors; 6007 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 6008 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 6009 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 6010 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 6011 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 6012 console.info(`Addr: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 6013 console.info(`SR: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 6014 console.info(`C ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 6015 console.info(`CM ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 6016 } 6017 } 6018}); 6019``` 6020 6021## AudioEffectInfoArray<sup>10+</sup> 6022 6023type AudioEffectInfoArray = Array<Readonly<AudioEffectMode>> 6024 6025待查询ContentType和StreamUsage组合场景下的音效模式数组类型,[AudioEffectMode](#audioeffectmode10)数组,只读。 6026 6027**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6028 6029| 类型 | 说明 | 6030|---------|---------------------------------------------------------------| 6031| Array<Readonly<AudioEffectMode>> | 待查询ContentType和StreamUsage组合场景下的音效模式数组类型,[AudioEffectMode](#audioeffectmode10)数组,只读。 | 6032 6033## AudioDeviceDescriptors 6034 6035type AudioDeviceDescriptors = Array<Readonly<AudioDeviceDescriptor>> 6036 6037设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 6038 6039**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 6040 6041**系统能力:** SystemCapability.Multimedia.Audio.Device 6042 6043| 类型 | 说明 | 6044|---------|---------------------------------------------------------------| 6045| Array<Readonly<AudioDeviceDescriptor>> | 设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 | 6046 6047## AudioDeviceDescriptor 6048 6049描述音频设备。 6050 6051| 名称 | 类型 | 可读 | 可写 | 说明 | 6052| ----------------------------- | -------------------------- | ---- | ---- | ---------- | 6053| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6054| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6055| id<sup>9+</sup> | number | 是 | 否 | 唯一的设备id。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6056| name<sup>9+</sup> | string | 是 | 否 | 设备名称。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6057| address<sup>9+</sup> | string | 是 | 否 | 设备地址。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6058| sampleRates<sup>9+</sup> | Array<number> | 是 | 否 | 支持的采样率。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6059| channelCounts<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道数。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6060| channelMasks<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道掩码。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6061| displayName<sup>10+</sup> | string | 是 | 否 | 设备显示名。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Device <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6062| encodingTypes<sup>11+</sup> | Array<[AudioEncodingType](#audioencodingtype8)> | 是 | 否 | 支持的编码类型。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Core <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 6063| spatializationSupported<sup>18+</sup> | boolean | 是 | 否 | 设备是否支持空间音频。true表示支持空间音频,false表示不支持空间音频。 <br> **系统能力:** SystemCapability.Multimedia.Audio.Spatialization| 6064 6065**示例:** 6066 6067```ts 6068import { audio } from '@kit.AudioKit'; 6069 6070function displayDeviceProp(value: audio.AudioDeviceDescriptor) { 6071 deviceRoleValue = value.deviceRole; 6072 deviceTypeValue = value.deviceType; 6073} 6074 6075let deviceRoleValue: audio.DeviceRole | undefined = undefined; 6076let deviceTypeValue: audio.DeviceType | undefined = undefined; 6077audio.getAudioManager().getRoutingManager().getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((value: audio.AudioDeviceDescriptors) => { 6078 console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG'); 6079 value.forEach(displayDeviceProp); 6080 if (deviceTypeValue != undefined && deviceRoleValue != undefined){ 6081 console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : PASS'); 6082 } else { 6083 console.error('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : FAIL'); 6084 } 6085}); 6086``` 6087## AudioDataCallbackResult<sup>12+</sup> 6088 6089表示音频数据回调结果的枚举。 6090 6091**系统能力:** SystemCapability.Multimedia.Audio.Core 6092 6093| 名称 | 值 | 说明 | 6094| ---------------------| --------| ----------------- | 6095| INVALID | -1 | 表示该回调数据无效。 | 6096| VALID | 0 | 表示该回调数据有效。 | 6097 6098## AudioRendererWriteDataCallback<sup>12+</sup> 6099 6100type AudioRendererWriteDataCallback = (data: ArrayBuffer) => AudioDataCallbackResult | void 6101 6102回调函数类型,用于音频渲染器的数据写入,回调函数结束后,音频服务会把data指向的数据放入队列里等待播放,因此请勿在回调外再次更改data指向的数据, 且务必保证往data填满待播放数据, 否则会导致音频服务播放杂音。 6103 6104**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6105 6106**参数:** 6107 6108| 参数名 | 类型 |必填 | 说明 | 6109| :--------------| :--------| :----- | :------------ | 6110| data | ArrayBuffer | 是 | 待写入缓冲区的数据。 | 6111 6112**返回值:** 6113 6114| 类型 | 说明 | 6115|--------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------| 6116| [AudioDataCallbackResult](#audiodatacallbackresult12) \| void | 如果返回 void 或 AudioDataCallbackResult.VALID:表示数据有效,将播放音频数据;如果返回 AudioDataCallbackResult.INVALID:表示数据无效,且音频数据不播放。 | 6117 6118## AudioTimestampInfo<sup>19+</sup> 6119 6120音频流时间戳和当前数据帧位置信息。 6121 6122**系统能力:** SystemCapability.Multimedia.Audio.Core 6123 6124| 名称 | 类型 | 只读 | 可选 | 说明 | 6125| ---------| ------ | ---- | ---- |-----------------------------------| 6126| framePos | number | 是 | 否 | 当前播放或者录制的数据帧位置。 | 6127| timestamp | number | 是 | 否 | 播放或者录制到当前数据帧位置时对应的时间戳。 | 6128 6129## AudioRenderer<sup>8+</sup> 6130 6131提供音频渲染的相关接口。 6132 6133在使用AudioRenderer的接口之前,需先通过[createAudioRenderer](#audiocreateaudiorenderer8)获取AudioRenderer实例。 6134 6135### 属性 6136 6137**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6138 6139| 名称 | 类型 | 可读 | 可写 | 说明 | 6140| ----- | -------------------------- | ---- | ---- | ------------------ | 6141| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频渲染器的状态。 | 6142 6143**示例:** 6144 6145```ts 6146import { audio } from '@kit.AudioKit'; 6147 6148let state: audio.AudioState = audioRenderer.state; 6149``` 6150 6151### getRendererInfo<sup>8+</sup> 6152 6153getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void 6154 6155获取当前创建的音频渲染器信息。使用callback异步回调。 6156 6157**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6158 6159**参数:** 6160 6161| 参数名 | 类型 | 必填 | 说明 | 6162| :------- | :------------------------------------------------------- | :--- | :--------------------- | 6163| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是 | 回调函数。当获取音频渲染器的信息成功,err为undefined,data为获取到的音频渲染器的信息;否则为错误对象。 | 6164 6165**示例:** 6166 6167```ts 6168import { BusinessError } from '@kit.BasicServicesKit'; 6169 6170audioRenderer.getRendererInfo((err: BusinessError, rendererInfo: audio.AudioRendererInfo) => { 6171 console.info('Renderer GetRendererInfo:'); 6172 console.info(`Renderer content: ${rendererInfo.content}`); 6173 console.info(`Renderer usage: ${rendererInfo.usage}`); 6174 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`); 6175}); 6176``` 6177 6178### getRendererInfo<sup>8+</sup> 6179 6180getRendererInfo(): Promise<AudioRendererInfo\> 6181 6182获取当前创建的音频渲染器信息。使用Promise异步回调。 6183 6184**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6185 6186**返回值:** 6187 6188| 类型 | 说明 | 6189| -------------------------------------------------- | ------------------------------- | 6190| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise对象,返回音频渲染器信息。 | 6191 6192**示例:** 6193 6194```ts 6195import { BusinessError } from '@kit.BasicServicesKit'; 6196 6197audioRenderer.getRendererInfo().then((rendererInfo: audio.AudioRendererInfo) => { 6198 console.info('Renderer GetRendererInfo:'); 6199 console.info(`Renderer content: ${rendererInfo.content}`); 6200 console.info(`Renderer usage: ${rendererInfo.usage}`); 6201 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 6202}).catch((err: BusinessError) => { 6203 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${err}`); 6204}); 6205``` 6206 6207### getRendererInfoSync<sup>10+</sup> 6208 6209getRendererInfoSync(): AudioRendererInfo 6210 6211获取当前创建的音频渲染器信息。同步返回结果。 6212 6213**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6214 6215**返回值:** 6216 6217| 类型 | 说明 | 6218| -------------------------------------------------- | ------------------------------- | 6219| [AudioRendererInfo](#audiorendererinfo8) | 返回音频渲染器信息。 | 6220 6221**示例:** 6222 6223```ts 6224import { BusinessError } from '@kit.BasicServicesKit'; 6225 6226try { 6227 let rendererInfo: audio.AudioRendererInfo = audioRenderer.getRendererInfoSync(); 6228 console.info(`Renderer content: ${rendererInfo.content}`); 6229 console.info(`Renderer usage: ${rendererInfo.usage}`); 6230 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 6231} catch (err) { 6232 let error = err as BusinessError; 6233 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${error}`); 6234} 6235``` 6236 6237### getStreamInfo<sup>8+</sup> 6238 6239getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 6240 6241获取音频流信息。使用callback异步回调。 6242 6243**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6244 6245**参数:** 6246 6247| 参数名 | 类型 | 必填 | 说明 | 6248| :------- | :--------------------------------------------------- | :--- | :------------------- | 6249| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取音频流信息成功,err为undefined,data为获取到的音频流信息;否则为错误对象。 | 6250 6251**示例:** 6252 6253```ts 6254import { BusinessError } from '@kit.BasicServicesKit'; 6255 6256audioRenderer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 6257 console.info('Renderer GetStreamInfo:'); 6258 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 6259 console.info(`Renderer channel: ${streamInfo.channels}`); 6260 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 6261 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 6262}); 6263``` 6264 6265### getStreamInfo<sup>8+</sup> 6266 6267getStreamInfo(): Promise<AudioStreamInfo\> 6268 6269获取音频流信息。使用Promise异步回调。 6270 6271**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6272 6273**返回值:** 6274 6275| 类型 | 说明 | 6276| :--------------------------------------------- | :--------------------- | 6277| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回音频流信息。 | 6278 6279**示例:** 6280 6281```ts 6282import { BusinessError } from '@kit.BasicServicesKit'; 6283 6284audioRenderer.getStreamInfo().then((streamInfo: audio.AudioStreamInfo) => { 6285 console.info('Renderer GetStreamInfo:'); 6286 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 6287 console.info(`Renderer channel: ${streamInfo.channels}`); 6288 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 6289 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 6290}).catch((err: BusinessError) => { 6291 console.error(`ERROR: ${err}`); 6292}); 6293``` 6294 6295### getStreamInfoSync<sup>10+</sup> 6296 6297getStreamInfoSync(): AudioStreamInfo 6298 6299获取音频流信息。同步返回结果。 6300 6301**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6302 6303**返回值:** 6304 6305| 类型 | 说明 | 6306| :--------------------------------------------- | :--------------------- | 6307| [AudioStreamInfo](#audiostreaminfo8) | 返回音频流信息。 | 6308 6309**示例:** 6310 6311```ts 6312import { BusinessError } from '@kit.BasicServicesKit'; 6313 6314try { 6315 let streamInfo: audio.AudioStreamInfo = audioRenderer.getStreamInfoSync(); 6316 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 6317 console.info(`Renderer channel: ${streamInfo.channels}`); 6318 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 6319 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 6320} catch (err) { 6321 let error = err as BusinessError; 6322 console.error(`ERROR: ${error}`); 6323} 6324``` 6325 6326### getAudioStreamId<sup>9+</sup> 6327 6328getAudioStreamId(callback: AsyncCallback<number\>): void 6329 6330获取音频流id。使用callback异步回调。 6331 6332**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6333 6334**参数:** 6335 6336| 参数名 | 类型 | 必填 | 说明 | 6337| :------- | :--------------------------------------------------- | :--- | :------------------- | 6338| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 6339 6340**示例:** 6341 6342```ts 6343import { BusinessError } from '@kit.BasicServicesKit'; 6344 6345audioRenderer.getAudioStreamId((err: BusinessError, streamId: number) => { 6346 console.info(`Renderer GetStreamId: ${streamId}`); 6347}); 6348``` 6349 6350### getAudioStreamId<sup>9+</sup> 6351 6352getAudioStreamId(): Promise<number\> 6353 6354获取音频流id。使用Promise异步回调。 6355 6356**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6357 6358**返回值:** 6359 6360| 类型 | 说明 | 6361| :--------------------------------------------- | :--------------------- | 6362| Promise<number\> | Promise对象,返回音频流id。 | 6363 6364**示例:** 6365 6366```ts 6367import { BusinessError } from '@kit.BasicServicesKit'; 6368 6369audioRenderer.getAudioStreamId().then((streamId: number) => { 6370 console.info(`Renderer getAudioStreamId: ${streamId}`); 6371}).catch((err: BusinessError) => { 6372 console.error(`ERROR: ${err}`); 6373}); 6374``` 6375 6376### getAudioStreamIdSync<sup>10+</sup> 6377 6378getAudioStreamIdSync(): number 6379 6380获取音频流id。同步返回结果。 6381 6382**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6383 6384**返回值:** 6385 6386| 类型 | 说明 | 6387| :--------------------------------------------- | :--------------------- | 6388| number | 返回音频流id。 | 6389 6390**示例:** 6391 6392```ts 6393import { BusinessError } from '@kit.BasicServicesKit'; 6394 6395try { 6396 let streamId: number = audioRenderer.getAudioStreamIdSync(); 6397 console.info(`Renderer getAudioStreamIdSync: ${streamId}`); 6398} catch (err) { 6399 let error = err as BusinessError; 6400 console.error(`ERROR: ${error}`); 6401} 6402``` 6403 6404### setAudioEffectMode<sup>10+</sup> 6405 6406setAudioEffectMode(mode: AudioEffectMode, callback: AsyncCallback\<void>): void 6407 6408设置当前音效模式。使用callback异步回调。 6409 6410**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6411 6412**参数:** 6413 6414| 参数名 | 类型 | 必填 | 说明 | 6415| -------- | ---------------------------------------- | ---- | ------------------------ | 6416| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 6417| callback | AsyncCallback\<void> | 是 | 回调函数。当设置当前音效模式成功,err为undefined,否则为错误对象。 | 6418 6419**错误码:** 6420 6421以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 6422 6423| 错误码ID | 错误信息 | 6424| ------- | ----------------------------------------------| 6425| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6426| 6800101 | Parameter verification failed. Return by callback. | 6427 6428**示例:** 6429 6430```ts 6431import { BusinessError } from '@kit.BasicServicesKit'; 6432 6433audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => { 6434 if (err) { 6435 console.error('Failed to set params'); 6436 } else { 6437 console.info('Callback invoked to indicate a successful audio effect mode setting.'); 6438 } 6439}); 6440``` 6441 6442### setAudioEffectMode<sup>10+</sup> 6443 6444setAudioEffectMode(mode: AudioEffectMode): Promise\<void> 6445 6446设置当前音效模式。使用Promise异步回调。 6447 6448**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6449 6450**参数:** 6451 6452| 参数名 | 类型 | 必填 | 说明 | 6453| ------ | ---------------------------------------- | ---- | ------------ | 6454| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 6455 6456**返回值:** 6457 6458| 类型 | 说明 | 6459| -------------- | ------------------------- | 6460| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6461 6462**错误码:** 6463 6464以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 6465 6466| 错误码ID | 错误信息 | 6467| ------- | ---------------------------------------------| 6468| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6469| 6800101 | Parameter verification failed. Return by promise. | 6470 6471**示例:** 6472 6473```ts 6474import { BusinessError } from '@kit.BasicServicesKit'; 6475 6476audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT).then(() => { 6477 console.info('setAudioEffectMode SUCCESS'); 6478}).catch((err: BusinessError) => { 6479 console.error(`ERROR: ${err}`); 6480}); 6481``` 6482 6483### getAudioEffectMode<sup>10+</sup> 6484 6485getAudioEffectMode(callback: AsyncCallback\<AudioEffectMode>): void 6486 6487获取当前音效模式。使用callback异步回调。 6488 6489**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6490 6491**参数:** 6492 6493| 参数名 | 类型 | 必填 | 说明 | 6494| -------- | ------------------------------------------------------- | ---- | ------------------ | 6495| callback | AsyncCallback<[AudioEffectMode](#audioeffectmode10)> | 是 | 回调函数。当获取当前音效模式成功,err为undefined,data为获取到的当前音效模式;否则为错误对象。 | 6496 6497**示例:** 6498 6499```ts 6500import { BusinessError } from '@kit.BasicServicesKit'; 6501 6502audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => { 6503 if (err) { 6504 console.error('Failed to get params'); 6505 } else { 6506 console.info(`getAudioEffectMode: ${effectMode}`); 6507 } 6508}); 6509``` 6510 6511### getAudioEffectMode<sup>10+</sup> 6512 6513getAudioEffectMode(): Promise\<AudioEffectMode> 6514 6515获取当前音效模式。使用Promise异步回调。 6516 6517**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6518 6519**返回值:** 6520 6521| 类型 | 说明 | 6522| ------------------------------------------------- | ------------------------- | 6523| Promise<[AudioEffectMode](#audioeffectmode10)> | Promise对象,返回当前音效模式。 | 6524 6525**示例:** 6526 6527```ts 6528import { BusinessError } from '@kit.BasicServicesKit'; 6529 6530audioRenderer.getAudioEffectMode().then((effectMode: audio.AudioEffectMode) => { 6531 console.info(`getAudioEffectMode: ${effectMode}`); 6532}).catch((err: BusinessError) => { 6533 console.error(`ERROR: ${err}`); 6534}); 6535``` 6536 6537### start<sup>8+</sup> 6538 6539start(callback: AsyncCallback<void\>): void 6540 6541启动音频渲染器。使用callback异步回调。 6542 6543**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6544 6545**参数:** 6546 6547| 参数名 | 类型 | 必填 | 说明 | 6548| -------- | -------------------- | ---- | ---------- | 6549| callback | AsyncCallback\<void> | 是 | 回调函数。当启动音频渲染器成功,err为undefined,否则为错误对象。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 6550 6551**示例:** 6552 6553```ts 6554import { BusinessError } from '@kit.BasicServicesKit'; 6555 6556audioRenderer.start((err: BusinessError) => { 6557 if (err) { 6558 console.error('Renderer start failed.'); 6559 } else { 6560 console.info('Renderer start success.'); 6561 } 6562}); 6563``` 6564 6565### start<sup>8+</sup> 6566 6567start(): Promise<void\> 6568 6569启动音频渲染器。使用Promise异步回调。 6570 6571**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6572 6573**返回值:** 6574 6575| 类型 | 说明 | 6576| -------------- | ------------------------- | 6577| Promise\<void> | Promise对象,成功表示启动音频渲染器成功。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 6578 6579**示例:** 6580 6581```ts 6582import { BusinessError } from '@kit.BasicServicesKit'; 6583 6584audioRenderer.start().then(() => { 6585 console.info('Renderer started'); 6586}).catch((err: BusinessError) => { 6587 console.error(`ERROR: ${err}`); 6588}); 6589``` 6590 6591### pause<sup>8+</sup> 6592 6593pause(callback: AsyncCallback\<void>): void 6594 6595暂停音频渲染。使用callback异步回调。 6596 6597**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6598 6599**参数:** 6600 6601| 参数名 | 类型 | 必填 | 说明 | 6602| -------- | -------------------- | ---- | ---------------- | 6603| callback | AsyncCallback\<void> | 是 | 回调函数。当暂停渲染成功,err为undefined,否则为错误对象。 | 6604 6605**示例:** 6606 6607```ts 6608import { BusinessError } from '@kit.BasicServicesKit'; 6609 6610audioRenderer.pause((err: BusinessError) => { 6611 if (err) { 6612 console.error('Renderer pause failed'); 6613 } else { 6614 console.info('Renderer paused.'); 6615 } 6616}); 6617``` 6618 6619### pause<sup>8+</sup> 6620 6621pause(): Promise\<void> 6622 6623暂停音频渲染。使用Promise异步回调。 6624 6625**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6626 6627**返回值:** 6628 6629| 类型 | 说明 | 6630| -------------- | ------------------------- | 6631| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6632 6633**示例:** 6634 6635```ts 6636import { BusinessError } from '@kit.BasicServicesKit'; 6637 6638audioRenderer.pause().then(() => { 6639 console.info('Renderer paused'); 6640}).catch((err: BusinessError) => { 6641 console.error(`ERROR: ${err}`); 6642}); 6643``` 6644 6645### drain<sup>8+</sup> 6646 6647drain(callback: AsyncCallback\<void>): void 6648 6649检查缓冲区是否已被耗尽。使用callback异步回调。 6650 6651**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6652 6653**参数:** 6654 6655| 参数名 | 类型 | 必填 | 说明 | 6656| -------- | -------------------- | ---- | ---------------- | 6657| callback | AsyncCallback\<void> | 是 | 回调函数。当检查缓冲区是否已被耗尽成功,err为undefined,否则为错误对象。 | 6658 6659**示例:** 6660 6661```ts 6662import { BusinessError } from '@kit.BasicServicesKit'; 6663 6664audioRenderer.drain((err: BusinessError) => { 6665 if (err) { 6666 console.error('Renderer drain failed'); 6667 } else { 6668 console.info('Renderer drained.'); 6669 } 6670}); 6671``` 6672 6673### drain<sup>8+</sup> 6674 6675drain(): Promise\<void> 6676 6677检查缓冲区是否已被耗尽。使用Promise异步回调。 6678 6679**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6680 6681**返回值:** 6682 6683| 类型 | 说明 | 6684| -------------- | ------------------------- | 6685| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6686 6687**示例:** 6688 6689```ts 6690import { BusinessError } from '@kit.BasicServicesKit'; 6691 6692audioRenderer.drain().then(() => { 6693 console.info('Renderer drained successfully'); 6694}).catch((err: BusinessError) => { 6695 console.error(`ERROR: ${err}`); 6696}); 6697``` 6698 6699### flush<sup>11+</sup> 6700 6701flush(): Promise\<void> 6702 6703清空缓冲区([AudioState](#audiostate8)为STATE_RUNNING、STATE_PAUSED、STATE_STOPPED状态下可用)。使用Promise异步回调。 6704 6705**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6706 6707**返回值:** 6708 6709| 类型 | 说明 | 6710| -------------- | ------------------------- | 6711| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6712 6713**错误码:** 6714 6715以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 6716 6717| 错误码ID | 错误信息 | 6718| ------- | --------------------------------------------| 6719| 6800103 | Operation not permit at current state. Return by promise. | 6720 6721**示例:** 6722 6723```ts 6724import { BusinessError } from '@kit.BasicServicesKit'; 6725 6726audioRenderer.flush().then(() => { 6727 console.info('Renderer flushed successfully'); 6728}).catch((err: BusinessError) => { 6729 console.error(`ERROR: ${err}`); 6730}); 6731``` 6732 6733### stop<sup>8+</sup> 6734 6735stop(callback: AsyncCallback\<void>): void 6736 6737停止音频渲染。使用callback异步回调。 6738 6739**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6740 6741**参数:** 6742 6743| 参数名 | 类型 | 必填 | 说明 | 6744| -------- | -------------------- | ---- | ---------------- | 6745| callback | AsyncCallback\<void> | 是 | 回调函数。当停止渲染成功,err为undefined,否则为错误对象。 | 6746 6747**示例:** 6748 6749```ts 6750import { BusinessError } from '@kit.BasicServicesKit'; 6751 6752audioRenderer.stop((err: BusinessError) => { 6753 if (err) { 6754 console.error('Renderer stop failed'); 6755 } else { 6756 console.info('Renderer stopped.'); 6757 } 6758}); 6759``` 6760 6761### stop<sup>8+</sup> 6762 6763stop(): Promise\<void> 6764 6765停止音频渲染。使用Promise异步回调。 6766 6767**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6768 6769**返回值:** 6770 6771| 类型 | 说明 | 6772| -------------- | ------------------------- | 6773| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6774 6775**示例:** 6776 6777```ts 6778import { BusinessError } from '@kit.BasicServicesKit'; 6779 6780audioRenderer.stop().then(() => { 6781 console.info('Renderer stopped successfully'); 6782}).catch((err: BusinessError) => { 6783 console.error(`ERROR: ${err}`); 6784}); 6785``` 6786 6787### release<sup>8+</sup> 6788 6789release(callback: AsyncCallback\<void>): void 6790 6791释放音频渲染器。使用callback异步回调。 6792 6793**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6794 6795**参数:** 6796 6797| 参数名 | 类型 | 必填 | 说明 | 6798| -------- | -------------------- | ---- | ---------------- | 6799| callback | AsyncCallback\<void> | 是 | 回调函数。当释放音频渲染器成功,err为undefined,否则为错误对象。 | 6800 6801**示例:** 6802 6803```ts 6804import { BusinessError } from '@kit.BasicServicesKit'; 6805 6806audioRenderer.release((err: BusinessError) => { 6807 if (err) { 6808 console.error('Renderer release failed'); 6809 } else { 6810 console.info('Renderer released.'); 6811 } 6812}); 6813``` 6814 6815### release<sup>8+</sup> 6816 6817release(): Promise\<void> 6818 6819释放音频渲染器。使用Promise异步回调。 6820 6821**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6822 6823**返回值:** 6824 6825| 类型 | 说明 | 6826| -------------- | ------------------------- | 6827| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 6828 6829**示例:** 6830 6831```ts 6832import { BusinessError } from '@kit.BasicServicesKit'; 6833 6834audioRenderer.release().then(() => { 6835 console.info('Renderer released successfully'); 6836}).catch((err: BusinessError) => { 6837 console.error(`ERROR: ${err}`); 6838}); 6839``` 6840 6841### write<sup>8+(deprecated)</sup> 6842 6843write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void 6844 6845写入缓冲区。使用callback异步回调。 6846 6847> **说明:** 6848> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('writeData')](#onwritedata11)替代。 6849 6850**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6851 6852**参数:** 6853 6854| 参数名 | 类型 | 必填 | 说明 | 6855| -------- | ---------------------- | ---- | --------------------------------------------------- | 6856| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6857| callback | AsyncCallback\<number> | 是 | 回调函数。当写入缓冲区成功,err为undefined,data为获取到的写入的字节数;否则为错误对象。 | 6858 6859**示例:** 6860 6861```ts 6862import { BusinessError } from '@kit.BasicServicesKit'; 6863import { fileIo as fs } from '@kit.CoreFileKit'; 6864import { common } from '@kit.AbilityKit'; 6865 6866let bufferSize: number; 6867class Options { 6868 offset?: number; 6869 length?: number; 6870} 6871audioRenderer.getBufferSize().then((data: number)=> { 6872 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6873 bufferSize = data; 6874 console.info(`Buffer size: ${bufferSize}`); 6875 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext。 6876 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 6877 let path = context.cacheDir; 6878 let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 6879 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6880 fs.stat(filePath).then(async (stat: fs.Stat) => { 6881 let buf = new ArrayBuffer(bufferSize); 6882 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6883 for (let i = 0;i < len; i++) { 6884 let options: Options = { 6885 offset: i * bufferSize, 6886 length: bufferSize 6887 }; 6888 let readSize: number = await fs.read(file.fd, buf, options); 6889 let writeSize: number = await new Promise((resolve,reject)=>{ 6890 audioRenderer.write(buf,(err: BusinessError, writeSize: number)=>{ 6891 if(err){ 6892 reject(err) 6893 }else{ 6894 resolve(writeSize) 6895 } 6896 }) 6897 }) 6898 } 6899 }); 6900 }).catch((err: BusinessError) => { 6901 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6902}); 6903``` 6904 6905### write<sup>8+(deprecated)</sup> 6906 6907write(buffer: ArrayBuffer): Promise\<number> 6908 6909写入缓冲区。使用Promise异步回调。 6910 6911> **说明:** 6912> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('writeData')](#onwritedata11)替代。 6913 6914**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6915 6916**参数:** 6917 6918| 参数名 | 类型 | 必填 | 说明 | 6919| -------- | ---------------------- | ---- | --------------------------------------------------- | 6920| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6921 6922**返回值:** 6923 6924| 类型 | 说明 | 6925| ---------------- | ------------------------------------------------------------ | 6926| Promise\<number> | Promise对象,返回写入的字节数。 | 6927 6928**示例:** 6929 6930```ts 6931import { BusinessError } from '@kit.BasicServicesKit'; 6932import { fileIo as fs } from '@kit.CoreFileKit'; 6933import { common } from '@kit.AbilityKit'; 6934 6935let bufferSize: number; 6936class Options { 6937 offset?: number; 6938 length?: number; 6939} 6940audioRenderer.getBufferSize().then((data: number) => { 6941 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6942 bufferSize = data; 6943 console.info(`BufferSize: ${bufferSize}`); 6944 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext。 6945 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 6946 let path = context.cacheDir; 6947 let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 6948 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6949 fs.stat(filePath).then(async (stat: fs.Stat) => { 6950 let buf = new ArrayBuffer(bufferSize); 6951 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6952 for (let i = 0;i < len; i++) { 6953 let options: Options = { 6954 offset: i * bufferSize, 6955 length: bufferSize 6956 }; 6957 let readSize: number = await fs.read(file.fd, buf, options); 6958 try{ 6959 let writeSize: number = await audioRenderer.write(buf); 6960 } catch(err) { 6961 let error = err as BusinessError; 6962 console.error(`audioRenderer.write err: ${error}`); 6963 } 6964 } 6965 }); 6966}).catch((err: BusinessError) => { 6967 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6968}); 6969``` 6970 6971### getAudioTime<sup>8+</sup> 6972 6973getAudioTime(callback: AsyncCallback\<number>): void 6974 6975获取当前播放位置的时间戳(从1970年1月1日开始),单位为纳秒。使用callback异步回调。 6976 6977**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6978 6979**参数:** 6980 6981| 参数名 | 类型 | 必填 | 说明 | 6982| -------- | ---------------------- | ---- | ---------------- | 6983| callback | AsyncCallback\<number> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 6984 6985**示例:** 6986 6987```ts 6988import { BusinessError } from '@kit.BasicServicesKit'; 6989 6990audioRenderer.getAudioTime((err: BusinessError, timestamp: number) => { 6991 console.info(`Current timestamp: ${timestamp}`); 6992}); 6993``` 6994 6995### getAudioTime<sup>8+</sup> 6996 6997getAudioTime(): Promise\<number> 6998 6999获取当前播放位置的时间戳(从1970年1月1日开始),单位为纳秒。使用Promise异步回调。 7000 7001**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7002 7003**返回值:** 7004 7005| 类型 | 描述 | 7006| ---------------- | ----------------------- | 7007| Promise\<number> | Promise对象,返回时间戳。 | 7008 7009**示例:** 7010 7011```ts 7012import { BusinessError } from '@kit.BasicServicesKit'; 7013 7014audioRenderer.getAudioTime().then((timestamp: number) => { 7015 console.info(`Current timestamp: ${timestamp}`); 7016}).catch((err: BusinessError) => { 7017 console.error(`ERROR: ${err}`); 7018}); 7019``` 7020 7021### getAudioTimeSync<sup>10+</sup> 7022 7023getAudioTimeSync(): number 7024 7025获取当前播放位置的时间戳(从1970年1月1日开始),单位为纳秒。同步返回结果。 7026 7027**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7028 7029**返回值:** 7030 7031| 类型 | 描述 | 7032| ---------------- | ----------------------- | 7033| number | 返回时间戳。 | 7034 7035**示例:** 7036 7037```ts 7038import { BusinessError } from '@kit.BasicServicesKit'; 7039 7040try { 7041 let timestamp: number = audioRenderer.getAudioTimeSync(); 7042 console.info(`Current timestamp: ${timestamp}`); 7043} catch (err) { 7044 let error = err as BusinessError; 7045 console.error(`ERROR: ${error}`); 7046} 7047``` 7048 7049### getAudioTimestampInfo<sup>19+</sup> 7050 7051getAudioTimestampInfo(): Promise\<AudioTimestampInfo> 7052 7053获取音频流时间戳和当前数据帧位置信息。使用Promise异步回调。 7054 7055**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7056 7057**返回值:** 7058 7059| 类型 | 描述 | 7060|-------------------------------------------------------| ----------------------- | 7061| Promise\<[AudioTimestampInfo](#audiotimestampinfo19)> | Promise对象,返回音频流时间戳和当前数据帧位置信息。 | 7062 7063**错误码:** 7064 7065以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7066 7067| 错误码ID | 错误信息 | 7068| ------- | --------------------------------------------| 7069| 6800103 | Operation not permit at current state. | 7070 7071**示例:** 7072 7073```ts 7074import { BusinessError } from '@kit.BasicServicesKit'; 7075 7076audioRenderer.getAudioTimestampInfo().then((audioTimestampInfo: audio.AudioTimestampInfo) => { 7077 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 7078}).catch((err: BusinessError) => { 7079 console.error(`ERROR: ${err}`); 7080}); 7081``` 7082 7083### getAudioTimestampInfoSync<sup>19+</sup> 7084 7085getAudioTimestampInfoSync(): AudioTimestampInfo 7086 7087获取音频流时间戳和当前数据帧位置信息。同步返回结果。 7088 7089**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7090 7091**返回值:** 7092 7093| 类型 | 描述 | 7094| ---------------- | ----------------------- | 7095| [AudioTimestampInfo](#audiotimestampinfo19) | 返回音频流时间戳和当前数据帧位置信息。 | 7096 7097**错误码:** 7098 7099以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 7100 7101| 错误码ID | 错误信息 | 7102| ------- | --------------------------------------------| 7103| 6800103 | Operation not permit at current state. | 7104 7105**示例:** 7106 7107```ts 7108import { BusinessError } from '@kit.BasicServicesKit'; 7109 7110try { 7111 let audioTimestampInfo: audio.AudioTimestampInfo = audioRenderer.getAudioTimestampInfoSync(); 7112 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 7113} catch (err) { 7114 let error = err as BusinessError; 7115 console.error(`ERROR: ${error}`); 7116} 7117``` 7118 7119### getBufferSize<sup>8+</sup> 7120 7121getBufferSize(callback: AsyncCallback\<number>): void 7122 7123获取音频渲染器的最小缓冲区大小。使用callback异步回调。 7124 7125**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7126 7127**参数:** 7128 7129| 参数名 | 类型 | 必填 | 说明 | 7130| -------- | ---------------------- | ---- | -------------------- | 7131| callback | AsyncCallback\<number> | 是 | 回调函数。当获取音频渲染器的最小缓冲区大小成功,err为undefined,data为获取到的最小缓冲区大小;否则为错误对象。 | 7132 7133**示例:** 7134 7135```ts 7136import { BusinessError } from '@kit.BasicServicesKit'; 7137 7138let bufferSize: number; 7139 7140audioRenderer.getBufferSize((err: BusinessError, data: number) => { 7141 if (err) { 7142 console.error('getBufferSize error'); 7143 } else { 7144 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 7145 bufferSize = data; 7146 } 7147}); 7148``` 7149 7150### getBufferSize<sup>8+</sup> 7151 7152getBufferSize(): Promise\<number> 7153 7154获取音频渲染器的最小缓冲区大小。使用Promise异步回调。 7155 7156**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7157 7158**返回值:** 7159 7160| 类型 | 说明 | 7161| ---------------- | --------------------------- | 7162| Promise\<number> | Promise对象,返回缓冲区大小。 | 7163 7164**示例:** 7165 7166```ts 7167import { BusinessError } from '@kit.BasicServicesKit'; 7168 7169let bufferSize: number; 7170 7171audioRenderer.getBufferSize().then((data: number) => { 7172 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 7173 bufferSize = data; 7174}).catch((err: BusinessError) => { 7175 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 7176}); 7177``` 7178 7179### getBufferSizeSync<sup>10+</sup> 7180 7181getBufferSizeSync(): number 7182 7183获取音频渲染器的最小缓冲区大小。同步返回结果。 7184 7185**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7186 7187**返回值:** 7188 7189| 类型 | 说明 | 7190| ---------------- | --------------------------- | 7191| number | 返回缓冲区大小。 | 7192 7193**示例:** 7194 7195```ts 7196import { BusinessError } from '@kit.BasicServicesKit'; 7197 7198let bufferSize: number = 0; 7199 7200try { 7201 bufferSize = audioRenderer.getBufferSizeSync(); 7202 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${bufferSize}`); 7203} catch (err) { 7204 let error = err as BusinessError; 7205 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${error}`); 7206} 7207``` 7208 7209### setRenderRate<sup>8+(deprecated)</sup> 7210 7211setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void 7212 7213设置音频渲染速率。使用callback异步回调。 7214 7215> **说明:** 7216> 从API version 8开始支持,从API version 11开始废弃,建议使用[setSpeed](#setspeed11)替代。 7217 7218**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7219 7220**参数:** 7221 7222| 参数名 | 类型 | 必填 | 说明 | 7223| -------- | ---------------------------------------- | ---- | ------------------------ | 7224| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 7225| callback | AsyncCallback\<void> | 是 | 回调函数。当设置音频渲染速率成功,err为undefined,否则为错误对象。 | 7226 7227**示例:** 7228 7229```ts 7230import { BusinessError } from '@kit.BasicServicesKit'; 7231 7232audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err: BusinessError) => { 7233 if (err) { 7234 console.error('Failed to set params'); 7235 } else { 7236 console.info('Callback invoked to indicate a successful render rate setting.'); 7237 } 7238}); 7239``` 7240 7241### setRenderRate<sup>8+(deprecated)</sup> 7242 7243setRenderRate(rate: AudioRendererRate): Promise\<void> 7244 7245设置音频渲染速率。使用Promise异步回调。 7246 7247> **说明:** 7248> 从API version 8开始支持,从API version 11开始废弃,建议使用[setSpeed](#setspeed11)替代。 7249 7250**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7251 7252**参数:** 7253 7254| 参数名 | 类型 | 必填 | 说明 | 7255| ------ | ---------------------------------------- | ---- | ------------ | 7256| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 7257 7258**返回值:** 7259 7260| 类型 | 说明 | 7261| -------------- | ------------------------- | 7262| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 7263 7264**示例:** 7265 7266```ts 7267import { BusinessError } from '@kit.BasicServicesKit'; 7268 7269audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => { 7270 console.info('setRenderRate SUCCESS'); 7271}).catch((err: BusinessError) => { 7272 console.error(`ERROR: ${err}`); 7273}); 7274``` 7275 7276### setSpeed<sup>11+</sup> 7277 7278setSpeed(speed: number): void 7279 7280设置播放倍速。 7281 7282**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7283 7284**参数:** 7285 7286| 参数名 | 类型 | 必填 | 说明 | 7287| ------ | ---------------------------------------- | ---- |----------------------| 7288| speed | number | 是 | 设置播放的倍速值(倍速范围:0.125-4.0)。 | 7289 7290**错误码:** 7291 7292以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 7293 7294| 错误码ID | 错误信息 | 7295| ------- | --------------------------------------------| 7296| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7297| 6800101 | Parameter verification failed. | 7298 7299**示例:** 7300 7301```ts 7302audioRenderer.setSpeed(1.5); 7303``` 7304 7305### getRenderRate<sup>8+(deprecated)</sup> 7306 7307getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void 7308 7309获取音频渲染速率。使用callback异步回调。 7310 7311> **说明:** 7312> 从API version 8开始支持,从API version 11开始废弃,建议使用[getSpeed](#getspeed11)替代。 7313 7314**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7315 7316**参数:** 7317 7318| 参数名 | 类型 | 必填 | 说明 | 7319| -------- | ------------------------------------------------------- | ---- | ------------------ | 7320| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是 | 回调函数。当获取当前渲染速率成功,err为undefined,data为获取到的当前渲染速率;否则为错误对象。 | 7321 7322**示例:** 7323 7324```ts 7325import { BusinessError } from '@kit.BasicServicesKit'; 7326 7327audioRenderer.getRenderRate((err: BusinessError, renderRate: audio.AudioRendererRate) => { 7328 console.info(`getRenderRate: ${renderRate}`); 7329}); 7330``` 7331 7332### getRenderRate<sup>8+(deprecated)</sup> 7333 7334getRenderRate(): Promise\<AudioRendererRate> 7335 7336获取音频渲染速率。使用Promise异步回调。 7337 7338> **说明:** 7339> 从API version 8开始支持,从API version 11开始废弃,建议使用[getSpeed](#getspeed11)替代。 7340 7341**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7342 7343**返回值:** 7344 7345| 类型 | 说明 | 7346| ------------------------------------------------- | ------------------------- | 7347| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise对象,返回渲染速率。 | 7348 7349**示例:** 7350 7351```ts 7352import { BusinessError } from '@kit.BasicServicesKit'; 7353 7354audioRenderer.getRenderRate().then((renderRate: audio.AudioRendererRate) => { 7355 console.info(`getRenderRate: ${renderRate}`); 7356}).catch((err: BusinessError) => { 7357 console.error(`ERROR: ${err}`); 7358}); 7359``` 7360 7361### getRenderRateSync<sup>10+(deprecated)</sup> 7362 7363getRenderRateSync(): AudioRendererRate 7364 7365获取音频渲染速率。同步返回结果。 7366 7367> **说明:** 7368> 从API version 10开始支持,从API version 11开始废弃,建议使用[getSpeed](#getspeed11)替代。 7369 7370**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7371 7372**返回值:** 7373 7374| 类型 | 说明 | 7375| ------------------------------------------------- | ------------------------- | 7376| [AudioRendererRate](#audiorendererrate8) | 返回渲染速率。 | 7377 7378**示例:** 7379 7380```ts 7381import { BusinessError } from '@kit.BasicServicesKit'; 7382 7383try { 7384 let renderRate: audio.AudioRendererRate = audioRenderer.getRenderRateSync(); 7385 console.info(`getRenderRate: ${renderRate}`); 7386} catch (err) { 7387 let error = err as BusinessError; 7388 console.error(`ERROR: ${error}`); 7389} 7390``` 7391 7392### getSpeed<sup>11+</sup> 7393 7394getSpeed(): number 7395 7396获取播放倍速。 7397 7398**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7399 7400**返回值:** 7401 7402| 类型 | 说明 | 7403| ------------------------------------------------- |-----------| 7404| number | 返回播放的倍速值。 | 7405 7406**示例:** 7407 7408```ts 7409let speed = audioRenderer.getSpeed(); 7410``` 7411 7412### setInterruptMode<sup>9+</sup> 7413 7414setInterruptMode(mode: InterruptMode): Promise<void> 7415 7416设置应用的焦点模型。使用Promise异步回调。 7417 7418**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 7419 7420**参数:** 7421 7422| 参数名 | 类型 | 必填 | 说明 | 7423| ---------- | ---------------------------------- | ------ | ---------- | 7424| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 7425 7426**返回值:** 7427 7428| 类型 | 说明 | 7429| ------------------- | ----------------------------- | 7430| Promise<void> | Promise对象。无返回结果的Promise对象。 | 7431 7432**示例:** 7433 7434```ts 7435import { BusinessError } from '@kit.BasicServicesKit'; 7436 7437let mode = 0; 7438 7439audioRenderer.setInterruptMode(mode).then(() => { 7440 console.info('setInterruptMode Success!'); 7441}).catch((err: BusinessError) => { 7442 console.error(`setInterruptMode Fail: ${err}`); 7443}); 7444``` 7445### setInterruptMode<sup>9+</sup> 7446 7447setInterruptMode(mode: InterruptMode, callback: AsyncCallback\<void>): void 7448 7449设置应用的焦点模型。使用callback异步回调。 7450 7451**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 7452 7453**参数:** 7454 7455| 参数名 | 类型 | 必填 | 说明 | 7456| ------- | ----------------------------------- | ------ | -------------- | 7457|mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。| 7458|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的焦点模型成功,err为undefined,否则为错误对象。| 7459 7460**示例:** 7461 7462```ts 7463import { BusinessError } from '@kit.BasicServicesKit'; 7464 7465let mode = 1; 7466 7467audioRenderer.setInterruptMode(mode, (err: BusinessError) => { 7468 if(err){ 7469 console.error(`setInterruptMode Fail: ${err}`); 7470 } 7471 console.info('setInterruptMode Success!'); 7472}); 7473``` 7474 7475### setInterruptModeSync<sup>10+</sup> 7476 7477setInterruptModeSync(mode: InterruptMode): void 7478 7479设置应用的焦点模型。同步设置。 7480 7481**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 7482 7483**参数:** 7484 7485| 参数名 | 类型 | 必填 | 说明 | 7486| ---------- | ---------------------------------- | ------ | ---------- | 7487| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 7488 7489**错误码:** 7490 7491以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 7492 7493| 错误码ID | 错误信息 | 7494| ------- | --------------------------------------------| 7495| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7496| 6800101 | Parameter verification failed. | 7497 7498**示例:** 7499 7500```ts 7501import { BusinessError } from '@kit.BasicServicesKit'; 7502 7503try { 7504 audioRenderer.setInterruptModeSync(0); 7505 console.info('setInterruptMode Success!'); 7506} catch (err) { 7507 let error = err as BusinessError; 7508 console.error(`setInterruptMode Fail: ${error}`); 7509} 7510``` 7511 7512### setVolume<sup>9+</sup> 7513 7514setVolume(volume: number): Promise<void> 7515 7516设置音频流的音量。使用Promise异步回调。 7517 7518**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7519 7520**参数:** 7521 7522| 参数名 | 类型 | 必填 | 说明 | 7523| ---------- | ------- | ------ | ------------------- | 7524| volume | number | 是 | 音量值范围为[0.0, 1.0]。 | 7525 7526**返回值:** 7527 7528| 类型 | 说明 | 7529| ------------------- | ----------------------------- | 7530| Promise<void> | Promise对象。无返回结果的Promise对象。 | 7531 7532**示例:** 7533 7534```ts 7535import { BusinessError } from '@kit.BasicServicesKit'; 7536 7537audioRenderer.setVolume(0.5).then(() => { 7538 console.info('setVolume Success!'); 7539}).catch((err: BusinessError) => { 7540 console.error(`setVolume Fail: ${err}`); 7541}); 7542``` 7543### setVolume<sup>9+</sup> 7544 7545setVolume(volume: number, callback: AsyncCallback\<void>): void 7546 7547设置音频流的音量。使用callback异步回调。 7548 7549**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7550 7551**参数:** 7552 7553| 参数名 | 类型 | 必填 | 说明 | 7554| ------- | -----------| ------ | ------------------- | 7555|volume | number | 是 | 音量值范围为[0.0, 1.0]。 | 7556|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的音量成功,err为undefined,否则为错误对象。| 7557 7558**示例:** 7559 7560```ts 7561import { BusinessError } from '@kit.BasicServicesKit'; 7562 7563audioRenderer.setVolume(0.5, (err: BusinessError) => { 7564 if(err){ 7565 console.error(`setVolume Fail: ${err}`); 7566 return; 7567 } 7568 console.info('setVolume Success!'); 7569}); 7570``` 7571### getVolume<sup>12+</sup> 7572 7573getVolume(): number 7574 7575获取音频流的音量。同步返回结果。 7576 7577**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7578 7579**返回值:** 7580 7581| 类型 | 说明 | 7582| ---------------- | --------------------------- | 7583| number | 返回音量大小,音量值范围为[0.0, 1.0]。 | 7584 7585**示例:** 7586 7587```ts 7588import { BusinessError } from '@kit.BasicServicesKit'; 7589 7590try { 7591 let value: number = audioRenderer.getVolume(); 7592 console.info(`Indicate that the volume is obtained ${value}.`); 7593} catch (err) { 7594 let error = err as BusinessError; 7595 console.error(`Failed to obtain the volume, error ${error}.`); 7596} 7597``` 7598 7599### getMinStreamVolume<sup>10+</sup> 7600 7601getMinStreamVolume(callback: AsyncCallback<number>): void 7602 7603获取音频流的最小音量。使用callback异步回调。 7604 7605**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7606 7607**参数:** 7608 7609| 参数名 | 类型 | 必填 | 说明 | 7610| ------- | -----------| ------ | ------------------- | 7611|callback |AsyncCallback<number> | 是 |回调函数。当获取音频流的最小音量成功,err为undefined,data为获取到的应用基于音频流的最小音量(音量范围[0, 1]);否则为错误对象。| 7612 7613**示例:** 7614 7615```ts 7616import { BusinessError } from '@kit.BasicServicesKit'; 7617 7618audioRenderer.getMinStreamVolume((err: BusinessError, minVolume: number) => { 7619 if (err) { 7620 console.error(`getMinStreamVolume error: ${err}`); 7621 } else { 7622 console.info(`getMinStreamVolume Success! ${minVolume}`); 7623 } 7624}); 7625``` 7626### getMinStreamVolume<sup>10+</sup> 7627 7628getMinStreamVolume(): Promise<number> 7629 7630获取音频流的最小音量。使用Promise异步回调。 7631 7632**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7633 7634**返回值:** 7635 7636| 类型 | 说明 | 7637| ------------------- | ----------------------------- | 7638| Promise<number>| Promise对象,返回音频流最小音量(音量范围[0, 1])。| 7639 7640**示例:** 7641 7642```ts 7643import { BusinessError } from '@kit.BasicServicesKit'; 7644 7645audioRenderer.getMinStreamVolume().then((value: number) => { 7646 console.info(`Get min stream volume Success! ${value}`); 7647}).catch((err: BusinessError) => { 7648 console.error(`Get min stream volume Fail: ${err}`); 7649}); 7650``` 7651 7652### getMinStreamVolumeSync<sup>10+</sup> 7653 7654getMinStreamVolumeSync(): number 7655 7656获取音频流的最小音量。同步返回结果。 7657 7658**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7659 7660**返回值:** 7661 7662| 类型 | 说明 | 7663| ------------------- | ----------------------------- | 7664| number| 返回音频流最小音量(音量范围[0, 1])。| 7665 7666**示例:** 7667 7668```ts 7669import { BusinessError } from '@kit.BasicServicesKit'; 7670 7671try { 7672 let value: number = audioRenderer.getMinStreamVolumeSync(); 7673 console.info(`Get min stream volume Success! ${value}`); 7674} catch (err) { 7675 let error = err as BusinessError; 7676 console.error(`Get min stream volume Fail: ${error}`); 7677} 7678``` 7679 7680### getMaxStreamVolume<sup>10+</sup> 7681 7682getMaxStreamVolume(callback: AsyncCallback<number>): void 7683 7684获取音频流的最大音量。使用callback异步回调。 7685 7686**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7687 7688**参数:** 7689 7690| 参数名 | 类型 | 必填 | 说明 | 7691| ------- | -----------| ------ | ------------------- | 7692|callback | AsyncCallback<number> | 是 |回调函数。当获取音频流的最大音量成功,err为undefined,data为获取到的应用基于音频流的最大音量(音量范围[0, 1]);否则为错误对象。| 7693 7694**示例:** 7695 7696```ts 7697import { BusinessError } from '@kit.BasicServicesKit'; 7698 7699audioRenderer.getMaxStreamVolume((err: BusinessError, maxVolume: number) => { 7700 if (err) { 7701 console.error(`getMaxStreamVolume Fail: ${err}`); 7702 } else { 7703 console.info(`getMaxStreamVolume Success! ${maxVolume}`); 7704 } 7705}); 7706``` 7707### getMaxStreamVolume<sup>10+</sup> 7708 7709getMaxStreamVolume(): Promise<number> 7710 7711获取音频流的最大音量。使用Promise异步回调。 7712 7713**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7714 7715**返回值:** 7716 7717| 类型 | 说明 | 7718| ------------------- | ----------------------------- | 7719| Promise<number>| Promise对象,返回音频流最大音量(音量范围[0, 1])。| 7720 7721**示例:** 7722 7723```ts 7724import { BusinessError } from '@kit.BasicServicesKit'; 7725 7726audioRenderer.getMaxStreamVolume().then((value: number) => { 7727 console.info(`Get max stream volume Success! ${value}`); 7728}).catch((err: BusinessError) => { 7729 console.error(`Get max stream volume Fail: ${err}`); 7730}); 7731``` 7732 7733### getMaxStreamVolumeSync<sup>10+</sup> 7734 7735getMaxStreamVolumeSync(): number 7736 7737获取音频流的最大音量。同步返回结果。 7738 7739**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7740 7741**返回值:** 7742 7743| 类型 | 说明 | 7744| ------------------- | ----------------------------- | 7745| number| 返回音频流最大音量(音量范围[0, 1])。| 7746 7747**示例:** 7748 7749```ts 7750import { BusinessError } from '@kit.BasicServicesKit'; 7751 7752try { 7753 let value: number = audioRenderer.getMaxStreamVolumeSync(); 7754 console.info(`Get max stream volume Success! ${value}`); 7755} catch (err) { 7756 let error = err as BusinessError; 7757 console.error(`Get max stream volume Fail: ${error}`); 7758} 7759``` 7760 7761### getUnderflowCount<sup>10+</sup> 7762 7763getUnderflowCount(callback: AsyncCallback<number>): void 7764 7765获取当前播放音频流的欠载音频帧数量。使用callback异步回调。 7766 7767**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7768 7769**参数:** 7770 7771| 参数名 | 类型 | 必填 | 说明 | 7772| ------- | -----------| ------ | ------------------- | 7773|callback | AsyncCallback<number> | 是 |回调函数。当获取当前播放音频流的欠载音频帧数量成功,err为undefined,data为获取到的当前播放音频流的欠载音频帧数量;否则为错误对象。| 7774 7775**示例:** 7776 7777```ts 7778import { BusinessError } from '@kit.BasicServicesKit'; 7779 7780audioRenderer.getUnderflowCount((err: BusinessError, underflowCount: number) => { 7781 if (err) { 7782 console.error(`getUnderflowCount Fail: ${err}`); 7783 } else { 7784 console.info(`getUnderflowCount Success! ${underflowCount}`); 7785 } 7786}); 7787``` 7788### getUnderflowCount<sup>10+</sup> 7789 7790getUnderflowCount(): Promise<number> 7791 7792获取当前播放音频流的欠载音频帧数量。使用Promise异步回调。 7793 7794**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7795 7796**返回值:** 7797 7798| 类型 | 说明 | 7799| ------------------- | ----------------------------- | 7800| Promise<number>| Promise对象,返回音频流的欠载音频帧数量。| 7801 7802**示例:** 7803 7804```ts 7805import { BusinessError } from '@kit.BasicServicesKit'; 7806 7807audioRenderer.getUnderflowCount().then((value: number) => { 7808 console.info(`Get underflow count Success! ${value}`); 7809}).catch((err: BusinessError) => { 7810 console.error(`Get underflow count Fail: ${err}`); 7811}); 7812``` 7813 7814### getUnderflowCountSync<sup>10+</sup> 7815 7816getUnderflowCountSync(): number 7817 7818获取当前播放音频流的欠载音频帧数量,同步返回数据。 7819 7820**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7821 7822**返回值:** 7823 7824| 类型 | 说明 | 7825| ------------------- | ----------------------------- | 7826| number| 返回音频流的欠载音频帧数量。| 7827 7828**示例:** 7829 7830```ts 7831import { BusinessError } from '@kit.BasicServicesKit'; 7832 7833try { 7834 let value: number = audioRenderer.getUnderflowCountSync(); 7835 console.info(`Get underflow count Success! ${value}`); 7836} catch (err) { 7837 let error = err as BusinessError; 7838 console.error(`Get underflow count Fail: ${error}`); 7839} 7840``` 7841 7842### getCurrentOutputDevices<sup>10+</sup> 7843 7844getCurrentOutputDevices(callback: AsyncCallback<AudioDeviceDescriptors>): void 7845 7846获取音频流输出设备信息。使用callback异步回调。 7847 7848**系统能力:** SystemCapability.Multimedia.Audio.Device 7849 7850**参数:** 7851 7852| 参数名 | 类型 | 必填 | 说明 | 7853| ------- | -----------| ------ | ------------------- | 7854|callback | AsyncCallback\<[AudioDeviceDescriptors](#audiodevicedescriptors)>| 是 |回调函数。当获取音频流输出设备信息成功,err为undefined,data为获取到的音频流输出设备信息;否则为错误对象。| 7855 7856**示例:** 7857 7858```ts 7859import { BusinessError } from '@kit.BasicServicesKit'; 7860 7861audioRenderer.getCurrentOutputDevices((err: BusinessError, deviceInfo: audio.AudioDeviceDescriptors) => { 7862 if (err) { 7863 console.error(`getCurrentOutputDevices Fail: ${err}`); 7864 } else { 7865 for (let i = 0; i < deviceInfo.length; i++) { 7866 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7867 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7868 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7869 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7870 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7871 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7872 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7873 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7874 } 7875 } 7876}); 7877``` 7878### getCurrentOutputDevices<sup>10+</sup> 7879 7880getCurrentOutputDevices(): Promise<AudioDeviceDescriptors> 7881 7882获取音频流输出设备信息。使用Promise异步回调。 7883 7884**系统能力:** SystemCapability.Multimedia.Audio.Device 7885 7886**返回值:** 7887 7888| 类型 | 说明 | 7889| ------------------- | ----------------------------- | 7890| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)>| Promise对象,返回音频流的输出设备信息。 | 7891 7892**示例:** 7893 7894```ts 7895import { BusinessError } from '@kit.BasicServicesKit'; 7896 7897audioRenderer.getCurrentOutputDevices().then((deviceInfo: audio.AudioDeviceDescriptors) => { 7898 for (let i = 0; i < deviceInfo.length; i++) { 7899 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7900 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7901 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7902 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7903 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7904 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7905 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7906 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7907 } 7908}).catch((err: BusinessError) => { 7909 console.error(`Get current output devices Fail: ${err}`); 7910}); 7911``` 7912 7913### getCurrentOutputDevicesSync<sup>10+</sup> 7914 7915getCurrentOutputDevicesSync(): AudioDeviceDescriptors 7916 7917获取音频流输出设备信息。同步返回结果。 7918 7919**系统能力:** SystemCapability.Multimedia.Audio.Device 7920 7921**返回值:** 7922 7923| 类型 | 说明 | 7924| ------------------- | ----------------------------- | 7925| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回音频流的输出设备信息。 | 7926 7927**示例:** 7928 7929```ts 7930import { BusinessError } from '@kit.BasicServicesKit'; 7931 7932try { 7933 let deviceInfo: audio.AudioDeviceDescriptors = audioRenderer.getCurrentOutputDevicesSync(); 7934 for (let i = 0; i < deviceInfo.length; i++) { 7935 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7936 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7937 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7938 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7939 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7940 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7941 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7942 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7943 } 7944} catch (err) { 7945 let error = err as BusinessError; 7946 console.error(`Get current output devices Fail: ${error}`); 7947} 7948``` 7949### setChannelBlendMode<sup>11+</sup> 7950 7951setChannelBlendMode(mode: ChannelBlendMode): void 7952 7953设置单双声道混合模式。同步返回结果。 7954 7955**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7956 7957**参数:** 7958 7959| 参数名 | 类型 | 必填 | 说明 | 7960| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 7961| mode | [ChannelBlendMode](#channelblendmode11) | 是 | 声道混合模式类型。 | 7962 7963**错误码:** 7964 7965以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 7966 7967| 错误码ID | 错误信息 | 7968| ------- | --------------------------------------------| 7969| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 7970| 6800101 | Parameter verification failed. | 7971| 6800103 | Operation not permit at current state. | 7972 7973**示例:** 7974 7975```ts 7976let mode = audio.ChannelBlendMode.MODE_DEFAULT; 7977 7978audioRenderer.setChannelBlendMode(mode); 7979console.info(`BlendMode: ${mode}`); 7980``` 7981### setVolumeWithRamp<sup>11+</sup> 7982 7983setVolumeWithRamp(volume: number, duration: number): void 7984 7985设置音量渐变模式。同步返回结果。 7986 7987**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7988 7989**参数:** 7990 7991| 参数名 | 类型 | 必填 | 说明 | 7992| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 7993| volume | number | 是 | 渐变目标音量值,音量范围为[0.0, 1.0]。 | 7994| duration | number | 是 | 渐变持续时间,单位为ms。 | 7995 7996**错误码:** 7997 7998以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 7999 8000| 错误码ID | 错误信息 | 8001| ------- | --------------------------------------------| 8002| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8003| 6800101 | Parameter verification failed. | 8004 8005**示例:** 8006 8007```ts 8008let volume = 0.5; 8009let duration = 1000; 8010 8011audioRenderer.setVolumeWithRamp(volume, duration); 8012console.info(`setVolumeWithRamp: ${volume}`); 8013``` 8014 8015### setSilentModeAndMixWithOthers<sup>12+</sup> 8016 8017setSilentModeAndMixWithOthers(on: boolean): void 8018 8019设置静音并发播放模式。 8020 8021当设置为true,打开静音并发播放模式,系统将让此音频流静音播放,并且不会打断其他音频流。设置为false,将关闭静音并发播放,音频流可根据系统焦点策略抢占焦点。 8022 8023**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8024 8025**参数:** 8026 8027| 参数名 | 类型 | 必填 | 说明 | 8028| ------ | ---------------------------------------- | ---- |----------------------| 8029| on | boolean | 是 | 打开/关闭静音并发播放模式。true表示打开,false表示关闭。 | 8030 8031**示例:** 8032 8033```ts 8034audioRenderer.setSilentModeAndMixWithOthers(true); 8035``` 8036 8037### getSilentModeAndMixWithOthers<sup>12+</sup> 8038 8039getSilentModeAndMixWithOthers(): boolean 8040 8041获取静音并发播放模式。 8042 8043**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8044 8045**返回值:** 8046 8047| 类型 | 说明 | 8048| ------------------------------------------------- |-----------| 8049| boolean | 静音并发播放模式状态。返回true表示打开,返回false表示关闭。 | 8050 8051**示例:** 8052 8053```ts 8054let on = audioRenderer.getSilentModeAndMixWithOthers(); 8055``` 8056 8057### setDefaultOutputDevice<sup>12+</sup> 8058 8059setDefaultOutputDevice(deviceType: DeviceType): Promise<void> 8060 8061设置默认发声设备。使用Promise异步回调。 8062 8063> **说明:** 8064> 8065> - 本接口仅适用于[StreamUsage](#streamusage)为语音消息、VoIP语音通话或者VoIP视频通话的场景,支持听筒、扬声器和系统默认设备。 8066> 8067> - 本接口允许在AudioRenderer创建后随时调用,系统记录应用设置的默认本机内置发声设备。应用启动播放时,若外接设备如蓝牙耳机或有线耳机已接入,系统优先从外接设备发声;否则,系统遵循应用设置的默认本机内置发声设备。 8068> 8069> - 本接口优先级低于[AVCastPicker](../apis-avsession-kit/ohos-multimedia-avcastpicker.md#avcastpicker)。如果使用AVCastPicker切换过发声设备,再次调用本接口切换设备将不生效。 8070 8071**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8072 8073**参数:** 8074 8075| 参数名 | 类型 | 必填 | 说明 | 8076| ---------- |----------------| ------ |---------------------------------------------------------| 8077| deviceType | [DeviceType](#devicetype) | 是 | 设备类型。<br>仅支持以下设备:EARPIECE(听筒)、SPEAKER(扬声器)和DEFAULT(系统默认设备)。 | 8078 8079**返回值:** 8080 8081| 类型 | 说明 | 8082| ------------------- | ----------------------------- | 8083| Promise<void> | Promise对象。无返回结果的Promise对象。 | 8084 8085**错误码:** 8086 8087以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8088 8089| 错误码ID | 错误信息 | 8090| ------- | --------------------------------------------| 8091| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8092| 6800101 | Parameter verification failed. | 8093| 6800103 | Operation not permit at current state. | 8094 8095**示例:** 8096 8097```ts 8098import { BusinessError } from '@kit.BasicServicesKit'; 8099 8100// 本接口允许在AudioRenderer创建以后的任何时间被调用。 8101// 未播放时调用,系统会记录应用设置的默认本机内置发声设备,当应用启动播放时从设置的默认本机内置发声设备发声。 8102// 正在播放时调用,在没有外接设备如蓝牙耳机/有线耳机,系统会立即切换到设置的默认本机内置发声设备发声;否则系统会先记录应用设置的默认本机内置发声设备,等外接设备移除后再切换到设置的默认本机内置发声设备发声。 8103audioRenderer.setDefaultOutputDevice(audio.DeviceType.SPEAKER).then(() => { 8104 console.info('setDefaultOutputDevice Success!'); 8105}).catch((err: BusinessError) => { 8106 console.error(`setDefaultOutputDevice Fail: ${err}`); 8107}); 8108``` 8109 8110### on('audioInterrupt')<sup>9+</sup> 8111 8112on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 8113 8114监听音频中断事件(当音频焦点发生变化时触发)。使用callback异步回调。 8115 8116AudioRenderer对象在start事件时获取焦点,在pause、stop等事件时释放焦点,无需开发者主动申请。 8117 8118调用此方法后,如果AudioRenderer对象获取焦点失败或发生中断事件(如被其他音频打断等),会收到[InterruptEvent](#interruptevent9)。建议应用根据InterruptEvent的信息进行进一步处理。更多信息请参阅文档[音频焦点和音频会话介绍](../../media/audio/audio-playback-concurrency.md)。 8119 8120**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8121 8122**参数:** 8123 8124| 参数名 | 类型 | 必填 | 说明 | 8125| -------- | -------------------------------------------- | ---- | ----------------------------------------------------------- | 8126| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当音频焦点状态发生变化时,触发该事件。 | 8127| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回中断事件信息。 | 8128 8129**错误码:** 8130 8131以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8132 8133| 错误码ID | 错误信息 | 8134| ------- | --------------------------------------------| 8135| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8136| 6800101 | Parameter verification failed. | 8137 8138**示例:** 8139 8140```ts 8141import { audio } from '@kit.AudioKit'; 8142 8143let isPlaying: boolean; // 标识符,表示是否正在渲染。 8144let isDucked: boolean; // 标识符,表示是否被降低音量。 8145onAudioInterrupt(); 8146 8147async function onAudioInterrupt(){ 8148 audioRenderer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 8149 // 在发生音频打断事件时,audioRenderer收到interruptEvent回调,此处根据其内容做相应处理。 8150 // 1. 可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 8151 // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 8152 // 2. 必选:读取interruptEvent.hintType的类型,做出相应的处理。 8153 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 8154 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 8155 switch (interruptEvent.hintType) { 8156 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 8157 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 8158 console.info('Force paused. Update playing status and stop writing'); 8159 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8160 break; 8161 case audio.InterruptHint.INTERRUPT_HINT_STOP: 8162 // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发。 8163 console.info('Force stopped. Update playing status and stop writing'); 8164 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8165 break; 8166 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 8167 // 音频流已被降低音量渲染。 8168 console.info('Force ducked. Update volume status'); 8169 isDucked = true; // 简化处理,代表应用更新音量状态的若干操作。 8170 break; 8171 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 8172 // 音频流已被恢复正常音量渲染。 8173 console.info('Force ducked. Update volume status'); 8174 isDucked = false; // 简化处理,代表应用更新音量状态的若干操作。 8175 break; 8176 default: 8177 console.info('Invalid interruptEvent'); 8178 break; 8179 } 8180 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 8181 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 8182 switch (interruptEvent.hintType) { 8183 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 8184 // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染)。 8185 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 8186 console.info('Resume force paused renderer or ignore'); 8187 // 若选择继续渲染,需在此处主动执行开始渲染的若干操作。 8188 break; 8189 default: 8190 console.info('Invalid interruptEvent'); 8191 break; 8192 } 8193 } 8194 }); 8195} 8196``` 8197 8198### off('audioInterrupt')<sup>18+</sup> 8199 8200off(type: 'audioInterrupt', callback?: Callback<InterruptEvent>): void 8201 8202取消监听音频中断事件。使用callback异步回调。 8203 8204**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8205 8206**参数:** 8207 8208| 参数名 | 类型 | 必填 | 说明 | 8209| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 8210| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当取消监听音频中断事件时,触发该事件。 | 8211| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 否 | 回调函数,返回中断事件信息。 | 8212 8213**错误码:** 8214 8215以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8216 8217| 错误码ID | 错误信息 | 8218| ------- | --------------------------------------------| 8219| 6800101 | Parameter verification failed. | 8220 8221**示例:** 8222 8223```ts 8224// 取消该事件的所有监听。 8225audioRenderer.off('audioInterrupt'); 8226 8227// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8228let isPlaying: boolean; // 标识符,表示是否正在渲染。 8229let isDucked: boolean; // 标识符,表示是否被降低音量。 8230 8231let audioInterruptCallback = (interruptEvent: audio.InterruptEvent) => { 8232 // 在发生音频打断事件时,audioRenderer收到interruptEvent回调,此处根据其内容做相应处理。 8233 // 1. 可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 8234 // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 8235 // 2. 必选:读取interruptEvent.hintType的类型,做出相应的处理。 8236 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 8237 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 8238 switch (interruptEvent.hintType) { 8239 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 8240 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 8241 console.info('Force paused. Update playing status and stop writing'); 8242 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8243 break; 8244 case audio.InterruptHint.INTERRUPT_HINT_STOP: 8245 // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发。 8246 console.info('Force stopped. Update playing status and stop writing'); 8247 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作。 8248 break; 8249 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 8250 // 音频流已被降低音量渲染。 8251 console.info('Force ducked. Update volume status'); 8252 isDucked = true; // 简化处理,代表应用更新音量状态的若干操作。 8253 break; 8254 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 8255 // 音频流已被恢复正常音量渲染。 8256 console.info('Force ducked. Update volume status'); 8257 isDucked = false; // 简化处理,代表应用更新音量状态的若干操作。 8258 break; 8259 default: 8260 console.info('Invalid interruptEvent'); 8261 break; 8262 } 8263 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 8264 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 8265 switch (interruptEvent.hintType) { 8266 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 8267 // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染)。 8268 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 8269 console.info('Resume force paused renderer or ignore'); 8270 // 若选择继续渲染,需在此处主动执行开始渲染的若干操作。 8271 break; 8272 default: 8273 console.info('Invalid interruptEvent'); 8274 break; 8275 } 8276 } 8277}; 8278 8279audioRenderer.on('audioInterrupt', audioInterruptCallback); 8280 8281audioRenderer.off('audioInterrupt', audioInterruptCallback); 8282``` 8283 8284### on('markReach')<sup>8+</sup> 8285 8286on(type: 'markReach', frame: number, callback: Callback<number>): void 8287 8288监听标记到达事件(当渲染的帧数到达frame参数的值时触发,仅调用一次)。使用callback异步回调。 8289 8290如果将frame设置为100,当渲染帧数到达第100帧时,系统将上报信息。 8291 8292**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8293 8294**参数:** 8295 8296| 参数名 | 类型 | 必填 | 说明 | 8297| :------- | :----------------------- | :--- | :---------------------------------------- | 8298| type | string | 是 | 事件回调类型,支持的事件为'markReach',当渲染的帧数到达frame参数的值时,触发该事件。 | 8299| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 8300| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 8301 8302**示例:** 8303 8304```ts 8305audioRenderer.on('markReach', 1000, (position: number) => { 8306 if (position == 1000) { 8307 console.info('ON Triggered successfully'); 8308 } 8309}); 8310``` 8311 8312 8313### off('markReach')<sup>8+</sup> 8314 8315off(type: 'markReach', callback?: Callback<number>): void 8316 8317取消监听标记到达事件。使用callback异步回调。 8318 8319**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8320 8321**参数:** 8322 8323| 参数名 | 类型 | 必填 | 说明 | 8324| :----- | :----- | :--- | :------------------------------------------------ | 8325| type | string | 是 | 事件回调类型,支持的事件为'markReach',当取消监听标记到达事件时,触发该事件。 | 8326| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 8327 8328**示例:** 8329 8330```ts 8331// 取消该事件的所有监听。 8332audioRenderer.off('markReach'); 8333 8334// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8335let markReachCallback = (position: number) => { 8336 if (position == 1000) { 8337 console.info('ON Triggered successfully'); 8338 } 8339}; 8340 8341audioRenderer.on('markReach', 1000, markReachCallback); 8342 8343audioRenderer.off('markReach', markReachCallback); 8344``` 8345 8346### on('periodReach')<sup>8+</sup> 8347 8348on(type: 'periodReach', frame: number, callback: Callback<number>): void 8349 8350监听标记到达事件(每当渲染的帧数达到frame参数的值时触发,即按周期上报信息)。使用callback异步回调。 8351 8352如果将frame设置为10,每渲染10帧数据均会上报信息(例如:第10帧、第20帧、第30帧......)。 8353 8354**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8355 8356**参数:** 8357 8358| 参数名 | 类型 | 必填 | 说明 | 8359| :------- | :----------------------- | :--- | :------------------------------------------ | 8360| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当渲染的帧数达到frame参数的值时,触发该事件。 | 8361| frame | number | 是 | 触发事件的帧数。该值必须大于 0。 | 8362| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 8363 8364**示例:** 8365 8366```ts 8367audioRenderer.on('periodReach', 1000, (position: number) => { 8368 if (position == 1000) { 8369 console.info('ON Triggered successfully'); 8370 } 8371}); 8372``` 8373 8374### off('periodReach')<sup>8+</sup> 8375 8376off(type: 'periodReach', callback?: Callback<number>): void 8377 8378取消监听标记到达事件。使用callback异步回调。 8379 8380**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8381 8382**参数:** 8383 8384| 参数名 | 类型 | 必填 | 说明 | 8385| :----- | :----- | :--- | :-------------------------------------------------- | 8386| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当取消监听标记到达事件时,触发该事件。 | 8387| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 8388 8389**示例:** 8390 8391```ts 8392// 取消该事件的所有监听。 8393audioRenderer.off('periodReach'); 8394 8395// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8396let periodReachCallback = (position: number) => { 8397 if (position == 1000) { 8398 console.info('ON Triggered successfully'); 8399 } 8400}; 8401 8402audioRenderer.on('periodReach', periodReachCallback); 8403 8404audioRenderer.off('periodReach', periodReachCallback); 8405``` 8406 8407### on('stateChange')<sup>8+</sup> 8408 8409on(type: 'stateChange', callback: Callback<AudioState\>): void 8410 8411监听状态变化事件(当AudioRenderer的状态发生变化时触发)。使用callback异步回调。 8412 8413**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8414 8415**参数:** 8416 8417| 参数名 | 类型 | 必填 | 说明 | 8418| :------- | :------------------------- | :--- | :------------------------------------------ | 8419| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当AudioRenderer的状态发生变化时,触发该事件。 | 8420| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 8421 8422**示例:** 8423 8424```ts 8425audioRenderer.on('stateChange', (state: audio.AudioState) => { 8426 if (state == 1) { 8427 console.info('audio renderer state is: STATE_PREPARED'); 8428 } 8429 if (state == 2) { 8430 console.info('audio renderer state is: STATE_RUNNING'); 8431 } 8432}); 8433``` 8434 8435### off('stateChange')<sup>18+</sup> 8436 8437off(type: 'stateChange', callback?: Callback<AudioState>): void 8438 8439取消监听到达标记事件。使用callback异步回调。 8440 8441**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8442 8443**参数:** 8444 8445| 参数名 | 类型 | 必填 | 说明 | 8446| :----- | :----- | :--- | :-------------------------------------------------- | 8447| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当取消监听到达标记事件时,触发该事件。 | 8448| callback | Callback\<[AudioState](#audiostate8)> | 否 | 回调函数,返回当前音频的状态。 | 8449 8450**错误码:** 8451 8452以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 8453 8454| 错误码ID | 错误信息 | 8455| ------- | --------------------------------------------| 8456| 6800101 | Parameter verification failed. | 8457 8458**示例:** 8459 8460```ts 8461// 取消该事件的所有监听。 8462audioRenderer.off('stateChange'); 8463 8464// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8465let stateChangeCallback = (state: audio.AudioState) => { 8466 if (state == 1) { 8467 console.info('audio renderer state is: STATE_PREPARED'); 8468 } 8469 if (state == 2) { 8470 console.info('audio renderer state is: STATE_RUNNING'); 8471 } 8472}; 8473 8474audioRenderer.on('stateChange', stateChangeCallback); 8475 8476audioRenderer.off('stateChange', stateChangeCallback); 8477``` 8478 8479### on('outputDeviceChange')<sup>10+</sup> 8480 8481on(type: 'outputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 8482 8483监听音频输出设备变化事件(当音频输出设备发生变化时触发)。使用callback异步回调。 8484 8485**系统能力:** SystemCapability.Multimedia.Audio.Device 8486 8487**参数:** 8488 8489| 参数名 | 类型 | 必填 | 说明 | 8490| :------- | :------------------------- | :--- | :------------------------------------------ | 8491| type | string | 是 | 事件回调类型,支持的事件为'outputDeviceChange',当音频输出设备发生变化时,触发该事件。 | 8492| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数,返回当前音频流的输出设备描述信息。 | 8493 8494**错误码:** 8495 8496以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8497 8498| 错误码ID | 错误信息 | 8499| ------- | --------------------------------------------| 8500| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8501| 6800101 | Parameter verification failed. | 8502 8503**示例:** 8504 8505```ts 8506audioRenderer.on('outputDeviceChange', (deviceInfo: audio.AudioDeviceDescriptors) => { 8507 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 8508 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 8509 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 8510}); 8511``` 8512 8513### off('outputDeviceChange')<sup>10+</sup> 8514 8515off(type: 'outputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 8516 8517取消监听音频输出设备变化事件。使用callback异步回调。 8518 8519**系统能力:** SystemCapability.Multimedia.Audio.Device 8520 8521**参数:** 8522 8523| 参数名 | 类型 | 必填 | 说明 | 8524| :------- | :------------------------- | :--- | :------------------------------------------ | 8525| type | string | 是 | 事件回调类型,支持的事件为'outputDeviceChange',当取消监听音频输出设备变化事件时,触发该事件。 | 8526| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回当前音频流的输出设备描述信息。 | 8527 8528**错误码:** 8529 8530以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8531 8532| 错误码ID | 错误信息 | 8533| ------- | --------------------------------------------| 8534| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8535| 6800101 | Parameter verification failed. | 8536 8537**示例:** 8538 8539```ts 8540// 取消该事件的所有监听。 8541audioRenderer.off('outputDeviceChange'); 8542 8543// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8544let outputDeviceChangeCallback = (deviceInfo: audio.AudioDeviceDescriptors) => { 8545 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 8546 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 8547 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 8548}; 8549 8550audioRenderer.on('outputDeviceChange', outputDeviceChangeCallback); 8551 8552audioRenderer.off('outputDeviceChange', outputDeviceChangeCallback); 8553``` 8554 8555### on('outputDeviceChangeWithInfo')<sup>11+</sup> 8556 8557on(type: 'outputDeviceChangeWithInfo', callback: Callback\<AudioStreamDeviceChangeInfo>): void 8558 8559监听音频流输出设备变化及原因事件(当音频输出设备发生变化时触发)。使用callback异步回调。 8560 8561**系统能力:** SystemCapability.Multimedia.Audio.Device 8562 8563**参数:** 8564 8565| 参数名 | 类型 | 必填 | 说明 | 8566| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 8567| type | string | 是 | 事件回调类型,支持的事件为'outputDeviceChangeWithInfo',当音频输出设备发生变化时,触发该事件。 | 8568| callback | Callback\<[AudioStreamDeviceChangeInfo](#audiostreamdevicechangeinfo11)> | 是 | 回调函数,返回当前音频流的输出设备描述信息及变化原因。 | 8569 8570**错误码:** 8571 8572以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8573 8574| 错误码ID | 错误信息 | 8575| ------- | --------------------------------------------| 8576| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8577| 6800101 | Parameter verification failed. | 8578 8579**示例:** 8580 8581```ts 8582audioRenderer.on('outputDeviceChangeWithInfo', (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 8583 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 8584 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 8585 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 8586 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 8587}); 8588``` 8589 8590### off('outputDeviceChangeWithInfo')<sup>11+</sup> 8591 8592off(type: 'outputDeviceChangeWithInfo', callback?: Callback\<AudioStreamDeviceChangeInfo>): void 8593 8594取消监听音频流输出设备变化及原因事件。使用callback异步回调。 8595 8596**系统能力:** SystemCapability.Multimedia.Audio.Device 8597 8598**参数:** 8599 8600| 参数名 | 类型 | 必填 | 说明 | 8601| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 8602| type | string | 是 | 事件回调类型,支持的事件为'outputDeviceChangeWithInfo',当取消监听音频流输出设备变化及原因事件时,触发该事件。 | 8603| callback | Callback\<[AudioStreamDeviceChangeInfo](#audiostreamdevicechangeinfo11)> | 否 | 回调函数,返回当前音频流的输出设备描述信息及变化原因。 | 8604 8605**错误码:** 8606 8607以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8608 8609| 错误码ID | 错误信息 | 8610| ------- | --------------------------------------------| 8611| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8612| 6800101 | Parameter verification failed. | 8613 8614**示例:** 8615 8616```ts 8617// 取消该事件的所有监听。 8618audioRenderer.off('outputDeviceChangeWithInfo'); 8619 8620// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8621let outputDeviceChangeWithInfoCallback = (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 8622 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 8623 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 8624 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 8625 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 8626}; 8627 8628audioRenderer.on('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 8629 8630audioRenderer.off('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 8631``` 8632 8633### on('writeData')<sup>11+</sup> 8634 8635on(type: 'writeData', callback: AudioRendererWriteDataCallback): void 8636 8637监听音频数据写入回调事件(当需要写入音频数据时触发),使用 callback 方式返回结果。 8638 8639回调函数仅用来写入音频数据,请勿在回调函数中调用AudioRenderer相关接口。 8640 8641**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8642 8643**参数:** 8644 8645| 参数名 | 类型 | 必填 | 说明 | 8646| :------- |:--------------------------------| :--- |:--------------------------------------| 8647| type | string | 是 | 事件回调类型,支持的事件为'writeData',当需要写入音频数据时,触发该事件。 | 8648| callback | [AudioRendererWriteDataCallback](#audiorendererwritedatacallback12) | 是 | 回调函数,入参代表应用接收待写入的数据缓冲区。<br>API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果[AudioDataCallbackResult](#audiodatacallbackresult12)。 | 8649 8650**错误码:** 8651 8652以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8653 8654| 错误码ID | 错误信息 | 8655| ------- | --------------------------------------------| 8656| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 8657| 6800101 | Parameter verification failed. | 8658 8659**示例:** 8660 8661```ts 8662import { BusinessError } from '@kit.BasicServicesKit'; 8663import {fileIo as fs} from '@kit.CoreFileKit'; 8664import { common } from '@kit.AbilityKit'; 8665 8666class Options { 8667 offset?: number; 8668 length?: number; 8669} 8670 8671let bufferSize: number = 0; 8672// 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext。 8673let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 8674let path = context.cacheDir; 8675// 确保该沙箱路径下存在该资源。 8676let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 8677let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 8678let writeDataCallback = (buffer: ArrayBuffer) => { 8679 let options: Options = { 8680 offset: bufferSize, 8681 length: buffer.byteLength 8682 }; 8683 8684 try { 8685 fs.readSync(file.fd, buffer, options); 8686 bufferSize += buffer.byteLength; 8687 // API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果。 8688 return audio.AudioDataCallbackResult.VALID; 8689 } catch (error) { 8690 console.error('Error reading file:', error); 8691 // API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果。 8692 return audio.AudioDataCallbackResult.INVALID; 8693 } 8694}; 8695 8696audioRenderer.on('writeData', writeDataCallback); 8697audioRenderer.start().then(() => { 8698 console.info('Renderer started'); 8699}).catch((err: BusinessError) => { 8700 console.error(`ERROR: ${err}`); 8701}); 8702``` 8703 8704### off('writeData')<sup>11+</sup> 8705 8706off(type: 'writeData', callback?: AudioRendererWriteDataCallback): void 8707 8708取消监听音频数据写入回调事件。使用callback异步回调。 8709 8710**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8711 8712**参数:** 8713 8714| 参数名 | 类型 | 必填 | 说明 | 8715| :------- |:--------------------------------| :--- |:--------------------------------------| 8716| type | string | 是 | 事件回调类型,支持的事件为'writeData',当取消监听音频数据写入回调事件时,触发该事件。 | 8717| callback | [AudioRendererWriteDataCallback](#audiorendererwritedatacallback12) | 否 | 回调函数,入参代表应用接收待写入的数据缓冲区。<br>API version 11 不支持返回回调结果,从 API version 12 开始支持返回回调结果[AudioDataCallbackResult](#audiodatacallbackresult12)。 | 8718 8719**错误码:** 8720 8721以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 8722 8723| 错误码ID | 错误信息 | 8724| ------- | --------------------------------------------| 8725| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8726| 6800101 | Parameter verification failed. | 8727 8728**示例:** 8729 8730```ts 8731// 取消该事件的所有监听。 8732audioRenderer.off('writeData'); 8733 8734// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 8735let writeDataCallback = (data: ArrayBuffer) => { 8736 console.info(`write data: ${data}`); 8737}; 8738 8739audioRenderer.on('writeData', writeDataCallback); 8740 8741audioRenderer.off('writeData', writeDataCallback); 8742``` 8743 8744## AudioCapturer<sup>8+</sup> 8745 8746提供音频采集的相关接口。 8747 8748在使用AudioCapturer的接口之前,需先通过[createAudioCapturer](#audiocreateaudiocapturer8)获取AudioCapturer实例。 8749 8750### 属性 8751 8752**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8753 8754| 名称 | 类型 | 可读 | 可写 | 说明 | 8755| :---- | :------------------------- | :--- | :--- | :--------------- | 8756| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频采集器状态。 | 8757 8758**示例:** 8759 8760```ts 8761import { audio } from '@kit.AudioKit'; 8762 8763let state: audio.AudioState = audioCapturer.state; 8764``` 8765 8766### getCapturerInfo<sup>8+</sup> 8767 8768getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void 8769 8770获取音频采集器信息。使用callback异步回调。 8771 8772**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8773 8774**参数:** 8775 8776| 参数名 | 类型 | 必填 | 说明 | 8777| :------- | :-------------------------------- | :--- | :----------------------------------- | 8778| callback | AsyncCallback<[AudioCapturerInfo](#audiocapturerinfo8)\> | 是 | 回调函数。当获取音频采集器信息成功,err为undefined,data为获取到的音频采集器信息;否则为错误对象。 | 8779 8780**示例:** 8781 8782```ts 8783import { BusinessError } from '@kit.BasicServicesKit'; 8784 8785audioCapturer.getCapturerInfo((err: BusinessError, capturerInfo: audio.AudioCapturerInfo) => { 8786 if (err) { 8787 console.error('Failed to get capture info'); 8788 } else { 8789 console.info('Capturer getCapturerInfo:'); 8790 console.info(`Capturer source: ${capturerInfo.source}`); 8791 console.info(`Capturer flags: ${capturerInfo.capturerFlags}`); 8792 } 8793}); 8794``` 8795 8796 8797### getCapturerInfo<sup>8+</sup> 8798 8799getCapturerInfo(): Promise<AudioCapturerInfo\> 8800 8801获取音频采集器信息。使用Promise异步回调。 8802 8803**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8804 8805**返回值:** 8806 8807| 类型 | 说明 | 8808| :------------------------------------------------ | :---------------------------------- | 8809| Promise<[AudioCapturerInfo](#audiocapturerinfo8)\> | Promise对象,返回音频采集器信息。 | 8810 8811**示例:** 8812 8813```ts 8814import { BusinessError } from '@kit.BasicServicesKit'; 8815 8816audioCapturer.getCapturerInfo().then((audioParamsGet: audio.AudioCapturerInfo) => { 8817 if (audioParamsGet != undefined) { 8818 console.info('AudioFrameworkRecLog: Capturer CapturerInfo:'); 8819 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 8820 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 8821 } else { 8822 console.info(`AudioFrameworkRecLog: audioParamsGet is : ${audioParamsGet}`); 8823 console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect'); 8824 } 8825}).catch((err: BusinessError) => { 8826 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${err}`); 8827}) 8828``` 8829 8830### getCapturerInfoSync<sup>10+</sup> 8831 8832getCapturerInfoSync(): AudioCapturerInfo 8833 8834获取音频采集器信息。同步返回结果。 8835 8836**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8837 8838**返回值:** 8839 8840| 类型 | 说明 | 8841| :------------------------------------------------ | :---------------------------------- | 8842| [AudioCapturerInfo](#audiocapturerinfo8) | 返回音频采集器信息。 | 8843 8844**示例:** 8845 8846```ts 8847import { BusinessError } from '@kit.BasicServicesKit'; 8848 8849try { 8850 let audioParamsGet: audio.AudioCapturerInfo = audioCapturer.getCapturerInfoSync(); 8851 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 8852 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 8853} catch (err) { 8854 let error = err as BusinessError; 8855 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${error}`); 8856} 8857``` 8858 8859### getStreamInfo<sup>8+</sup> 8860 8861getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 8862 8863获取音频采集器流信息。使用callback异步回调。 8864 8865**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8866 8867**参数:** 8868 8869| 参数名 | 类型 | 必填 | 说明 | 8870| :------- | :--------------------------------------------------- | :--- | :------------------------------- | 8871| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取音频采集器流信息成功,err为undefined,data为获取到的音频采集器流信息;否则为错误对象。 | 8872 8873**示例:** 8874 8875```ts 8876import { BusinessError } from '@kit.BasicServicesKit'; 8877 8878audioCapturer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 8879 if (err) { 8880 console.error('Failed to get stream info'); 8881 } else { 8882 console.info('Capturer GetStreamInfo:'); 8883 console.info(`Capturer sampling rate: ${streamInfo.samplingRate}`); 8884 console.info(`Capturer channel: ${streamInfo.channels}`); 8885 console.info(`Capturer format: ${streamInfo.sampleFormat}`); 8886 console.info(`Capturer encoding type: ${streamInfo.encodingType}`); 8887 } 8888}); 8889``` 8890 8891### getStreamInfo<sup>8+</sup> 8892 8893getStreamInfo(): Promise<AudioStreamInfo\> 8894 8895获取音频采集器流信息。使用Promise异步回调。 8896 8897**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8898 8899**返回值:** 8900 8901| 类型 | 说明 | 8902| :--------------------------------------------- | :------------------------------ | 8903| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回音频流信息。 | 8904 8905**示例:** 8906 8907```ts 8908import { BusinessError } from '@kit.BasicServicesKit'; 8909 8910audioCapturer.getStreamInfo().then((audioParamsGet: audio.AudioStreamInfo) => { 8911 console.info('getStreamInfo:'); 8912 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 8913 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 8914 console.info(`channels: ${audioParamsGet.channels}`); 8915 console.info(`encodingType: ${audioParamsGet.encodingType}`); 8916}).catch((err: BusinessError) => { 8917 console.error(`getStreamInfo :ERROR: ${err}`); 8918}); 8919``` 8920 8921### getStreamInfoSync<sup>10+</sup> 8922 8923getStreamInfoSync(): AudioStreamInfo 8924 8925获取音频采集器流信息。同步返回结果。 8926 8927**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8928 8929**返回值:** 8930 8931| 类型 | 说明 | 8932| :--------------------------------------------- | :------------------------------ | 8933| [AudioStreamInfo](#audiostreaminfo8) | 返回音频流信息。 | 8934 8935**示例:** 8936 8937```ts 8938import { BusinessError } from '@kit.BasicServicesKit'; 8939 8940try { 8941 let audioParamsGet: audio.AudioStreamInfo = audioCapturer.getStreamInfoSync(); 8942 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 8943 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 8944 console.info(`channels: ${audioParamsGet.channels}`); 8945 console.info(`encodingType: ${audioParamsGet.encodingType}`); 8946} catch (err) { 8947 let error = err as BusinessError; 8948 console.error(`getStreamInfo :ERROR: ${error}`); 8949} 8950``` 8951 8952### getAudioStreamId<sup>9+</sup> 8953 8954getAudioStreamId(callback: AsyncCallback<number\>): void 8955 8956获取音频流id。使用callback异步回调。 8957 8958**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8959 8960**参数:** 8961 8962| 参数名 | 类型 | 必填 | 说明 | 8963| :------- | :--------------------------------------------------- | :--- | :------------------- | 8964| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 8965 8966**示例:** 8967 8968```ts 8969import { BusinessError } from '@kit.BasicServicesKit'; 8970 8971audioCapturer.getAudioStreamId((err: BusinessError, streamId: number) => { 8972 console.info(`audioCapturer GetStreamId: ${streamId}`); 8973}); 8974``` 8975 8976### getAudioStreamId<sup>9+</sup> 8977 8978getAudioStreamId(): Promise<number\> 8979 8980获取音频流id。使用Promise异步回调。 8981 8982**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8983 8984**返回值:** 8985 8986| 类型 | 说明 | 8987| :----------------| :--------------------- | 8988| Promise<number\> | Promise对象,返回音频流id。 | 8989 8990**示例:** 8991 8992```ts 8993import { BusinessError } from '@kit.BasicServicesKit'; 8994 8995audioCapturer.getAudioStreamId().then((streamId: number) => { 8996 console.info(`audioCapturer getAudioStreamId: ${streamId}`); 8997}).catch((err: BusinessError) => { 8998 console.error(`ERROR: ${err}`); 8999}); 9000``` 9001 9002### getAudioStreamIdSync<sup>10+</sup> 9003 9004getAudioStreamIdSync(): number 9005 9006获取音频流id。同步返回结果。 9007 9008**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9009 9010**返回值:** 9011 9012| 类型 | 说明 | 9013| :----------------| :--------------------- | 9014| number | 返回音频流id。 | 9015 9016**示例:** 9017 9018```ts 9019import { BusinessError } from '@kit.BasicServicesKit'; 9020 9021try { 9022 let streamId: number = audioCapturer.getAudioStreamIdSync(); 9023 console.info(`audioCapturer getAudioStreamIdSync: ${streamId}`); 9024} catch (err) { 9025 let error = err as BusinessError; 9026 console.error(`ERROR: ${error}`); 9027} 9028``` 9029 9030### start<sup>8+</sup> 9031 9032start(callback: AsyncCallback<void\>): void 9033 9034启动音频采集器。使用callback异步回调。 9035 9036**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9037 9038**参数:** 9039 9040| 参数名 | 类型 | 必填 | 说明 | 9041| :------- | :------------------- | :--- | :----------------------------- | 9042| callback | AsyncCallback<void\> | 是 | 回调函数。当启动音频采集器成功,err为undefined,否则为错误对象。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 9043 9044**示例:** 9045 9046```ts 9047import { BusinessError } from '@kit.BasicServicesKit'; 9048 9049audioCapturer.start((err: BusinessError) => { 9050 if (err) { 9051 console.error('Capturer start failed.'); 9052 } else { 9053 console.info('Capturer start success.'); 9054 } 9055}); 9056``` 9057 9058 9059### start<sup>8+</sup> 9060 9061start(): Promise<void\> 9062 9063启动音频采集器。使用Promise异步回调。 9064 9065**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9066 9067**返回值:** 9068 9069| 类型 | 说明 | 9070| :------------- | :---------------------------- | 9071| Promise<void\> | Promise对象,成功表示启动音频采集器成功。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 9072 9073**示例:** 9074 9075```ts 9076import { BusinessError } from '@kit.BasicServicesKit'; 9077 9078audioCapturer.start().then(() => { 9079 console.info('AudioFrameworkRecLog: ---------START---------'); 9080 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 9081 console.info(`AudioFrameworkRecLog: AudioCapturer: STATE: ${audioCapturer.state}`); 9082 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 9083 if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) { 9084 console.info('AudioFrameworkRecLog: AudioCapturer is in Running State'); 9085 } 9086}).catch((err: BusinessError) => { 9087 console.error(`AudioFrameworkRecLog: Capturer start :ERROR : ${err}`); 9088}); 9089``` 9090 9091### stop<sup>8+</sup> 9092 9093stop(callback: AsyncCallback<void\>): void 9094 9095停止音频采集。使用callback异步回调。 9096 9097**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9098 9099**参数:** 9100 9101| 参数名 | 类型 | 必填 | 说明 | 9102| :------- | :------------------- | :--- | :----------------------------- | 9103| callback | AsyncCallback<void\> | 是 | 回调函数。当停止音频采集成功,err为undefined,否则为错误对象。 | 9104 9105**示例:** 9106 9107```ts 9108import { BusinessError } from '@kit.BasicServicesKit'; 9109 9110audioCapturer.stop((err: BusinessError) => { 9111 if (err) { 9112 console.error('Capturer stop failed'); 9113 } else { 9114 console.info('Capturer stopped.'); 9115 } 9116}); 9117``` 9118 9119 9120### stop<sup>8+</sup> 9121 9122stop(): Promise<void\> 9123 9124停止音频采集。使用Promise异步回调。 9125 9126**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9127 9128**返回值:** 9129 9130| 类型 | 说明 | 9131| :------------- | :---------------------------- | 9132| Promise<void\> | Promise对象。无返回结果的Promise对象。 | 9133 9134**示例:** 9135 9136```ts 9137import { BusinessError } from '@kit.BasicServicesKit'; 9138 9139audioCapturer.stop().then(() => { 9140 console.info('AudioFrameworkRecLog: ---------STOP RECORD---------'); 9141 console.info('AudioFrameworkRecLog: Capturer stopped: SUCCESS'); 9142 if ((audioCapturer.state == audio.AudioState.STATE_STOPPED)){ 9143 console.info('AudioFrameworkRecLog: State is Stopped:'); 9144 } 9145}).catch((err: BusinessError) => { 9146 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 9147}); 9148``` 9149 9150### release<sup>8+</sup> 9151 9152release(callback: AsyncCallback<void\>): void 9153 9154释放音频采集器。使用callback异步回调。 9155 9156**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9157 9158**参数:** 9159 9160| 参数名 | 类型 | 必填 | 说明 | 9161| :------- | :------------------- | :--- | :---------------------------------- | 9162| callback | AsyncCallback<void\> | 是 | 回调函数。当释放音频采集器成功,err为undefined,否则为错误对象。 | 9163 9164**示例:** 9165 9166```ts 9167import { BusinessError } from '@kit.BasicServicesKit'; 9168 9169audioCapturer.release((err: BusinessError) => { 9170 if (err) { 9171 console.error('capturer release failed'); 9172 } else { 9173 console.info('capturer released.'); 9174 } 9175}); 9176``` 9177 9178 9179### release<sup>8+</sup> 9180 9181release(): Promise<void\> 9182 9183释放音频采集器。使用Promise异步回调。 9184 9185**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9186 9187**返回值:** 9188 9189| 类型 | 说明 | 9190| :------------- | :---------------------------- | 9191| Promise<void\> | Promise对象。无返回结果的Promise对象。 | 9192 9193**示例:** 9194 9195```ts 9196import { BusinessError } from '@kit.BasicServicesKit'; 9197 9198audioCapturer.release().then(() => { 9199 console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------'); 9200 console.info('AudioFrameworkRecLog: Capturer release : SUCCESS'); 9201 console.info(`AudioFrameworkRecLog: AudioCapturer : STATE : ${audioCapturer.state}`); 9202}).catch((err: BusinessError) => { 9203 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 9204}); 9205``` 9206 9207### read<sup>8+(deprecated)</sup> 9208 9209read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void 9210 9211读入缓冲区。使用callback异步回调。 9212 9213> **说明:** 9214> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('readData')](#onreaddata11)替代。 9215 9216**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9217 9218**参数:** 9219 9220| 参数名 | 类型 | 必填 | 说明 | 9221| :------------- | :-------------------------- | :--- | :------------------------------- | 9222| size | number | 是 | 读入的字节数。 | 9223| isBlockingRead | boolean | 是 | 是否阻塞读操作。true表示阻塞,false表示不阻塞。 | 9224| callback | AsyncCallback<ArrayBuffer\> | 是 | 回调函数。当读入缓冲区成功,err为undefined,data为获取到的缓冲区;否则为错误对象。 | 9225 9226**示例:** 9227 9228```ts 9229import { BusinessError } from '@kit.BasicServicesKit'; 9230 9231let bufferSize: number = 0; 9232 9233audioCapturer.getBufferSize().then((data: number) => { 9234 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 9235 bufferSize = data; 9236}).catch((err: BusinessError) => { 9237 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR: ${err}`); 9238}); 9239 9240audioCapturer.read(bufferSize, true, (err: BusinessError, buffer: ArrayBuffer) => { 9241 if (!err) { 9242 console.info('Success in reading the buffer data'); 9243 } 9244}); 9245``` 9246 9247### read<sup>8+(deprecated)</sup> 9248 9249read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\> 9250 9251读入缓冲区。使用Promise异步回调。 9252 9253> **说明:** 9254> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('readData')](#onreaddata11)替代。 9255 9256**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9257 9258**参数:** 9259 9260| 参数名 | 类型 | 必填 | 说明 | 9261| :------------- | :------ | :--- | :--------------- | 9262| size | number | 是 | 读入的字节数。 | 9263| isBlockingRead | boolean | 是 | 是否阻塞读操作。true表示阻塞,false表示不阻塞。 | 9264 9265**返回值:** 9266 9267| 类型 | 说明 | 9268| :-------------------- | :----------------------------------------------------- | 9269| Promise<ArrayBuffer\> | Promise对象,返回读取的缓冲区数据。 | 9270 9271**示例:** 9272 9273```ts 9274import { BusinessError } from '@kit.BasicServicesKit'; 9275 9276let bufferSize: number = 0; 9277 9278audioCapturer.getBufferSize().then((data: number) => { 9279 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 9280 bufferSize = data; 9281}).catch((err: BusinessError) => { 9282 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR ${err}`); 9283}); 9284console.info(`Buffer size: ${bufferSize}`); 9285 9286audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 9287 console.info('buffer read successfully'); 9288}).catch((err: BusinessError) => { 9289 console.error(`ERROR : ${err}`); 9290}); 9291``` 9292 9293### getAudioTime<sup>8+</sup> 9294 9295getAudioTime(callback: AsyncCallback<number\>): void 9296 9297获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。使用callback异步回调。 9298 9299**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9300 9301**参数:** 9302 9303| 参数名 | 类型 | 必填 | 说明 | 9304| :------- | :--------------------- | :--- | :----------------------------- | 9305| callback | AsyncCallback<number\> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 9306 9307**示例:** 9308 9309```ts 9310import { BusinessError } from '@kit.BasicServicesKit'; 9311 9312audioCapturer.getAudioTime((err: BusinessError, timestamp: number) => { 9313 console.info(`Current timestamp: ${timestamp}`); 9314}); 9315``` 9316 9317### getAudioTime<sup>8+</sup> 9318 9319getAudioTime(): Promise<number\> 9320 9321获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。使用Promise异步回调。 9322 9323**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9324 9325**返回值:** 9326 9327| 类型 | 说明 | 9328| :--------------- | :---------------------------- | 9329| Promise<number\> | Promise对象,返回时间戳(从1970年1月1日开始),单位为纳秒。 | 9330 9331**示例:** 9332 9333```ts 9334import { BusinessError } from '@kit.BasicServicesKit'; 9335 9336audioCapturer.getAudioTime().then((audioTime: number) => { 9337 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTime : Success ${audioTime}`); 9338}).catch((err: BusinessError) => { 9339 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 9340}); 9341``` 9342 9343### getAudioTimeSync<sup>10+</sup> 9344 9345getAudioTimeSync(): number 9346 9347获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。同步返回结果。 9348 9349**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9350 9351**返回值:** 9352 9353| 类型 | 说明 | 9354| :--------------- | :---------------------------- | 9355| number | 返回时间戳。 | 9356 9357**示例:** 9358 9359```ts 9360import { BusinessError } from '@kit.BasicServicesKit'; 9361 9362try { 9363 let audioTime: number = audioCapturer.getAudioTimeSync(); 9364 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : Success ${audioTime}`); 9365} catch (err) { 9366 let error = err as BusinessError; 9367 console.error(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : ERROR : ${error}`); 9368} 9369``` 9370 9371### getAudioTimestampInfo<sup>19+</sup> 9372 9373getAudioTimestampInfo(): Promise\<AudioTimestampInfo> 9374 9375获取音频流时间戳和当前数据帧位置信息。使用Promise异步回调。 9376 9377**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9378 9379**返回值:** 9380 9381| 类型 | 描述 | 9382|-------------------------------------------------------| ----------------------- | 9383| Promise\<[AudioTimestampInfo](#audiotimestampinfo19)> | Promise对象,返回音频流时间戳和当前数据帧位置信息。 | 9384 9385**错误码:** 9386 9387以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 9388 9389| 错误码ID | 错误信息 | 9390| ------- | --------------------------------------------| 9391| 6800103 | Operation not permit at current state. | 9392 9393**示例:** 9394 9395```ts 9396import { BusinessError } from '@kit.BasicServicesKit'; 9397 9398audioCapturer.getAudioTimestampInfo().then((audioTimestampInfo: audio.AudioTimestampInfo) => { 9399 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 9400}).catch((err: BusinessError) => { 9401 console.error(`ERROR: ${err}`); 9402}); 9403``` 9404 9405### getAudioTimestampInfoSync<sup>19+</sup> 9406 9407getAudioTimestampInfoSync(): AudioTimestampInfo 9408 9409获取音频流时间戳和当前数据帧位置信息。同步返回结果。 9410 9411**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9412 9413**返回值:** 9414 9415| 类型 | 描述 | 9416| ---------------- | ----------------------- | 9417| [AudioTimestampInfo](#audiotimestampinfo19) | 返回音频流时间戳和当前数据帧位置信息。 | 9418 9419**错误码:** 9420 9421以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 9422 9423| 错误码ID | 错误信息 | 9424| ------- | --------------------------------------------| 9425| 6800103 | Operation not permit at current state. | 9426 9427**示例:** 9428 9429```ts 9430import { BusinessError } from '@kit.BasicServicesKit'; 9431 9432try { 9433 let audioTimestampInfo: audio.AudioTimestampInfo = audioCapturer.getAudioTimestampInfoSync(); 9434 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 9435} catch (err) { 9436 let error = err as BusinessError; 9437 console.error(`ERROR: ${error}`); 9438} 9439``` 9440 9441### getBufferSize<sup>8+</sup> 9442 9443getBufferSize(callback: AsyncCallback<number\>): void 9444 9445获取采集器合理的最小缓冲区大小。使用callback异步回调。 9446 9447**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9448 9449**参数:** 9450 9451| 参数名 | 类型 | 必填 | 说明 | 9452| :------- | :--------------------- | :--- | :----------------------------------- | 9453| callback | AsyncCallback<number\> | 是 | 回调函数。当获取采集器合理的最小缓冲区大小成功,err为undefined,data为获取到的采集器合理的最小缓冲区大小;否则为错误对象。 | 9454 9455**示例:** 9456 9457```ts 9458import { BusinessError } from '@kit.BasicServicesKit'; 9459 9460audioCapturer.getBufferSize((err: BusinessError, bufferSize: number) => { 9461 if (!err) { 9462 console.info(`BufferSize : ${bufferSize}`); 9463 audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 9464 console.info(`Buffer read is ${buffer.byteLength}`); 9465 }).catch((err: BusinessError) => { 9466 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 9467 }); 9468 } 9469}); 9470``` 9471 9472### getBufferSize<sup>8+</sup> 9473 9474getBufferSize(): Promise<number\> 9475 9476获取采集器合理的最小缓冲区大小。使用Promise异步回调。 9477 9478**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9479 9480**返回值:** 9481 9482| 类型 | 说明 | 9483| :--------------- | :---------------------------------- | 9484| Promise<number\> | Promise对象,返回缓冲区大小。 | 9485 9486**示例:** 9487 9488```ts 9489import { BusinessError } from '@kit.BasicServicesKit'; 9490 9491let bufferSize: number = 0; 9492 9493audioCapturer.getBufferSize().then((data: number) => { 9494 console.info(`AudioFrameworkRecLog: getBufferSize :SUCCESS ${data}`); 9495 bufferSize = data; 9496}).catch((err: BusinessError) => { 9497 console.error(`AudioFrameworkRecLog: getBufferSize :ERROR : ${err}`); 9498}); 9499``` 9500 9501### getBufferSizeSync<sup>10+</sup> 9502 9503getBufferSizeSync(): number 9504 9505获取采集器合理的最小缓冲区大小。同步返回结果。 9506 9507**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9508 9509**返回值:** 9510 9511| 类型 | 说明 | 9512| :--------------- | :---------------------------------- | 9513| number | 返回缓冲区大小。 | 9514 9515**示例:** 9516 9517```ts 9518import { BusinessError } from '@kit.BasicServicesKit'; 9519 9520let bufferSize: number = 0; 9521 9522try { 9523 bufferSize = audioCapturer.getBufferSizeSync(); 9524 console.info(`AudioFrameworkRecLog: getBufferSizeSync :SUCCESS ${bufferSize}`); 9525} catch (err) { 9526 let error = err as BusinessError; 9527 console.error(`AudioFrameworkRecLog: getBufferSizeSync :ERROR : ${error}`); 9528} 9529``` 9530 9531### getCurrentInputDevices<sup>11+</sup> 9532 9533getCurrentInputDevices(): AudioDeviceDescriptors 9534 9535获取录音流输入设备信息。同步返回结果。 9536 9537**系统能力:** SystemCapability.Multimedia.Audio.Device 9538 9539**返回值:** 9540 9541| 类型 | 说明 | 9542| ---------------------- | ------------------------------------------------------ | 9543| [AudioDeviceDescriptors](#audiodevicedescriptors) | 同步接口,返回设备属性数组类型数据。 | 9544 9545**示例:** 9546 9547```ts 9548let deviceDescriptors: audio.AudioDeviceDescriptors = audioCapturer.getCurrentInputDevices(); 9549console.info(`Device id: ${deviceDescriptors[0].id}`); 9550console.info(`Device type: ${deviceDescriptors[0].deviceType}`); 9551console.info(`Device role: ${deviceDescriptors[0].deviceRole}`); 9552console.info(`Device name: ${deviceDescriptors[0].name}`); 9553console.info(`Device address: ${deviceDescriptors[0].address}`); 9554console.info(`Device samplerates: ${deviceDescriptors[0].sampleRates[0]}`); 9555console.info(`Device channelcounts: ${deviceDescriptors[0].channelCounts[0]}`); 9556console.info(`Device channelmask: ${deviceDescriptors[0].channelMasks[0]}`); 9557if (deviceDescriptors[0].encodingTypes) { 9558 console.info(`Device encodingTypes: ${deviceDescriptors[0].encodingTypes[0]}`); 9559} 9560``` 9561 9562### getCurrentAudioCapturerChangeInfo<sup>11+</sup> 9563 9564getCurrentAudioCapturerChangeInfo(): AudioCapturerChangeInfo 9565 9566获取录音流配置。同步返回结果。 9567 9568**系统能力:** SystemCapability.Multimedia.Audio.Device 9569 9570**返回值:** 9571 9572| 类型 | 说明 | 9573| :--------------- | :---------------------------------- | 9574| [AudioCapturerChangeInfo](#audiocapturerchangeinfo9) | 同步接口,返回描述音频采集器更改信息。 | 9575 9576**示例:** 9577 9578```ts 9579let info: audio.AudioCapturerChangeInfo = audioCapturer.getCurrentAudioCapturerChangeInfo(); 9580console.info(`Info streamId: ${info.streamId}`); 9581console.info(`Info source: ${info.capturerInfo.source}`); 9582console.info(`Info capturerFlags: ${info.capturerInfo.capturerFlags}`); 9583console.info(`Info muted: ${info.muted}`); 9584console.info(`Info type: ${info.deviceDescriptors[0].deviceType}`); 9585console.info(`Info role: ${info.deviceDescriptors[0].deviceRole}`); 9586console.info(`Info name: ${info.deviceDescriptors[0].name}`); 9587console.info(`Info address: ${info.deviceDescriptors[0].address}`); 9588console.info(`Info samplerates: ${info.deviceDescriptors[0].sampleRates[0]}`); 9589console.info(`Info channelcounts: ${info.deviceDescriptors[0].channelCounts[0]}`); 9590console.info(`Info channelmask: ${info.deviceDescriptors[0].channelMasks[0]}`); 9591if (info.deviceDescriptors[0].encodingTypes) { 9592 console.info(`Device encodingTypes: ${info.deviceDescriptors[0].encodingTypes[0]}`); 9593} 9594``` 9595 9596### on('audioInterrupt')<sup>10+</sup> 9597 9598on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 9599 9600监听音频中断事件(当音频焦点发生变化时触发)。使用callback异步回调。 9601 9602AudioCapturer对象在start事件时获取焦点,在pause、stop等事件时释放焦点,无需开发者主动申请。 9603 9604调用此方法后,如果AudioCapturer对象获取焦点失败或发生中断事件(如被其他音频打断等),会收到[InterruptEvent](#interruptevent9)。建议应用根据InterruptEvent的信息进行进一步处理。更多信息请参阅文档[音频焦点和音频会话介绍](../../media/audio/audio-playback-concurrency.md)。 9605 9606**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 9607 9608**参数:** 9609 9610| 参数名 | 类型 | 必填 | 说明 | 9611| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 9612| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当音频焦点状态发生变化时,触发该事件。 | 9613| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回中断事件信息。 | 9614 9615**错误码:** 9616 9617以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9618 9619| 错误码ID | 错误信息 | 9620| ------- | --------------------------------------------| 9621| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9622| 6800101 | Parameter verification failed. | 9623 9624**示例:** 9625 9626```ts 9627import { audio } from '@kit.AudioKit'; 9628 9629let isCapturing: boolean; // 标识符,表示是否正在采集。 9630onAudioInterrupt(); 9631 9632async function onAudioInterrupt(){ 9633 audioCapturer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 9634 // 在发生音频打断事件时,audioCapturer收到interruptEvent回调,此处根据其内容做相应处理。 9635 // 1、可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 9636 // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 9637 // 2、必选:读取interruptEvent.hintType的类型,做出相应的处理。 9638 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 9639 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 9640 switch (interruptEvent.hintType) { 9641 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 9642 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 9643 console.info('Force paused. Update capturing status and stop reading'); 9644 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 9645 break; 9646 case audio.InterruptHint.INTERRUPT_HINT_STOP: 9647 // 音频流已被停止,永久失去焦点,若想恢复采集,需用户主动触发。 9648 console.info('Force stopped. Update capturing status and stop reading'); 9649 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 9650 break; 9651 default: 9652 console.info('Invalid interruptEvent'); 9653 break; 9654 } 9655 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 9656 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 9657 switch (interruptEvent.hintType) { 9658 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 9659 // 建议应用继续采集(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复采集)。 9660 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 9661 console.info('Resume force paused renderer or ignore'); 9662 // 若选择继续采集,需在此处主动执行开始采集的若干操作。 9663 break; 9664 default: 9665 console.info('Invalid interruptEvent'); 9666 break; 9667 } 9668 } 9669 }); 9670} 9671``` 9672 9673### off('audioInterrupt')<sup>10+</sup> 9674 9675off(type: 'audioInterrupt'): void 9676 9677取消监听音频中断事件。 9678 9679**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 9680 9681**参数:** 9682 9683| 参数名 | 类型 | 必填 | 说明 | 9684| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 9685| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当取消监听音频中断事件时,触发该事件。 | 9686 9687**错误码:** 9688 9689以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9690 9691| 错误码ID | 错误信息 | 9692| ------- | --------------------------------------------| 9693| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9694| 6800101 | Parameter verification failed. | 9695 9696**示例:** 9697 9698```ts 9699audioCapturer.off('audioInterrupt'); 9700``` 9701 9702### on('inputDeviceChange')<sup>11+</sup> 9703 9704on(type: 'inputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 9705 9706监听音频输入设备变化事件(当音频输入设备发生变化时触发)。使用callback异步回调。 9707 9708**系统能力:** SystemCapability.Multimedia.Audio.Device 9709 9710**参数:** 9711 9712| 参数名 | 类型 | 必填 | 说明 | 9713| :------- | :------------------------- | :--- | :------------------------------------------ | 9714| type | string | 是 | 事件回调类型,支持的事件为'inputDeviceChange',当音频输入设备发生变化时,触发该事件。 | 9715| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数,返回监听的音频输入设备变化(返回数据为切换后的设备信息)。 | 9716 9717**错误码:** 9718 9719以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9720 9721| 错误码ID | 错误信息 | 9722| ------- | --------------------------------------------| 9723| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9724| 6800101 | Parameter verification failed. | 9725 9726**示例:** 9727 9728```ts 9729audioCapturer.on('inputDeviceChange', (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 9730 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 9731 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 9732 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 9733}); 9734``` 9735### off('inputDeviceChange')<sup>11+</sup> 9736 9737off(type: 'inputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 9738 9739取消监听音频输入设备更改事件。使用callback异步回调。 9740 9741**系统能力:** SystemCapability.Multimedia.Audio.Device 9742 9743**参数:** 9744 9745| 参数名 | 类型 | 必填 | 说明 | 9746| :------- | :------------------------- | :--- |:-----------------------------------------| 9747| type | string | 是 | 事件回调类型,支持的事件为'inputDeviceChange',当取消监听音频输入设备更改事件时,触发该事件。 | 9748| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回监听的音频输入设备信息。 | 9749 9750**错误码:** 9751 9752以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9753 9754| 错误码ID | 错误信息 | 9755| ------- | --------------------------------------------| 9756| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9757| 6800101 | Parameter verification failed. | 9758 9759**示例:** 9760 9761```ts 9762// 取消该事件的所有监听。 9763audioCapturer.off('inputDeviceChange'); 9764 9765// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 9766let inputDeviceChangeCallback = (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 9767 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 9768 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 9769 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 9770}; 9771 9772audioCapturer.on('inputDeviceChange', inputDeviceChangeCallback); 9773 9774audioCapturer.off('inputDeviceChange', inputDeviceChangeCallback); 9775``` 9776 9777### on('audioCapturerChange')<sup>11+</sup> 9778 9779on(type: 'audioCapturerChange', callback: Callback\<AudioCapturerChangeInfo>): void 9780 9781监听录音流配置变化事件(当音频录制流状态变化、设备变化时触发)。使用callback异步回调。订阅内部是异步实现,是非精确回调,在录音流配置变化的同时注册回调,收到的返回结果存在变化可能性。 9782 9783**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9784 9785**参数:** 9786 9787| 参数名 | 类型 | 必填 | 说明 | 9788| :------- | :------------------------- | :--- | :------------------------------------------ | 9789| type | string | 是 | 事件回调类型,支持的事件为'audioCapturerChange',当音频录制流状态变化、设备变化时,触发该事件。 | 9790| callback | Callback\<[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)> | 是 | 回调函数,录音流配置或状态变化时返回监听的录音流当前配置和状态信息。 | 9791 9792**错误码:** 9793 9794以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9795 9796| 错误码ID | 错误信息 | 9797| ------- | --------------------------------------------| 9798| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9799| 6800101 | Parameter verification failed. | 9800 9801**示例:** 9802 9803```ts 9804audioCapturer.on('audioCapturerChange', (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 9805 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 9806 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 9807 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 9808}); 9809``` 9810 9811### off('audioCapturerChange')<sup>11+</sup> 9812 9813off(type: 'audioCapturerChange', callback?: Callback\<AudioCapturerChangeInfo>): void 9814 9815取消监听录音流配置变化事件。使用callback异步回调。 9816 9817**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9818 9819**参数:** 9820 9821| 参数名 | 类型 | 必填 | 说明 | 9822| :------- | :------------------------- | :--- | :------------------------------------------ | 9823| type | string | 是 | 事件回调类型,支持的事件为'audioCapturerChange',当取消监听录音流配置变化事件时,触发该事件。 | 9824| callback | Callback\<[AudioCapturerChangeInfo](#audiocapturerchangeinfo9)> | 否 | 回调函数,返回取消监听的录音流配置或状态变化。 | 9825 9826**错误码:** 9827 9828以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 9829 9830| 错误码ID | 错误信息 | 9831| ------- | --------------------------------------------| 9832| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 9833| 6800101 | Parameter verification failed. | 9834 9835**示例:** 9836 9837```ts 9838// 取消该事件的所有监听。 9839audioCapturer.off('audioCapturerChange'); 9840 9841// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 9842let audioCapturerChangeCallback = (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 9843 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 9844 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 9845 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 9846}; 9847 9848audioCapturer.on('audioCapturerChange', audioCapturerChangeCallback); 9849 9850audioCapturer.off('audioCapturerChange', audioCapturerChangeCallback); 9851``` 9852 9853### on('markReach')<sup>8+</sup> 9854 9855on(type: 'markReach', frame: number, callback: Callback<number>): void 9856 9857监听标记到达事件(当采集的帧数达到frame参数的值时触发,仅调用一次)。使用callback异步回调。 9858 9859如果将frame设置为100,当采集帧数到达第100帧时,系统将上报信息。 9860 9861**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9862 9863**参数:** 9864 9865| 参数名 | 类型 | 必填 | 说明 | 9866| :------- | :---------------------- | :--- | :----------------------------------------- | 9867| type | string | 是 | 事件回调类型,支持的事件为'markReach',当采集的帧数达到frame参数的值时,触发该事件。 | 9868| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 9869| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 9870 9871**示例:** 9872 9873```ts 9874audioCapturer.on('markReach', 1000, (position: number) => { 9875 if (position == 1000) { 9876 console.info('ON Triggered successfully'); 9877 } 9878}); 9879``` 9880 9881### off('markReach')<sup>8+</sup> 9882 9883off(type: 'markReach', callback?: Callback<number>): void 9884 9885取消监听标记到达事件。使用callback异步回调。 9886 9887**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9888 9889**参数:** 9890 9891| 参数名 | 类型 | 必填 | 说明 | 9892| :----- | :----- | :--- | :------------------------------------------------ | 9893| type | string | 是 | 事件回调类型,支持的事件为'markReach',当取消监听标记到达事件时,触发该事件。 | 9894| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 9895 9896**示例:** 9897 9898```ts 9899// 取消该事件的所有监听。 9900audioCapturer.off('markReach'); 9901 9902// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 9903let markReachCallback = (position: number) => { 9904 if (position == 1000) { 9905 console.info('ON Triggered successfully'); 9906 } 9907}; 9908 9909audioCapturer.on('markReach', 1000, markReachCallback); 9910 9911audioCapturer.off('markReach', markReachCallback); 9912``` 9913 9914### on('periodReach')<sup>8+</sup> 9915 9916on(type: 'periodReach', frame: number, callback: Callback<number>): void 9917 9918监听标记到达事件(当采集的帧数达到frame参数的值时触发,即按周期上报信息)。使用callback异步回调。 9919 9920如果将frame设置为10,每渲染10帧数据均会上报信息(例如:第10帧、第20帧、第30帧......)。 9921 9922**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9923 9924**参数:** 9925 9926| 参数名 | 类型 | 必填 | 说明 | 9927| :------- | :----------------------- | :--- | :------------------------------------------ | 9928| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当采集的帧数达到frame参数的值时,触发该事件。 | 9929| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 9930| callback | Callback\<number> | 是 |回调函数,返回frame参数的值。 | 9931 9932**示例:** 9933 9934```ts 9935audioCapturer.on('periodReach', 1000, (position: number) => { 9936 if (position == 1000) { 9937 console.info('ON Triggered successfully'); 9938 } 9939}); 9940``` 9941 9942### off('periodReach')<sup>8+</sup> 9943 9944off(type: 'periodReach', callback?: Callback<number>): void 9945 9946取消监听标记到达事件。使用callback异步回调。 9947 9948**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9949 9950**参数:** 9951 9952| 参数名 | 类型 | 必填 | 说明 | 9953| :----- | :----- | :--- | :-------------------------------------------------- | 9954| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当取消监听标记到达事件时,触发该事件。 | 9955| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 9956 9957**示例:** 9958 9959```ts 9960// 取消该事件的所有监听。 9961audioCapturer.off('periodReach'); 9962 9963// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 9964let periodReachCallback = (position: number) => { 9965 if (position == 1000) { 9966 console.info('ON Triggered successfully'); 9967 } 9968}; 9969 9970audioCapturer.on('periodReach', periodReachCallback); 9971 9972audioCapturer.off('periodReach', periodReachCallback); 9973``` 9974 9975### on('stateChange')<sup>8+</sup> 9976 9977on(type: 'stateChange', callback: Callback<AudioState\>): void 9978 9979监听状态变化事件(当AudioCapturer状态发生变化时触发)。使用callback异步回调。 9980 9981**系统能力:** SystemCapability.Multimedia.Audio.Capturer 9982 9983**参数:** 9984 9985| 参数名 | 类型 | 必填 | 说明 | 9986| :------- | :------------------------- | :--- | :------------------------------------------ | 9987| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当AudioCapturer状态发生变化时,触发该事件。 | 9988| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 9989 9990**示例:** 9991 9992```ts 9993audioCapturer.on('stateChange', (state: audio.AudioState) => { 9994 if (state == 1) { 9995 console.info('audio capturer state is: STATE_PREPARED'); 9996 } 9997 if (state == 2) { 9998 console.info('audio capturer state is: STATE_RUNNING'); 9999 } 10000}); 10001``` 10002 10003### off('stateChange')<sup>18+</sup> 10004 10005off(type: 'stateChange', callback?: Callback<AudioState>): void 10006 10007取消监听到达标记事件。使用callback异步回调。 10008 10009**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10010 10011**参数:** 10012 10013| 参数名 | 类型 | 必填 | 说明 | 10014| :----- | :----- | :--- | :-------------------------------------------------- | 10015| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当取消监听到达标记事件时,触发该事件。 | 10016| callback | Callback\<[AudioState](#audiostate8)> | 否 | 回调函数,返回当前音频的状态。 | 10017 10018**错误码:** 10019 10020以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 10021 10022| 错误码ID | 错误信息 | 10023| ------- | --------------------------------------------| 10024| 6800101 | Parameter verification failed. | 10025 10026**示例:** 10027 10028```ts 10029// 取消该事件的所有监听。 10030audioCapturer.off('stateChange'); 10031 10032// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 10033let stateChangeCallback = (state: audio.AudioState) => { 10034 if (state == 1) { 10035 console.info('audio renderer state is: STATE_PREPARED'); 10036 } 10037 if (state == 2) { 10038 console.info('audio renderer state is: STATE_RUNNING'); 10039 } 10040}; 10041 10042audioCapturer.on('stateChange', stateChangeCallback); 10043 10044audioCapturer.off('stateChange', stateChangeCallback); 10045``` 10046 10047### on('readData')<sup>11+</sup> 10048 10049on(type: 'readData', callback: Callback\<ArrayBuffer>): void 10050 10051监听音频数据读取回调事件(当需要读取音频流数据时触发)。使用callback异步回调。 10052 10053回调函数仅用来读取音频数据,请勿在回调函数中调用AudioCapturer相关接口。 10054 10055**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10056 10057**参数:** 10058 10059| 参数名 | 类型 | 必填 | 说明 | 10060| :------- |:-----------------------| :--- |:--------------------------| 10061| type | string | 是 | 事件回调类型,支持的事件为'readData',当需要读取音频流数据时,触发该事件。 | 10062| callback | Callback\<ArrayBuffer> | 是 | 回调函数,返回读到的数据缓冲区。 | 10063 10064**错误码:** 10065 10066以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 10067 10068| 错误码ID | 错误信息 | 10069| ------- | --------------------------------------------| 10070| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 10071| 6800101 | Parameter verification failed. | 10072 10073**示例:** 10074 10075```ts 10076import { BusinessError } from '@kit.BasicServicesKit'; 10077import { fileIo as fs } from '@kit.CoreFileKit'; 10078import { common } from '@kit.AbilityKit'; 10079 10080class Options { 10081 offset?: number; 10082 length?: number; 10083} 10084 10085let bufferSize: number = 0; 10086// 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext。 10087let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 10088let path = context.cacheDir; 10089// 确保该沙箱路径下存在该资源。 10090let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 10091let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 10092let readDataCallback = (buffer: ArrayBuffer) => { 10093 let options: Options = { 10094 offset: bufferSize, 10095 length: buffer.byteLength 10096 }; 10097 fs.writeSync(file.fd, buffer, options); 10098 bufferSize += buffer.byteLength; 10099} 10100 10101audioCapturer.on('readData', readDataCallback); 10102 10103audioCapturer.start((err: BusinessError) => { 10104 if (err) { 10105 console.error('Capturer start failed.'); 10106 } else { 10107 console.info('Capturer start success.'); 10108 } 10109}); 10110``` 10111 10112### off('readData')<sup>11+</sup> 10113 10114off(type: 'readData', callback?: Callback\<ArrayBuffer>): void 10115 10116取消监听音频数据读取回调事件。使用callback异步回调。 10117 10118**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10119 10120**参数:** 10121 10122| 参数名 | 类型 | 必填 | 说明 | 10123| :------- |:-----------------------| :--- |:-------------------------------------------| 10124| type | string | 是 | 事件回调类型,支持的事件为'readData',当取消监听音频数据读取回调事件时,触发该事件。 | 10125| callback | Callback\<ArrayBuffer> | 否 | 回调函数,返回读到的数据缓冲区。 | 10126 10127**错误码:** 10128 10129以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 10130 10131| 错误码ID | 错误信息 | 10132| ------- | --------------------------------------------| 10133| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 10134| 6800101 | Parameter verification failed. | 10135 10136**示例:** 10137 10138```ts 10139// 取消该事件的所有监听。 10140audioCapturer.off('readData'); 10141 10142// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 10143let readDataCallback = (data: ArrayBuffer) => { 10144 console.info(`read data: ${data}`); 10145}; 10146 10147audioCapturer.on('readData', readDataCallback); 10148 10149audioCapturer.off('readData', readDataCallback); 10150``` 10151 10152### getOverflowCount<sup>12+</sup> 10153 10154getOverflowCount(): Promise<number> 10155 10156获取当前录制音频流的过载音频帧数量。使用Promise异步回调。 10157 10158**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10159 10160**返回值:** 10161 10162| 类型 | 说明 | 10163| ------------------- | ----------------------------- | 10164| Promise<number>| Promise对象,返回音频流的过载音频帧数量。| 10165 10166**示例:** 10167 10168```ts 10169import { BusinessError } from '@kit.BasicServicesKit'; 10170 10171audioCapturer.getOverflowCount().then((value: number) => { 10172 console.info(`Get overflow count Success! ${value}`); 10173}).catch((err: BusinessError) => { 10174 console.error(`Get overflow count Fail: ${err}`); 10175}); 10176``` 10177 10178### getOverflowCountSync<sup>12+</sup> 10179 10180getOverflowCountSync(): number 10181 10182获取当前录制音频流的过载音频帧数量。同步返回数据。 10183 10184**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10185 10186**返回值:** 10187 10188| 类型 | 说明 | 10189| ------------------- | ----------------------------- | 10190| number| 返回音频流的过载音频帧数量。| 10191 10192**示例:** 10193 10194```ts 10195import { BusinessError } from '@kit.BasicServicesKit'; 10196 10197try { 10198 let value: number = audioCapturer.getOverflowCountSync(); 10199 console.info(`Get overflow count Success! ${value}`); 10200} catch (err) { 10201 let error = err as BusinessError; 10202 console.error(`Get overflow count Fail: ${error}`); 10203} 10204``` 10205 10206### setWillMuteWhenInterrupted<sup>20+</sup> 10207 10208setWillMuteWhenInterrupted(muteWhenInterrupted: boolean): Promise<void> 10209 10210设置当前录制音频流是否启用静音打断模式。使用Promise异步回调。 10211 10212**系统能力:** SystemCapability.Multimedia.Audio.Capturer 10213 10214**参数:** 10215 10216| 参数名 | 类型 | 必填 | 说明 | 10217| ---------- |---------------- | ------ |---------------------------------------------------------| 10218| muteWhenInterrupted | boolean | 是 | 设置当前录制音频流是否启用静音打断模式, true表示启用,false表示不启用,保持为默认打断模式。 | 10219 10220**返回值:** 10221 10222| 类型 | 说明 | 10223| ------------------- | ----------------------------- | 10224| Promise<void>| Promise对象。无返回结果的Promise对象。| 10225 10226**错误码:** 10227 10228以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 10229 10230| 错误码ID | 错误信息 | 10231| ------- | --------------------------------------------| 10232| 6800103 | Operation not permit at current state. | 10233 10234**示例:** 10235 10236```ts 10237import { BusinessError } from '@kit.BasicServicesKit'; 10238 10239audioRenderer.setWillMuteWhenInterrupted(true).then(() => { 10240 console.info('setWillMuteWhenInterrupted Success!'); 10241}).catch((err: BusinessError) => { 10242 console.error(`setWillMuteWhenInterrupted Fail: ${err}`); 10243}); 10244``` 10245 10246## ActiveDeviceType<sup>(deprecated)</sup> 10247 10248表示活跃设备类型的枚举。 10249 10250> **说明:** 10251> 10252> 从API version 7开始支持,从API version 9开始废弃,建议使用[CommunicationDeviceType](#communicationdevicetype9)替代。 10253 10254**系统能力:** SystemCapability.Multimedia.Audio.Device 10255 10256| 名称 | 值 | 说明 | 10257| ------------- | ------ | ---------------------------------------------------- | 10258| SPEAKER | 2 | 扬声器。 | 10259| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 10260 10261## InterruptActionType<sup>(deprecated)</sup> 10262 10263表示中断事件返回类型的枚举。 10264 10265> **说明:** 10266> 10267> 从API version 7开始支持,从API version 9开始废弃,无替代接口。 10268 10269**系统能力:** SystemCapability.Multimedia.Audio.Renderer 10270 10271| 名称 | 值 | 说明 | 10272| -------------- | ------ | ------------------ | 10273| TYPE_ACTIVATED | 0 | 表示触发焦点事件。 | 10274| TYPE_INTERRUPT | 1 | 表示音频打断事件。 | 10275 10276## AudioInterrupt<sup>(deprecated)</sup> 10277 10278音频监听事件传入的参数。 10279 10280> **说明:** 10281> 10282> 从API version 7开始支持,从API version 9开始废弃,无替代接口。 10283 10284**系统能力:** SystemCapability.Multimedia.Audio.Renderer 10285 10286| 名称 | 类型 | 必填 | 说明 | 10287| --------------- | --------------------------- | ----| ------------------------------------------------------------ | 10288| streamUsage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 10289| contentType | [ContentType](#contenttypedeprecated) | 是 | 音频打断媒体类型。 | 10290| pauseWhenDucked | boolean | 是 | 音频打断时是否可以暂停音频播放。true表示音频播放可以在音频打断期间暂停,false表示音频播放不可以在音频打断期间暂停。 | 10291 10292## InterruptAction<sup>(deprecated)</sup> 10293 10294音频打断/获取焦点事件的回调方法。 10295 10296> **说明:** 10297> 10298> 从API version 7开始支持,从API version 9开始废弃,建议使用[InterruptEvent](#interruptevent9)替代。 10299 10300**系统能力:** SystemCapability.Multimedia.Audio.Renderer 10301 10302| 名称 | 类型 | 必填 | 说明 | 10303| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 10304| actionType | [InterruptActionType](#interruptactiontypedeprecated) | 是 | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。 | 10305| type | [InterruptType](#interrupttype) | 否 | 打断事件类型。 | 10306| hint | [InterruptHint](#interrupthint) | 否 | 打断事件提示。 | 10307| activated | boolean | 否 | 焦点获取/释放是否成功。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 | 10308