1# @ohos.multimedia.audio (音频管理) 2 3音频管理提供管理音频的一些基础能力,包括对音频音量、音频设备的管理,以及对音频数据的采集和渲染等。 4 5该模块提供以下音频相关的常用功能: 6 7- [AudioManager](#audiomanager):音频管理。 8- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。 9- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM音频数据。 10- [TonePlayer](#toneplayer9):用于管理和播放DTMF(Dual Tone Multi Frequency,双音多频)音调,如拨号音、通话回铃音等。 11 12> **说明:** 13> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import audio from '@ohos.multimedia.audio'; 19``` 20 21## 常量 22 23| 名称 | 类型 | 可读 | 可写 | 说明 | 24| --------------------------------------- | ----------| ---- | ---- | ------------------ | 25| LOCAL_NETWORK_ID<sup>9+</sup> | string | 是 | 否 | 本地设备网络id。<br/>此接口为系统接口。<br> **系统能力:** SystemCapability.Multimedia.Audio.Device | 26| DEFAULT_VOLUME_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音量组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Volume | 27| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | 是 | 否 | 默认音频中断组id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Interrupt | 28 29**示例:** 30 31```ts 32import audio from '@ohos.multimedia.audio'; 33 34const defaultVolumeGroupId = audio.DEFAULT_VOLUME_GROUP_ID; 35const defaultInterruptGroupId = audio.DEFAULT_INTERRUPT_GROUP_ID; 36``` 37 38## audio.getAudioManager 39 40getAudioManager(): AudioManager 41 42获取音频管理器。 43 44**系统能力:** SystemCapability.Multimedia.Audio.Core 45 46**返回值:** 47 48| 类型 | 说明 | 49| ----------------------------- | ------------ | 50| [AudioManager](#audiomanager) | 音频管理对象。 | 51 52**示例:** 53```ts 54import audio from '@ohos.multimedia.audio'; 55 56let audioManager = audio.getAudioManager(); 57``` 58 59## audio.createAudioRenderer<sup>8+</sup> 60 61createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void 62 63获取音频渲染器。使用callback方式异步返回结果。 64 65**系统能力:** SystemCapability.Multimedia.Audio.Renderer 66 67**参数:** 68 69| 参数名 | 类型 | 必填 | 说明 | 70| -------- | ----------------------------------------------- | ---- | ---------------- | 71| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 72| callback | AsyncCallback<[AudioRenderer](#audiorenderer8)> | 是 | 回调函数。当获取音频渲染器成功,err为undefined,data为获取到的音频渲染器对象;否则为错误对象。 | 73 74**示例:** 75 76```ts 77import audio from '@ohos.multimedia.audio'; 78 79let audioStreamInfo: audio.AudioStreamInfo = { 80 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 81 channels: audio.AudioChannel.CHANNEL_1, 82 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 83 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 84} 85 86let audioRendererInfo: audio.AudioRendererInfo = { 87 usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 88 rendererFlags: 0 89} 90 91let audioRendererOptions: audio.AudioRendererOptions = { 92 streamInfo: audioStreamInfo, 93 rendererInfo: audioRendererInfo 94} 95 96audio.createAudioRenderer(audioRendererOptions,(err, data) => { 97 if (err) { 98 console.error(`AudioRenderer Created: Error: ${err}`); 99 } else { 100 console.info('AudioRenderer Created: Success: SUCCESS'); 101 let audioRenderer = data; 102 } 103}); 104``` 105 106## audio.createAudioRenderer<sup>8+</sup> 107 108createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\> 109 110获取音频渲染器。使用Promise方式异步返回结果。 111 112**系统能力:** SystemCapability.Multimedia.Audio.Renderer 113 114**参数:** 115 116| 参数名 | 类型 | 必填 | 说明 | 117| :------ | :--------------------------------------------- | :--- | :----------- | 118| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 119 120**返回值:** 121 122| 类型 | 说明 | 123| ----------------------------------------- | ---------------- | 124| Promise<[AudioRenderer](#audiorenderer8)> | Promise对象,返回音频渲染器对象。 | 125 126**示例:** 127 128```ts 129import audio from '@ohos.multimedia.audio'; 130import { BusinessError } from '@ohos.base'; 131 132let audioStreamInfo: audio.AudioStreamInfo = { 133 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 134 channels: audio.AudioChannel.CHANNEL_1, 135 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 136 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 137} 138 139let audioRendererInfo: audio.AudioRendererInfo = { 140 usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 141 rendererFlags: 0 142} 143 144let audioRendererOptions: audio.AudioRendererOptions = { 145 streamInfo: audioStreamInfo, 146 rendererInfo: audioRendererInfo 147} 148 149let audioRenderer: audio.AudioRenderer; 150audio.createAudioRenderer(audioRendererOptions).then((data) => { 151 audioRenderer = data; 152 console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS'); 153}).catch((err: BusinessError) => { 154 console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`); 155}); 156``` 157 158## audio.createAudioCapturer<sup>8+</sup> 159 160createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void 161 162获取音频采集器。使用callback方式异步返回结果。 163 164**系统能力:** SystemCapability.Multimedia.Audio.Capturer 165 166**需要权限:** ohos.permission.MICROPHONE 167 168仅设置Mic音频源(即[SourceType](#sourcetype)为SOURCE_TYPE_MIC)时需要该权限。 169 170**参数:** 171 172| 参数名 | 类型 | 必填 | 说明 | 173| :------- | :---------------------------------------------- | :--- | :--------------- | 174| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 175| callback | AsyncCallback<[AudioCapturer](#audiocapturer8)> | 是 | 回调函数。当获取音频采集器成功,err为undefined,data为获取到的音频采集器对象;否则为错误对象。 | 176 177**示例:** 178 179```ts 180import audio from '@ohos.multimedia.audio'; 181 182let audioStreamInfo: audio.AudioStreamInfo = { 183 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 184 channels: audio.AudioChannel.CHANNEL_2, 185 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 186 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 187} 188 189let audioCapturerInfo: audio.AudioCapturerInfo = { 190 source: audio.SourceType.SOURCE_TYPE_MIC, 191 capturerFlags: 0 192} 193 194let audioCapturerOptions: audio.AudioCapturerOptions = { 195 streamInfo: audioStreamInfo, 196 capturerInfo: audioCapturerInfo 197} 198 199audio.createAudioCapturer(audioCapturerOptions, (err, data) => { 200 if (err) { 201 console.error(`AudioCapturer Created : Error: ${err}`); 202 } else { 203 console.info('AudioCapturer Created : Success : SUCCESS'); 204 let audioCapturer = data; 205 } 206}); 207``` 208 209## audio.createAudioCapturer<sup>8+</sup> 210 211createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\> 212 213获取音频采集器。使用Promise 方式异步返回结果。 214 215**系统能力:** SystemCapability.Multimedia.Audio.Capturer 216 217**需要权限:** ohos.permission.MICROPHONE 218 219仅设置Mic音频源(即[SourceType](#sourcetype)为SOURCE_TYPE_MIC)时需要该权限。 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 224| :------ | :--------------------------------------------- | :--- | :--------------- | 225| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 226 227**返回值:** 228 229| 类型 | 说明 | 230| ----------------------------------------- | -------------- | 231| Promise<[AudioCapturer](#audiocapturer8)> | Promise对象,返回音频采集器对象。 | 232 233**示例:** 234 235```ts 236import audio from '@ohos.multimedia.audio'; 237import { BusinessError } from '@ohos.base'; 238 239let audioStreamInfo: audio.AudioStreamInfo = { 240 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 241 channels: audio.AudioChannel.CHANNEL_2, 242 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 243 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 244} 245 246let audioCapturerInfo: audio.AudioCapturerInfo = { 247 source: audio.SourceType.SOURCE_TYPE_MIC, 248 capturerFlags: 0 249} 250 251let audioCapturerOptions:audio.AudioCapturerOptions = { 252 streamInfo: audioStreamInfo, 253 capturerInfo: audioCapturerInfo 254} 255 256let audioCapturer: audio.AudioCapturer; 257audio.createAudioCapturer(audioCapturerOptions).then((data) => { 258 audioCapturer = data; 259 console.info('AudioCapturer Created : Success : Stream Type: SUCCESS'); 260}).catch((err: BusinessError) => { 261 console.error(`AudioCapturer Created : ERROR : ${err}`); 262}); 263``` 264 265## audio.createTonePlayer<sup>9+</sup> 266 267createTonePlayer(options: AudioRendererInfo, callback: AsyncCallback<TonePlayer>): void 268 269创建DTMF播放器。使用callback方式异步返回结果。 270 271**系统能力:** SystemCapability.Multimedia.Audio.Tone 272 273**系统接口:** 该接口为系统接口 274 275**参数:** 276 277| 参数名 | 类型 | 必填 | 说明 | 278| -------- | ----------------------------------------------- | ---- | -------------- | 279| options | [AudioRendererInfo](#audiorendererinfo8) | 是 | 配置音频渲染器信息。| 280| callback | AsyncCallback<[TonePlayer](#toneplayer9)> | 是 | 回调函数。当获取DTMF播放器成功,err为undefined,data为获取到的DTMF播放器对象;否则为错误对象。| 281 282**示例:** 283 284```ts 285import audio from '@ohos.multimedia.audio'; 286 287let audioRendererInfo: audio.AudioRendererInfo = { 288 usage : audio.StreamUsage.STREAM_USAGE_DTMF, 289 rendererFlags : 0 290} 291let tonePlayer: audio.TonePlayer; 292 293audio.createTonePlayer(audioRendererInfo, (err, data) => { 294 console.info(`callback call createTonePlayer: audioRendererInfo: ${audioRendererInfo}`); 295 if (err) { 296 console.error(`callback call createTonePlayer return error: ${err.message}`); 297 } else { 298 console.info(`callback call createTonePlayer return data: ${data}`); 299 tonePlayer = data; 300 } 301}); 302``` 303 304## audio.createTonePlayer<sup>9+</sup> 305 306createTonePlayer(options: AudioRendererInfo): Promise<TonePlayer> 307 308创建DTMF播放器。使用Promise方式异步返回结果。 309 310**系统能力:** SystemCapability.Multimedia.Audio.Tone 311 312**系统接口:** 该接口为系统接口 313 314**参数:** 315 316| 参数名 | 类型 | 必填 | 说明 | 317| :------ | :---------------------------------------------| :--- | :----------- | 318| options | [AudioRendererInfo](#audiorendererinfo8) | 是 | 配置音频渲染器信息。 | 319 320**返回值:** 321 322| 类型 | 说明 | 323| ----------------------------------------- | -------------------------------- | 324| Promise<[TonePlayer](#toneplayer9)> | Promise对象,返回DTMF播放器对象。 | 325 326**示例:** 327 328```ts 329import audio from '@ohos.multimedia.audio'; 330 331let tonePlayer: audio.TonePlayer; 332async function createTonePlayerBefore(){ 333 let audioRendererInfo: audio.AudioRendererInfo = { 334 usage : audio.StreamUsage.STREAM_USAGE_DTMF, 335 rendererFlags : 0 336 } 337 tonePlayer = await audio.createTonePlayer(audioRendererInfo); 338} 339``` 340 341## AudioVolumeType 342 343枚举,音频流类型。 344 345**系统能力:** SystemCapability.Multimedia.Audio.Volume 346 347| 名称 | 值 | 说明 | 348| ---------------------------- | ------ | ---------- | 349| VOICE_CALL<sup>8+</sup> | 0 | 语音电话。 | 350| RINGTONE | 2 | 铃声。 | 351| MEDIA | 3 | 媒体。 | 352| ALARM<sup>10+</sup> | 4 | 闹钟。 | 353| ACCESSIBILITY<sup>10+</sup> | 5 | 无障碍。 | 354| VOICE_ASSISTANT<sup>8+</sup> | 9 | 语音助手。 | 355| ULTRASONIC<sup>10+</sup> | 10 | 超声波。<br/>此接口为系统接口。| 356| ALL<sup>9+</sup> | 100 | 所有公共音频流。<br/>此接口为系统接口。| 357 358## InterruptRequestResultType<sup>9+</sup> 359 360枚举,音频中断请求结果类型。 361 362**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 363 364**系统接口:** 该接口为系统接口 365 366| 名称 | 值 | 说明 | 367| ---------------------------- | ------ | ---------- | 368| INTERRUPT_REQUEST_GRANT | 0 | 请求音频中断成功。 | 369| INTERRUPT_REQUEST_REJECT | 1 | 请求音频中断失败,可能具有较高优先级类型。 | 370 371## InterruptMode<sup>9+</sup> 372 373枚举,焦点模型。 374 375**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 376 377| 名称 | 值 | 说明 | 378| ---------------------------- | ------ | ---------- | 379| SHARE_MODE | 0 | 共享焦点模式。 | 380| INDEPENDENT_MODE | 1 | 独立焦点模式。 | 381 382## DeviceFlag 383 384枚举,可获取的设备种类。 385 386**系统能力:** SystemCapability.Multimedia.Audio.Device 387 388| 名称 | 值 | 说明 | 389| ------------------------------- | ------ |---------------------------| 390| NONE_DEVICES_FLAG<sup>9+</sup> | 0 | 无设备。 <br/>此接口为系统接口。 | 391| OUTPUT_DEVICES_FLAG | 1 | 输出设备。 | 392| INPUT_DEVICES_FLAG | 2 | 输入设备。 | 393| ALL_DEVICES_FLAG | 3 | 所有设备。 | 394| DISTRIBUTED_OUTPUT_DEVICES_FLAG<sup>9+</sup> | 4 | 分布式输出设备。<br/>此接口为系统接口。 | 395| DISTRIBUTED_INPUT_DEVICES_FLAG<sup>9+</sup> | 8 | 分布式输入设备。<br/>此接口为系统接口。 | 396| ALL_DISTRIBUTED_DEVICES_FLAG<sup>9+</sup> | 12 | 分布式输入和输出设备。<br/>此接口为系统接口。 | 397 398## DeviceRole 399 400枚举,设备角色。 401 402**系统能力:** SystemCapability.Multimedia.Audio.Device 403 404| 名称 | 值 | 说明 | 405| ------------- | ------ | -------------- | 406| INPUT_DEVICE | 1 | 输入设备角色。 | 407| OUTPUT_DEVICE | 2 | 输出设备角色。 | 408 409## DeviceType 410 411枚举,设备类型。 412 413**系统能力:** SystemCapability.Multimedia.Audio.Device 414 415| 名称 | 值 | 说明 | 416| ---------------------| ------ | --------------------------------------------------------- | 417| INVALID | 0 | 无效设备。 | 418| EARPIECE | 1 | 听筒。 | 419| SPEAKER | 2 | 扬声器。 | 420| WIRED_HEADSET | 3 | 有线耳机,带麦克风。 | 421| WIRED_HEADPHONES | 4 | 有线耳机,无麦克风。 | 422| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 423| BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。 | 424| MIC | 15 | 麦克风。 | 425| USB_HEADSET | 22 | USB耳机,带麦克风。 | 426| DEFAULT<sup>9+</sup> | 1000 | 默认设备类型。 | 427 428## CommunicationDeviceType<sup>9+</sup> 429 430枚举,用于通信的可用设备类型。 431 432**系统能力:** SystemCapability.Multimedia.Audio.Communication 433 434| 名称 | 值 | 说明 | 435| ------------- | ------ | -------------| 436| SPEAKER | 2 | 扬声器。 | 437 438## AudioRingMode 439 440枚举,铃声模式。 441 442**系统能力:** SystemCapability.Multimedia.Audio.Communication 443 444| 名称 | 值 | 说明 | 445| ------------------- | ------ | ---------- | 446| RINGER_MODE_SILENT | 0 | 静音模式。 | 447| RINGER_MODE_VIBRATE | 1 | 震动模式。 | 448| RINGER_MODE_NORMAL | 2 | 响铃模式。 | 449 450## AudioSampleFormat<sup>8+</sup> 451 452枚举,音频采样格式。 453 454**系统能力:** SystemCapability.Multimedia.Audio.Core 455 456| 名称 | 值 | 说明 | 457| ---------------------------------- | ------ | -------------------------- | 458| SAMPLE_FORMAT_INVALID | -1 | 无效格式。 | 459| SAMPLE_FORMAT_U8 | 0 | 无符号8位整数。 | 460| SAMPLE_FORMAT_S16LE | 1 | 带符号的16位整数,小尾数。 | 461| SAMPLE_FORMAT_S24LE | 2 | 带符号的24位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 462| SAMPLE_FORMAT_S32LE | 3 | 带符号的32位整数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 463| SAMPLE_FORMAT_F32LE<sup>9+</sup> | 4 | 带符号的32位浮点数,小尾数。 <br>由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。| 464 465## AudioErrors<sup>9+</sup> 466 467枚举,音频错误码。 468 469**系统能力:** SystemCapability.Multimedia.Audio.Core 470 471| 名称 | 值 | 说明 | 472| ---------------------| --------| ----------------- | 473| ERROR_INVALID_PARAM | 6800101 | 无效入参。 | 474| ERROR_NO_MEMORY | 6800102 | 分配内存失败。 | 475| ERROR_ILLEGAL_STATE | 6800103 | 状态不支持。 | 476| ERROR_UNSUPPORTED | 6800104 | 参数选项不支持。 | 477| ERROR_TIMEOUT | 6800105 | 处理超时。 | 478| ERROR_STREAM_LIMIT | 6800201 | 音频流数量达到限制。| 479| ERROR_SYSTEM | 6800301 | 系统处理异常。 | 480 481## AudioChannel<sup>8+</sup> 482 483枚举, 音频声道。 484 485**系统能力:** SystemCapability.Multimedia.Audio.Core 486 487| 名称 | 值 | 说明 | 488| --------- | -------- |------| 489| CHANNEL_1 | 0x1 << 0 | 单声道。 | 490| CHANNEL_2 | 0x1 << 1 | 双声道。 | 491 492## AudioSamplingRate<sup>8+</sup> 493 494枚举,音频采样率,具体设备支持的采样率规格会存在差异。 495 496**系统能力:** SystemCapability.Multimedia.Audio.Core 497 498| 名称 | 值 | 说明 | 499| ----------------- | ------ | --------------- | 500| SAMPLE_RATE_8000 | 8000 | 采样率为8000。 | 501| SAMPLE_RATE_11025 | 11025 | 采样率为11025。 | 502| SAMPLE_RATE_12000 | 12000 | 采样率为12000。 | 503| SAMPLE_RATE_16000 | 16000 | 采样率为16000。 | 504| SAMPLE_RATE_22050 | 22050 | 采样率为22050。 | 505| SAMPLE_RATE_24000 | 24000 | 采样率为24000。 | 506| SAMPLE_RATE_32000 | 32000 | 采样率为32000。 | 507| SAMPLE_RATE_44100 | 44100 | 采样率为44100。 | 508| SAMPLE_RATE_48000 | 48000 | 采样率为48000。 | 509| SAMPLE_RATE_64000 | 64000 | 采样率为64000。 | 510| SAMPLE_RATE_96000 | 96000 | 采样率为96000。 | 511 512## AudioEncodingType<sup>8+</sup> 513 514枚举,音频编码类型。 515 516**系统能力:** SystemCapability.Multimedia.Audio.Core 517 518| 名称 | 值 | 说明 | 519| --------------------- | ------ | --------- | 520| ENCODING_TYPE_INVALID | -1 | 无效。 | 521| ENCODING_TYPE_RAW | 0 | PCM编码。 | 522 523## ContentType<sup>(deprecated)</sup> 524 525枚举,音频内容类型。 526 527> **说明:** 528> 从 API version 7 开始支持,从 API version 10 开始废弃。建议使用[StreamUsage](#streamusage)替代。 529 530**系统能力:** SystemCapability.Multimedia.Audio.Core 531 532| 名称 | 值 | 说明 | 533| ---------------------------------- | ------ | ---------- | 534| CONTENT_TYPE_UNKNOWN | 0 | 未知类型。 | 535| CONTENT_TYPE_SPEECH | 1 | 语音。 | 536| CONTENT_TYPE_MUSIC | 2 | 音乐。 | 537| CONTENT_TYPE_MOVIE | 3 | 电影。 | 538| CONTENT_TYPE_SONIFICATION | 4 | 通知音。 | 539| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5 | 铃声。 | 540 541## StreamUsage 542 543枚举,音频流使用类型。 544 545**系统能力:** SystemCapability.Multimedia.Audio.Core 546 547| 名称 | 值 | 说明 | 548| ------------------------------------------| ------ |---------------------------------| 549| STREAM_USAGE_UNKNOWN | 0 | 未知类型。 | 550| 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替代。 | 551| STREAM_USAGE_MUSIC<sup>10+</sup> | 1 | 音乐。 | 552| STREAM_USAGE_VOICE_COMMUNICATION | 2 | 语音通信。 | 553| STREAM_USAGE_VOICE_ASSISTANT<sup>9+</sup> | 3 | 语音播报。 | 554| STREAM_USAGE_ALARM<sup>10+</sup> | 4 | 闹钟。 | 555| STREAM_USAGE_VOICE_MESSAGE<sup>10+</sup> | 5 | 语音消息。 | 556| STREAM_USAGE_NOTIFICATION_RINGTONE<sup>(deprecated)</sup> | 6 | 通知铃声。<br/> 从 API version 10 开始废弃。建议使用该枚举中的STREAM_USAGE_RINGTONE替代。 | 557| STREAM_USAGE_RINGTONE<sup>10+</sup> | 6 | 铃声。 | 558| STREAM_USAGE_NOTIFICATION<sup>10+</sup> | 7 | 通知。 | 559| STREAM_USAGE_ACCESSIBILITY<sup>10+</sup> | 8 | 无障碍。 | 560| STREAM_USAGE_SYSTEM<sup>10+</sup> | 9 | 系统音(如屏幕锁定或按键音)。<br/>此接口为系统接口。 | 561| STREAM_USAGE_MOVIE<sup>10+</sup> | 10 | 电影或视频。 | 562| STREAM_USAGE_GAME<sup>10+</sup> | 11 | 游戏音效。 | 563| STREAM_USAGE_AUDIOBOOK<sup>10+</sup> | 12 | 有声读物。 | 564| STREAM_USAGE_NAVIGATION<sup>10+</sup> | 13 | 导航。 | 565| STREAM_USAGE_DTMF<sup>10+</sup> | 14 | 拨号音。<br/>此接口为系统接口。 | 566| STREAM_USAGE_ENFORCED_TONE<sup>10+</sup> | 15 | 强制音(如相机快门音)。<br/>此接口为系统接口。 | 567| STREAM_USAGE_ULTRASONIC<sup>10+</sup> | 16 | 超声波(目前仅提供给MSDP使用)。<br/>此接口为系统接口。 | 568 569 570## InterruptRequestType<sup>9+</sup> 571 572枚举,音频中断请求类型。 573 574**系统接口:** 该接口为系统接口 575 576**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 577 578| 名称 | 值 | 说明 | 579| ---------------------------------- | ------ | ------------------------- | 580| INTERRUPT_REQUEST_TYPE_DEFAULT | 0 | 默认类型,可中断音频请求。 | 581 582## AudioState<sup>8+</sup> 583 584枚举,音频状态。 585 586**系统能力:** SystemCapability.Multimedia.Audio.Core 587 588| 名称 | 值 | 说明 | 589| -------------- | ------ | ---------------- | 590| STATE_INVALID | -1 | 无效状态。 | 591| STATE_NEW | 0 | 创建新实例状态。 | 592| STATE_PREPARED | 1 | 准备状态。 | 593| STATE_RUNNING | 2 | 运行状态。 | 594| STATE_STOPPED | 3 | 停止状态。 | 595| STATE_RELEASED | 4 | 释放状态。 | 596| STATE_PAUSED | 5 | 暂停状态。 | 597 598## AudioEffectMode<sup>10+</sup> 599 600枚举,音效模式。 601 602**系统能力:** SystemCapability.Multimedia.Audio.Renderer 603 604| 名称 | 值 | 说明 | 605| ------------------ | ------ | ---------- | 606| EFFECT_NONE | 0 | 关闭音效。 | 607| EFFECT_DEFAULT | 1 | 默认音效。 | 608 609## AudioRendererRate<sup>8+</sup> 610 611枚举,音频渲染速度。 612 613**系统能力:** SystemCapability.Multimedia.Audio.Renderer 614 615| 名称 | 值 | 说明 | 616| ------------------ | ------ | ---------- | 617| RENDER_RATE_NORMAL | 0 | 正常速度。 | 618| RENDER_RATE_DOUBLE | 1 | 2倍速。 | 619| RENDER_RATE_HALF | 2 | 0.5倍数。 | 620 621## InterruptType 622 623枚举,中断类型。 624 625**系统能力:** SystemCapability.Multimedia.Audio.Renderer 626 627| 名称 | 值 | 说明 | 628| -------------------- | ------ | ---------------------- | 629| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断事件开始。 | 630| INTERRUPT_TYPE_END | 2 | 音频播放中断事件结束。 | 631 632## InterruptForceType<sup>9+</sup> 633 634枚举,强制打断类型。 635 636**系统能力:** SystemCapability.Multimedia.Audio.Renderer 637 638| 名称 | 值 | 说明 | 639| --------------- | ------ | ------------------------------------ | 640| INTERRUPT_FORCE | 0 | 由系统进行操作,强制打断音频播放。 | 641| INTERRUPT_SHARE | 1 | 由应用进行操作,可以选择打断或忽略。 | 642 643## InterruptHint 644 645枚举,中断提示。 646 647**系统能力:** SystemCapability.Multimedia.Audio.Renderer 648 649| 名称 | 值 | 说明 | 650| ---------------------------------- | ------ | -------------------------------------------- | 651| INTERRUPT_HINT_NONE<sup>8+</sup> | 0 | 无提示。 | 652| INTERRUPT_HINT_RESUME | 1 | 提示音频恢复。 | 653| INTERRUPT_HINT_PAUSE | 2 | 提示音频暂停。 | 654| INTERRUPT_HINT_STOP | 3 | 提示音频停止。 | 655| INTERRUPT_HINT_DUCK | 4 | 提示音频躲避。(躲避:音量减弱,而不会停止) | 656| INTERRUPT_HINT_UNDUCK<sup>8+</sup> | 5 | 提示音量恢复。 | 657 658## AudioStreamInfo<sup>8+</sup> 659 660音频流信息。 661 662**系统能力:** SystemCapability.Multimedia.Audio.Core 663 664| 名称 | 类型 | 必填 | 说明 | 665| ------------ | ------------------------------------------------- | ---- | ------------------ | 666| samplingRate | [AudioSamplingRate](#audiosamplingrate8) | 是 | 音频文件的采样率。 | 667| channels | [AudioChannel](#audiochannel8) | 是 | 音频文件的通道数。 | 668| sampleFormat | [AudioSampleFormat](#audiosampleformat8) | 是 | 音频采样格式。 | 669| encodingType | [AudioEncodingType](#audioencodingtype8) | 是 | 音频编码格式。 | 670 671## AudioRendererInfo<sup>8+</sup> 672 673音频渲染器信息。 674 675**系统能力:** SystemCapability.Multimedia.Audio.Core 676 677| 名称 | 类型 | 必填 | 说明 | 678| ------------- | --------------------------- | ---- | ---------------- | 679| content | [ContentType](#contenttypedeprecated) | 否 | 音频内容类型。<br>API version 8、9为必填参数,从API version 10开始,变更为可选参数。 | 680| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 681| rendererFlags | number | 是 | 音频渲染器标志。<br>0代表普通音频渲染器,1代表低时延音频渲染器。ArkTS接口暂不支持低时延音频渲染器。 | 682 683## InterruptResult<sup>9+</sup> 684 685音频中断结果。 686 687**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 688 689**系统接口:** 该接口为系统接口 690 691| 名称 | 类型 | 必填 | 说明 | 692| --------------| -------------------------------------------------------------- | ---- | ---------------- | 693| requestResult | [InterruptRequestResultType](#interruptrequestresulttype9) | 是 | 表示音频请求中断类型。 | 694| interruptNode | number | 是 | 音频请求中断的节点。 | 695 696## AudioRendererOptions<sup>8+</sup> 697 698音频渲染器选项信息。 699 700| 名称 | 类型 | 必填 | 说明 | 701| ------------ | ---------------------------------------- | ---- | ---------------- | 702| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 703| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Renderer | 704| privacyType<sup>10+</sup> | [AudioPrivacyType](#audioprivacytype) | 否 | 表示音频流是否可以被其他应用录制,默认值为0。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture | 705 706## AudioPrivacyType<sup>10+</sup><a name="audioprivacytype"></a> 707 708枚举类型,用于标识对应播放音频流是否支持被其他应用录制。 709 710**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 711 712| 名称 | 值 | 说明 | 713| -------------------- | ---- | -------------------------------- | 714| PRIVACY_TYPE_PUBLIC | 0 | 表示音频流可以被其他应用录制。 | 715| PRIVACY_TYPE_PRIVATE | 1 | 表示音频流不可以被其他应用录制。 | 716 717## InterruptEvent<sup>9+</sup> 718 719播放中断时,应用接收的中断事件。 720 721**系统能力:** SystemCapability.Multimedia.Audio.Renderer 722 723| 名称 | 类型 |必填 | 说明 | 724| --------- | ------------------------------------------ | ---- | ------------------------------------ | 725| eventType | [InterruptType](#interrupttype) | 是 | 中断事件类型,开始或是结束。 | 726| forceType | [InterruptForceType](#interruptforcetype9) | 是 | 操作是由系统执行或是由应用程序执行。 | 727| hintType | [InterruptHint](#interrupthint) | 是 | 中断提示。 | 728 729## VolumeEvent<sup>9+</sup> 730 731音量改变时,应用接收的事件。 732 733**系统能力:** SystemCapability.Multimedia.Audio.Volume 734 735| 名称 | 类型 | 必填 | 说明 | 736| ---------- | ----------------------------------- | ---- |-------------------------------------------| 737| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 738| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 739| updateUi | boolean | 是 | 在UI中显示音量变化,true为显示,false为不显示。 | 740| volumeGroupId | number | 是 | 音量组id,可用于getGroupManager入参。<br/>此接口为系统接口。 | 741| networkId | string | 是 | 网络id。<br/>此接口为系统接口。 | 742 743## MicStateChangeEvent<sup>9+</sup> 744 745麦克风状态变化时,应用接收的事件。 746 747**系统能力:** SystemCapability.Multimedia.Audio.Device 748 749| 名称 | 类型 | 必填 | 说明 | 750| ---------- | ----------------------------------- | ---- |-------------------------------------------------------- | 751| mute | boolean | 是 | 回调返回系统麦克风静音状态,true为静音,false为非静音。 | 752 753## ConnectType<sup>9+</sup> 754 755枚举,设备连接类型。 756 757**系统接口:** 该接口为系统接口 758 759**系统能力:** SystemCapability.Multimedia.Audio.Volume 760 761| 名称 | 值 | 说明 | 762| :------------------------------ | :----- | :--------------------- | 763| CONNECT_TYPE_LOCAL | 1 | 本地设备。 | 764| CONNECT_TYPE_DISTRIBUTED | 2 | 分布式设备。 | 765 766## VolumeGroupInfos<sup>9+</sup> 767 768音量组信息,数组类型,为[VolumeGroupInfo](#volumegroupinfo9)的数组,只读。 769 770**系统接口:** 该接口为系统接口 771 772**系统能力:** SystemCapability.Multimedia.Audio.Volume 773 774## VolumeGroupInfo<sup>9+</sup> 775 776音量组信息。 777 778**系统接口:** 该接口为系统接口 779 780**系统能力:** SystemCapability.Multimedia.Audio.Volume 781 782| 名称 | 类型 | 可读 | 可写 | 说明 | 783| -------------------------- | -------------------------- | ---- | ---- | ---------- | 784| networkId<sup>9+</sup> | string | 是 | 否 | 组网络id。 | 785| groupId<sup>9+</sup> | number | 是 | 否 | 组设备组id。 | 786| mappingId<sup>9+</sup> | number | 是 | 否 | 组映射id。 | 787| groupName<sup>9+</sup> | string | 是 | 否 | 组名。 | 788| type<sup>9+</sup> | [ConnectType](#connecttype9)| 是 | 否 | 连接设备类型。 | 789 790## DeviceChangeAction 791 792描述设备连接状态变化和设备信息。 793 794**系统能力:** SystemCapability.Multimedia.Audio.Device 795 796| 名称 | 类型 | 必填 | 说明 | 797| :---------------- | :------------------------------------------------ | :--- | :----------------- | 798| type | [DeviceChangeType](#devicechangetype) | 是 | 设备连接状态变化。 | 799| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 设备信息。 | 800 801## DeviceChangeType 802 803枚举,设备连接状态变化。 804 805**系统能力:** SystemCapability.Multimedia.Audio.Device 806 807| 名称 | 值 | 说明 | 808| :--------- | :--- | :------------- | 809| CONNECT | 0 | 设备连接。 | 810| DISCONNECT | 1 | 断开设备连接。 | 811 812## AudioCapturerOptions<sup>8+</sup> 813 814音频采集器选项信息。 815 816| 名称 | 类型 | 必填 | 说明 | 817| ----------------------------------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 818| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 819| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo) | 是 | 表示采集器信息。 <br/>**系统能力:** SystemCapability.Multimedia.Audio.Capturer | 820| playbackCaptureConfig<sup>10+</sup> | [AudioPlaybackCaptureConfig](#audioplaybackcaptureconfig) | 否 | 音频内录的配置信息。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture | 821 822## AudioCapturerInfo<sup>8+</sup><a name="audiocapturerinfo"></a> 823 824描述音频采集器信息。 825 826**系统能力:** SystemCapability.Multimedia.Audio.Core 827 828| 名称 | 类型 | 必填 | 说明 | 829| :------------ | :------------------------ | :--- | :--------------- | 830| source | [SourceType](#sourcetype) | 是 | 音源类型。 | 831| capturerFlags | number | 是 | 音频采集器标志。<br>0代表普通音频采集器,1代表低时延音频采集器。ArkTS接口暂不支持低时延音频采集器。 | 832 833## SourceType<sup>8+</sup><a name="sourcetype"></a> 834 835枚举,音源类型。 836 837| 名称 | 值 | 说明 | 838| :------------------------------------------- | :----- | :--------------------- | 839| SOURCE_TYPE_INVALID | -1 | 无效的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 840| SOURCE_TYPE_MIC | 0 | Mic音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 841| SOURCE_TYPE_VOICE_RECOGNITION<sup>9+</sup> | 1 | 语音识别源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 842| SOURCE_TYPE_PLAYBACK_CAPTURE<sup>10+</sup> | 2 | 播放音频流(内录)录制音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture | 843| SOURCE_TYPE_WAKEUP <sup>10+</sup> | 3 | 语音唤醒音频流录制音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core <br/>**需要权限:** ohos.permission.MANAGE_INTELLIGENT_VOICE <br/> 此接口为系统接口| 844| SOURCE_TYPE_VOICE_COMMUNICATION | 7 | 语音通话场景的音频源。<br/>**系统能力:** SystemCapability.Multimedia.Audio.Core | 845 846## AudioPlaybackCaptureConfig<sup>10+</sup><a name="audioplaybackcaptureconfig"></a> 847 848播放音频流录制(内录)的配置信息。 849 850**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 851 852| 名称 | 类型 | 必填 | 说明 | 853| ------------- | --------------------------------------------- | ---- | -------------------------------- | 854| filterOptions | [CaptureFilterOptions](#capturefilteroptions) | 是 | 需要录制的播放音频流的筛选信息。 | 855 856## CaptureFilterOptions<sup>10+</sup><a name="capturefilteroptions"></a> 857 858待录制的播放音频流的筛选信息。 859 860**需要权限:** ohos.permission.CAPTURE_VOICE_DOWNLINK_AUDIO 861 862当应用指定录制的StreamUsage值中包含SOURCE_TYPE_VOICE_COMMUNICATION的播放音频流时,需要校验应用是否拥有该权限。 863 864**系统能力:** SystemCapability.Multimedia.Audio.PlaybackCapture 865 866| 名称 | 类型 | 必填 | 说明 | 867| ------ | ---------------------------------- | ---- | ------------------------------------------------------------ | 868| usages | Array<[StreamUsage](#streamusage)> | 是 | 指定需要录制的播放音频流的[StreamUsage](#streamusage)类型。可同时指定0个或多个StreamUsage。Array为空时,默认录制StreamUsage为STREAM_USAGE_MEDIA的播放音频流。 | 869 870## AudioScene<sup>8+</sup><a name="audioscene"></a> 871 872枚举,音频场景。 873 874**系统能力:** SystemCapability.Multimedia.Audio.Communication 875 876| 名称 | 值 | 说明 | 877| :--------------------- | :----- | :-------------------------------------------- | 878| AUDIO_SCENE_DEFAULT | 0 | 默认音频场景。 | 879| AUDIO_SCENE_RINGING | 1 | 响铃模式。<br/>此接口为系统接口。 | 880| AUDIO_SCENE_PHONE_CALL | 2 | 电话模式。<br/>此接口为系统接口。 | 881| AUDIO_SCENE_VOICE_CHAT | 3 | 语音聊天模式。 | 882 883## VolumeAdjustType<sup>10+</sup> 884 885枚举,音量调节类型。 886 887**系统接口:** 该接口为系统接口 888 889**系统能力:** SystemCapability.Multimedia.Audio.Volume 890 891| 名称 | 值 | 说明 | 892| :--------------------- | :----- | :-------------------------------------------- | 893| VOLUME_UP | 0 | 向上调节音量。<br/>此接口为系统接口。 | 894| VOLUME_DOWN | 1 | 向下调节音量。<br/>此接口为系统接口。 | 895 896## AudioManager 897 898管理音频音量和音频设备。在调用AudioManager的接口前,需要先通过[getAudioManager](#audiogetaudiomanager)创建实例。 899 900### setAudioParameter 901 902setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void 903 904音频参数设置,使用callback方式异步返回结果。 905 906本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 907 908**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 909 910**系统能力:** SystemCapability.Multimedia.Audio.Core 911 912**参数:** 913 914| 参数名 | 类型 | 必填 | 说明 | 915| -------- | ------------------------- | ---- | ------------------------ | 916| key | string | 是 | 被设置的音频参数的键。 | 917| value | string | 是 | 被设置的音频参数的值。 | 918| callback | AsyncCallback<void> | 是 | 回调函数。当音频参数设置成功,err为undefined,否则为错误对象。 | 919 920**示例:** 921 922```ts 923import { BusinessError } from '@ohos.base'; 924 925audioManager.setAudioParameter('key_example', 'value_example', (err: BusinessError) => { 926 if (err) { 927 console.error(`Failed to set the audio parameter. ${err}`); 928 return; 929 } 930 console.info('Callback invoked to indicate a successful setting of the audio parameter.'); 931}); 932``` 933 934### setAudioParameter 935 936setAudioParameter(key: string, value: string): Promise<void> 937 938音频参数设置,使用Promise方式异步返回结果。 939 940本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 941 942**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 943 944**系统能力:** SystemCapability.Multimedia.Audio.Core 945 946**参数:** 947 948| 参数名 | 类型 | 必填 | 说明 | 949| ------ | ------ | ---- | ---------------------- | 950| key | string | 是 | 被设置的音频参数的键。 | 951| value | string | 是 | 被设置的音频参数的值。 | 952 953**返回值:** 954 955| 类型 | 说明 | 956| ------------------- | ------------------------------- | 957| Promise<void> | Promise对象,无返回结果。 | 958 959**示例:** 960 961```ts 962audioManager.setAudioParameter('key_example', 'value_example').then(() => { 963 console.info('Promise returned to indicate a successful setting of the audio parameter.'); 964}); 965``` 966 967### getAudioParameter 968 969getAudioParameter(key: string, callback: AsyncCallback<string>): void 970 971获取指定音频参数值,使用callback方式异步返回结果。 972 973本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 974 975**系统能力:** SystemCapability.Multimedia.Audio.Core 976 977**参数:** 978 979| 参数名 | 类型 | 必填 | 说明 | 980| -------- | --------------------------- | ---- | ---------------------------- | 981| key | string | 是 | 待获取的音频参数的键。 | 982| callback | AsyncCallback<string> | 是 | 回调函数。当获取指定音频参数值成功,err为undefined,data为获取到的指定音频参数值;否则为错误对象。 | 983 984**示例:** 985 986```ts 987import { BusinessError } from '@ohos.base'; 988 989audioManager.getAudioParameter('key_example', (err: BusinessError, value: string) => { 990 if (err) { 991 console.error(`Failed to obtain the value of the audio parameter. ${err}`); 992 return; 993 } 994 console.info(`Callback invoked to indicate that the value of the audio parameter is obtained ${value}.`); 995}); 996``` 997 998### getAudioParameter 999 1000getAudioParameter(key: string): Promise<string> 1001 1002获取指定音频参数值,使用Promise方式异步返回结果。 1003 1004本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1005 1006**系统能力:** SystemCapability.Multimedia.Audio.Core 1007 1008**参数:** 1009 1010| 参数名 | 类型 | 必填 | 说明 | 1011| ------ | ------ | ---- | ---------------------- | 1012| key | string | 是 | 待获取的音频参数的键。 | 1013 1014**返回值:** 1015 1016| 类型 | 说明 | 1017| --------------------- | ----------------------------------- | 1018| Promise<string> | Promise对象,返回获取的音频参数的值。 | 1019 1020**示例:** 1021 1022```ts 1023audioManager.getAudioParameter('key_example').then((value: string) => { 1024 console.info(`Promise returned to indicate that the value of the audio parameter is obtained ${value}.`); 1025}); 1026``` 1027 1028### setAudioScene<sup>8+</sup> 1029 1030setAudioScene\(scene: AudioScene, callback: AsyncCallback<void\>\): void 1031 1032设置音频场景模式,使用callback方式异步返回结果。 1033 1034**系统接口:** 该接口为系统接口 1035 1036**系统能力:** SystemCapability.Multimedia.Audio.Communication 1037 1038**参数:** 1039 1040| 参数名 | 类型 | 必填 | 说明 | 1041| :------- | :----------------------------------- | :--- | :------------------- | 1042| scene | <a href="#audioscene">AudioScene</a> | 是 | 音频场景模式。 | 1043| callback | AsyncCallback<void\> | 是 | 回调函数。当设置音频场景模式成功,err为undefined,否则为错误对象。 | 1044 1045**示例:** 1046 1047```ts 1048import { BusinessError } from '@ohos.base'; 1049 1050audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL, (err: BusinessError) => { 1051 if (err) { 1052 console.error(`Failed to set the audio scene mode. ${err}`); 1053 return; 1054 } 1055 console.info('Callback invoked to indicate a successful setting of the audio scene mode.'); 1056}); 1057``` 1058 1059### setAudioScene<sup>8+</sup> 1060 1061setAudioScene\(scene: AudioScene\): Promise<void\> 1062 1063设置音频场景模式,使用Promise方式返回异步结果。 1064 1065**系统接口:** 该接口为系统接口 1066 1067**系统能力:** SystemCapability.Multimedia.Audio.Communication 1068 1069**参数:** 1070 1071| 参数名 | 类型 | 必填 | 说明 | 1072| :----- | :----------------------------------- | :--- | :------------- | 1073| scene | <a href="#audioscene">AudioScene</a> | 是 | 音频场景模式。 | 1074 1075**返回值:** 1076 1077| 类型 | 说明 | 1078| :------------- | :------------------- | 1079| Promise<void\> | Promise对象,无返回结果。 | 1080 1081**示例:** 1082 1083```ts 1084import { BusinessError } from '@ohos.base'; 1085 1086audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL).then(() => { 1087 console.info('Promise returned to indicate a successful setting of the audio scene mode.'); 1088}).catch ((err: BusinessError) => { 1089 console.error(`Failed to set the audio scene mode ${err}`); 1090}); 1091``` 1092 1093### getAudioScene<sup>8+</sup> 1094 1095getAudioScene\(callback: AsyncCallback<AudioScene\>\): void 1096 1097获取音频场景模式,使用callback方式返回异步结果。 1098 1099**系统能力:** SystemCapability.Multimedia.Audio.Communication 1100 1101**参数:** 1102 1103| 参数名 | 类型 | 必填 | 说明 | 1104| :------- | :-------------------------------------------------- | :--- | :--------------------------- | 1105| callback | AsyncCallback<<a href="#audioscene">AudioScene</a>> | 是 | 回调函数。当获取音频场景模式成功,err为undefined,data为获取到的音频场景模式;否则为错误对象。 | 1106 1107**示例:** 1108 1109```ts 1110import { BusinessError } from '@ohos.base'; 1111 1112audioManager.getAudioScene((err: BusinessError, value: audio.AudioScene) => { 1113 if (err) { 1114 console.error(`Failed to obtain the audio scene mode. ${err}`); 1115 return; 1116 } 1117 console.info(`Callback invoked to indicate that the audio scene mode is obtained ${value}.`); 1118}); 1119``` 1120 1121### getAudioScene<sup>8+</sup> 1122 1123getAudioScene\(\): Promise<AudioScene\> 1124 1125获取音频场景模式,使用Promise方式返回异步结果。 1126 1127**系统能力:** SystemCapability.Multimedia.Audio.Communication 1128 1129**返回值:** 1130 1131| 类型 | 说明 | 1132| :-------------------------------------------- | :--------------------------- | 1133| Promise<<a href="#audioscene">AudioScene</a>> | Promise对象,返回音频场景模式。 | 1134 1135**示例:** 1136 1137```ts 1138import { BusinessError } from '@ohos.base'; 1139 1140audioManager.getAudioScene().then((value: audio.AudioScene) => { 1141 console.info(`Promise returned to indicate that the audio scene mode is obtained ${value}.`); 1142}).catch ((err: BusinessError) => { 1143 console.error(`Failed to obtain the audio scene mode ${err}`); 1144}); 1145``` 1146 1147### getAudioSceneSync<sup>10+</sup> 1148 1149getAudioSceneSync\(\): AudioScene 1150 1151获取音频场景模式,同步返回结果。 1152 1153**系统能力:** SystemCapability.Multimedia.Audio.Communication 1154 1155**返回值:** 1156 1157| 类型 | 说明 | 1158| :-------------------------------------------- | :--------------------------- | 1159| <a href="#audioscene">AudioScene</a> | 音频场景模式。 | 1160 1161**示例:** 1162 1163```ts 1164import { BusinessError } from '@ohos.base'; 1165 1166try { 1167 let value: audio.AudioScene = audioManager.getAudioSceneSync(); 1168 console.info(`indicate that the audio scene mode is obtained ${value}.`); 1169} catch (err) { 1170 let error = err as BusinessError; 1171 console.error(`Failed to obtain the audio scene mode ${error}`); 1172} 1173``` 1174 1175### getVolumeManager<sup>9+</sup> 1176 1177getVolumeManager(): AudioVolumeManager 1178 1179获取音频音量管理器。 1180 1181**系统能力:** SystemCapability.Multimedia.Audio.Volume 1182 1183**返回值:** 1184 1185| 类型 | 说明 | 1186|-----------------------------------------| ----------------------------- | 1187| [AudioVolumeManager](#audiovolumemanager9) | AudioVolumeManager实例 | 1188 1189**示例:** 1190 1191```ts 1192import audio from '@ohos.multimedia.audio'; 1193 1194let audioVolumeManager: audio.AudioVolumeManager = audioManager.getVolumeManager(); 1195``` 1196 1197### getStreamManager<sup>9+</sup> 1198 1199getStreamManager(): AudioStreamManager 1200 1201获取音频流管理器。 1202 1203**系统能力:** SystemCapability.Multimedia.Audio.Core 1204 1205**返回值:** 1206 1207| 类型 | 说明 | 1208|--------------------------------------------| ----------------------------- | 1209| [AudioStreamManager](#audiostreammanager9) | AudioStreamManager实例 | 1210 1211**示例:** 1212 1213```ts 1214import audio from '@ohos.multimedia.audio'; 1215 1216let audioStreamManager: audio.AudioStreamManager = audioManager.getStreamManager(); 1217``` 1218 1219### getRoutingManager<sup>9+</sup> 1220 1221getRoutingManager(): AudioRoutingManager 1222 1223获取音频路由设备管理器。 1224 1225**系统能力:** SystemCapability.Multimedia.Audio.Device 1226 1227**返回值:** 1228 1229| 类型 | 说明 | 1230|------------------------------------------| ----------------------------- | 1231| [AudioRoutingManager](#audioroutingmanager9) | AudioRoutingManager实例 | 1232 1233**示例:** 1234 1235```ts 1236import audio from '@ohos.multimedia.audio'; 1237 1238let audioRoutingManager: audio.AudioRoutingManager = audioManager.getRoutingManager(); 1239``` 1240 1241### setVolume<sup>(deprecated)</sup> 1242 1243setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void 1244 1245设置指定流的音量,使用callback方式异步返回结果。 1246 1247> **说明:** 1248> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[setVolume](#setvolume9)替代,替代接口能力仅对系统应用开放。 1249 1250**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1251 1252仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1253 1254**系统能力:** SystemCapability.Multimedia.Audio.Volume 1255 1256**参数:** 1257 1258| 参数名 | 类型 | 必填 | 说明 | 1259| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1260| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1261| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 1262| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定流的音量成功,err为undefined,否则为错误对象。 | 1263 1264**示例:** 1265 1266```ts 1267import { BusinessError } from '@ohos.base'; 1268 1269audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err: BusinessError) => { 1270 if (err) { 1271 console.error(`Failed to set the volume. ${err}`); 1272 return; 1273 } 1274 console.info('Callback invoked to indicate a successful volume setting.'); 1275}); 1276``` 1277 1278### setVolume<sup>(deprecated)</sup> 1279 1280setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> 1281 1282设置指定流的音量,使用Promise方式异步返回结果。 1283 1284> **说明:** 1285> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[setVolume](#setvolume9)替代,替代接口能力仅对系统应用开放。 1286 1287**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1288 1289仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 1290 1291**系统能力:** SystemCapability.Multimedia.Audio.Volume 1292 1293**参数:** 1294 1295| 参数名 | 类型 | 必填 | 说明 | 1296| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1297| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1298| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 1299 1300**返回值:** 1301 1302| 类型 | 说明 | 1303| ------------------- | ----------------------------- | 1304| Promise<void> | Promise对象,无返回结果。 | 1305 1306**示例:** 1307 1308```ts 1309audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => { 1310 console.info('Promise returned to indicate a successful volume setting.'); 1311}); 1312``` 1313 1314### getVolume<sup>(deprecated)</sup> 1315 1316getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1317 1318获取指定流的音量,使用callback方式异步返回结果。 1319 1320> **说明:** 1321> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getVolume](#getvolume9)替代。 1322 1323**系统能力:** SystemCapability.Multimedia.Audio.Volume 1324 1325**参数:** 1326 1327| 参数名 | 类型 | 必填 | 说明 | 1328| ---------- | ----------------------------------- | ---- | ------------------ | 1329| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1330| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。 | 1331 1332**示例:** 1333 1334```ts 1335import { BusinessError } from '@ohos.base'; 1336 1337audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1338 if (err) { 1339 console.error(`Failed to obtain the volume. ${err}`); 1340 return; 1341 } 1342 console.info('Callback invoked to indicate that the volume is obtained.'); 1343}); 1344``` 1345 1346### getVolume<sup>(deprecated)</sup> 1347 1348getVolume(volumeType: AudioVolumeType): Promise<number> 1349 1350获取指定流的音量,使用Promise方式异步返回结果。 1351 1352> **说明:** 1353> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getVolume](#getvolume9)替代。 1354 1355**系统能力:** SystemCapability.Multimedia.Audio.Volume 1356 1357**参数:** 1358 1359| 参数名 | 类型 | 必填 | 说明 | 1360| ---------- | ----------------------------------- | ---- | ------------ | 1361| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1362 1363**返回值:** 1364 1365| 类型 | 说明 | 1366| --------------------- | ------------------------- | 1367| Promise<number> | Promise对象,返回音量大小。 | 1368 1369**示例:** 1370 1371```ts 1372audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1373 console.info(`Promise returned to indicate that the volume is obtained ${value} .`); 1374}); 1375``` 1376 1377### getMinVolume<sup>(deprecated)</sup> 1378 1379getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1380 1381获取指定流的最小音量,使用callback方式异步返回结果。 1382 1383> **说明:** 1384> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMinVolume](#getminvolume9)替代。 1385 1386**系统能力:** SystemCapability.Multimedia.Audio.Volume 1387 1388**参数:** 1389 1390| 参数名 | 类型 | 必填 | 说明 | 1391| ---------- | ----------------------------------- | ---- | ------------------ | 1392| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1393| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 1394 1395**示例:** 1396 1397```ts 1398import { BusinessError } from '@ohos.base'; 1399 1400audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1401 if (err) { 1402 console.error(`Failed to obtain the minimum volume. ${err}`); 1403 return; 1404 } 1405 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 1406}); 1407``` 1408 1409### getMinVolume<sup>(deprecated)</sup> 1410 1411getMinVolume(volumeType: AudioVolumeType): Promise<number> 1412 1413获取指定流的最小音量,使用Promise方式异步返回结果。 1414 1415> **说明:** 1416> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMinVolume](#getminvolume9)替代。 1417 1418**系统能力:** SystemCapability.Multimedia.Audio.Volume 1419 1420**参数:** 1421 1422| 参数名 | 类型 | 必填 | 说明 | 1423| ---------- | ----------------------------------- | ---- | ------------ | 1424| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1425 1426**返回值:** 1427 1428| 类型 | 说明 | 1429| --------------------- | ------------------------- | 1430| Promise<number> | Promise对象,返回最小音量。 | 1431 1432**示例:** 1433 1434```ts 1435audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 1436 console.info(`Promised returned to indicate that the minimum volume is obtained. ${value}`); 1437}); 1438``` 1439 1440### getMaxVolume<sup>(deprecated)</sup> 1441 1442getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 1443 1444获取指定流的最大音量,使用callback方式异步返回结果。 1445 1446> **说明:** 1447> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMaxVolume](#getmaxvolume9)替代。 1448 1449**系统能力:** SystemCapability.Multimedia.Audio.Volume 1450 1451**参数:** 1452 1453| 参数名 | 类型 | 必填 | 说明 | 1454| ---------- | ----------------------------------- | ---- | ---------------------- | 1455| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1456| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 1457 1458**示例:** 1459 1460```ts 1461import { BusinessError } from '@ohos.base'; 1462 1463audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 1464 if (err) { 1465 console.error(`Failed to obtain the maximum volume. ${err}`); 1466 return; 1467 } 1468 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 1469}); 1470``` 1471 1472### getMaxVolume<sup>(deprecated)</sup> 1473 1474getMaxVolume(volumeType: AudioVolumeType): Promise<number> 1475 1476获取指定流的最大音量,使用Promise方式异步返回结果。 1477 1478> **说明:** 1479> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getMaxVolume](#getmaxvolume9)替代。 1480 1481**系统能力:** SystemCapability.Multimedia.Audio.Volume 1482 1483**参数:** 1484 1485| 参数名 | 类型 | 必填 | 说明 | 1486| ---------- | ----------------------------------- | ---- | ------------ | 1487| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1488 1489**返回值:** 1490 1491| 类型 | 说明 | 1492| --------------------- | ----------------------------- | 1493| Promise<number> | Promise对象,返回最大音量。 | 1494 1495**示例:** 1496 1497```ts 1498audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 1499 console.info('Promised returned to indicate that the maximum volume is obtained.'); 1500}); 1501``` 1502 1503### mute<sup>(deprecated)</sup> 1504 1505mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void 1506 1507设置指定音量流静音,使用callback方式异步返回结果。 1508 1509> **说明:** 1510> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[mute](#mute9)替代,替代接口能力仅对系统应用开放。 1511 1512**系统能力:** SystemCapability.Multimedia.Audio.Volume 1513 1514**参数:** 1515 1516| 参数名 | 类型 | 必填 | 说明 | 1517| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1518| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1519| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 1520| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定音量流静音成功,err为undefined,否则为错误对象。 | 1521 1522**示例:** 1523 1524```ts 1525import { BusinessError } from '@ohos.base'; 1526 1527audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err: BusinessError) => { 1528 if (err) { 1529 console.error(`Failed to mute the stream. ${err}`); 1530 return; 1531 } 1532 console.info('Callback invoked to indicate that the stream is muted.'); 1533}); 1534``` 1535 1536### mute<sup>(deprecated)</sup> 1537 1538mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> 1539 1540设置指定音量流静音,使用Promise方式异步返回结果。 1541 1542> **说明:** 1543> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[mute](#mute9)替代,替代接口能力仅对系统应用开放。 1544 1545**系统能力:** SystemCapability.Multimedia.Audio.Volume 1546 1547**参数:** 1548 1549| 参数名 | 类型 | 必填 | 说明 | 1550| ---------- | ----------------------------------- | ---- | ------------------------------------- | 1551| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1552| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 1553 1554**返回值:** 1555 1556| 类型 | 说明 | 1557| ------------------- | ----------------------------- | 1558| Promise<void> | Promise对象,无返回结果。 | 1559 1560**示例:** 1561 1562 1563```ts 1564audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => { 1565 console.info('Promise returned to indicate that the stream is muted.'); 1566}); 1567``` 1568 1569### isMute<sup>(deprecated)</sup> 1570 1571isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1572 1573获取指定音量流是否被静音,使用callback方式异步返回结果。 1574 1575> **说明:** 1576> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMute](#ismute9)替代。 1577 1578**系统能力:** SystemCapability.Multimedia.Audio.Volume 1579 1580**参数:** 1581 1582| 参数名 | 类型 | 必填 | 说明 | 1583| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 1584| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1585| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否被静音成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 1586 1587**示例:** 1588 1589```ts 1590import { BusinessError } from '@ohos.base'; 1591 1592audioManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1593 if (err) { 1594 console.error(`Failed to obtain the mute status. ${err}`); 1595 return; 1596 } 1597 console.info(`Callback invoked to indicate that the mute status of the stream is obtained. ${value}`); 1598}); 1599``` 1600 1601### isMute<sup>(deprecated)</sup> 1602 1603isMute(volumeType: AudioVolumeType): Promise<boolean> 1604 1605获取指定音量流是否被静音,使用Promise方式异步返回结果。 1606 1607> **说明:** 1608> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMute](#ismute9)替代。 1609 1610**系统能力:** SystemCapability.Multimedia.Audio.Volume 1611 1612**参数:** 1613 1614| 参数名 | 类型 | 必填 | 说明 | 1615| ---------- | ----------------------------------- | ---- | ------------ | 1616| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1617 1618**返回值:** 1619 1620| 类型 | 说明 | 1621| ---------------------- | ------------------------------------------------------ | 1622| Promise<boolean> | Promise对象,返回流静音状态,true为静音,false为非静音。 | 1623 1624**示例:** 1625 1626```ts 1627audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1628 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 1629}); 1630``` 1631 1632### isActive<sup>(deprecated)</sup> 1633 1634isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 1635 1636获取指定音量流是否为活跃状态,使用callback方式异步返回结果。 1637 1638> **说明:** 1639> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioStreamManager中的[isActive](#isactive9)替代。 1640 1641**系统能力:** SystemCapability.Multimedia.Audio.Volume 1642 1643**参数:** 1644 1645| 参数名 | 类型 | 必填 | 说明 | 1646| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 1647| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1648| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否为活跃状态成功,err为undefined,data为true为活跃,false为不活跃;否则为错误对象。 | 1649 1650**示例:** 1651 1652```ts 1653import { BusinessError } from '@ohos.base'; 1654 1655audioManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 1656 if (err) { 1657 console.error(`Failed to obtain the active status of the stream. ${err}`); 1658 return; 1659 } 1660 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 1661}); 1662``` 1663 1664### isActive<sup>(deprecated)</sup> 1665 1666isActive(volumeType: AudioVolumeType): Promise<boolean> 1667 1668获取指定音量流是否为活跃状态,使用Promise方式异步返回结果。 1669 1670> **说明:** 1671> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioStreamManager中的[isActive](#isactive9)替代。 1672 1673**系统能力:** SystemCapability.Multimedia.Audio.Volume 1674 1675**参数:** 1676 1677| 参数名 | 类型 | 必填 | 说明 | 1678| ---------- | ----------------------------------- | ---- | ------------ | 1679| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1680 1681**返回值:** 1682 1683| 类型 | 说明 | 1684| ---------------------- | -------------------------------------------------------- | 1685| Promise<boolean> | Promise对象,返回流的活跃状态,true为活跃,false为不活跃。 | 1686 1687**示例:** 1688 1689```ts 1690audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 1691 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 1692}); 1693``` 1694 1695### setRingerMode<sup>(deprecated)</sup> 1696 1697setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void 1698 1699设置铃声模式,使用callback方式异步返回结果。 1700 1701> **说明:** 1702> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[setRingerMode](#setringermode9)替代,替代接口能力仅对系统应用开放。 1703 1704**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1705 1706仅在静音和非静音状态切换时需要该权限。 1707 1708**系统能力:** SystemCapability.Multimedia.Audio.Communication 1709 1710**参数:** 1711 1712| 参数名 | 类型 | 必填 | 说明 | 1713| -------- | ------------------------------- | ---- | ------------------------ | 1714| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1715| callback | AsyncCallback<void> | 是 | 回调函数。当设置铃声模式成功,err为undefined,否则为错误对象。 | 1716 1717**示例:** 1718 1719```ts 1720import { BusinessError } from '@ohos.base'; 1721 1722audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err: BusinessError) => { 1723 if (err) { 1724 console.error(`Failed to set the ringer mode. ${err}`); 1725 return; 1726 } 1727 console.info('Callback invoked to indicate a successful setting of the ringer mode.'); 1728}); 1729``` 1730 1731### setRingerMode<sup>(deprecated)</sup> 1732 1733setRingerMode(mode: AudioRingMode): Promise<void> 1734 1735设置铃声模式,使用Promise方式异步返回结果。 1736 1737> **说明:** 1738> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用AudioVolumeGroupManager中的[setRingerMode](#setringermode9)替代,替代接口能力仅对系统应用开放。 1739 1740 1741**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 1742 1743仅在静音和非静音状态切换时需要该权限。 1744 1745**系统能力:** SystemCapability.Multimedia.Audio.Communication 1746 1747**参数:** 1748 1749| 参数名 | 类型 | 必填 | 说明 | 1750| ------ | ------------------------------- | ---- | -------------- | 1751| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1752 1753**返回值:** 1754 1755| 类型 | 说明 | 1756| ------------------- | ------------------------------- | 1757| Promise<void> | Promise对象,无返回结果。 | 1758 1759**示例:** 1760 1761```ts 1762audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => { 1763 console.info('Promise returned to indicate a successful setting of the ringer mode.'); 1764}); 1765``` 1766 1767### getRingerMode<sup>(deprecated)</sup> 1768 1769getRingerMode(callback: AsyncCallback<AudioRingMode>): void 1770 1771获取铃声模式,使用callback方式异步返回结果。 1772 1773> **说明:** 1774> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getRingerMode](#getringermode9)替代。 1775 1776**系统能力:** SystemCapability.Multimedia.Audio.Communication 1777 1778**参数:** 1779 1780| 参数名 | 类型 | 必填 | 说明 | 1781| -------- | ---------------------------------------------------- | ---- | ------------------------ | 1782| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 1783 1784**示例:** 1785 1786```ts 1787import { BusinessError } from '@ohos.base'; 1788 1789audioManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 1790 if (err) { 1791 console.error(`Failed to obtain the ringer mode. ${err}`); 1792 return; 1793 } 1794 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 1795}); 1796``` 1797 1798### getRingerMode<sup>(deprecated)</sup> 1799 1800getRingerMode(): Promise<AudioRingMode> 1801 1802获取铃声模式,使用Promise方式异步返回结果。 1803 1804> **说明:** 1805> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[getRingerMode](#getringermode9)替代。 1806 1807**系统能力:** SystemCapability.Multimedia.Audio.Communication 1808 1809**返回值:** 1810 1811| 类型 | 说明 | 1812| ---------------------------------------------- | ------------------------------- | 1813| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 1814 1815**示例:** 1816 1817```ts 1818audioManager.getRingerMode().then((value: audio.AudioRingMode) => { 1819 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 1820}); 1821``` 1822 1823### getDevices<sup>(deprecated)</sup> 1824 1825getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 1826 1827获取音频设备列表,使用callback方式异步返回结果。 1828 1829> **说明:** 1830> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[getDevices](#getdevices9)替代。 1831 1832**系统能力:** SystemCapability.Multimedia.Audio.Device 1833 1834**参数:** 1835 1836| 参数名 | 类型 | 必填 | 说明 | 1837| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 1838| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1839| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 1840 1841**示例:** 1842```ts 1843import { BusinessError } from '@ohos.base'; 1844 1845audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 1846 if (err) { 1847 console.error(`Failed to obtain the device list. ${err}`); 1848 return; 1849 } 1850 console.info('Callback invoked to indicate that the device list is obtained.'); 1851}); 1852``` 1853 1854### getDevices<sup>(deprecated)</sup> 1855 1856getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 1857 1858获取音频设备列表,使用Promise方式异步返回结果。 1859 1860> **说明:** 1861> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[getDevices](#getdevices9)替代。 1862 1863**系统能力:** SystemCapability.Multimedia.Audio.Device 1864 1865**参数:** 1866 1867| 参数名 | 类型 | 必填 | 说明 | 1868| ---------- | ------------------------- | ---- | ---------------- | 1869| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1870 1871**返回值:** 1872 1873| 类型 | 说明 | 1874| ------------------------------------------------------------ | ------------------------- | 1875| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 1876 1877**示例:** 1878 1879```ts 1880audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 1881 console.info('Promise returned to indicate that the device list is obtained.'); 1882}); 1883``` 1884 1885### setDeviceActive<sup>(deprecated)</sup> 1886 1887setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void 1888 1889设置设备激活状态,使用callback方式异步返回结果。 1890 1891> **说明:** 1892> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[setCommunicationDevice](#setcommunicationdevice9)替代。 1893 1894**系统能力:** SystemCapability.Multimedia.Audio.Device 1895 1896**参数:** 1897 1898| 参数名 | 类型 | 必填 | 说明 | 1899| ---------- | ------------------------------------- | ---- |-------------| 1900| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1901| active | boolean | 是 | 设备激活状态。 | 1902| callback | AsyncCallback<void> | 是 | 回调函数。当设置设备激活状态成功,err为undefined,否则为错误对象。 | 1903 1904**示例:** 1905 1906```ts 1907import { BusinessError } from '@ohos.base'; 1908 1909audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err: BusinessError) => { 1910 if (err) { 1911 console.error(`Failed to set the active status of the device. ${err}`); 1912 return; 1913 } 1914 console.info('Callback invoked to indicate that the device is set to the active status.'); 1915}); 1916``` 1917 1918### setDeviceActive<sup>(deprecated)</sup> 1919 1920setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void> 1921 1922设置设备激活状态,使用Promise方式异步返回结果。 1923 1924> **说明:** 1925> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[setCommunicationDevice](#setcommunicationdevice9)替代。 1926 1927**系统能力:** SystemCapability.Multimedia.Audio.Device 1928 1929**参数:** 1930 1931| 参数名 | 类型 | 必填 | 说明 | 1932| ---------- | ------------------------------------- | ---- | ------------------ | 1933| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1934| active | boolean | 是 | 设备激活状态。 | 1935 1936**返回值:** 1937 1938| 类型 | 说明 | 1939| ------------------- | ------------------------------- | 1940| Promise<void> | Promise对象,无返回结果。 | 1941 1942**示例:** 1943 1944 1945```ts 1946audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => { 1947 console.info('Promise returned to indicate that the device is set to the active status.'); 1948}); 1949``` 1950 1951### isDeviceActive<sup>(deprecated)</sup> 1952 1953isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void 1954 1955获取指定设备的激活状态,使用callback方式异步返回结果。 1956 1957> **说明:** 1958> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 1959 1960**系统能力:** SystemCapability.Multimedia.Audio.Device 1961 1962**参数:** 1963 1964| 参数名 | 类型 | 必填 | 说明 | 1965| ---------- | ------------------------------------- | ---- | ------------------------ | 1966| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1967| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定设备的激活状态成功,err为undefined,data为true为激活,false为未激活;否则为错误对象。 | 1968 1969**示例:** 1970 1971```ts 1972import { BusinessError } from '@ohos.base'; 1973 1974audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 1975 if (err) { 1976 console.error(`Failed to obtain the active status of the device. ${err}`); 1977 return; 1978 } 1979 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 1980}); 1981``` 1982 1983### isDeviceActive<sup>(deprecated)</sup> 1984 1985isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean> 1986 1987获取指定设备的激活状态,使用Promise方式异步返回结果。 1988 1989> **说明:** 1990> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[isCommunicationDeviceActive](#iscommunicationdeviceactive9)替代。 1991 1992**系统能力:** SystemCapability.Multimedia.Audio.Device 1993 1994**参数:** 1995 1996| 参数名 | 类型 | 必填 | 说明 | 1997| ---------- | ------------------------------------- | ---- | ------------------ | 1998| deviceType | [ActiveDeviceType](#activedevicetypedeprecated) | 是 | 活跃音频设备类型。 | 1999 2000**返回值:** 2001 2002| Type | Description | 2003| ---------------------- |---------------------------------------| 2004| Promise<boolean> | Promise对象,返回设备的激活状态,true激活,false未激活。 | 2005 2006**示例:** 2007 2008```ts 2009audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value: boolean) => { 2010 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 2011}); 2012``` 2013 2014### setMicrophoneMute<sup>(deprecated)</sup> 2015 2016setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 2017 2018设置麦克风静音状态,使用callback方式异步返回结果。 2019 2020> **说明:** 2021> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[setMicrophoneMute](#setmicrophonemute9)替代。 2022 2023**需要权限:** ohos.permission.MICROPHONE 2024 2025**系统能力:** SystemCapability.Multimedia.Audio.Device 2026 2027**参数:** 2028 2029| 参数名 | 类型 | 必填 | 说明 | 2030| -------- | ------------------------- | ---- | --------------------------------------------- | 2031| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 2032| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 2033 2034**示例:** 2035 2036```ts 2037import { BusinessError } from '@ohos.base'; 2038 2039audioManager.setMicrophoneMute(true, (err: BusinessError) => { 2040 if (err) { 2041 console.error(`Failed to mute the microphone. ${err}`); 2042 return; 2043 } 2044 console.info('Callback invoked to indicate that the microphone is muted.'); 2045}); 2046``` 2047 2048### setMicrophoneMute<sup>(deprecated)</sup> 2049 2050setMicrophoneMute(mute: boolean): Promise<void> 2051 2052设置麦克风静音状态,使用Promise方式异步返回结果。 2053 2054> **说明:** 2055> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[setMicrophoneMute](#setmicrophonemute9)替代。 2056 2057**需要权限:** ohos.permission.MICROPHONE 2058 2059**系统能力:** SystemCapability.Multimedia.Audio.Device 2060 2061**参数:** 2062 2063| 参数名 | 类型 | 必填 | 说明 | 2064| ------ | ------- | ---- | --------------------------------------------- | 2065| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 2066 2067**返回值:** 2068 2069| 类型 | 说明 | 2070| ------------------- | ------------------------------- | 2071| Promise<void> | Promise对象,无返回结果。 | 2072 2073**示例:** 2074 2075```ts 2076audioManager.setMicrophoneMute(true).then(() => { 2077 console.info('Promise returned to indicate that the microphone is muted.'); 2078}); 2079``` 2080 2081### isMicrophoneMute<sup>(deprecated)</sup> 2082 2083isMicrophoneMute(callback: AsyncCallback<boolean>): void 2084 2085获取麦克风静音状态,使用callback方式异步返回结果。 2086 2087> **说明:** 2088> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMicrophoneMute](#ismicrophonemute9)替代。 2089 2090**需要权限:** ohos.permission.MICROPHONE 2091 2092**系统能力:** SystemCapability.Multimedia.Audio.Device 2093 2094**参数:** 2095 2096| 参数名 | 类型 | 必填 | 说明 | 2097| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 2098| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 2099 2100**示例:** 2101 2102```ts 2103import { BusinessError } from '@ohos.base'; 2104 2105audioManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 2106 if (err) { 2107 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 2108 return; 2109 } 2110 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 2111}); 2112``` 2113 2114### isMicrophoneMute<sup>(deprecated)</sup> 2115 2116isMicrophoneMute(): Promise<boolean> 2117 2118获取麦克风静音状态,使用Promise方式异步返回结果。 2119 2120> **说明:** 2121> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[isMicrophoneMute](#ismicrophonemute9)替代。 2122 2123**需要权限:** ohos.permission.MICROPHONE 2124 2125**系统能力:** SystemCapability.Multimedia.Audio.Device 2126 2127**返回值:** 2128 2129| 类型 | 说明 | 2130| ---------------------- | ------------------------------------------------------------ | 2131| Promise<boolean> | Promise对象,返回系统麦克风静音状态,true为静音,false为非静音。 | 2132 2133**示例:** 2134 2135```ts 2136audioManager.isMicrophoneMute().then((value: boolean) => { 2137 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 2138}); 2139``` 2140 2141### on('volumeChange')<sup>(deprecated)</sup> 2142 2143on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void 2144 2145> **说明:** 2146> 从 API version 8 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeManager中的[on('volumeChange')](#onvolumechange9)替代。 2147 2148监听系统音量变化事件,使用callback方式返回结果。 2149 2150**系统接口:** 该接口为系统接口 2151 2152目前此订阅接口在单进程多AudioManager实例的使用场景下,仅最后一个实例的订阅生效,其他实例的订阅会被覆盖(即使最后一个实例没有进行订阅),因此推荐使用单一AudioManager实例进行开发。 2153 2154**系统能力:** SystemCapability.Multimedia.Audio.Volume 2155 2156**参数:** 2157 2158| 参数名 | 类型 | 必填 | 说明 | 2159| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2160| type | string | 是 | 事件回调类型,支持的事件为:'volumeChange'(系统音量变化事件,检测到系统音量改变时,触发该事件)。 | 2161| callback | Callback<[VolumeEvent](#volumeevent9)> | 是 | 回调函数,返回变化后的音量信息。 | 2162 2163**示例:** 2164 2165```ts 2166audioManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => { 2167 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2168 console.info(`Volume level: ${volumeEvent.volume} `); 2169 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2170}); 2171``` 2172 2173### on('ringerModeChange')<sup>(deprecated)</sup> 2174 2175on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void 2176 2177监听铃声模式变化事件,使用callback方式返回结果。 2178 2179> **说明:** 2180> 从 API version 8 开始支持,从 API version 9 开始废弃,建议使用AudioVolumeGroupManager中的[on('ringerModeChange')](#onringermodechange9)替代。 2181 2182**系统接口:** 该接口为系统接口 2183 2184**系统能力:** SystemCapability.Multimedia.Audio.Communication 2185 2186**参数:** 2187 2188| 参数名 | 类型 | 必填 | 说明 | 2189| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2190| type | string | 是 | 事件回调类型,支持的事件为:'ringerModeChange'(铃声模式变化事件,检测到铃声模式改变时,触发该事件)。 | 2191| callback | Callback<[AudioRingMode](#audioringmode)> | 是 | 回调函数,返回变化后的铃音模式。 | 2192 2193**示例:** 2194 2195```ts 2196audioManager.on('ringerModeChange', (ringerMode: audio.AudioRingMode) => { 2197 console.info(`Updated ringermode: ${ringerMode}`); 2198}); 2199``` 2200 2201### on('deviceChange')<sup>(deprecated)</sup> 2202 2203on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void 2204 2205设备更改。音频设备连接状态变化,使用callback方式返回结果。 2206 2207> **说明:** 2208> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[on('deviceChange')](#ondevicechange9)替代。 2209 2210**系统能力:** SystemCapability.Multimedia.Audio.Device 2211 2212**参数:** 2213 2214| 参数名 | 类型 | 必填 | 说明 | 2215| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 2216| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 2217| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 2218 2219**示例:** 2220 2221```ts 2222audioManager.on('deviceChange', (deviceChanged: audio.DeviceChangeAction) => { 2223 console.info(`device change type : ${deviceChanged.type} `); 2224 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `); 2225 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `); 2226 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `); 2227}); 2228``` 2229 2230### off('deviceChange')<sup>(deprecated)</sup> 2231 2232off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 2233 2234取消订阅音频设备连接变化事件,使用callback方式返回结果。 2235 2236> **说明:** 2237> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用AudioRoutingManager中的[off('deviceChange')](#offdevicechange9)替代。 2238 2239**系统能力:** SystemCapability.Multimedia.Audio.Device 2240 2241**参数:** 2242 2243| 参数名 | 类型 | 必填 | 说明 | 2244| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 2245| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 2246| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 2247 2248**示例:** 2249 2250```ts 2251audioManager.off('deviceChange'); 2252``` 2253 2254### on('interrupt') 2255 2256on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback\<InterruptAction>): void 2257 2258请求焦点并开始监听音频打断事件(当应用程序的音频被另一个播放事件中断,回调通知此应用程序),使用callback方式返回结果。 2259 2260与[on('audioInterrupt')](#onaudiointerrupt9)作用一致,均用于监听焦点变化。为无音频流的场景(未曾创建AudioRenderer对象),比如FM、语音唤醒等提供焦点变化监听功能。 2261 2262**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2263 2264**参数:** 2265 2266| 参数名 | 类型 | 必填 | 说明 | 2267| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2268| type | string | 是 | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 | 2269| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2270| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 是 | 回调函数,返回音频打断时,应用接收的中断事件信息。 | 2271 2272**示例:** 2273 2274```ts 2275import audio from '@ohos.multimedia.audio'; 2276 2277let interAudioInterrupt: audio.AudioInterrupt = { 2278 streamUsage:2, 2279 contentType:0, 2280 pauseWhenDucked:true 2281}; 2282audioManager.on('interrupt', interAudioInterrupt, (InterruptAction: audio.InterruptAction) => { 2283 if (InterruptAction.actionType === 0) { 2284 console.info('An event to gain the audio focus starts.'); 2285 console.info(`Focus gain event: ${InterruptAction} `); 2286 } 2287 if (InterruptAction.actionType === 1) { 2288 console.info('An audio interruption event starts.'); 2289 console.info(`Audio interruption event: ${InterruptAction} `); 2290 } 2291}); 2292``` 2293 2294### off('interrupt') 2295 2296off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback\<InterruptAction>): void 2297 2298取消监听音频打断事件(删除监听事件,取消打断),使用callback方式返回结果。 2299 2300**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2301 2302**参数:** 2303 2304| 参数名 | 类型 | 必填 | 说明 | 2305| --------- |---------------------------------------------------------| ---- | ------------------------------------------------------------ | 2306| type | string | 是 | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 | 2307| interrupt | [AudioInterrupt](#audiointerruptdeprecated) | 是 | 音频打断事件类型的参数。 | 2308| callback | Callback<[InterruptAction](#interruptactiondeprecated)> | 否 | 回调函数,返回删除监听事件,取消打断时,应用接收的中断事件信息。 | 2309 2310**示例:** 2311 2312```ts 2313import audio from '@ohos.multimedia.audio'; 2314 2315let interAudioInterrupt: audio.AudioInterrupt = { 2316 streamUsage:2, 2317 contentType:0, 2318 pauseWhenDucked:true 2319}; 2320audioManager.off('interrupt', interAudioInterrupt, (InterruptAction: audio.InterruptAction) => { 2321 if (InterruptAction.actionType === 0) { 2322 console.info('An event to release the audio focus starts.'); 2323 console.info(`Focus release event: ${InterruptAction} `); 2324 } 2325}); 2326``` 2327 2328## AudioVolumeManager<sup>9+</sup> 2329 2330音量管理。在使用AudioVolumeManager的接口前,需要使用[getVolumeManager](#getvolumemanager9)获取AudioVolumeManager实例。 2331 2332### getVolumeGroupInfos<sup>9+</sup> 2333 2334getVolumeGroupInfos(networkId: string, callback: AsyncCallback<VolumeGroupInfos\>\): void 2335 2336获取音量组信息列表,使用callback方式异步返回结果。 2337 2338**系统接口:** 该接口为系统接口 2339 2340**系统能力:** SystemCapability.Multimedia.Audio.Volume 2341 2342**参数:** 2343 2344| 参数名 | 类型 | 必填 | 说明 | 2345| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 2346| networkId | string | 是 | 设备的网络id。本地设备audio.LOCAL_NETWORK_ID。 | 2347| callback | AsyncCallback<[VolumeGroupInfos](#volumegroupinfos9)> | 是 | 回调函数。当获取音量组信息列表成功,err为undefined,data为获取到的音量组信息列表;否则为错误对象。 | 2348 2349**示例:** 2350```ts 2351import { BusinessError } from '@ohos.base'; 2352 2353audioVolumeManager.getVolumeGroupInfos(audio.LOCAL_NETWORK_ID, (err: BusinessError, value: audio.VolumeGroupInfos) => { 2354 if (err) { 2355 console.error(`Failed to obtain the volume group infos list. ${err}`); 2356 return; 2357 } 2358 console.info('Callback invoked to indicate that the volume group infos list is obtained.'); 2359}); 2360``` 2361 2362### getVolumeGroupInfos<sup>9+</sup> 2363 2364getVolumeGroupInfos(networkId: string\): Promise<VolumeGroupInfos\> 2365 2366获取音量组信息列表,使用Promise方式异步返回结果。 2367 2368**系统接口:** 该接口为系统接口 2369 2370**系统能力:** SystemCapability.Multimedia.Audio.Volume 2371 2372**参数:** 2373 2374| 参数名 | 类型 | 必填 | 说明 | 2375| ---------- | ------------------| ---- | -------------------- | 2376| networkId | string | 是 | 设备的网络id。本地设备audio.LOCAL_NETWORK_ID。 | 2377 2378**返回值:** 2379 2380| 类型 | 说明 | 2381| ------------------- | ----------------------------- | 2382| Promise<[VolumeGroupInfos](#volumegroupinfos9)> | Promise对象,返回音量组信息列表。 | 2383 2384**示例:** 2385 2386```ts 2387async function getVolumeGroupInfos(){ 2388 let volumegroupinfos: audio.VolumeGroupInfos = await audio.getAudioManager().getVolumeManager().getVolumeGroupInfos(audio.LOCAL_NETWORK_ID); 2389 console.info('Promise returned to indicate that the volumeGroup list is obtained.'+JSON.stringify(volumegroupinfos)) 2390} 2391``` 2392 2393### getVolumeGroupInfosSync<sup>10+</sup> 2394 2395getVolumeGroupInfosSync(networkId: string\): VolumeGroupInfos 2396 2397获取音量组信息列表,同步返回结果。 2398 2399**系统接口:** 该接口为系统接口 2400 2401**系统能力:** SystemCapability.Multimedia.Audio.Volume 2402 2403**参数:** 2404 2405| 参数名 | 类型 | 必填 | 说明 | 2406| ---------- | ------------------| ---- | -------------------- | 2407| networkId | string | 是 | 设备的网络id。本地设备audio.LOCAL_NETWORK_ID。 | 2408 2409**返回值:** 2410 2411| 类型 | 说明 | 2412| ------------------- | ----------------------------- | 2413| [VolumeGroupInfos](#volumegroupinfos9) | 音量组信息列表。 | 2414 2415**错误码:** 2416 2417以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2418 2419| 错误码ID | 错误信息 | 2420| ------- | --------------------------------------------| 2421| 6800101 | invalid parameter error | 2422 2423**示例:** 2424 2425```ts 2426import { BusinessError } from '@ohos.base'; 2427 2428try { 2429 let volumegroupinfos: audio.VolumeGroupInfos = audioVolumeManager.getVolumeGroupInfosSync(audio.LOCAL_NETWORK_ID); 2430 console.info(`Indicate that the volumeGroup list is obtained. ${JSON.stringify(volumegroupinfos)}`); 2431} catch (err) { 2432 let error = err as BusinessError; 2433 console.error(`Failed to obtain the volumeGroup list ${error}`); 2434} 2435``` 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,默认使用LOCAL_VOLUME_GROUP_ID。 | 2450| callback | AsyncCallback<[AudioVolumeGroupManager](#audiovolumegroupmanager9)> | 是 | 回调函数。当获取音频组管理器成功,err为undefined,data为获取到的音频组管理器对象;否则为错误对象。 | 2451 2452**示例:** 2453 2454```ts 2455import { BusinessError } from '@ohos.base'; 2456 2457let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2458audioVolumeManager.getVolumeGroupManager(groupId, (err: BusinessError, value: audio.AudioVolumeGroupManager) => { 2459 if (err) { 2460 console.error(`Failed to obtain the volume group infos list. ${err}`); 2461 return; 2462 } 2463 console.info('Callback invoked to indicate that the volume group infos list is obtained.'); 2464}); 2465 2466``` 2467 2468### getVolumeGroupManager<sup>9+</sup> 2469 2470getVolumeGroupManager(groupId: number\): Promise<AudioVolumeGroupManager\> 2471 2472获取音频组管理器,使用Promise方式异步返回结果。 2473 2474**系统能力:** SystemCapability.Multimedia.Audio.Volume 2475 2476**参数:** 2477 2478| 参数名 | 类型 | 必填 | 说明 | 2479| ---------- | ---------------------------------------- | ---- |----------------------------------| 2480| groupId | number | 是 | 音量组id,默认使用LOCAL_VOLUME_GROUP_ID。 | 2481 2482**返回值:** 2483 2484| 类型 | 说明 | 2485| ------------------- | ----------------------------- | 2486| Promise< [AudioVolumeGroupManager](#audiovolumegroupmanager9) > | Promise对象,返回音量组实例。 | 2487 2488**示例:** 2489 2490```ts 2491import audio from '@ohos.multimedia.audio'; 2492 2493let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; 2494let audioVolumeGroupManager: audio.AudioVolumeGroupManager | undefined = undefined; 2495async function getVolumeGroupManager(){ 2496 audioVolumeGroupManager = await audioVolumeManager.getVolumeGroupManager(groupId); 2497 console.info('Promise returned to indicate that the volume group infos list is obtained.'); 2498} 2499``` 2500 2501### getVolumeGroupManagerSync<sup>10+</sup> 2502 2503getVolumeGroupManagerSync(groupId: number\): AudioVolumeGroupManager 2504 2505获取音频组管理器,同步返回结果。 2506 2507**系统能力:** SystemCapability.Multimedia.Audio.Volume 2508 2509**参数:** 2510 2511| 参数名 | 类型 | 必填 | 说明 | 2512| ---------- | ---------------------------------------- | ---- |----------------------------------| 2513| groupId | number | 是 | 音量组id,默认使用LOCAL_VOLUME_GROUP_ID。 | 2514 2515**返回值:** 2516 2517| 类型 | 说明 | 2518| ------------------- | ----------------------------- | 2519| [AudioVolumeGroupManager](#audiovolumegroupmanager9) | 音量组实例。 | 2520 2521**错误码:** 2522 2523以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2524 2525| 错误码ID | 错误信息 | 2526| ------- | --------------------------------------------| 2527| 6800101 | invalid parameter error | 2528 2529**示例:** 2530 2531```ts 2532import { BusinessError } from '@ohos.base'; 2533 2534try { 2535 let audioVolumeGroupManager: audio.AudioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(audio.DEFAULT_VOLUME_GROUP_ID); 2536 console.info(`Get audioVolumeGroupManager success.`); 2537} catch (err) { 2538 let error = err as BusinessError; 2539 console.error(`Failed to get audioVolumeGroupManager, error: ${error}`); 2540} 2541``` 2542 2543### on('volumeChange')<sup>9+</sup> 2544 2545on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void 2546 2547监听系统音量变化事件,使用callback方式返回结果。 2548 2549**系统能力:** SystemCapability.Multimedia.Audio.Volume 2550 2551**参数:** 2552 2553| 参数名 | 类型 | 必填 | 说明 | 2554| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 2555| type | string | 是 | 事件回调类型,支持的事件为:'volumeChange'。 | 2556| callback | Callback<[VolumeEvent](#volumeevent9)> | 是 | 回调函数,返回变化后的音量信息。 | 2557 2558**错误码:** 2559 2560以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2561 2562| 错误码ID | 错误信息 | 2563| ------- | --------------------------------------------| 2564| 6800101 | if input parameter value error | 2565 2566**示例:** 2567 2568```ts 2569audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => { 2570 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 2571 console.info(`Volume level: ${volumeEvent.volume} `); 2572 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 2573}); 2574``` 2575 2576## AudioVolumeGroupManager<sup>9+</sup> 2577 2578管理音频组音量。在调用AudioVolumeGroupManager的接口前,需要先通过 [getVolumeGroupManager](#getvolumegroupmanager9) 创建实例。 2579 2580### setVolume<sup>9+</sup> 2581 2582setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void 2583 2584设置指定流的音量,使用callback方式异步返回结果。 2585 2586**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 2587 2588仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 2589 2590**系统接口:** 该接口为系统接口 2591 2592**系统能力:** SystemCapability.Multimedia.Audio.Volume 2593 2594**参数:** 2595 2596| 参数名 | 类型 | 必填 | 说明 | 2597| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 2598| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2599| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 2600| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定流的音量成功,err为undefined,否则为错误对象。 | 2601 2602**示例:** 2603 2604```ts 2605import { BusinessError } from '@ohos.base'; 2606 2607audioVolumeGroupManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err: BusinessError) => { 2608 if (err) { 2609 console.error(`Failed to set the volume. ${err}`); 2610 return; 2611 } 2612 console.info('Callback invoked to indicate a successful volume setting.'); 2613}); 2614``` 2615 2616### setVolume<sup>9+</sup> 2617 2618setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> 2619 2620设置指定流的音量,使用Promise方式异步返回结果。 2621 2622**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 2623 2624仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 2625 2626**系统接口:** 该接口为系统接口 2627 2628**系统能力:** SystemCapability.Multimedia.Audio.Volume 2629 2630**参数:** 2631 2632| 参数名 | 类型 | 必填 | 说明 | 2633| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 2634| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2635| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 2636 2637**返回值:** 2638 2639| 类型 | 说明 | 2640| ------------------- | ----------------------------- | 2641| Promise<void> | Promise对象,无返回结果。 | 2642 2643**示例:** 2644 2645```ts 2646audioVolumeGroupManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => { 2647 console.info('Promise returned to indicate a successful volume setting.'); 2648}); 2649``` 2650 2651### getVolume<sup>9+</sup> 2652 2653getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2654 2655获取指定流的音量,使用callback方式异步返回结果。 2656 2657**系统能力:** SystemCapability.Multimedia.Audio.Volume 2658 2659**参数:** 2660 2661| 参数名 | 类型 | 必填 | 说明 | 2662| ---------- | ----------------------------------- | ---- | ------------------ | 2663| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2664| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的音量成功,err为undefined,data为获取到的指定流的音量;否则为错误对象。 | 2665 2666**示例:** 2667 2668```ts 2669import { BusinessError } from '@ohos.base'; 2670 2671audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2672 if (err) { 2673 console.error(`Failed to obtain the volume. ${err}`); 2674 return; 2675 } 2676 console.info('Callback invoked to indicate that the volume is obtained.'); 2677}); 2678``` 2679 2680### getVolume<sup>9+</sup> 2681 2682getVolume(volumeType: AudioVolumeType): Promise<number> 2683 2684获取指定流的音量,使用Promise方式异步返回结果。 2685 2686**系统能力:** SystemCapability.Multimedia.Audio.Volume 2687 2688**参数:** 2689 2690| 参数名 | 类型 | 必填 | 说明 | 2691| ---------- | ----------------------------------- | ---- | ------------ | 2692| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2693 2694**返回值:** 2695 2696| 类型 | 说明 | 2697| --------------------- | ------------------------- | 2698| Promise<number> | Promise对象,返回音量大小。 | 2699 2700**示例:** 2701 2702```ts 2703audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2704 console.info(`Promise returned to indicate that the volume is obtained ${value}.`); 2705}); 2706``` 2707 2708### getVolumeSync<sup>10+</sup> 2709 2710getVolumeSync(volumeType: AudioVolumeType): number; 2711 2712获取指定流的音量,同步返回结果。 2713 2714**系统能力:** SystemCapability.Multimedia.Audio.Volume 2715 2716**参数:** 2717 2718| 参数名 | 类型 | 必填 | 说明 | 2719| ---------- | ----------------------------------- | ---- | ------------ | 2720| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2721 2722**返回值:** 2723 2724| 类型 | 说明 | 2725| --------------------- | ------------------------- | 2726| number | 返回音量大小。 | 2727 2728**错误码:** 2729 2730以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2731 2732| 错误码ID | 错误信息 | 2733| ------- | --------------------------------------------| 2734| 6800101 | invalid parameter error | 2735 2736**示例:** 2737 2738```ts 2739import { BusinessError } from '@ohos.base'; 2740 2741try { 2742 let value: number = audioVolumeGroupManager.getVolumeSync(audio.AudioVolumeType.MEDIA); 2743 console.info(`Indicate that the volume is obtained ${value}.`); 2744} catch (err) { 2745 let error = err as BusinessError; 2746 console.error(`Failed to obtain the volume, error ${error}.`); 2747} 2748``` 2749 2750### getMinVolume<sup>9+</sup> 2751 2752getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2753 2754获取指定流的最小音量,使用callback方式异步返回结果。 2755 2756**系统能力:** SystemCapability.Multimedia.Audio.Volume 2757 2758**参数:** 2759 2760| 参数名 | 类型 | 必填 | 说明 | 2761| ---------- | ----------------------------------- | ---- | ------------------ | 2762| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2763| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最小音量成功,err为undefined,data为获取到的指定流的最小音量;否则为错误对象。 | 2764 2765**示例:** 2766 2767```ts 2768import { BusinessError } from '@ohos.base'; 2769 2770audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2771 if (err) { 2772 console.error(`Failed to obtain the minimum volume. ${err}`); 2773 return; 2774 } 2775 console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`); 2776}); 2777``` 2778 2779### getMinVolume<sup>9+</sup> 2780 2781getMinVolume(volumeType: AudioVolumeType): Promise<number> 2782 2783获取指定流的最小音量,使用Promise方式异步返回结果。 2784 2785**系统能力:** SystemCapability.Multimedia.Audio.Volume 2786 2787**参数:** 2788 2789| 参数名 | 类型 | 必填 | 说明 | 2790| ---------- | ----------------------------------- | ---- | ------------ | 2791| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2792 2793**返回值:** 2794 2795| 类型 | 说明 | 2796| --------------------- | ------------------------- | 2797| Promise<number> | Promise对象,返回最小音量。 | 2798 2799**示例:** 2800 2801```ts 2802audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => { 2803 console.info(`Promised returned to indicate that the minimum volume is obtained ${value}.`); 2804}); 2805``` 2806 2807### getMinVolumeSync<sup>10+</sup> 2808 2809getMinVolumeSync(volumeType: AudioVolumeType): number; 2810 2811获取指定流的最小音量,同步返回结果。 2812 2813**系统能力:** SystemCapability.Multimedia.Audio.Volume 2814 2815**参数:** 2816 2817| 参数名 | 类型 | 必填 | 说明 | 2818| ---------- | ----------------------------------- | ---- | ------------ | 2819| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2820 2821**返回值:** 2822 2823| 类型 | 说明 | 2824| --------------------- | ------------------------- | 2825| number | 返回最小音量。 | 2826 2827**错误码:** 2828 2829以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2830 2831| 错误码ID | 错误信息 | 2832| ------- | --------------------------------------------| 2833| 6800101 | invalid parameter error | 2834 2835**示例:** 2836 2837```ts 2838import { BusinessError } from '@ohos.base'; 2839 2840try { 2841 let value: number = audioVolumeGroupManager.getMinVolumeSync(audio.AudioVolumeType.MEDIA); 2842 console.info(`Indicate that the minimum volume is obtained ${value}.`); 2843} catch (err) { 2844 let error = err as BusinessError; 2845 console.error(`Failed to obtain the minimum volume, error ${error}.`); 2846} 2847``` 2848 2849### getMaxVolume<sup>9+</sup> 2850 2851getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 2852 2853获取指定流的最大音量,使用callback方式异步返回结果。 2854 2855**系统能力:** SystemCapability.Multimedia.Audio.Volume 2856 2857**参数:** 2858 2859| 参数名 | 类型 | 必填 | 说明 | 2860| ---------- | ----------------------------------- | ---- | ---------------------- | 2861| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2862| callback | AsyncCallback<number> | 是 | 回调函数。当获取指定流的最大音量成功,err为undefined,data为获取到的指定流的最大音量;否则为错误对象。 | 2863 2864**示例:** 2865 2866```ts 2867import { BusinessError } from '@ohos.base'; 2868 2869audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => { 2870 if (err) { 2871 console.error(`Failed to obtain the maximum volume. ${err}`); 2872 return; 2873 } 2874 console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`); 2875}); 2876``` 2877 2878### getMaxVolume<sup>9+</sup> 2879 2880getMaxVolume(volumeType: AudioVolumeType): Promise<number> 2881 2882获取指定流的最大音量,使用Promise方式异步返回结果。 2883 2884**系统能力:** SystemCapability.Multimedia.Audio.Volume 2885 2886**参数:** 2887 2888| 参数名 | 类型 | 必填 | 说明 | 2889| ---------- | ----------------------------------- | ---- | ------------ | 2890| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2891 2892**返回值:** 2893 2894| 类型 | 说明 | 2895| --------------------- | ----------------------------- | 2896| Promise<number> | Promise对象,返回最大音量大小。 | 2897 2898**示例:** 2899 2900```ts 2901audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data: number) => { 2902 console.info('Promised returned to indicate that the maximum volume is obtained.'); 2903}); 2904``` 2905 2906### getMaxVolumeSync<sup>10+</sup> 2907 2908getMaxVolumeSync(volumeType: AudioVolumeType): number; 2909 2910获取指定流的最大音量,同步返回结果。 2911 2912**系统能力:** SystemCapability.Multimedia.Audio.Volume 2913 2914**参数:** 2915 2916| 参数名 | 类型 | 必填 | 说明 | 2917| ---------- | ----------------------------------- | ---- | ------------ | 2918| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2919 2920**返回值:** 2921 2922| 类型 | 说明 | 2923| --------------------- | ----------------------------- | 2924| number | 返回最大音量大小。 | 2925 2926**错误码:** 2927 2928以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 2929 2930| 错误码ID | 错误信息 | 2931| ------- | --------------------------------------------| 2932| 6800101 | invalid parameter error | 2933 2934**示例:** 2935 2936```ts 2937import { BusinessError } from '@ohos.base'; 2938 2939try { 2940 let value: number = audioVolumeGroupManager.getMaxVolumeSync(audio.AudioVolumeType.MEDIA); 2941 console.info(`Indicate that the maximum volume is obtained. ${value}`); 2942} catch (err) { 2943 let error = err as BusinessError; 2944 console.error(`Failed to obtain the maximum volume, error ${error}.`); 2945} 2946``` 2947 2948### mute<sup>9+</sup> 2949 2950mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void 2951 2952设置指定音量流静音,使用callback方式异步返回结果。 2953 2954**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 2955 2956仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 2957 2958**系统接口:** 该接口为系统接口 2959 2960**系统能力:** SystemCapability.Multimedia.Audio.Volume 2961 2962**参数:** 2963 2964| 参数名 | 类型 | 必填 | 说明 | 2965| ---------- | ----------------------------------- | ---- | ------------------------------------- | 2966| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 2967| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 2968| callback | AsyncCallback<void> | 是 | 回调函数。当设置指定音量流静音成功,err为undefined,否则为错误对象。 | 2969 2970**示例:** 2971 2972```ts 2973import { BusinessError } from '@ohos.base'; 2974 2975audioVolumeGroupManager.mute(audio.AudioVolumeType.MEDIA, true, (err: BusinessError) => { 2976 if (err) { 2977 console.error(`Failed to mute the stream. ${err}`); 2978 return; 2979 } 2980 console.info('Callback invoked to indicate that the stream is muted.'); 2981}); 2982``` 2983 2984### mute<sup>9+</sup> 2985 2986mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> 2987 2988设置指定音量流静音,使用Promise方式异步返回结果。 2989 2990**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 2991 2992仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 2993 2994**系统接口:** 该接口为系统接口 2995 2996**系统能力:** SystemCapability.Multimedia.Audio.Volume 2997 2998**参数:** 2999 3000| 参数名 | 类型 | 必填 | 说明 | 3001| ---------- | ----------------------------------- | ---- | ------------------------------------- | 3002| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3003| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 3004 3005**返回值:** 3006 3007| 类型 | 说明 | 3008| ------------------- | ----------------------------- | 3009| Promise<void> | Promise对象,无返回结果。 | 3010 3011**示例:** 3012 3013```ts 3014audioVolumeGroupManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => { 3015 console.info('Promise returned to indicate that the stream is muted.'); 3016}); 3017``` 3018 3019### isMute<sup>9+</sup> 3020 3021isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 3022 3023获取指定音量流是否被静音,使用callback方式异步返回结果。 3024 3025**系统能力:** SystemCapability.Multimedia.Audio.Volume 3026 3027**参数:** 3028 3029| 参数名 | 类型 | 必填 | 说明 | 3030| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 3031| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3032| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音量流是否被静音成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 3033 3034**示例:** 3035 3036```ts 3037import { BusinessError } from '@ohos.base'; 3038 3039audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 3040 if (err) { 3041 console.error(`Failed to obtain the mute status. ${err}`); 3042 return; 3043 } 3044 console.info(`Callback invoked to indicate that the mute status of the stream is obtained ${value}.`); 3045}); 3046``` 3047 3048### isMute<sup>9+</sup> 3049 3050isMute(volumeType: AudioVolumeType): Promise<boolean> 3051 3052获取指定音量流是否被静音,使用Promise方式异步返回结果。 3053 3054**系统能力:** SystemCapability.Multimedia.Audio.Volume 3055 3056**参数:** 3057 3058| 参数名 | 类型 | 必填 | 说明 | 3059| ---------- | ----------------------------------- | ---- | ------------ | 3060| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3061 3062**返回值:** 3063 3064| 类型 | 说明 | 3065| ---------------------- | ------------------------------------------------------ | 3066| Promise<boolean> | Promise对象,返回流静音状态,true为静音,false为非静音。 | 3067 3068**示例:** 3069 3070```ts 3071audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 3072 console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`); 3073}); 3074``` 3075 3076### isMuteSync<sup>10+</sup> 3077 3078isMuteSync(volumeType: AudioVolumeType): boolean 3079 3080获取指定音量流是否被静音,同步返回结果。 3081 3082**系统能力:** SystemCapability.Multimedia.Audio.Volume 3083 3084**参数:** 3085 3086| 参数名 | 类型 | 必填 | 说明 | 3087| ---------- | ----------------------------------- | ---- | ------------ | 3088| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3089 3090**返回值:** 3091 3092| 类型 | 说明 | 3093| ---------------------- | ------------------------------------------------------ | 3094| boolean | 返回流静音状态,true为静音,false为非静音。 | 3095 3096**错误码:** 3097 3098以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3099 3100| 错误码ID | 错误信息 | 3101| ------- | --------------------------------------------| 3102| 6800101 | invalid parameter error | 3103 3104**示例:** 3105 3106```ts 3107import { BusinessError } from '@ohos.base'; 3108 3109try { 3110 let value: boolean = audioVolumeGroupManager.isMuteSync(audio.AudioVolumeType.MEDIA); 3111 console.info(`Indicate that the mute status of the stream is obtained ${value}.`); 3112} catch (err) { 3113 let error = err as BusinessError; 3114 console.error(`Failed to obtain the mute status of the stream, error ${error}.`); 3115} 3116``` 3117 3118### setRingerMode<sup>9+</sup> 3119 3120setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void 3121 3122设置铃声模式,使用callback方式异步返回结果。 3123 3124**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3125 3126仅在静音和非静音状态切换时需要该权限。 3127 3128**系统接口:** 该接口为系统接口 3129 3130**系统能力:** SystemCapability.Multimedia.Audio.Volume 3131 3132**参数:** 3133 3134| 参数名 | 类型 | 必填 | 说明 | 3135| -------- | ------------------------------- | ---- | ------------------------ | 3136| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 3137| callback | AsyncCallback<void> | 是 | 回调函数。当设置铃声模式成功,err为undefined,否则为错误对象。 | 3138 3139**示例:** 3140 3141```ts 3142import { BusinessError } from '@ohos.base'; 3143 3144audioVolumeGroupManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err: BusinessError) => { 3145 if (err) { 3146 console.error(`Failed to set the ringer mode. ${err}`); 3147 return; 3148 } 3149 console.info('Callback invoked to indicate a successful setting of the ringer mode.'); 3150}); 3151``` 3152 3153### setRingerMode<sup>9+</sup> 3154 3155setRingerMode(mode: AudioRingMode): Promise<void> 3156 3157设置铃声模式,使用Promise方式异步返回结果。 3158 3159**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3160 3161仅在静音和非静音状态切换时需要该权限。 3162 3163**系统接口:** 该接口为系统接口 3164 3165**系统能力:** SystemCapability.Multimedia.Audio.Volume 3166 3167**参数:** 3168 3169| 参数名 | 类型 | 必填 | 说明 | 3170| ------ | ------------------------------- | ---- | -------------- | 3171| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 3172 3173**返回值:** 3174 3175| 类型 | 说明 | 3176| ------------------- | ------------------------------- | 3177| Promise<void> | Promise对象,无返回结果。 | 3178 3179**示例:** 3180 3181```ts 3182audioVolumeGroupManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => { 3183 console.info('Promise returned to indicate a successful setting of the ringer mode.'); 3184}); 3185``` 3186 3187### getRingerMode<sup>9+</sup> 3188 3189getRingerMode(callback: AsyncCallback<AudioRingMode>): void 3190 3191获取铃声模式,使用callback方式异步返回结果。 3192 3193**系统能力:** SystemCapability.Multimedia.Audio.Volume 3194 3195**参数:** 3196 3197| 参数名 | 类型 | 必填 | 说明 | 3198| -------- | ---------------------------------------------------- | ---- | ------------------------ | 3199| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调函数。当获取铃声模式成功,err为undefined,data为获取到的铃声模式;否则为错误对象。 | 3200 3201**示例:** 3202 3203```ts 3204import { BusinessError } from '@ohos.base'; 3205 3206audioVolumeGroupManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => { 3207 if (err) { 3208 console.error(`Failed to obtain the ringer mode. ${err}`); 3209 return; 3210 } 3211 console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`); 3212}); 3213``` 3214 3215### getRingerMode<sup>9+</sup> 3216 3217getRingerMode(): Promise<AudioRingMode> 3218 3219获取铃声模式,使用Promise方式异步返回结果。 3220 3221**系统能力:** SystemCapability.Multimedia.Audio.Volume 3222 3223**返回值:** 3224 3225| 类型 | 说明 | 3226| ---------------------------------------------- | ------------------------------- | 3227| Promise<[AudioRingMode](#audioringmode)> | Promise对象,返回系统的铃声模式。 | 3228 3229**示例:** 3230 3231```ts 3232audioVolumeGroupManager.getRingerMode().then((value: audio.AudioRingMode) => { 3233 console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`); 3234}); 3235``` 3236 3237### getRingerModeSync<sup>10+</sup> 3238 3239getRingerModeSync(): AudioRingMode 3240 3241获取铃声模式,同步返回结果。 3242 3243**系统能力:** SystemCapability.Multimedia.Audio.Volume 3244 3245**返回值:** 3246 3247| 类型 | 说明 | 3248| ---------------------------------------------- | ------------------------------- | 3249| [AudioRingMode](#audioringmode) | 返回系统的铃声模式。 | 3250 3251**示例:** 3252 3253```ts 3254import { BusinessError } from '@ohos.base'; 3255 3256try { 3257 let value: audio.AudioRingMode = audioVolumeGroupManager.getRingerModeSync(); 3258 console.info(`Indicate that the ringer mode is obtained ${value}.`); 3259} catch (err) { 3260 let error = err as BusinessError; 3261 console.error(`Failed to obtain the ringer mode, error ${error}.`); 3262} 3263``` 3264 3265### on('ringerModeChange')<sup>9+</sup> 3266 3267on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void 3268 3269监听铃声模式变化事件,使用callback方式返回结果。 3270 3271**系统能力:** SystemCapability.Multimedia.Audio.Volume 3272 3273**参数:** 3274 3275| 参数名 | 类型 | 必填 | 说明 | 3276| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 3277| type | string | 是 | 事件回调类型,支持的事件为:'ringerModeChange'(铃声模式变化事件,检测到铃声模式改变时,触发该事件)。 | 3278| callback | Callback<[AudioRingMode](#audioringmode)> | 是 | 回调函数,返回变化后的铃音模式。 | 3279 3280**错误码:** 3281 3282以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3283 3284| 错误码ID | 错误信息 | 3285| ------- | --------------------------------------------| 3286| 6800101 | if input parameter value error | 3287 3288**示例:** 3289 3290```ts 3291audioVolumeGroupManager.on('ringerModeChange', (ringerMode: audio.AudioRingMode) => { 3292 console.info(`Updated ringermode: ${ringerMode}`); 3293}); 3294``` 3295### setMicrophoneMute<sup>9+</sup> 3296 3297setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 3298 3299设置麦克风静音状态,使用callback方式异步返回结果。 3300 3301**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG 3302 3303**系统能力:** SystemCapability.Multimedia.Audio.Volume 3304 3305**参数:** 3306 3307| 参数名 | 类型 | 必填 | 说明 | 3308| -------- | ------------------------- | ---- | --------------------------------------------- | 3309| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 3310| callback | AsyncCallback<void> | 是 | 回调函数。当设置麦克风静音状态成功,err为undefined,否则为错误对象。 | 3311 3312**示例:** 3313 3314```ts 3315import { BusinessError } from '@ohos.base'; 3316 3317audioVolumeGroupManager.setMicrophoneMute(true, (err: BusinessError) => { 3318 if (err) { 3319 console.error(`Failed to mute the microphone. ${err}`); 3320 return; 3321 } 3322 console.info('Callback invoked to indicate that the microphone is muted.'); 3323}); 3324``` 3325 3326### setMicrophoneMute<sup>9+</sup> 3327 3328setMicrophoneMute(mute: boolean): Promise<void> 3329 3330设置麦克风静音状态,使用Promise方式异步返回结果。 3331 3332**需要权限:** ohos.permission.MANAGE_AUDIO_CONFIG 3333 3334**系统能力:** SystemCapability.Multimedia.Audio.Volume 3335 3336**参数:** 3337 3338| 参数名 | 类型 | 必填 | 说明 | 3339| ------ | ------- | ---- | --------------------------------------------- | 3340| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 3341 3342**返回值:** 3343 3344| 类型 | 说明 | 3345| ------------------- | ------------------------------- | 3346| Promise<void> | Promise对象,无返回结果。 | 3347 3348**示例:** 3349 3350```ts 3351audioVolumeGroupManager.setMicrophoneMute(true).then(() => { 3352 console.info('Promise returned to indicate that the microphone is muted.'); 3353}); 3354``` 3355 3356### isMicrophoneMute<sup>9+</sup> 3357 3358isMicrophoneMute(callback: AsyncCallback<boolean>): void 3359 3360获取麦克风静音状态,使用callback方式异步返回结果。 3361 3362**系统能力:** SystemCapability.Multimedia.Audio.Volume 3363 3364**参数:** 3365 3366| 参数名 | 类型 | 必填 | 说明 | 3367| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 3368| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取麦克风静音状态成功,err为undefined,data为true为静音,false为非静音;否则为错误对象。 | 3369 3370**示例:** 3371 3372```ts 3373import { BusinessError } from '@ohos.base'; 3374 3375audioVolumeGroupManager.isMicrophoneMute((err: BusinessError, value: boolean) => { 3376 if (err) { 3377 console.error(`Failed to obtain the mute status of the microphone. ${err}`); 3378 return; 3379 } 3380 console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`); 3381}); 3382``` 3383 3384### isMicrophoneMute<sup>9+</sup> 3385 3386isMicrophoneMute(): Promise<boolean> 3387 3388获取麦克风静音状态,使用Promise方式异步返回结果。 3389 3390**系统能力:** SystemCapability.Multimedia.Audio.Volume 3391 3392**返回值:** 3393 3394| 类型 | 说明 | 3395| ---------------------- | ------------------------------------------------------------ | 3396| Promise<boolean> | Promise对象,返回系统麦克风静音状态,true为静音,false为非静音。 | 3397 3398**示例:** 3399 3400```ts 3401audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => { 3402 console.info(`Promise returned to indicate that the mute status of the microphone is obtained ${value}.`); 3403}); 3404``` 3405 3406### isMicrophoneMuteSync<sup>10+</sup> 3407 3408isMicrophoneMuteSync(): boolean 3409 3410获取麦克风静音状态,同步返回结果。 3411 3412**系统能力:** SystemCapability.Multimedia.Audio.Volume 3413 3414**返回值:** 3415 3416| 类型 | 说明 | 3417| ---------------------- | ------------------------------------------------------------ | 3418| boolean | 返回系统麦克风静音状态,true为静音,false为非静音。 | 3419 3420**示例:** 3421 3422```ts 3423import { BusinessError } from '@ohos.base'; 3424 3425try { 3426 let value: boolean = audioVolumeGroupManager.isMicrophoneMuteSync(); 3427 console.info(`Indicate that the mute status of the microphone is obtained ${value}.`); 3428} catch (err) { 3429 let error = err as BusinessError; 3430 console.error(`Failed to obtain the mute status of the microphone, error ${error}.`); 3431} 3432``` 3433 3434### on('micStateChange')<sup>9+</sup> 3435 3436on(type: 'micStateChange', callback: Callback<MicStateChangeEvent>): void 3437 3438监听系统麦克风状态更改事件,使用callback方式返回结果。 3439 3440目前此订阅接口在单进程多AudioManager实例的使用场景下,仅最后一个实例的订阅生效,其他实例的订阅会被覆盖(即使最后一个实例没有进行订阅),因此推荐使用单一AudioManager实例进行开发。 3441 3442**系统能力:** SystemCapability.Multimedia.Audio.Volume 3443 3444**参数:** 3445 3446| 参数名 | 类型 | 必填 | 说明 | 3447| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 3448| type | string | 是 | 事件回调类型,支持的事件为:'micStateChange'(系统麦克风状态变化事件,检测到系统麦克风状态改变时,触发该事件)。 | 3449| callback | Callback<[MicStateChangeEvent](#micstatechangeevent9)> | 是 | 回调函数,返回变更后的麦克风状态。 | 3450 3451**错误码:** 3452 3453以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3454 3455| 错误码ID | 错误信息 | 3456| ------- | --------------------------------------------| 3457| 6800101 | if input parameter value error | 3458 3459**示例:** 3460 3461```ts 3462audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => { 3463 console.info(`Current microphone status is: ${micStateChange.mute} `); 3464}); 3465``` 3466 3467### isVolumeUnadjustable<sup>10+</sup> 3468 3469isVolumeUnadjustable(): boolean 3470 3471获取固定音量模式开关状态,打开时进入固定音量模式,此时音量固定无法被调节,使用同步方式返回结果。 3472 3473**系统能力:** SystemCapability.Multimedia.Audio.Volume 3474 3475**返回值:** 3476 3477| 类型 | 说明 | 3478| ---------------------- | ------------------------------------------------------ | 3479| boolean | 同步接口,返回固定音量模式开关状态,true为固定音量模式,false为非固定音量模式。 | 3480 3481**示例:** 3482 3483```ts 3484let volumeAdjustSwitch: boolean = audioVolumeGroupManager.isVolumeUnadjustable(); 3485console.info(`Whether it is volume unadjustable: ${volumeAdjustSwitch}.`); 3486``` 3487 3488### adjustVolumeByStep<sup>10+</sup> 3489 3490adjustVolumeByStep(adjustType: VolumeAdjustType, callback: AsyncCallback<void>): void 3491 3492调节当前最高优先级的流的音量,使音量值按步长加或减,使用callback方式异步返回结果。 3493 3494**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3495 3496仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 3497 3498**系统接口:** 该接口为系统接口 3499 3500**系统能力:** SystemCapability.Multimedia.Audio.Volume 3501 3502**参数:** 3503 3504| 参数名 | 类型 | 必填 | 说明 | 3505| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3506| adjustType | [VolumeAdjustType](#volumeadjusttype10) | 是 | 音量调节方向。 | 3507| callback | AsyncCallback<void> | 是 | 回调函数。当调节当前最高优先级的流的音量成功,err为undefined,否则为错误对象。 | 3508 3509**错误码:** 3510 3511以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3512 3513| 错误码ID | 错误信息 | 3514| ------- | --------------------------------------------| 3515| 6800101 | Invalid parameter error. Return by callback. | 3516| 6800301 | System error. Return by callback. | 3517 3518**示例:** 3519 3520```ts 3521import { BusinessError } from '@ohos.base'; 3522 3523audioVolumeGroupManager.adjustVolumeByStep(audio.VolumeAdjustType.VOLUME_UP, (err: BusinessError) => { 3524 if (err) { 3525 console.error(`Failed to adjust the volume by step. ${err}`); 3526 return; 3527 } else { 3528 console.info('Success to adjust the volume by step.'); 3529 } 3530}); 3531``` 3532### adjustVolumeByStep<sup>10+</sup> 3533 3534adjustVolumeByStep(adjustType: VolumeAdjustType): Promise<void> 3535 3536单步设置当前最高优先级的流的音量,使用Promise方式异步返回结果。 3537 3538**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3539 3540仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 3541 3542**系统接口:** 该接口为系统接口 3543 3544**系统能力:** SystemCapability.Multimedia.Audio.Volume 3545 3546**参数:** 3547 3548| 参数名 | 类型 | 必填 | 说明 | 3549| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3550| adjustType | [VolumeAdjustType](#volumeadjusttype10) | 是 | 音量调节方向。 | 3551 3552**返回值:** 3553 3554| 类型 | 说明 | 3555| ------------------- | ----------------------------- | 3556| Promise<void> | Promise对象,无返回结果。 | 3557 3558**错误码:** 3559 3560以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3561 3562| 错误码ID | 错误信息 | 3563| ------- | --------------------------------------------| 3564| 6800101 | Invalid parameter error. Return by promise. | 3565| 6800301 | System error. Return by promise. | 3566 3567**示例:** 3568 3569```ts 3570import { BusinessError } from '@ohos.base'; 3571 3572audioVolumeGroupManager.adjustVolumeByStep(audio.VolumeAdjustType.VOLUME_UP).then(() => { 3573 console.info('Success to adjust the volume by step.'); 3574}).catch((error: BusinessError) => { 3575 console.error('Fail to adjust the volume by step.'); 3576}); 3577``` 3578 3579### adjustSystemVolumeByStep<sup>10+</sup> 3580 3581adjustSystemVolumeByStep(volumeType: AudioVolumeType, adjustType: VolumeAdjustType, callback: AsyncCallback<void>): void 3582 3583单步设置指定流的音量,使用callback方式异步返回结果。 3584 3585**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3586 3587仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 3588 3589**系统接口:** 该接口为系统接口 3590 3591**系统能力:** SystemCapability.Multimedia.Audio.Volume 3592 3593**参数:** 3594 3595| 参数名 | 类型 | 必填 | 说明 | 3596| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3597| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3598| adjustType | [VolumeAdjustType](#volumeadjusttype10) | 是 | 音量调节方向。 | 3599| callback | AsyncCallback<void> | 是 | 回调函数。当单步设置指定流的音量成功,err为undefined,否则为错误对象。 | 3600 3601**错误码:** 3602 3603以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3604 3605| 错误码ID | 错误信息 | 3606| ------- | --------------------------------------------| 3607| 6800101 | Invalid parameter error. Return by callback. | 3608| 6800301 | System error. Return by callback. | 3609 3610**示例:** 3611 3612```ts 3613import { BusinessError } from '@ohos.base'; 3614 3615audioVolumeGroupManager.adjustSystemVolumeByStep(audio.AudioVolumeType.MEDIA, audio.VolumeAdjustType.VOLUME_UP, (err: BusinessError) => { 3616 if (err) { 3617 console.error(`Failed to adjust the system volume by step ${err}`); 3618 } else { 3619 console.info('Success to adjust the system volume by step.'); 3620 } 3621}); 3622``` 3623### adjustSystemVolumeByStep<sup>10+</sup> 3624 3625adjustSystemVolumeByStep(volumeType: AudioVolumeType, adjustType: VolumeAdjustType): Promise<void> 3626 3627单步设置指定流的音量,使用Promise方式异步返回结果。 3628 3629**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY 3630 3631仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 3632 3633**系统接口:** 该接口为系统接口 3634 3635**系统能力:** SystemCapability.Multimedia.Audio.Volume 3636 3637**参数:** 3638 3639| 参数名 | 类型 | 必填 | 说明 | 3640| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3641| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3642| adjustType | [VolumeAdjustType](#volumeadjusttype10) | 是 | 音量调节方向。 | 3643 3644**返回值:** 3645 3646| 类型 | 说明 | 3647| ------------------- | ----------------------------- | 3648| Promise<void> | Promise对象,无返回结果。 | 3649 3650**错误码:** 3651 3652以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3653 3654| 错误码ID | 错误信息 | 3655| ------- | --------------------------------------------| 3656| 6800101 | Invalid parameter error. Return by promise. | 3657| 6800301 | System error. Return by promise. | 3658 3659**示例:** 3660 3661```ts 3662import { BusinessError } from '@ohos.base'; 3663 3664audioVolumeGroupManager.adjustSystemVolumeByStep(audio.AudioVolumeType.MEDIA, audio.VolumeAdjustType.VOLUME_UP).then(() => { 3665 console.info('Success to adjust the system volume by step.'); 3666}).catch((error: BusinessError) => { 3667 console.error('Fail to adjust the system volume by step.'); 3668}); 3669``` 3670 3671### getSystemVolumeInDb<sup>10+</sup> 3672 3673getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType, callback: AsyncCallback<number>): void 3674 3675获取音量增益dB值,使用callback方式异步返回结果。 3676 3677**系统能力:** SystemCapability.Multimedia.Audio.Volume 3678 3679**参数:** 3680 3681| 参数名 | 类型 | 必填 | 说明 | 3682| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3683| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3684| volumeLevel | number | 是 | 音量等级。 | 3685| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3686| callback | AsyncCallback<number> | 是 | 回调函数。当获取音量增益dB值成功,err为undefined,data为获取到的音量增益dB值;否则为错误对象。 | 3687 3688**错误码:** 3689 3690以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3691 3692| 错误码ID | 错误信息 | 3693| ------- | --------------------------------------------| 3694| 6800101 | Invalid parameter error. Return by callback. | 3695| 6800301 | System error. Return by callback. | 3696 3697**示例:** 3698 3699```ts 3700import { BusinessError } from '@ohos.base'; 3701 3702audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER, (err: BusinessError, dB: number) => { 3703 if (err) { 3704 console.error(`Failed to get the volume DB. ${err}`); 3705 } else { 3706 console.info(`Success to get the volume DB. ${dB}`); 3707 } 3708}); 3709``` 3710### getSystemVolumeInDb<sup>10+</sup> 3711 3712getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): Promise<number> 3713 3714获取音量增益dB值,使用Promise方式异步返回结果。 3715 3716**系统能力:** SystemCapability.Multimedia.Audio.Volume 3717 3718**参数:** 3719 3720| 参数名 | 类型 | 必填 | 说明 | 3721| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3722| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3723| volumeLevel | number | 是 | 音量等级。 | 3724| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3725 3726**返回值:** 3727 3728| 类型 | 说明 | 3729| --------------------- | ---------------------------------- | 3730| Promise<number> | Promise对象,返回对应的音量增益dB值。 | 3731 3732**错误码:** 3733 3734以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3735 3736| 错误码ID | 错误信息 | 3737| ------- | --------------------------------------------| 3738| 6800101 | Invalid parameter error. Return by promise. | 3739| 6800301 | System error. Return by promise. | 3740 3741**示例:** 3742 3743```ts 3744import { BusinessError } from '@ohos.base'; 3745 3746audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER).then((value: number) => { 3747 console.info(`Success to get the volume DB. ${value}`); 3748}).catch((error: BusinessError) => { 3749 console.error(`Fail to adjust the system volume by step. ${error}`); 3750}); 3751``` 3752 3753### getSystemVolumeInDbSync<sup>10+</sup> 3754 3755getSystemVolumeInDbSync(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): number 3756 3757获取音量增益dB值,同步返回结果。 3758 3759**系统能力:** SystemCapability.Multimedia.Audio.Volume 3760 3761**参数:** 3762 3763| 参数名 | 类型 | 必填 | 说明 | 3764| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 3765| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 3766| volumeLevel | number | 是 | 音量等级。 | 3767| device | [DeviceType](#devicetype) | 是 | 设备类型。 | 3768 3769**返回值:** 3770 3771| 类型 | 说明 | 3772| --------------------- | ---------------------------------- | 3773| number | 返回对应的音量增益dB值。 | 3774 3775**错误码:** 3776 3777以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 3778 3779| 错误码ID | 错误信息 | 3780| ------- | --------------------------------------------| 3781| 6800101 | invalid parameter error | 3782 3783**示例:** 3784 3785```ts 3786import { BusinessError } from '@ohos.base'; 3787 3788try { 3789 let value: number = audioVolumeGroupManager.getSystemVolumeInDbSync(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER); 3790 console.info(`Success to get the volume DB. ${value}`); 3791} catch (err) { 3792 let error = err as BusinessError; 3793 console.error(`Fail to adjust the system volume by step. ${error}`); 3794} 3795``` 3796 3797## AudioStreamManager<sup>9+</sup> 3798 3799管理音频流。在使用AudioStreamManager的API前,需要使用[getStreamManager](#getstreammanager9)获取AudioStreamManager实例。 3800 3801### getCurrentAudioRendererInfoArray<sup>9+</sup> 3802 3803getCurrentAudioRendererInfoArray(callback: AsyncCallback<AudioRendererChangeInfoArray>): void 3804 3805获取当前音频渲染器的信息。使用callback异步回调。 3806 3807**系统能力**: SystemCapability.Multimedia.Audio.Renderer 3808 3809**参数:** 3810 3811| 参数名 | 类型 | 必填 | 说明 | 3812| -------- | ----------------------------------- | -------- | --------------------------- | 3813| callback | AsyncCallback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数。当获取当前音频渲染器的信息成功,err为undefined,data为获取到的当前音频渲染器的信息;否则为错误对象。 | 3814 3815**示例:** 3816 3817```ts 3818import { BusinessError } from '@ohos.base'; 3819 3820audioStreamManager.getCurrentAudioRendererInfoArray(async (err: BusinessError, AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3821 console.info('getCurrentAudioRendererInfoArray **** Get Callback Called ****'); 3822 if (err) { 3823 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3824 } else { 3825 if (AudioRendererChangeInfoArray != null) { 3826 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3827 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3828 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3829 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3830 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3831 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3832 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3833 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3834 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3835 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3836 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3837 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3838 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3839 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3840 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3841 } 3842 } 3843 } 3844 } 3845}); 3846``` 3847 3848### getCurrentAudioRendererInfoArray<sup>9+</sup> 3849 3850getCurrentAudioRendererInfoArray(): Promise<AudioRendererChangeInfoArray> 3851 3852获取当前音频渲染器的信息。使用Promise异步回调。 3853 3854**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3855 3856**返回值:** 3857 3858| 类型 | 说明 | 3859| ---------------------------------------------------------------------------------| --------------------------------------- | 3860| Promise<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | Promise对象,返回当前音频渲染器信息。 | 3861 3862**示例:** 3863 3864```ts 3865import { BusinessError } from '@ohos.base'; 3866 3867async function getCurrentAudioRendererInfoArray(){ 3868 await audioStreamManager.getCurrentAudioRendererInfoArray().then((AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 3869 console.info(`getCurrentAudioRendererInfoArray ######### Get Promise is called ##########`); 3870 if (AudioRendererChangeInfoArray != null) { 3871 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 3872 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 3873 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3874 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3875 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3876 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3877 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3878 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3879 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3880 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3881 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3882 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3883 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3884 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3885 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3886 } 3887 } 3888 } 3889 }).catch((err: BusinessError) => { 3890 console.error(`getCurrentAudioRendererInfoArray :ERROR: ${err}`); 3891 }); 3892} 3893``` 3894### getCurrentAudioRendererInfoArraySync<sup>10+</sup> 3895 3896getCurrentAudioRendererInfoArraySync(): AudioRendererChangeInfoArray 3897 3898获取当前音频渲染器的信息,同步返回结果。 3899 3900**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3901 3902**返回值:** 3903 3904| 类型 | 说明 | 3905| ---------------------------------------------------------------------------------| --------------------------------------- | 3906| [AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9) | 返回当前音频渲染器信息。 | 3907 3908**示例:** 3909 3910```ts 3911import { BusinessError } from '@ohos.base'; 3912 3913try { 3914 let audioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray = audioStreamManager.getCurrentAudioRendererInfoArraySync(); 3915 console.info(`getCurrentAudioRendererInfoArraySync success.`); 3916 if (audioRendererChangeInfoArray != null) { 3917 for (let i = 0; i < audioRendererChangeInfoArray.length; i++) { 3918 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = audioRendererChangeInfoArray[i]; 3919 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 3920 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 3921 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 3922 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 3923 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 3924 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 3925 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 3926 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 3927 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 3928 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 3929 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 3930 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 3931 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 3932 } 3933 } 3934 } 3935} catch (err) { 3936 let error = err as BusinessError; 3937 console.error(`getCurrentAudioRendererInfoArraySync :ERROR: ${error}`); 3938} 3939``` 3940 3941### getCurrentAudioCapturerInfoArray<sup>9+</sup> 3942 3943getCurrentAudioCapturerInfoArray(callback: AsyncCallback<AudioCapturerChangeInfoArray>): void 3944 3945获取当前音频采集器的信息。使用callback异步回调。 3946 3947**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3948 3949**参数:** 3950 3951| 参数名 | 类型 | 必填 | 说明 | 3952| ---------- | ----------------------------------- | --------- | -------------------------------------------------------- | 3953| callback | AsyncCallback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数。当获取当前音频采集器的信息成功,err为undefined,data为获取到的当前音频采集器的信息;否则为错误对象。 | 3954 3955**示例:** 3956 3957```ts 3958import { BusinessError } from '@ohos.base'; 3959 3960audioStreamManager.getCurrentAudioCapturerInfoArray(async (err: BusinessError, AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 3961 console.info('getCurrentAudioCapturerInfoArray **** Get Callback Called ****'); 3962 if (err) { 3963 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 3964 } else { 3965 if (AudioCapturerChangeInfoArray != null) { 3966 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 3967 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 3968 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 3969 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 3970 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 3971 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 3972 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 3973 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 3974 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 3975 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 3976 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 3977 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 3978 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 3979 } 3980 } 3981 } 3982 } 3983}); 3984``` 3985 3986### getCurrentAudioCapturerInfoArray<sup>9+</sup> 3987 3988getCurrentAudioCapturerInfoArray(): Promise<AudioCapturerChangeInfoArray> 3989 3990获取当前音频采集器的信息。使用Promise异步回调。 3991 3992**系统能力:** SystemCapability.Multimedia.Audio.Renderer 3993 3994**返回值:** 3995 3996| 类型 | 说明 | 3997| -----------------------------------------------------------------------------| ----------------------------------- | 3998| Promise<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | Promise对象,返回当前音频渲染器信息。 | 3999 4000**示例:** 4001 4002```ts 4003import { BusinessError } from '@ohos.base'; 4004 4005async function getCurrentAudioCapturerInfoArray(){ 4006 await audioStreamManager.getCurrentAudioCapturerInfoArray().then((AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4007 console.info('getCurrentAudioCapturerInfoArray **** Get Promise Called ****'); 4008 if (AudioCapturerChangeInfoArray != null) { 4009 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4010 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4011 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4012 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4013 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4014 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4015 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4016 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4017 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4018 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4019 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4020 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4021 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4022 } 4023 } 4024 } 4025 }).catch((err: BusinessError) => { 4026 console.error(`getCurrentAudioCapturerInfoArray :ERROR: ${err}`); 4027 }); 4028} 4029``` 4030### getCurrentAudioCapturerInfoArraySync<sup>10+</sup> 4031 4032getCurrentAudioCapturerInfoArraySync(): AudioCapturerChangeInfoArray 4033 4034获取当前音频采集器的信息,同步返回结果。 4035 4036**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4037 4038**返回值:** 4039 4040| 类型 | 说明 | 4041| -----------------------------------------------------------------------------| ----------------------------------- | 4042| [AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9) | 返回当前音频采集器信息。 | 4043 4044**示例:** 4045 4046```ts 4047import { BusinessError } from '@ohos.base'; 4048 4049try { 4050 let audioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray = audioStreamManager.getCurrentAudioCapturerInfoArraySync(); 4051 console.info('getCurrentAudioCapturerInfoArraySync success.'); 4052 if (audioCapturerChangeInfoArray != null) { 4053 for (let i = 0; i < audioCapturerChangeInfoArray.length; i++) { 4054 console.info(`StreamId for ${i} is: ${audioCapturerChangeInfoArray[i].streamId}`); 4055 console.info(`Source for ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.source}`); 4056 console.info(`Flag ${i} is: ${audioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4057 for (let j = 0; j < audioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4058 console.info(`Id: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4059 console.info(`Type: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4060 console.info(`Role: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4061 console.info(`Name: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4062 console.info(`Address: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4063 console.info(`SampleRate: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4064 console.info(`ChannelCount: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4065 console.info(`ChannelMask: ${i} : ${audioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4066 } 4067 } 4068 } 4069} catch (err) { 4070 let error = err as BusinessError; 4071 console.error(`getCurrentAudioCapturerInfoArraySync ERROR: ${error}`); 4072} 4073``` 4074 4075### on('audioRendererChange')<sup>9+</sup> 4076 4077on(type: 'audioRendererChange', callback: Callback<AudioRendererChangeInfoArray>): void 4078 4079监听音频渲染器更改事件,使用callback方式返回结果。 4080 4081**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4082 4083**参数:** 4084 4085| 参数名 | 类型 | 必填 | 说明 | 4086| -------- | ---------- | --------- | ------------------------------------------------------------------------ | 4087| type | string | 是 | 事件类型,支持的事件`'audioRendererChange'`:当音频渲染器发生更改时触发。 | 4088| callback | Callback<[AudioRendererChangeInfoArray](#audiorendererchangeinfoarray9)> | 是 | 回调函数,返回当前音频渲染器信息。 | 4089 4090**错误码:** 4091 4092以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4093 4094| 错误码ID | 错误信息 | 4095| ------- | --------------------------------------------| 4096| 6800101 | if input parameter value error | 4097 4098**示例:** 4099 4100```ts 4101audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => { 4102 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 4103 let AudioRendererChangeInfo: audio.AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 4104 console.info(`## RendererChange on is called for ${i} ##`); 4105 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 4106 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 4107 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 4108 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 4109 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 4110 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 4111 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 4112 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 4113 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 4114 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 4115 console.info(`SampleRate: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 4116 console.info(`ChannelCount: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 4117 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks[0]}`); 4118 } 4119 } 4120}); 4121``` 4122 4123### off('audioRendererChange')<sup>9+</sup> 4124 4125off(type: 'audioRendererChange'): void 4126 4127取消监听音频渲染器更改事件。 4128 4129**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4130 4131**参数:** 4132 4133| 参数名 | 类型 | 必填 | 说明 | 4134| -------- | ------- | ---- | ---------------- | 4135| type | string | 是 | 事件类型,支持的事件`'audioRendererChange'`:音频渲染器更改事件。 | 4136 4137**错误码:** 4138 4139以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4140 4141| 错误码ID | 错误信息 | 4142| ------- | --------------------------------------------| 4143| 6800101 | if input parameter value error | 4144 4145**示例:** 4146 4147```ts 4148audioStreamManager.off('audioRendererChange'); 4149console.info('######### RendererChange Off is called #########'); 4150``` 4151 4152### on('audioCapturerChange')<sup>9+</sup> 4153 4154on(type: 'audioCapturerChange', callback: Callback<AudioCapturerChangeInfoArray>): void 4155 4156监听音频采集器更改事件,使用callback方式返回结果。 4157 4158**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4159 4160**参数:** 4161 4162| 参数名 | 类型 | 必填 | 说明 | 4163| -------- | ------- | --------- | ----------------------------------------------------------------------- | 4164| type | string | 是 | 事件类型,支持的事件`'audioCapturerChange'`:当音频采集器发生更改时触发。 | 4165| callback | Callback<[AudioCapturerChangeInfoArray](#audiocapturerchangeinfoarray9)> | 是 | 回调函数,返回当前音频采集器信息。 | 4166 4167**错误码:** 4168 4169以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4170 4171| 错误码ID | 错误信息 | 4172| ------- | --------------------------------------------| 4173| 6800101 | if input parameter value error | 4174 4175**示例:** 4176 4177```ts 4178audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 4179 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 4180 console.info(`## CapChange on is called for element ${i} ##`); 4181 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 4182 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 4183 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 4184 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 4185 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 4186 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 4187 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 4188 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 4189 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 4190 console.info(`SampleRate: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 4191 console.info(`ChannelCount: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 4192 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 4193 } 4194 } 4195}); 4196``` 4197 4198### off('audioCapturerChange')<sup>9+</sup> 4199 4200off(type: 'audioCapturerChange'): void 4201 4202取消监听音频采集器更改事件。 4203 4204**系统能力:** SystemCapability.Multimedia.Audio.Capturer 4205 4206**参数:** 4207 4208| 参数名 | 类型 | 必填 | 说明 | 4209| -------- | -------- | --- | ------------------------------------------------------------- | 4210| type | string |是 | 事件类型,支持的事件`'audioCapturerChange'`:音频采集器更改事件。 | 4211 4212**错误码:** 4213 4214以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4215 4216| 错误码ID | 错误信息 | 4217| ------- | --------------------------------------------| 4218| 6800101 | if input parameter value error | 4219 4220**示例:** 4221 4222```ts 4223audioStreamManager.off('audioCapturerChange'); 4224console.info('######### CapturerChange Off is called #########'); 4225 4226``` 4227 4228### isActive<sup>9+</sup> 4229 4230isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 4231 4232获取指定音频流是否为活跃状态,使用callback方式异步返回结果。 4233 4234**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4235 4236**参数:** 4237 4238| 参数名 | 类型 | 必填 | 说明 | 4239| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 4240| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4241| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定音频流是否为活跃状态成功,err为undefined,data为true为活跃,false为不活跃;否则为错误对象。 | 4242 4243**示例:** 4244 4245```ts 4246import { BusinessError } from '@ohos.base'; 4247 4248audioStreamManager.isActive(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => { 4249if (err) { 4250 console.error(`Failed to obtain the active status of the stream. ${err}`); 4251 return; 4252} 4253 console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`); 4254}); 4255``` 4256 4257### isActive<sup>9+</sup> 4258 4259isActive(volumeType: AudioVolumeType): Promise<boolean> 4260 4261获取指定音频流是否为活跃状态,使用Promise方式异步返回结果。 4262 4263**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4264 4265**参数:** 4266 4267| 参数名 | 类型 | 必填 | 说明 | 4268| ---------- | ----------------------------------- | ---- | ------------ | 4269| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4270 4271**返回值:** 4272 4273| 类型 | 说明 | 4274| ---------------------- | -------------------------------------------------------- | 4275| Promise<boolean> | Promise对象,返回流的活跃状态,true为活跃,false为不活跃。 | 4276 4277**示例:** 4278 4279```ts 4280audioStreamManager.isActive(audio.AudioVolumeType.MEDIA).then((value: boolean) => { 4281 console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`); 4282}); 4283``` 4284 4285### isActiveSync<sup>10+</sup> 4286 4287isActiveSync(volumeType: AudioVolumeType): boolean 4288 4289获取指定音频流是否为活跃状态,同步返回结果。 4290 4291**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4292 4293**参数:** 4294 4295| 参数名 | 类型 | 必填 | 说明 | 4296| ---------- | ----------------------------------- | ---- | ------------ | 4297| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音频流类型。 | 4298 4299**返回值:** 4300 4301| 类型 | 说明 | 4302| ---------------------- | -------------------------------------------------------- | 4303| boolean | 返回流的活跃状态,true为活跃,false为不活跃。 | 4304 4305**错误码:** 4306 4307以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4308 4309| 错误码ID | 错误信息 | 4310| ------- | --------------------------------------------| 4311| 6800101 | invalid parameter error | 4312 4313**示例:** 4314 4315```ts 4316import { BusinessError } from '@ohos.base'; 4317 4318try { 4319 let value: boolean = audioStreamManager.isActiveSync(audio.AudioVolumeType.MEDIA); 4320 console.info(`Indicate that the active status of the stream is obtained ${value}.`); 4321} catch (err) { 4322 let error = err as BusinessError; 4323 console.error(`Failed to obtain the active status of the stream ${error}.`); 4324} 4325``` 4326 4327### getAudioEffectInfoArray<sup>10+</sup> 4328 4329getAudioEffectInfoArray(usage: StreamUsage, callback: AsyncCallback<AudioEffectInfoArray>): void 4330 4331获取当前音效模式的信息。使用callback异步回调。 4332 4333**系统能力**: SystemCapability.Multimedia.Audio.Renderer 4334 4335**参数:** 4336 4337| 参数名 | 类型 | 必填 | 说明 | 4338| -------- | ----------------------------------- | -------- | --------------------------- | 4339| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4340| callback | AsyncCallback<[AudioEffectInfoArray](#audioeffectinfoarray10)> | 是 | 回调函数。当获取当前音效模式的信息成功,err为undefined,data为获取到的当前音效模式的信息;否则为错误对象。| 4341 4342**错误码:** 4343 4344以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4345 4346| 错误码ID | 错误信息 | 4347| ------- | --------------------------------------------| 4348| 6800101 | Invalid parameter error. Return by callback.| 4349 4350**示例:** 4351 4352```ts 4353import { BusinessError } from '@ohos.base'; 4354 4355audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4356 console.info('getAudioEffectInfoArray **** Get Callback Called ****'); 4357 if (err) { 4358 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4359 return; 4360 } else { 4361 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4362 } 4363}); 4364``` 4365 4366### getAudioEffectInfoArray<sup>10+</sup> 4367 4368getAudioEffectInfoArray(usage: StreamUsage): Promise<AudioEffectInfoArray> 4369 4370获取当前音效模式的信息。使用Promise异步回调。 4371 4372**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4373 4374**参数:** 4375 4376| 参数名 | 类型 | 必填 | 说明 | 4377| -------- | ----------------------------------- | -------- | --------------------------- | 4378| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4379 4380**返回值:** 4381 4382| 类型 | 说明 | 4383| --------------------------------------------------------------------------| --------------------------------------- | 4384| Promise<[AudioEffectInfoArray](#audioeffectinfoarray10)> | Promise对象,返回当前音效模式的信息。 | 4385 4386**错误码:** 4387 4388以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4389 4390| 错误码ID | 错误信息 | 4391| ------- | --------------------------------------------| 4392| 6800101 | Invalid parameter error. Return by promise. | 4393 4394**示例:** 4395 4396```ts 4397import { BusinessError } from '@ohos.base'; 4398 4399audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC).then((audioEffectInfoArray: audio.AudioEffectInfoArray) => { 4400 console.info('getAudioEffectInfoArray ######### Get Promise is called ##########'); 4401 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4402}).catch((err: BusinessError) => { 4403 console.error(`getAudioEffectInfoArray :ERROR: ${err}`); 4404}); 4405``` 4406 4407### getAudioEffectInfoArraySync<sup>10+</sup> 4408 4409getAudioEffectInfoArraySync(usage: StreamUsage): AudioEffectInfoArray 4410 4411获取当前音效模式的信息,同步返回结果。 4412 4413**系统能力:** SystemCapability.Multimedia.Audio.Renderer 4414 4415**参数:** 4416 4417| 参数名 | 类型 | 必填 | 说明 | 4418| -------- | ----------------------------------- | -------- | --------------------------- | 4419| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 4420 4421**返回值:** 4422 4423| 类型 | 说明 | 4424| --------------------------------------------------------------------------| --------------------------------------- | 4425| [AudioEffectInfoArray](#audioeffectinfoarray10) | 返回当前音效模式的信息。 | 4426 4427**错误码:** 4428 4429以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4430 4431| 错误码ID | 错误信息 | 4432| ------- | --------------------------------------------| 4433| 6800101 | invalid parameter error | 4434 4435**示例:** 4436 4437```ts 4438import { BusinessError } from '@ohos.base'; 4439 4440try { 4441 let audioEffectInfoArray: audio.AudioEffectInfoArray = audioStreamManager.getAudioEffectInfoArraySync(audio.StreamUsage.STREAM_USAGE_MUSIC); 4442 console.info(`The effect modes are: ${audioEffectInfoArray}`); 4443} catch (err) { 4444 let error = err as BusinessError; 4445 console.error(`getAudioEffectInfoArraySync ERROR: ${error}`); 4446} 4447``` 4448 4449## AudioRoutingManager<sup>9+</sup> 4450 4451音频路由管理。在使用AudioRoutingManager的接口前,需要使用[getRoutingManager](#getroutingmanager9)获取AudioRoutingManager实例。 4452 4453### getDevices<sup>9+</sup> 4454 4455getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 4456 4457获取音频设备列表,使用callback方式异步返回结果。 4458 4459**系统能力:** SystemCapability.Multimedia.Audio.Device 4460 4461**参数:** 4462 4463| 参数名 | 类型 | 必填 | 说明 | 4464| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 4465| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4466| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取音频设备列表成功,err为undefined,data为获取到的音频设备列表;否则为错误对象。 | 4467 4468**示例:** 4469 4470```ts 4471import { BusinessError } from '@ohos.base'; 4472 4473audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err: BusinessError, value: audio.AudioDeviceDescriptors) => { 4474 if (err) { 4475 console.error(`Failed to obtain the device list. ${err}`); 4476 return; 4477 } 4478 console.info('Callback invoked to indicate that the device list is obtained.'); 4479}); 4480``` 4481 4482### getDevices<sup>9+</sup> 4483 4484getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 4485 4486获取音频设备列表,使用Promise方式异步返回结果。 4487 4488**系统能力:** SystemCapability.Multimedia.Audio.Device 4489 4490**参数:** 4491 4492| 参数名 | 类型 | 必填 | 说明 | 4493| ---------- | ------------------------- | ---- | ---------------- | 4494| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4495 4496**返回值:** 4497 4498| 类型 | 说明 | 4499| ------------------------------------------------------------ | ------------------------- | 4500| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回设备列表。 | 4501 4502**示例:** 4503 4504```ts 4505audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => { 4506 console.info('Promise returned to indicate that the device list is obtained.'); 4507}); 4508``` 4509 4510### getDevicesSync<sup>10+</sup> 4511 4512getDevicesSync(deviceFlag: DeviceFlag): AudioDeviceDescriptors 4513 4514获取音频设备列表,同步返回结果。 4515 4516**系统能力:** SystemCapability.Multimedia.Audio.Device 4517 4518**参数:** 4519 4520| 参数名 | 类型 | 必填 | 说明 | 4521| ---------- | ------------------------- | ---- | ---------------- | 4522| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4523 4524**返回值:** 4525 4526| 类型 | 说明 | 4527| ------------------------------------------------------------ | ------------------------- | 4528| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回设备列表。 | 4529 4530**错误码:** 4531 4532以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4533 4534| 错误码ID | 错误信息 | 4535| ------- | --------------------------------------------| 4536| 6800101 | invalid parameter error | 4537 4538**示例:** 4539 4540```ts 4541import { BusinessError } from '@ohos.base'; 4542 4543try { 4544 let data: audio.AudioDeviceDescriptors = audioRoutingManager.getDevicesSync(audio.DeviceFlag.OUTPUT_DEVICES_FLAG); 4545 console.info(`Indicate that the device list is obtained ${data}`); 4546} catch (err) { 4547 let error = err as BusinessError; 4548 console.error(`Failed to obtain the device list. ${error}`); 4549} 4550``` 4551 4552### on('deviceChange')<sup>9+</sup> 4553 4554on(type: 'deviceChange', deviceFlag: DeviceFlag, callback: Callback<DeviceChangeAction\>): void 4555 4556设备更改。音频设备连接状态变化,使用callback方式返回结果。 4557 4558**系统能力:** SystemCapability.Multimedia.Audio.Device 4559 4560**参数:** 4561 4562| 参数名 | 类型 | 必填 | 说明 | 4563| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 4564| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 4565| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 4566| callback | Callback<[DeviceChangeAction](#devicechangeaction)\> | 是 | 回调函数,返回设备更新详情。 | 4567 4568**错误码:** 4569 4570以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4571 4572| 错误码ID | 错误信息 | 4573| ------- | --------------------------------------------| 4574| 6800101 | if input parameter value error | 4575 4576**示例:** 4577 4578```ts 4579audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (deviceChanged: audio.DeviceChangeAction) => { 4580 console.info('device change type : ' + deviceChanged.type); 4581 console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length); 4582 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 4583 console.info('device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 4584}); 4585``` 4586 4587### off('deviceChange')<sup>9+</sup> 4588 4589off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 4590 4591取消订阅音频设备连接变化事件,使用callback方式返回结果。 4592 4593**系统能力:** SystemCapability.Multimedia.Audio.Device 4594 4595**参数:** 4596 4597| 参数名 | 类型 | 必填 | 说明 | 4598| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 4599| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 4600| callback | Callback<[DeviceChangeAction](#devicechangeaction)> | 否 | 回调函数,返回设备更新详情。 | 4601 4602**错误码:** 4603 4604以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4605 4606| 错误码ID | 错误信息 | 4607| ------- | --------------------------------------------| 4608| 6800101 | if input parameter value error | 4609 4610**示例:** 4611 4612```ts 4613audioRoutingManager.off('deviceChange'); 4614``` 4615 4616### selectInputDevice<sup>9+</sup> 4617 4618selectInputDevice(inputAudioDevices: AudioDeviceDescriptors, callback: AsyncCallback<void>): void 4619 4620选择音频输入设备,当前只能选择一个输入设备,使用callback方式异步返回结果。 4621 4622**系统接口:** 该接口为系统接口 4623 4624**系统能力:** SystemCapability.Multimedia.Audio.Device 4625 4626**参数:** 4627 4628| 参数名 | 类型 | 必填 | 说明 | 4629| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4630| inputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输入设备类。 | 4631| callback | AsyncCallback<void> | 是 | 回调函数。当选择音频输入设备成功,err为undefined,否则为错误对象。 | 4632 4633**示例:** 4634```ts 4635import audio from '@ohos.multimedia.audio'; 4636import { BusinessError } from '@ohos.base'; 4637 4638let inputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 4639 deviceRole : audio.DeviceRole.INPUT_DEVICE, 4640 deviceType : audio.DeviceType.MIC, 4641 id : 1, 4642 name : "", 4643 address : "", 4644 sampleRates : [44100], 4645 channelCounts : [2], 4646 channelMasks : [0], 4647 networkId : audio.LOCAL_NETWORK_ID, 4648 interruptGroupId : 1, 4649 volumeGroupId : 1, 4650 displayName : "", 4651}]; 4652 4653async function selectInputDevice(){ 4654 audioRoutingManager.selectInputDevice(inputAudioDeviceDescriptor, (err: BusinessError) => { 4655 if (err) { 4656 console.error(`Result ERROR: ${err}`); 4657 } else { 4658 console.info('Select input devices result callback: SUCCESS'); 4659 } 4660 }); 4661} 4662``` 4663 4664### selectInputDevice<sup>9+</sup> 4665 4666selectInputDevice(inputAudioDevices: AudioDeviceDescriptors): Promise<void> 4667 4668**系统接口:** 该接口为系统接口 4669 4670选择音频输入设备,当前只能选择一个输入设备,使用Promise方式异步返回结果。 4671 4672**系统能力:** SystemCapability.Multimedia.Audio.Device 4673 4674**参数:** 4675 4676| 参数名 | 类型 | 必填 | 说明 | 4677| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4678| inputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输入设备类。 | 4679 4680**返回值:** 4681 4682| 类型 | 说明 | 4683| --------------------- | --------------------------- | 4684| Promise<void> | Promise对象,无返回结果。 | 4685 4686**示例:** 4687 4688```ts 4689import audio from '@ohos.multimedia.audio'; 4690import { BusinessError } from '@ohos.base'; 4691 4692let inputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 4693 deviceRole : audio.DeviceRole.INPUT_DEVICE, 4694 deviceType : audio.DeviceType.MIC, 4695 id : 1, 4696 name : "", 4697 address : "", 4698 sampleRates : [44100], 4699 channelCounts : [2], 4700 channelMasks : [0], 4701 networkId : audio.LOCAL_NETWORK_ID, 4702 interruptGroupId : 1, 4703 volumeGroupId : 1, 4704 displayName : "", 4705}]; 4706 4707async function getRoutingManager(){ 4708 audioRoutingManager.selectInputDevice(inputAudioDeviceDescriptor).then(() => { 4709 console.info('Select input devices result promise: SUCCESS'); 4710 }).catch((err: BusinessError) => { 4711 console.error(`Result ERROR: ${err}`); 4712 }); 4713} 4714``` 4715 4716### setCommunicationDevice<sup>9+</sup> 4717 4718setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean, callback: AsyncCallback<void>): void 4719 4720设置通信设备激活状态,使用callback方式异步返回结果。 4721 4722该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 4723 4724**系统能力:** SystemCapability.Multimedia.Audio.Communication 4725 4726**参数:** 4727 4728| 参数名 | 类型 | 必填 | 说明 | 4729| ---------- | ------------------------------------- | ---- |-------------------------| 4730| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 音频设备类型。 | 4731| active | boolean | 是 | 设备激活状态,true激活,false未激活。 | 4732| callback | AsyncCallback<void> | 是 | 回调函数。当设置通信设备激活状态成功,err为undefined,否则为错误对象。 | 4733 4734**示例:** 4735 4736```ts 4737import { BusinessError } from '@ohos.base'; 4738 4739audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true, (err: BusinessError) => { 4740 if (err) { 4741 console.error(`Failed to set the active status of the device. ${err}`); 4742 return; 4743 } 4744 console.info('Callback invoked to indicate that the device is set to the active status.'); 4745}); 4746``` 4747 4748### setCommunicationDevice<sup>9+</sup> 4749 4750setCommunicationDevice(deviceType: CommunicationDeviceType, active: boolean): Promise<void> 4751 4752设置通信设备激活状态,使用Promise方式异步返回结果。 4753 4754该接口由于功能设计变化,将在后续版本废弃,不建议开发者使用。 4755 4756**系统能力:** SystemCapability.Multimedia.Audio.Communication 4757 4758**参数:** 4759 4760| 参数名 | 类型 | 必填 | 说明 | 4761| ---------- | ----------------------------------------------------- | ---- | ------------------ | 4762| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4763| active | boolean | 是 | 设备激活状态,true激活,false未激活。 | 4764 4765**返回值:** 4766 4767| 类型 | 说明 | 4768| ------------------- | ------------------------------- | 4769| Promise<void> | Promise对象,无返回结果。 | 4770 4771**示例:** 4772 4773```ts 4774audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true).then(() => { 4775 console.info('Promise returned to indicate that the device is set to the active status.'); 4776}); 4777``` 4778 4779### isCommunicationDeviceActive<sup>9+</sup> 4780 4781isCommunicationDeviceActive(deviceType: CommunicationDeviceType, callback: AsyncCallback<boolean>): void 4782 4783获取指定通信设备的激活状态,使用callback方式异步返回结果。 4784 4785**系统能力:** SystemCapability.Multimedia.Audio.Communication 4786 4787**参数:** 4788 4789| 参数名 | 类型 | 必填 | 说明 | 4790| ---------- | ---------------------------------------------------- | ---- | ------------------------ | 4791| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4792| callback | AsyncCallback<boolean> | 是 | 回调函数。当获取指定通信设备的激活状态成功,err为undefined,data为true为激活,false为未激活;否则为错误对象。 | 4793 4794**示例:** 4795 4796```ts 4797import { BusinessError } from '@ohos.base'; 4798 4799audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER, (err: BusinessError, value: boolean) => { 4800 if (err) { 4801 console.error(`Failed to obtain the active status of the device. ${err}`); 4802 return; 4803 } 4804 console.info('Callback invoked to indicate that the active status of the device is obtained.'); 4805}); 4806``` 4807 4808### isCommunicationDeviceActive<sup>9+</sup> 4809 4810isCommunicationDeviceActive(deviceType: CommunicationDeviceType): Promise<boolean> 4811 4812获取指定通信设备的激活状态,使用Promise方式异步返回结果。 4813 4814**系统能力:** SystemCapability.Multimedia.Audio.Communication 4815 4816**参数:** 4817 4818| 参数名 | 类型 | 必填 | 说明 | 4819| ---------- | ---------------------------------------------------- | ---- | ------------------ | 4820| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4821 4822**返回值:** 4823 4824| Type | Description | 4825| ---------------------- | ------------------------------- | 4826| Promise<boolean> | Promise对象,返回设备的激活状态,true激活,false未激活。 | 4827 4828**示例:** 4829 4830```ts 4831audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER).then((value: boolean) => { 4832 console.info(`Promise returned to indicate that the active status of the device is obtained ${value}.`); 4833}); 4834``` 4835 4836### isCommunicationDeviceActiveSync<sup>10+</sup> 4837 4838isCommunicationDeviceActiveSync(deviceType: CommunicationDeviceType): boolean 4839 4840获取指定通信设备的激活状态,同步返回结果。 4841 4842**系统能力:** SystemCapability.Multimedia.Audio.Communication 4843 4844**参数:** 4845 4846| 参数名 | 类型 | 必填 | 说明 | 4847| ---------- | ---------------------------------------------------- | ---- | ------------------ | 4848| deviceType | [CommunicationDeviceType](#communicationdevicetype9) | 是 | 活跃音频设备类型。 | 4849 4850**返回值:** 4851 4852| Type | Description | 4853| ---------------------- | ------------------------------- | 4854| boolean | 返回设备的激活状态,true激活,false未激活。 | 4855 4856**错误码:** 4857 4858以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 4859 4860| 错误码ID | 错误信息 | 4861| ------- | --------------------------------------------| 4862| 6800101 | invalid parameter error | 4863 4864**示例:** 4865 4866```ts 4867import { BusinessError } from '@ohos.base'; 4868 4869try { 4870 let value: boolean = audioRoutingManager.isCommunicationDeviceActiveSync(audio.CommunicationDeviceType.SPEAKER); 4871 console.info(`Indicate that the active status of the device is obtained ${value}.`); 4872} catch (err) { 4873 let error = err as BusinessError; 4874 console.error(`Failed to obtain the active status of the device ${error}.`); 4875} 4876``` 4877 4878### selectOutputDevice<sup>9+</sup> 4879 4880selectOutputDevice(outputAudioDevices: AudioDeviceDescriptors, callback: AsyncCallback<void>): void 4881 4882选择音频输出设备,当前只能选择一个输出设备,使用callback方式异步返回结果。 4883 4884**系统接口:** 该接口为系统接口 4885 4886**系统能力:** SystemCapability.Multimedia.Audio.Device 4887 4888**参数:** 4889 4890| 参数名 | 类型 | 必填 | 说明 | 4891| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4892| outputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输出设备类。 | 4893| callback | AsyncCallback<void> | 是 | 回调函数。当选择音频输出设备成功,err为undefined,否则为错误对象。 | 4894 4895**示例:** 4896```ts 4897import audio from '@ohos.multimedia.audio'; 4898import { BusinessError } from '@ohos.base'; 4899 4900let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 4901 deviceRole : audio.DeviceRole.OUTPUT_DEVICE, 4902 deviceType : audio.DeviceType.SPEAKER, 4903 id : 1, 4904 name : "", 4905 address : "", 4906 sampleRates : [44100], 4907 channelCounts : [2], 4908 channelMasks : [0], 4909 networkId : audio.LOCAL_NETWORK_ID, 4910 interruptGroupId : 1, 4911 volumeGroupId : 1, 4912 displayName : "", 4913}]; 4914 4915async function selectOutputDevice(){ 4916 audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor, (err: BusinessError) => { 4917 if (err) { 4918 console.error(`Result ERROR: ${err}`); 4919 } else { 4920 console.info('Select output devices result callback: SUCCESS'); } 4921 }); 4922} 4923``` 4924 4925### selectOutputDevice<sup>9+</sup> 4926 4927selectOutputDevice(outputAudioDevices: AudioDeviceDescriptors): Promise<void> 4928 4929**系统接口:** 该接口为系统接口 4930 4931选择音频输出设备,当前只能选择一个输出设备,使用Promise方式异步返回结果。 4932 4933**系统能力:** SystemCapability.Multimedia.Audio.Device 4934 4935**参数:** 4936 4937| 参数名 | 类型 | 必填 | 说明 | 4938| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4939| outputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输出设备类。 | 4940 4941**返回值:** 4942 4943| 类型 | 说明 | 4944| --------------------- | --------------------------- | 4945| Promise<void> | Promise对象,无返回结果。 | 4946 4947**示例:** 4948 4949```ts 4950import audio from '@ohos.multimedia.audio'; 4951import { BusinessError } from '@ohos.base'; 4952 4953let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 4954 deviceRole : audio.DeviceRole.OUTPUT_DEVICE, 4955 deviceType : audio.DeviceType.SPEAKER, 4956 id : 1, 4957 name : "", 4958 address : "", 4959 sampleRates : [44100], 4960 channelCounts : [2], 4961 channelMasks : [0], 4962 networkId : audio.LOCAL_NETWORK_ID, 4963 interruptGroupId : 1, 4964 volumeGroupId : 1, 4965 displayName : "", 4966}]; 4967 4968async function selectOutputDevice(){ 4969 audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor).then(() => { 4970 console.info('Select output devices result promise: SUCCESS'); 4971 }).catch((err: BusinessError) => { 4972 console.error(`Result ERROR: ${err}`); 4973 }); 4974} 4975``` 4976 4977### selectOutputDeviceByFilter<sup>9+</sup> 4978 4979selectOutputDeviceByFilter(filter: AudioRendererFilter, outputAudioDevices: AudioDeviceDescriptors, callback: AsyncCallback<void>): void 4980 4981**系统接口:** 该接口为系统接口 4982 4983根据过滤条件,选择音频输出设备,当前只能选择一个输出设备,使用callback方式异步返回结果。 4984 4985**系统能力:** SystemCapability.Multimedia.Audio.Device 4986 4987**参数:** 4988 4989| 参数名 | 类型 | 必填 | 说明 | 4990| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 4991| filter | [AudioRendererFilter](#audiorendererfilter9) | 是 | 过滤条件类。 | 4992| outputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输出设备类。 | 4993| callback | AsyncCallback<void> | 是 | 回调函数。当选择音频输出设备成功,err为undefined,否则为错误对象。 | 4994 4995**示例:** 4996```ts 4997import audio from '@ohos.multimedia.audio'; 4998import { BusinessError } from '@ohos.base'; 4999 5000let outputAudioRendererFilter: audio.AudioRendererFilter = { 5001 uid : 20010041, 5002 rendererInfo : { 5003 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5004 rendererFlags : 0 5005 }, 5006 rendererId : 0 5007}; 5008 5009let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 5010 deviceRole : audio.DeviceRole.OUTPUT_DEVICE, 5011 deviceType : audio.DeviceType.SPEAKER, 5012 id : 1, 5013 name : "", 5014 address : "", 5015 sampleRates : [44100], 5016 channelCounts : [2], 5017 channelMasks : [0], 5018 networkId : audio.LOCAL_NETWORK_ID, 5019 interruptGroupId : 1, 5020 volumeGroupId : 1, 5021 displayName : "", 5022}]; 5023 5024async function selectOutputDeviceByFilter(){ 5025 audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor, (err: BusinessError) => { 5026 if (err) { 5027 console.error(`Result ERROR: ${err}`); 5028 } else { 5029 console.info('Select output devices by filter result callback: SUCCESS'); } 5030 }); 5031} 5032``` 5033 5034### selectOutputDeviceByFilter<sup>9+</sup> 5035 5036selectOutputDeviceByFilter(filter: AudioRendererFilter, outputAudioDevices: AudioDeviceDescriptors): Promise<void> 5037 5038**系统接口:** 该接口为系统接口 5039 5040根据过滤条件,选择音频输出设备,当前只能选择一个输出设备,使用Promise方式异步返回结果。 5041 5042**系统能力:** SystemCapability.Multimedia.Audio.Device 5043 5044**参数:** 5045 5046| 参数名 | 类型 | 必填 | 说明 | 5047| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5048| filter | [AudioRendererFilter](#audiorendererfilter9) | 是 | 过滤条件类。 | 5049| outputAudioDevices | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 输出设备类。 | 5050 5051**返回值:** 5052 5053| 类型 | 说明 | 5054| --------------------- | --------------------------- | 5055| Promise<void> | Promise对象,无返回结果。 | 5056 5057**示例:** 5058 5059```ts 5060import audio from '@ohos.multimedia.audio'; 5061import { BusinessError } from '@ohos.base'; 5062 5063let outputAudioRendererFilter: audio.AudioRendererFilter = { 5064 uid : 20010041, 5065 rendererInfo : { 5066 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5067 rendererFlags : 0 5068 }, 5069 rendererId : 0 5070}; 5071 5072let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 5073 deviceRole : audio.DeviceRole.OUTPUT_DEVICE, 5074 deviceType : audio.DeviceType.SPEAKER, 5075 id : 1, 5076 name : "", 5077 address : "", 5078 sampleRates : [44100], 5079 channelCounts : [2], 5080 channelMasks : [0], 5081 networkId : audio.LOCAL_NETWORK_ID, 5082 interruptGroupId : 1, 5083 volumeGroupId : 1, 5084 displayName : "", 5085}]; 5086 5087async function selectOutputDeviceByFilter(){ 5088 audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor).then(() => { 5089 console.info('Select output devices by filter result promise: SUCCESS'); 5090 }).catch((err: BusinessError) => { 5091 console.error(`Result ERROR: ${err}`); 5092 }) 5093} 5094``` 5095 5096### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 5097 5098getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 5099 5100根据音频信息,返回优先级最高的输出设备,使用callback方式异步返回结果。 5101 5102**系统能力:** SystemCapability.Multimedia.Audio.Device 5103 5104**参数:** 5105 5106| 参数名 | 类型 | 必填 | 说明 | 5107| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 5108| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 5109| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输出设备成功,err为undefined,data为获取到的优先级最高的输出设备信息;否则为错误对象。 | 5110 5111**错误码:** 5112 5113以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5114 5115| 错误码ID | 错误信息 | 5116| ------- |--------------------------------------------------| 5117| 6800101 | Input parameter value error. Return by callback. | 5118| 6800301 | System error. Return by callback. | 5119 5120**示例:** 5121```ts 5122import audio from '@ohos.multimedia.audio'; 5123import { BusinessError } from '@ohos.base'; 5124 5125let rendererInfo: audio.AudioRendererInfo = { 5126 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5127 rendererFlags : 0 5128} 5129 5130async function getPreferOutputDevice() { 5131 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 5132 if (err) { 5133 console.error(`Result ERROR: ${err}`); 5134 } else { 5135 console.info(`device descriptor: ${desc}`); 5136 } 5137 }); 5138} 5139``` 5140 5141### getPreferOutputDeviceForRendererInfo<sup>10+</sup> 5142getPreferOutputDeviceForRendererInfo(rendererInfo: AudioRendererInfo): Promise<AudioDeviceDescriptors> 5143 5144根据音频信息,返回优先级最高的输出设备,使用Promise方式异步返回结果。 5145 5146**系统能力:** SystemCapability.Multimedia.Audio.Device 5147 5148**参数:** 5149 5150| 参数名 | 类型 | 必填 | 说明 | 5151| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5152| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 5153 5154**返回值:** 5155 5156| 类型 | 说明 | 5157| --------------------- | --------------------------- | 5158| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输出设备信息。 | 5159 5160**错误码:** 5161 5162以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5163 5164| 错误码ID | 错误信息 | 5165| ------- |-------------------------------------------------| 5166| 6800101 | Input parameter value error. Return by promise. | 5167| 6800301 | System error. Return by promise. | 5168 5169**示例:** 5170 5171```ts 5172import audio from '@ohos.multimedia.audio'; 5173import { BusinessError } from '@ohos.base'; 5174 5175let rendererInfo: audio.AudioRendererInfo = { 5176 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5177 rendererFlags : 0 5178} 5179 5180async function getPreferOutputDevice() { 5181 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => { 5182 console.info(`device descriptor: ${desc}`); 5183 }).catch((err: BusinessError) => { 5184 console.error(`Result ERROR: ${err}`); 5185 }) 5186} 5187``` 5188 5189### getPreferredOutputDeviceForRendererInfoSync<sup>10+</sup> 5190getPreferredOutputDeviceForRendererInfoSync(rendererInfo: AudioRendererInfo): AudioDeviceDescriptors 5191 5192根据音频信息,返回优先级最高的输出设备,同步返回结果。 5193 5194**系统能力:** SystemCapability.Multimedia.Audio.Device 5195 5196**参数:** 5197 5198| 参数名 | 类型 | 必填 | 说明 | 5199| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5200| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 5201 5202**返回值:** 5203 5204| 类型 | 说明 | 5205| --------------------- | --------------------------- | 5206| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输出设备信息。 | 5207 5208**错误码:** 5209 5210以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5211 5212| 错误码ID | 错误信息 | 5213| ------- | --------------------------------------------| 5214| 6800101 | invalid parameter error. | 5215 5216**示例:** 5217 5218```ts 5219import audio from '@ohos.multimedia.audio'; 5220import { BusinessError } from '@ohos.base'; 5221 5222let rendererInfo: audio.AudioRendererInfo = { 5223 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5224 rendererFlags : 0 5225} 5226 5227try { 5228 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredOutputDeviceForRendererInfoSync(rendererInfo); 5229 console.info(`device descriptor: ${desc}`); 5230} catch (err) { 5231 let error = err as BusinessError; 5232 console.error(`Result ERROR: ${error}`); 5233} 5234``` 5235 5236### on('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 5237 5238on(type: 'preferOutputDeviceChangeForRendererInfo', rendererInfo: AudioRendererInfo, callback: Callback<AudioDeviceDescriptors\>): void 5239 5240订阅最高优先级输出设备变化事件,使用callback方式返回结果。 5241 5242**系统能力:** SystemCapability.Multimedia.Audio.Device 5243 5244**参数:** 5245 5246| 参数名 | 类型 | 必填 | 说明 | 5247| :------- | :--------------------------------------------------- | :--- |:--------------------------------------------------------| 5248| type | string | 是 | 订阅的事件的类型。支持事件:'preferOutputDeviceChangeForRendererInfo' | 5249| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 5250| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输出设备信息。 | 5251 5252**错误码:** 5253 5254以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5255 5256| 错误码ID | 错误信息 | 5257| ------- | --------------------------------------------| 5258| 6800101 | if input parameter value error | 5259 5260**示例:** 5261 5262```ts 5263import audio from '@ohos.multimedia.audio'; 5264 5265let rendererInfo: audio.AudioRendererInfo = { 5266 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5267 rendererFlags : 0 5268} 5269 5270audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => { 5271 console.info(`device descriptor: ${desc}`); 5272}); 5273``` 5274 5275### off('preferOutputDeviceChangeForRendererInfo')<sup>10+</sup> 5276 5277off(type: 'preferOutputDeviceChangeForRendererInfo', callback?: Callback<AudioDeviceDescriptors\>): void 5278 5279取消订阅最高优先级输出音频设备变化事件,使用callback方式返回结果。 5280 5281**系统能力:** SystemCapability.Multimedia.Audio.Device 5282 5283**参数:** 5284 5285| 参数名 | 类型 | 必填 | 说明 | 5286| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5287| type | string | 是 | 订阅的事件的类型。支持事件:'preferOutputDeviceChangeForRendererInfo' | 5288| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输出设备信息。 | 5289 5290**错误码:** 5291 5292以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5293 5294| 错误码ID | 错误信息 | 5295| ------- | --------------------------------------------| 5296| 6800101 | if input parameter value error | 5297 5298**示例:** 5299 5300```ts 5301audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo'); 5302``` 5303 5304### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 5305 5306getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo, callback: AsyncCallback<AudioDeviceDescriptors>): void 5307 5308根据音频信息,返回优先级最高的输入设备,使用callback方式异步返回结果。 5309 5310**系统能力:** SystemCapability.Multimedia.Audio.Device 5311 5312**参数:** 5313 5314| 参数名 | 类型 | 必填 | 说明 | 5315| --------------------------- | ------------------------------------------------------------ | ---- | ------------------------- | 5316| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5317| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数。当获取优先级最高的输入设备成功,err为undefined,data为获取到的优先级最高的输入设备信息;否则为错误对象。 | 5318 5319**错误码:** 5320 5321以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5322 5323| 错误码ID | 错误信息 | 5324| ------- | --------------------------------------------| 5325| 6800101 | Invalid parameter error. Return by callback.| 5326| 6800301 | System error. Return by callback. | 5327 5328**示例:** 5329```ts 5330import audio from '@ohos.multimedia.audio'; 5331import { BusinessError } from '@ohos.base'; 5332 5333let capturerInfo: audio.AudioCapturerInfo = { 5334 source: audio.SourceType.SOURCE_TYPE_MIC, 5335 capturerFlags: 0 5336} 5337 5338audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo, (err: BusinessError, desc: audio.AudioDeviceDescriptors) => { 5339 if (err) { 5340 console.error(`Result ERROR: ${err}`); 5341 } else { 5342 console.info(`device descriptor: ${desc}`); 5343 } 5344}); 5345``` 5346 5347### getPreferredInputDeviceForCapturerInfo<sup>10+</sup> 5348 5349getPreferredInputDeviceForCapturerInfo(capturerInfo: AudioCapturerInfo): Promise<AudioDeviceDescriptors> 5350 5351根据音频信息,返回优先级最高的输入设备,使用Promise方式异步返回结果。 5352 5353**系统能力:** SystemCapability.Multimedia.Audio.Device 5354 5355**参数:** 5356 5357| 参数名 | 类型 | 必填 | 说明 | 5358| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5359| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5360 5361**返回值:** 5362 5363| 类型 | 说明 | 5364| --------------------- | --------------------------- | 5365| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise对象,返回优先级最高的输入设备信息。 | 5366 5367**错误码:** 5368 5369以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5370 5371| 错误码ID | 错误信息 | 5372| ------- | --------------------------------------------| 5373| 6800101 | Invalid parameter error. Return by promise. | 5374| 6800301 | System error. Return by promise. | 5375 5376**示例:** 5377 5378```ts 5379import audio from '@ohos.multimedia.audio'; 5380import { BusinessError } from '@ohos.base'; 5381 5382let capturerInfo: audio.AudioCapturerInfo = { 5383 source: audio.SourceType.SOURCE_TYPE_MIC, 5384 capturerFlags: 0 5385} 5386 5387audioRoutingManager.getPreferredInputDeviceForCapturerInfo(capturerInfo).then((desc: audio.AudioDeviceDescriptors) => { 5388 console.info(`device descriptor: ${desc}`); 5389}).catch((err: BusinessError) => { 5390 console.error(`Result ERROR: ${err}`); 5391}); 5392``` 5393 5394### getPreferredInputDeviceForCapturerInfoSync<sup>10+</sup> 5395 5396getPreferredInputDeviceForCapturerInfoSync(capturerInfo: AudioCapturerInfo): AudioDeviceDescriptors 5397 5398根据音频信息,返回优先级最高的输入设备,同步返回结果。 5399 5400**系统能力:** SystemCapability.Multimedia.Audio.Device 5401 5402**参数:** 5403 5404| 参数名 | 类型 | 必填 | 说明 | 5405| ----------------------| ------------------------------------------------------------ | ---- | ------------------------- | 5406| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5407 5408**返回值:** 5409 5410| 类型 | 说明 | 5411| --------------------- | --------------------------- | 5412| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回优先级最高的输入设备信息。 | 5413 5414**错误码:** 5415 5416以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5417 5418| 错误码ID | 错误信息 | 5419| ------- | --------------------------------------------| 5420| 6800101 | invalid parameter error | 5421 5422**示例:** 5423 5424```ts 5425import audio from '@ohos.multimedia.audio'; 5426import { BusinessError } from '@ohos.base'; 5427 5428let capturerInfo: audio.AudioCapturerInfo = { 5429 source: audio.SourceType.SOURCE_TYPE_MIC, 5430 capturerFlags: 0 5431} 5432 5433try { 5434 let desc: audio.AudioDeviceDescriptors = audioRoutingManager.getPreferredInputDeviceForCapturerInfoSync(capturerInfo); 5435 console.info(`device descriptor: ${desc}`); 5436} catch (err) { 5437 let error = err as BusinessError; 5438 console.error(`Result ERROR: ${error}`); 5439} 5440``` 5441 5442### on('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5443 5444on(type: 'preferredInputDeviceChangeForCapturerInfo', capturerInfo: AudioCapturerInfo, callback: Callback<AudioDeviceDescriptors\>): void 5445 5446订阅最高优先级输入设备变化事件,使用callback方式返回结果。 5447 5448**系统能力:** SystemCapability.Multimedia.Audio.Device 5449 5450**参数:** 5451 5452| 参数名 | 类型 | 必填 | 说明 | 5453| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 5454| type | string | 是 | 订阅的事件的类型。支持事件:'preferredInputDeviceChangeForCapturerInfo' | 5455| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 表示采集器信息。 | 5456| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)\> | 是 | 回调函数,返回优先级最高的输入设备信息。 | 5457 5458**错误码:** 5459 5460以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5461 5462| 错误码ID | 错误信息 | 5463| ------- | --------------------------------------------| 5464| 6800101 | if input parameter value error | 5465 5466**示例:** 5467 5468```ts 5469import audio from '@ohos.multimedia.audio'; 5470 5471let capturerInfo: audio.AudioCapturerInfo = { 5472 source: audio.SourceType.SOURCE_TYPE_MIC, 5473 capturerFlags: 0 5474} 5475 5476audioRoutingManager.on('preferredInputDeviceChangeForCapturerInfo', capturerInfo, (desc: audio.AudioDeviceDescriptors) => { 5477 console.info(`device descriptor: ${desc}`); 5478}); 5479``` 5480 5481### off('preferredInputDeviceChangeForCapturerInfo')<sup>10+</sup> 5482 5483off(type: 'preferredInputDeviceChangeForCapturerInfo', callback?: Callback<AudioDeviceDescriptors\>): void 5484 5485取消订阅最高优先级输入音频设备变化事件,使用callback方式返回结果。 5486 5487**系统能力:** SystemCapability.Multimedia.Audio.Device 5488 5489**参数:** 5490 5491| 参数名 | 类型 | 必填 | 说明 | 5492| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 5493| type | string | 是 | 订阅的事件的类型。支持事件:'preferredInputDeviceChangeForCapturerInfo' | 5494| callback | Callback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回优先级最高的输入设备信息。 | 5495 5496**错误码:** 5497 5498以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5499 5500| 错误码ID | 错误信息 | 5501| ------- | --------------------------------------------| 5502| 6800101 | if input parameter value error | 5503 5504**示例:** 5505 5506```ts 5507audioRoutingManager.off('preferredInputDeviceChangeForCapturerInfo'); 5508``` 5509 5510## AudioRendererChangeInfoArray<sup>9+</sup> 5511 5512数组类型,AudioRenderChangeInfo数组,只读。 5513 5514**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5515 5516## AudioRendererChangeInfo<sup>9+</sup> 5517 5518描述音频渲染器更改信息。 5519 5520**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5521 5522| 名称 | 类型 | 可读 | 可写 | 说明 | 5523| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5524| streamId | number | 是 | 否 | 音频流唯一id。 | 5525| clientUid | number | 是 | 否 | 音频渲染器客户端应用程序的Uid。<br/>此接口为系统接口。 | 5526| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 否 | 音频渲染器信息。 | 5527| rendererState | [AudioState](#audiostate8) | 是 | 否 | 音频状态。<br/>此接口为系统接口。| 5528| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备描述。| 5529 5530**示例:** 5531 5532```ts 5533import audio from '@ohos.multimedia.audio'; 5534 5535const audioManager = audio.getAudioManager(); 5536let audioStreamManager = audioManager.getStreamManager(); 5537 5538audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => { 5539 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 5540 console.info(`## RendererChange on is called for ${i} ##`); 5541 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfoArray[i].streamId}`); 5542 console.info(`Content for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.content}`); 5543 console.info(`Stream for ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.usage}`); 5544 console.info(`Flag ${i} is: ${AudioRendererChangeInfoArray[i].rendererInfo.rendererFlags}`); 5545 let devDescriptor = AudioRendererChangeInfoArray[i].deviceDescriptors; 5546 for (let j = 0; j < AudioRendererChangeInfoArray[i].deviceDescriptors.length; j++) { 5547 console.info(`Id: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].id}`); 5548 console.info(`Type: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 5549 console.info(`Role: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 5550 console.info(`Name: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].name}`); 5551 console.info(`Addr: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].address}`); 5552 console.info(`SR: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 5553 console.info(`C ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 5554 console.info(`CM: ${i} : ${AudioRendererChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 5555 } 5556 } 5557}); 5558``` 5559 5560 5561## AudioCapturerChangeInfoArray<sup>9+</sup> 5562 5563数组类型,AudioCapturerChangeInfo数组,只读。 5564 5565**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5566 5567## AudioCapturerChangeInfo<sup>9+</sup> 5568 5569描述音频采集器更改信息。 5570 5571**系统能力:** SystemCapability.Multimedia.Audio.Capturer 5572 5573| 名称 | 类型 | 可读 | 可写 | 说明 | 5574| -------------------| ----------------------------------------- | ---- | ---- | ---------------------------- | 5575| streamId | number | 是 | 否 | 音频流唯一id。 | 5576| clientUid | number | 是 | 否 | 音频采集器客户端应用程序的Uid。<br/>此接口为系统接口。 | 5577| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo8) | 是 | 否 | 音频采集器信息。 | 5578| capturerState | [AudioState](#audiostate8) | 是 | 否 | 音频状态。<br/>此接口为系统接口。| 5579| deviceDescriptors | [AudioDeviceDescriptors](#audiodevicedescriptors) | 是 | 否 | 音频设备描述。| 5580 5581**示例:** 5582 5583```ts 5584import audio from '@ohos.multimedia.audio'; 5585 5586const audioManager = audio.getAudioManager(); 5587let audioStreamManager = audioManager.getStreamManager(); 5588 5589audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) => { 5590 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 5591 console.info(`## CapChange on is called for element ${i} ##`); 5592 console.info(`StrId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 5593 console.info(`Src for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 5594 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 5595 let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors; 5596 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 5597 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 5598 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 5599 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 5600 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 5601 console.info(`Addr: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 5602 console.info(`SR: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 5603 console.info(`C ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 5604 console.info(`CM ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks[0]}`); 5605 } 5606 } 5607}); 5608``` 5609 5610## AudioEffectInfoArray<sup>10+</sup> 5611 5612待查询ContentType和StreamUsage组合场景下的音效模式数组类型,[AudioEffectMode](#audioeffectmode10)数组,只读。 5613 5614## AudioDeviceDescriptors 5615 5616设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 5617 5618## AudioDeviceDescriptor 5619 5620描述音频设备。 5621 5622**系统能力:** SystemCapability.Multimedia.Audio.Device 5623 5624| 名称 | 类型 | 可读 | 可写 | 说明 | 5625| ----------------------------- | -------------------------- | ---- | ---- | ---------- | 5626| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 | 5627| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 | 5628| id<sup>9+</sup> | number | 是 | 否 | 设备id,唯一。 | 5629| name<sup>9+</sup> | string | 是 | 否 | 设备名称。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 | 5630| address<sup>9+</sup> | string | 是 | 否 | 设备地址。<br>如果是蓝牙设备,需要申请权限ohos.permission.USE_BLUETOOTH。 | 5631| sampleRates<sup>9+</sup> | Array<number> | 是 | 否 | 支持的采样率。 | 5632| channelCounts<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道数。 | 5633| channelMasks<sup>9+</sup> | Array<number> | 是 | 否 | 支持的通道掩码。 | 5634| displayName<sup>10+</sup> | string | 是 | 否 | 设备显示名。 | 5635| networkId<sup>9+</sup> | string | 是 | 否 | 设备组网的ID。<br/>此接口为系统接口。 | 5636| interruptGroupId<sup>9+</sup> | number | 是 | 否 | 设备所处的焦点组ID。<br/>此接口为系统接口。 | 5637| volumeGroupId<sup>9+</sup> | number | 是 | 否 | 设备所处的音量组ID。<br/>此接口为系统接口。 | 5638 5639**示例:** 5640 5641```ts 5642import audio from '@ohos.multimedia.audio'; 5643 5644function displayDeviceProp(value: audio.AudioDeviceDescriptor) { 5645 deviceRoleValue = value.deviceRole; 5646 deviceTypeValue = value.deviceType; 5647} 5648 5649let deviceRoleValue: audio.DeviceRole | undefined = undefined;; 5650let deviceTypeValue: audio.DeviceType | undefined = undefined;; 5651audio.getAudioManager().getDevices(1).then((value: audio.AudioDeviceDescriptors) => { 5652 console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG'); 5653 value.forEach(displayDeviceProp); 5654 if (deviceTypeValue != undefined && deviceRoleValue != undefined){ 5655 console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : PASS'); 5656 } else { 5657 console.error('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : FAIL'); 5658 } 5659}); 5660``` 5661 5662## AudioRendererFilter<sup>9+</sup> 5663 5664过滤条件类。在调用selectOutputDeviceByFilter接口前,需要先创建AudioRendererFilter实例。 5665 5666**系统接口:** 该接口为系统接口 5667 5668| 名称 | 类型 | 必填 | 说明 | 5669| -------------| ---------------------------------------- | ---- | -------------- | 5670| uid | number | 否 | 表示应用ID。<br> **系统能力:** SystemCapability.Multimedia.Audio.Core| 5671| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 否 | 表示渲染器信息。<br> **系统能力:** SystemCapability.Multimedia.Audio.Renderer| 5672| rendererId | number | 否 | 音频流唯一id。<br> **系统能力:** SystemCapability.Multimedia.Audio.Renderer| 5673 5674**示例:** 5675 5676```ts 5677import audio from '@ohos.multimedia.audio'; 5678 5679let outputAudioRendererFilter: audio.AudioRendererFilter = { 5680 uid : 20010041, 5681 rendererInfo : { 5682 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 5683 rendererFlags : 0 5684 }, 5685 rendererId : 0 5686}; 5687``` 5688 5689## AudioRenderer<sup>8+</sup> 5690 5691提供音频渲染的相关接口。在调用AudioRenderer的接口前,需要先通过[createAudioRenderer](#audiocreateaudiorenderer8)创建实例。 5692 5693### 属性 5694 5695**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5696 5697| 名称 | 类型 | 可读 | 可写 | 说明 | 5698| ----- | -------------------------- | ---- | ---- | ------------------ | 5699| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频渲染器的状态。 | 5700 5701**示例:** 5702 5703```ts 5704import audio from '@ohos.multimedia.audio'; 5705 5706let state: audio.AudioState = audioRenderer.state; 5707``` 5708 5709### getRendererInfo<sup>8+</sup> 5710 5711getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void 5712 5713获取当前被创建的音频渲染器的信息,使用callback方式异步返回结果。 5714 5715**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5716 5717**参数:** 5718 5719| 参数名 | 类型 | 必填 | 说明 | 5720| :------- | :------------------------------------------------------- | :--- | :--------------------- | 5721| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是 | 回调函数。当获取音频渲染器的信息成功,err为undefined,data为获取到的音频渲染器的信息;否则为错误对象。 | 5722 5723**示例:** 5724 5725```ts 5726import { BusinessError } from '@ohos.base'; 5727 5728audioRenderer.getRendererInfo((err: BusinessError, rendererInfo: audio.AudioRendererInfo) => { 5729 console.info('Renderer GetRendererInfo:'); 5730 console.info(`Renderer content: ${rendererInfo.content}`); 5731 console.info(`Renderer usage: ${rendererInfo.usage}`); 5732 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`); 5733}); 5734``` 5735 5736### getRendererInfo<sup>8+</sup> 5737 5738getRendererInfo(): Promise<AudioRendererInfo\> 5739 5740获取当前被创建的音频渲染器的信息,使用Promise方式异步返回结果。 5741 5742**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5743 5744**返回值:** 5745 5746| 类型 | 说明 | 5747| -------------------------------------------------- | ------------------------------- | 5748| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise对象,返回音频渲染器信息。 | 5749 5750**示例:** 5751 5752```ts 5753import { BusinessError } from '@ohos.base'; 5754 5755audioRenderer.getRendererInfo().then((rendererInfo: audio.AudioRendererInfo) => { 5756 console.info('Renderer GetRendererInfo:'); 5757 console.info(`Renderer content: ${rendererInfo.content}`); 5758 console.info(`Renderer usage: ${rendererInfo.usage}`); 5759 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 5760}).catch((err: BusinessError) => { 5761 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${err}`); 5762}); 5763``` 5764 5765### getRendererInfoSync<sup>10+</sup> 5766 5767getRendererInfoSync(): AudioRendererInfo 5768 5769获取当前被创建的音频渲染器的信息,同步返回结果。 5770 5771**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5772 5773**返回值:** 5774 5775| 类型 | 说明 | 5776| -------------------------------------------------- | ------------------------------- | 5777| [AudioRendererInfo](#audiorendererinfo8) | 返回音频渲染器信息。 | 5778 5779**示例:** 5780 5781```ts 5782import { BusinessError } from '@ohos.base'; 5783 5784try { 5785 let rendererInfo: audio.AudioRendererInfo = audioRenderer.getRendererInfoSync(); 5786 console.info(`Renderer content: ${rendererInfo.content}`); 5787 console.info(`Renderer usage: ${rendererInfo.usage}`); 5788 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 5789} catch (err) { 5790 let error = err as BusinessError; 5791 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${error}`); 5792} 5793``` 5794 5795### getStreamInfo<sup>8+</sup> 5796 5797getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 5798 5799获取音频流信息,使用callback方式异步返回结果。 5800 5801**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5802 5803**参数:** 5804 5805| 参数名 | 类型 | 必填 | 说明 | 5806| :------- | :--------------------------------------------------- | :--- | :------------------- | 5807| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取音频流信息成功,err为undefined,data为获取到的音频流信息;否则为错误对象。 | 5808 5809**示例:** 5810 5811```ts 5812import { BusinessError } from '@ohos.base'; 5813 5814audioRenderer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 5815 console.info('Renderer GetStreamInfo:'); 5816 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5817 console.info(`Renderer channel: ${streamInfo.channels}`); 5818 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5819 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5820}); 5821``` 5822 5823### getStreamInfo<sup>8+</sup> 5824 5825getStreamInfo(): Promise<AudioStreamInfo\> 5826 5827获取音频流信息,使用Promise方式异步返回结果。 5828 5829**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5830 5831**返回值:** 5832 5833| 类型 | 说明 | 5834| :--------------------------------------------- | :--------------------- | 5835| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回音频流信息. | 5836 5837**示例:** 5838 5839```ts 5840import { BusinessError } from '@ohos.base'; 5841 5842audioRenderer.getStreamInfo().then((streamInfo: audio.AudioStreamInfo) => { 5843 console.info('Renderer GetStreamInfo:'); 5844 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5845 console.info(`Renderer channel: ${streamInfo.channels}`); 5846 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5847 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5848}).catch((err: BusinessError) => { 5849 console.error(`ERROR: ${err}`); 5850}); 5851``` 5852 5853### getStreamInfoSync<sup>10+</sup> 5854 5855getStreamInfoSync(): AudioStreamInfo 5856 5857获取音频流信息,同步返回结果。 5858 5859**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5860 5861**返回值:** 5862 5863| 类型 | 说明 | 5864| :--------------------------------------------- | :--------------------- | 5865| [AudioStreamInfo](#audiostreaminfo8) | 返回音频流信息. | 5866 5867**示例:** 5868 5869```ts 5870import { BusinessError } from '@ohos.base'; 5871 5872try { 5873 let streamInfo: audio.AudioStreamInfo = audioRenderer.getStreamInfoSync(); 5874 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 5875 console.info(`Renderer channel: ${streamInfo.channels}`); 5876 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 5877 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 5878} catch (err) { 5879 let error = err as BusinessError; 5880 console.error(`ERROR: ${error}`); 5881} 5882``` 5883 5884### getAudioStreamId<sup>9+</sup> 5885 5886getAudioStreamId(callback: AsyncCallback<number\>): void 5887 5888获取音频流id,使用callback方式异步返回结果。 5889 5890**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5891 5892**参数:** 5893 5894| 参数名 | 类型 | 必填 | 说明 | 5895| :------- | :--------------------------------------------------- | :--- | :------------------- | 5896| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 5897 5898**示例:** 5899 5900```ts 5901import { BusinessError } from '@ohos.base'; 5902 5903audioRenderer.getAudioStreamId((err: BusinessError, streamId: number) => { 5904 console.info(`Renderer GetStreamId: ${streamId}`); 5905}); 5906``` 5907 5908### getAudioStreamId<sup>9+</sup> 5909 5910getAudioStreamId(): Promise<number\> 5911 5912获取音频流id,使用Promise方式异步返回结果。 5913 5914**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5915 5916**返回值:** 5917 5918| 类型 | 说明 | 5919| :--------------------------------------------- | :--------------------- | 5920| Promise<number\> | Promise对象,返回音频流id。 | 5921 5922**示例:** 5923 5924```ts 5925import { BusinessError } from '@ohos.base'; 5926 5927audioRenderer.getAudioStreamId().then((streamId: number) => { 5928 console.info(`Renderer getAudioStreamId: ${streamId}`); 5929}).catch((err: BusinessError) => { 5930 console.error(`ERROR: ${err}`); 5931}); 5932``` 5933 5934### getAudioStreamIdSync<sup>10+</sup> 5935 5936getAudioStreamIdSync(): number 5937 5938获取音频流id,同步返回结果。 5939 5940**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5941 5942**返回值:** 5943 5944| 类型 | 说明 | 5945| :--------------------------------------------- | :--------------------- | 5946| number | 返回音频流id。 | 5947 5948**示例:** 5949 5950```ts 5951import { BusinessError } from '@ohos.base'; 5952 5953try { 5954 let streamId: number = audioRenderer.getAudioStreamIdSync(); 5955 console.info(`Renderer getAudioStreamIdSync: ${streamId}`); 5956} catch (err) { 5957 let error = err as BusinessError; 5958 console.error(`ERROR: ${error}`); 5959} 5960``` 5961 5962### setAudioEffectMode<sup>10+</sup> 5963 5964setAudioEffectMode(mode: AudioEffectMode, callback: AsyncCallback\<void>): void 5965 5966设置当前音效模式。使用callback方式异步返回结果。 5967 5968**系统能力:** SystemCapability.Multimedia.Audio.Renderer 5969 5970**参数:** 5971 5972| 参数名 | 类型 | 必填 | 说明 | 5973| -------- | ---------------------------------------- | ---- | ------------------------ | 5974| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 5975| callback | AsyncCallback\<void> | 是 | 回调函数。当设置当前音效模式成功,err为undefined,否则为错误对象。 | 5976 5977**错误码:** 5978 5979以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 5980 5981| 错误码ID | 错误信息 | 5982| ------- | ----------------------------------------------| 5983| 6800101 | Invalid parameter error. Return by callback. | 5984 5985**示例:** 5986 5987```ts 5988import { BusinessError } from '@ohos.base'; 5989 5990audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => { 5991 if (err) { 5992 console.error('Failed to set params'); 5993 } else { 5994 console.info('Callback invoked to indicate a successful audio effect mode setting.'); 5995 } 5996}); 5997``` 5998 5999### setAudioEffectMode<sup>10+</sup> 6000 6001setAudioEffectMode(mode: AudioEffectMode): Promise\<void> 6002 6003设置当前音效模式。使用Promise方式异步返回结果。 6004 6005**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6006 6007**参数:** 6008 6009| 参数名 | 类型 | 必填 | 说明 | 6010| ------ | ---------------------------------------- | ---- | ------------ | 6011| mode | [AudioEffectMode](#audioeffectmode10) | 是 | 音效模式。 | 6012 6013**返回值:** 6014 6015| 类型 | 说明 | 6016| -------------- | ------------------------- | 6017| Promise\<void> | Promise对象,无返回结果。 | 6018 6019**错误码:** 6020 6021以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 6022 6023| 错误码ID | 错误信息 | 6024| ------- | ---------------------------------------------| 6025| 6800101 | Invalid parameter error. Return by promise. | 6026 6027**示例:** 6028 6029```ts 6030import { BusinessError } from '@ohos.base'; 6031 6032audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT).then(() => { 6033 console.info('setAudioEffectMode SUCCESS'); 6034}).catch((err: BusinessError) => { 6035 console.error(`ERROR: ${err}`); 6036}); 6037``` 6038 6039### getAudioEffectMode<sup>10+</sup> 6040 6041getAudioEffectMode(callback: AsyncCallback\<AudioEffectMode>): void 6042 6043获取当前音效模式。使用callback方式异步返回结果。 6044 6045**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6046 6047**参数:** 6048 6049| 参数名 | 类型 | 必填 | 说明 | 6050| -------- | ------------------------------------------------------- | ---- | ------------------ | 6051| callback | AsyncCallback<[AudioEffectMode](#audioeffectmode10)> | 是 | 回调函数。当获取当前音效模式成功,err为undefined,data为获取到的当前音效模式;否则为错误对象。 | 6052 6053**示例:** 6054 6055```ts 6056import { BusinessError } from '@ohos.base'; 6057 6058audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => { 6059 if (err) { 6060 console.error('Failed to get params'); 6061 } else { 6062 console.info(`getAudioEffectMode: ${effectMode}`); 6063 } 6064}); 6065``` 6066 6067### getAudioEffectMode<sup>10+</sup> 6068 6069getAudioEffectMode(): Promise\<AudioEffectMode> 6070 6071获取当前音效模式。使用Promise方式异步返回结果。 6072 6073**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6074 6075**返回值:** 6076 6077| 类型 | 说明 | 6078| ------------------------------------------------- | ------------------------- | 6079| Promise<[AudioEffectMode](#audioeffectmode10)> | Promise对象,返回当前音效模式。 | 6080 6081**示例:** 6082 6083```ts 6084import { BusinessError } from '@ohos.base'; 6085 6086audioRenderer.getAudioEffectMode().then((effectMode: audio.AudioEffectMode) => { 6087 console.info(`getAudioEffectMode: ${effectMode}`); 6088}).catch((err: BusinessError) => { 6089 console.error(`ERROR: ${err}`); 6090}); 6091``` 6092 6093### start<sup>8+</sup> 6094 6095start(callback: AsyncCallback<void\>): void 6096 6097启动音频渲染器。使用callback方式异步返回结果。 6098 6099**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6100 6101**参数:** 6102 6103| 参数名 | 类型 | 必填 | 说明 | 6104| -------- | -------------------- | ---- | ---------- | 6105| callback | AsyncCallback\<void> | 是 | 回调函数。当启动音频渲染器成功,err为undefined,否则为错误对象。 | 6106 6107**示例:** 6108 6109```ts 6110import { BusinessError } from '@ohos.base'; 6111 6112audioRenderer.start((err: BusinessError) => { 6113 if (err) { 6114 console.error('Renderer start failed.'); 6115 } else { 6116 console.info('Renderer start success.'); 6117 } 6118}); 6119``` 6120 6121### start<sup>8+</sup> 6122 6123start(): Promise<void\> 6124 6125启动音频渲染器。使用Promise方式异步返回结果。 6126 6127**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6128 6129**返回值:** 6130 6131| 类型 | 说明 | 6132| -------------- | ------------------------- | 6133| Promise\<void> | Promise对象,无返回结果。 | 6134 6135**示例:** 6136 6137```ts 6138import { BusinessError } from '@ohos.base'; 6139 6140audioRenderer.start().then(() => { 6141 console.info('Renderer started'); 6142}).catch((err: BusinessError) => { 6143 console.error(`ERROR: ${err}`); 6144}); 6145``` 6146 6147### pause<sup>8+</sup> 6148 6149pause(callback: AsyncCallback\<void>): void 6150 6151暂停渲染。使用callback方式异步返回结果。 6152 6153**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6154 6155**参数:** 6156 6157| 参数名 | 类型 | 必填 | 说明 | 6158| -------- | -------------------- | ---- | ---------------- | 6159| callback | AsyncCallback\<void> | 是 | 回调函数。当暂停渲染成功,err为undefined,否则为错误对象。 | 6160 6161**示例:** 6162 6163```ts 6164import { BusinessError } from '@ohos.base'; 6165 6166audioRenderer.pause((err: BusinessError) => { 6167 if (err) { 6168 console.error('Renderer pause failed'); 6169 } else { 6170 console.info('Renderer paused.'); 6171 } 6172}); 6173``` 6174 6175### pause<sup>8+</sup> 6176 6177pause(): Promise\<void> 6178 6179暂停渲染。使用Promise方式异步返回结果。 6180 6181**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6182 6183**返回值:** 6184 6185| 类型 | 说明 | 6186| -------------- | ------------------------- | 6187| Promise\<void> | Promise对象,无返回结果。 | 6188 6189**示例:** 6190 6191```ts 6192import { BusinessError } from '@ohos.base'; 6193 6194audioRenderer.pause().then(() => { 6195 console.info('Renderer paused'); 6196}).catch((err: BusinessError) => { 6197 console.error(`ERROR: ${err}`); 6198}); 6199``` 6200 6201### drain<sup>8+</sup> 6202 6203drain(callback: AsyncCallback\<void>): void 6204 6205检查缓冲区是否已被耗尽。使用callback方式异步返回结果。 6206 6207**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6208 6209**参数:** 6210 6211| 参数名 | 类型 | 必填 | 说明 | 6212| -------- | -------------------- | ---- | ---------------- | 6213| callback | AsyncCallback\<void> | 是 | 回调函数。当检查缓冲区是否已被耗尽成功,err为undefined,否则为错误对象。 | 6214 6215**示例:** 6216 6217```ts 6218import { BusinessError } from '@ohos.base'; 6219 6220audioRenderer.drain((err: BusinessError) => { 6221 if (err) { 6222 console.error('Renderer drain failed'); 6223 } else { 6224 console.info('Renderer drained.'); 6225 } 6226}); 6227``` 6228 6229### drain<sup>8+</sup> 6230 6231drain(): Promise\<void> 6232 6233检查缓冲区是否已被耗尽。使用Promise方式异步返回结果。 6234 6235**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6236 6237**返回值:** 6238 6239| 类型 | 说明 | 6240| -------------- | ------------------------- | 6241| Promise\<void> | Promise对象,无返回结果。 | 6242 6243**示例:** 6244 6245```ts 6246import { BusinessError } from '@ohos.base'; 6247 6248audioRenderer.drain().then(() => { 6249 console.info('Renderer drained successfully'); 6250}).catch((err: BusinessError) => { 6251 console.error(`ERROR: ${err}`); 6252}); 6253``` 6254 6255### stop<sup>8+</sup> 6256 6257stop(callback: AsyncCallback\<void>): void 6258 6259停止渲染。使用callback方式异步返回结果。 6260 6261**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6262 6263**参数:** 6264 6265| 参数名 | 类型 | 必填 | 说明 | 6266| -------- | -------------------- | ---- | ---------------- | 6267| callback | AsyncCallback\<void> | 是 | 回调函数。当停止渲染成功,err为undefined,否则为错误对象。 | 6268 6269**示例:** 6270 6271```ts 6272import { BusinessError } from '@ohos.base'; 6273 6274audioRenderer.stop((err: BusinessError) => { 6275 if (err) { 6276 console.error('Renderer stop failed'); 6277 } else { 6278 console.info('Renderer stopped.'); 6279 } 6280}); 6281``` 6282 6283### stop<sup>8+</sup> 6284 6285stop(): Promise\<void> 6286 6287停止渲染。使用Promise方式异步返回结果。 6288 6289**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6290 6291**返回值:** 6292 6293| 类型 | 说明 | 6294| -------------- | ------------------------- | 6295| Promise\<void> | Promise对象,无返回结果。 | 6296 6297**示例:** 6298 6299```ts 6300import { BusinessError } from '@ohos.base'; 6301 6302audioRenderer.stop().then(() => { 6303 console.info('Renderer stopped successfully'); 6304}).catch((err: BusinessError) => { 6305 console.error(`ERROR: ${err}`); 6306}); 6307``` 6308 6309### release<sup>8+</sup> 6310 6311release(callback: AsyncCallback\<void>): void 6312 6313释放音频渲染器。使用callback方式异步返回结果。 6314 6315**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6316 6317**参数:** 6318 6319| 参数名 | 类型 | 必填 | 说明 | 6320| -------- | -------------------- | ---- | ---------------- | 6321| callback | AsyncCallback\<void> | 是 | 回调函数。当释放音频渲染器成功,err为undefined,否则为错误对象。 | 6322 6323**示例:** 6324 6325```ts 6326import { BusinessError } from '@ohos.base'; 6327 6328audioRenderer.release((err: BusinessError) => { 6329 if (err) { 6330 console.error('Renderer release failed'); 6331 } else { 6332 console.info('Renderer released.'); 6333 } 6334}); 6335``` 6336 6337### release<sup>8+</sup> 6338 6339release(): Promise\<void> 6340 6341释放渲染器。使用Promise方式异步返回结果。 6342 6343**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6344 6345**返回值:** 6346 6347| 类型 | 说明 | 6348| -------------- | ------------------------- | 6349| Promise\<void> | Promise对象,无返回结果。 | 6350 6351**示例:** 6352 6353```ts 6354import { BusinessError } from '@ohos.base'; 6355 6356audioRenderer.release().then(() => { 6357 console.info('Renderer released successfully'); 6358}).catch((err: BusinessError) => { 6359 console.error(`ERROR: ${err}`); 6360}); 6361``` 6362 6363### write<sup>8+</sup> 6364 6365write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void 6366 6367写入缓冲区。使用callback方式异步返回结果。 6368 6369**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6370 6371**参数:** 6372 6373| 参数名 | 类型 | 必填 | 说明 | 6374| -------- | ---------------------- | ---- | --------------------------------------------------- | 6375| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6376| callback | AsyncCallback\<number> | 是 | 回调函数。当写入缓冲区成功,err为undefined,data为获取到的写入的字节数;否则为错误对象。 | 6377 6378**示例:** 6379 6380```ts 6381import { BusinessError } from '@ohos.base'; 6382import fs from '@ohos.file.fs'; 6383 6384let bufferSize: number; 6385class Options { 6386 offset?: number; 6387 length?: number; 6388} 6389audioRenderer.getBufferSize().then((data: number)=> { 6390 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6391 bufferSize = data; 6392 console.info(`Buffer size: ${bufferSize}`); 6393 let path = getContext().cacheDir; 6394 let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; 6395 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6396 fs.stat(filePath).then(async (stat: fs.Stat) => { 6397 let buf = new ArrayBuffer(bufferSize); 6398 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6399 for (let i = 0;i < len; i++) { 6400 let options: Options = { 6401 offset: i * bufferSize, 6402 length: bufferSize 6403 } 6404 let readsize: number = await fs.read(file.fd, buf, options) 6405 let writeSize: number = await new Promise((resolve,reject)=>{ 6406 audioRenderer.write(buf,(err: BusinessError, writeSize: number)=>{ 6407 if(err){ 6408 reject(err) 6409 }else{ 6410 resolve(writeSize) 6411 } 6412 }) 6413 }) 6414 } 6415 }); 6416 }).catch((err: BusinessError) => { 6417 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6418}); 6419 6420 6421``` 6422 6423### write<sup>8+</sup> 6424 6425write(buffer: ArrayBuffer): Promise\<number> 6426 6427写入缓冲区。使用Promise方式异步返回结果。 6428 6429**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6430 6431**参数:** 6432 6433| 参数名 | 类型 | 必填 | 说明 | 6434| -------- | ---------------------- | ---- | --------------------------------------------------- | 6435| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 6436 6437**返回值:** 6438 6439| 类型 | 说明 | 6440| ---------------- | ------------------------------------------------------------ | 6441| Promise\<number> | Promise对象,返回写入的字节数。 | 6442 6443**示例:** 6444 6445```ts 6446import { BusinessError } from '@ohos.base'; 6447import fs from '@ohos.file.fs'; 6448 6449let bufferSize: number; 6450class Options { 6451 offset?: number; 6452 length?: number; 6453} 6454audioRenderer.getBufferSize().then((data: number) => { 6455 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6456 bufferSize = data; 6457 console.info(`BufferSize: ${bufferSize}`); 6458 let path = getContext().cacheDir; 6459 let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; 6460 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 6461 fs.stat(filePath).then(async (stat: fs.Stat) => { 6462 let buf = new ArrayBuffer(bufferSize); 6463 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 6464 for (let i = 0;i < len; i++) { 6465 let options: Options = { 6466 offset: i * bufferSize, 6467 length: bufferSize 6468 } 6469 let readsize: number = await fs.read(file.fd, buf, options) 6470 try{ 6471 let writeSize: number = await audioRenderer.write(buf); 6472 } catch(err) { 6473 let error = err as BusinessError; 6474 console.error(`audioRenderer.write err: ${error}`); 6475 } 6476 } 6477 }); 6478}).catch((err: BusinessError) => { 6479 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6480}); 6481``` 6482 6483### getAudioTime<sup>8+</sup> 6484 6485getAudioTime(callback: AsyncCallback\<number>): void 6486 6487获取时间戳(从 1970 年 1 月 1 日开始)。使用callback方式异步返回结果。 6488 6489**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6490 6491**参数:** 6492 6493| 参数名 | 类型 | 必填 | 说明 | 6494| -------- | ---------------------- | ---- | ---------------- | 6495| callback | AsyncCallback\<number> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 6496 6497**示例:** 6498 6499```ts 6500import { BusinessError } from '@ohos.base'; 6501 6502audioRenderer.getAudioTime((err: BusinessError, timestamp: number) => { 6503 console.info(`Current timestamp: ${timestamp}`); 6504}); 6505``` 6506 6507### getAudioTime<sup>8+</sup> 6508 6509getAudioTime(): Promise\<number> 6510 6511获取时间戳(从 1970 年 1 月 1 日开始)。使用Promise方式异步返回结果。 6512 6513**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6514 6515**返回值:** 6516 6517| 类型 | 描述 | 6518| ---------------- | ----------------------- | 6519| Promise\<number> | Promise对象,返回时间戳。 | 6520 6521**示例:** 6522 6523```ts 6524import { BusinessError } from '@ohos.base'; 6525 6526audioRenderer.getAudioTime().then((timestamp: number) => { 6527 console.info(`Current timestamp: ${timestamp}`); 6528}).catch((err: BusinessError) => { 6529 console.error(`ERROR: ${err}`); 6530}); 6531``` 6532 6533### getAudioTimeSync<sup>10+</sup> 6534 6535getAudioTimeSync(): number 6536 6537获取时间戳(从 1970 年 1 月 1 日开始),同步返回结果。 6538 6539**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6540 6541**返回值:** 6542 6543| 类型 | 描述 | 6544| ---------------- | ----------------------- | 6545| number | 返回时间戳。 | 6546 6547**示例:** 6548 6549```ts 6550import { BusinessError } from '@ohos.base'; 6551 6552try { 6553 let timestamp: number = audioRenderer.getAudioTimeSync(); 6554 console.info(`Current timestamp: ${timestamp}`); 6555} catch (err) { 6556 let error = err as BusinessError; 6557 console.error(`ERROR: ${error}`); 6558} 6559``` 6560 6561### getBufferSize<sup>8+</sup> 6562 6563getBufferSize(callback: AsyncCallback\<number>): void 6564 6565获取音频渲染器的最小缓冲区大小。使用callback方式异步返回结果。 6566 6567**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6568 6569**参数:** 6570 6571| 参数名 | 类型 | 必填 | 说明 | 6572| -------- | ---------------------- | ---- | -------------------- | 6573| callback | AsyncCallback\<number> | 是 | 回调函数。当获取音频渲染器的最小缓冲区大小成功,err为undefined,data为获取到的最小缓冲区大小;否则为错误对象。 | 6574 6575**示例:** 6576 6577```ts 6578import { BusinessError } from '@ohos.base'; 6579 6580let bufferSize: number; 6581audioRenderer.getBufferSize((err: BusinessError, data: number) => { 6582 if (err) { 6583 console.error('getBufferSize error'); 6584 } else { 6585 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6586 bufferSize = data; 6587 } 6588}); 6589``` 6590 6591### getBufferSize<sup>8+</sup> 6592 6593getBufferSize(): Promise\<number> 6594 6595获取音频渲染器的最小缓冲区大小。使用Promise方式异步返回结果。 6596 6597**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6598 6599**返回值:** 6600 6601| 类型 | 说明 | 6602| ---------------- | --------------------------- | 6603| Promise\<number> | Promise对象,返回缓冲区大小。 | 6604 6605**示例:** 6606 6607```ts 6608import { BusinessError } from '@ohos.base'; 6609 6610let bufferSize: number; 6611audioRenderer.getBufferSize().then((data: number) => { 6612 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 6613 bufferSize = data; 6614}).catch((err: BusinessError) => { 6615 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 6616}); 6617``` 6618 6619### getBufferSizeSync<sup>10+</sup> 6620 6621getBufferSizeSync(): number 6622 6623获取音频渲染器的最小缓冲区大小,同步返回结果。 6624 6625**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6626 6627**返回值:** 6628 6629| 类型 | 说明 | 6630| ---------------- | --------------------------- | 6631| number | 返回缓冲区大小。 | 6632 6633**示例:** 6634 6635```ts 6636import { BusinessError } from '@ohos.base'; 6637 6638let bufferSize: number = 0; 6639try { 6640 bufferSize = audioRenderer.getBufferSizeSync(); 6641 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${bufferSize}`); 6642} catch (err) { 6643 let error = err as BusinessError; 6644 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${error}`); 6645} 6646``` 6647 6648### setRenderRate<sup>8+</sup> 6649 6650setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void 6651 6652设置音频渲染速率。使用callback方式异步返回结果。 6653 6654**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6655 6656**参数:** 6657 6658| 参数名 | 类型 | 必填 | 说明 | 6659| -------- | ---------------------------------------- | ---- | ------------------------ | 6660| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 6661| callback | AsyncCallback\<void> | 是 | 回调函数。当设置音频渲染速率成功,err为undefined,否则为错误对象。 | 6662 6663**示例:** 6664 6665```ts 6666import { BusinessError } from '@ohos.base'; 6667 6668audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err: BusinessError) => { 6669 if (err) { 6670 console.error('Failed to set params'); 6671 } else { 6672 console.info('Callback invoked to indicate a successful render rate setting.'); 6673 } 6674}); 6675``` 6676 6677### setRenderRate<sup>8+</sup> 6678 6679setRenderRate(rate: AudioRendererRate): Promise\<void> 6680 6681设置音频渲染速率。使用Promise方式异步返回结果。 6682 6683**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6684 6685**参数:** 6686 6687| 参数名 | 类型 | 必填 | 说明 | 6688| ------ | ---------------------------------------- | ---- | ------------ | 6689| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 6690 6691**返回值:** 6692 6693| 类型 | 说明 | 6694| -------------- | ------------------------- | 6695| Promise\<void> | Promise对象,无返回结果。 | 6696 6697**示例:** 6698 6699```ts 6700import { BusinessError } from '@ohos.base'; 6701 6702audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => { 6703 console.info('setRenderRate SUCCESS'); 6704}).catch((err: BusinessError) => { 6705 console.error(`ERROR: ${err}`); 6706}); 6707``` 6708 6709### getRenderRate<sup>8+</sup> 6710 6711getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void 6712 6713获取当前渲染速率。使用callback方式异步返回结果。 6714 6715**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6716 6717**参数:** 6718 6719| 参数名 | 类型 | 必填 | 说明 | 6720| -------- | ------------------------------------------------------- | ---- | ------------------ | 6721| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是 | 回调函数。当获取当前渲染速率成功,err为undefined,data为获取到的当前渲染速率;否则为错误对象。 | 6722 6723**示例:** 6724 6725```ts 6726import { BusinessError } from '@ohos.base'; 6727 6728audioRenderer.getRenderRate((err: BusinessError, renderRate: audio.AudioRendererRate) => { 6729 console.info(`getRenderRate: ${renderRate}`); 6730}); 6731``` 6732 6733### getRenderRate<sup>8+</sup> 6734 6735getRenderRate(): Promise\<AudioRendererRate> 6736 6737获取当前渲染速率。使用Promise方式异步返回结果。 6738 6739**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6740 6741**返回值:** 6742 6743| 类型 | 说明 | 6744| ------------------------------------------------- | ------------------------- | 6745| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise对象,返回渲染速率。 | 6746 6747**示例:** 6748 6749```ts 6750import { BusinessError } from '@ohos.base'; 6751 6752audioRenderer.getRenderRate().then((renderRate: audio.AudioRendererRate) => { 6753 console.info(`getRenderRate: ${renderRate}`); 6754}).catch((err: BusinessError) => { 6755 console.error(`ERROR: ${err}`); 6756}); 6757``` 6758 6759### getRenderRateSync<sup>10+</sup> 6760 6761getRenderRateSync(): AudioRendererRate 6762 6763获取当前渲染速率,同步返回结果。 6764 6765**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6766 6767**返回值:** 6768 6769| 类型 | 说明 | 6770| ------------------------------------------------- | ------------------------- | 6771| [AudioRendererRate](#audiorendererrate8) | 返回渲染速率。 | 6772 6773**示例:** 6774 6775```ts 6776import { BusinessError } from '@ohos.base'; 6777 6778try { 6779 let renderRate: audio.AudioRendererRate = audioRenderer.getRenderRateSync(); 6780 console.info(`getRenderRate: ${renderRate}`); 6781} catch (err) { 6782 let error = err as BusinessError; 6783 console.error(`ERROR: ${error}`); 6784} 6785``` 6786 6787### setInterruptMode<sup>9+</sup> 6788 6789setInterruptMode(mode: InterruptMode): Promise<void> 6790 6791设置应用的焦点模型。使用Promise异步回调。 6792 6793**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6794 6795**参数:** 6796 6797| 参数名 | 类型 | 必填 | 说明 | 6798| ---------- | ---------------------------------- | ------ | ---------- | 6799| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 6800 6801**返回值:** 6802 6803| 类型 | 说明 | 6804| ------------------- | ----------------------------- | 6805| Promise<void> | Promise对象,无返回结果。 | 6806 6807**示例:** 6808 6809```ts 6810import { BusinessError } from '@ohos.base'; 6811 6812let mode = 0; 6813audioRenderer.setInterruptMode(mode).then(() => { 6814 console.info('setInterruptMode Success!'); 6815}).catch((err: BusinessError) => { 6816 console.error(`setInterruptMode Fail: ${err}`); 6817}); 6818``` 6819### setInterruptMode<sup>9+</sup> 6820 6821setInterruptMode(mode: InterruptMode, callback: AsyncCallback\<void>): void 6822 6823设置应用的焦点模型。使用Callback回调返回执行结果。 6824 6825**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6826 6827**参数:** 6828 6829| 参数名 | 类型 | 必填 | 说明 | 6830| ------- | ----------------------------------- | ------ | -------------- | 6831|mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。| 6832|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的焦点模型成功,err为undefined,否则为错误对象。| 6833 6834**示例:** 6835 6836```ts 6837import { BusinessError } from '@ohos.base'; 6838 6839let mode = 1; 6840audioRenderer.setInterruptMode(mode, (err: BusinessError) => { 6841 if(err){ 6842 console.error(`setInterruptMode Fail: ${err}`); 6843 } 6844 console.info('setInterruptMode Success!'); 6845}); 6846``` 6847 6848### setInterruptModeSync<sup>10+</sup> 6849 6850setInterruptModeSync(mode: InterruptMode): void 6851 6852设置应用的焦点模型,同步设置。 6853 6854**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 6855 6856**参数:** 6857 6858| 参数名 | 类型 | 必填 | 说明 | 6859| ---------- | ---------------------------------- | ------ | ---------- | 6860| mode | [InterruptMode](#interruptmode9) | 是 | 焦点模型。 | 6861 6862**错误码:** 6863 6864以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 6865 6866| 错误码ID | 错误信息 | 6867| ------- | --------------------------------------------| 6868| 6800101 | invalid parameter error | 6869 6870**示例:** 6871 6872```ts 6873import { BusinessError } from '@ohos.base'; 6874 6875try { 6876 audioRenderer.setInterruptModeSync(0); 6877 console.info('setInterruptMode Success!'); 6878} catch (err) { 6879 let error = err as BusinessError; 6880 console.error(`setInterruptMode Fail: ${error}`); 6881} 6882``` 6883 6884### setVolume<sup>9+</sup> 6885 6886setVolume(volume: number): Promise<void> 6887 6888设置应用的音量。使用Promise异步回调。 6889 6890**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6891 6892**参数:** 6893 6894| 参数名 | 类型 | 必填 | 说明 | 6895| ---------- | ------- | ------ | ------------------- | 6896| volume | number | 是 | 音量值范围为0.0-1.0。 | 6897 6898**返回值:** 6899 6900| 类型 | 说明 | 6901| ------------------- | ----------------------------- | 6902| Promise<void> | Promise对象,无返回结果。 | 6903 6904**示例:** 6905 6906```ts 6907import { BusinessError } from '@ohos.base'; 6908 6909audioRenderer.setVolume(0.5).then(() => { 6910 console.info('setVolume Success!'); 6911}).catch((err: BusinessError) => { 6912 console.error(`setVolume Fail: ${err}`); 6913}); 6914``` 6915### setVolume<sup>9+</sup> 6916 6917setVolume(volume: number, callback: AsyncCallback\<void>): void 6918 6919设置应用的音量。使用Callback回调返回执行结果。 6920 6921**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6922 6923**参数:** 6924 6925| 参数名 | 类型 | 必填 | 说明 | 6926| ------- | -----------| ------ | ------------------- | 6927|volume | number | 是 | 音量值范围为0.0-1.0。 | 6928|callback | AsyncCallback\<void> | 是 |回调函数。当设置应用的音量成功,err为undefined,否则为错误对象。| 6929 6930**示例:** 6931 6932```ts 6933import { BusinessError } from '@ohos.base'; 6934 6935audioRenderer.setVolume(0.5, (err: BusinessError) => { 6936 if(err){ 6937 console.error(`setVolume Fail: ${err}`); 6938 return; 6939 } 6940 console.info('setVolume Success!'); 6941}); 6942``` 6943 6944### getMinStreamVolume<sup>10+</sup> 6945 6946getMinStreamVolume(callback: AsyncCallback<number>): void 6947 6948获取应用基于音频流的最小音量。使用Callback回调返回。 6949 6950**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6951 6952**参数:** 6953 6954| 参数名 | 类型 | 必填 | 说明 | 6955| ------- | -----------| ------ | ------------------- | 6956|callback |AsyncCallback<number> | 是 |回调函数。当获取应用基于音频流的最小音量成功,err为undefined,data为获取到的应用基于音频流的最小音量(音量范围0-1);否则为错误对象。| 6957 6958**示例:** 6959 6960```ts 6961import { BusinessError } from '@ohos.base'; 6962 6963audioRenderer.getMinStreamVolume((err: BusinessError, minVolume: number) => { 6964 if (err) { 6965 console.error(`getMinStreamVolume error: ${err}`); 6966 } else { 6967 console.info(`getMinStreamVolume Success! ${minVolume}`); 6968 } 6969}); 6970``` 6971### getMinStreamVolume<sup>10+</sup> 6972 6973getMinStreamVolume(): Promise<number> 6974 6975获取应用基于音频流的最小音量。使用Promise异步回调。 6976 6977**系统能力:** SystemCapability.Multimedia.Audio.Renderer 6978 6979**返回值:** 6980 6981| 类型 | 说明 | 6982| ------------------- | ----------------------------- | 6983| Promise<number>| Promise对象,返回音频流最小音量(音量范围0-1)。| 6984 6985**示例:** 6986 6987```ts 6988import { BusinessError } from '@ohos.base'; 6989 6990audioRenderer.getMinStreamVolume().then((value: number) => { 6991 console.info(`Get min stream volume Success! ${value}`); 6992}).catch((err: BusinessError) => { 6993 console.error(`Get min stream volume Fail: ${err}`); 6994}); 6995``` 6996 6997### getMinStreamVolumeSync<sup>10+</sup> 6998 6999getMinStreamVolumeSync(): number 7000 7001获取应用基于音频流的最小音量,同步返回结果。 7002 7003**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7004 7005**返回值:** 7006 7007| 类型 | 说明 | 7008| ------------------- | ----------------------------- | 7009| number| 返回音频流最小音量(音量范围0-1)。| 7010 7011**示例:** 7012 7013```ts 7014import { BusinessError } from '@ohos.base'; 7015 7016try { 7017 let value: number = audioRenderer.getMinStreamVolumeSync(); 7018 console.info(`Get min stream volume Success! ${value}`); 7019} catch (err) { 7020 let error = err as BusinessError; 7021 console.error(`Get min stream volume Fail: ${error}`); 7022} 7023``` 7024 7025### getMaxStreamVolume<sup>10+</sup> 7026 7027getMaxStreamVolume(callback: AsyncCallback<number>): void 7028 7029获取应用基于音频流的最大音量。使用Callback回调返回。 7030 7031**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7032 7033**参数:** 7034 7035| 参数名 | 类型 | 必填 | 说明 | 7036| ------- | -----------| ------ | ------------------- | 7037|callback | AsyncCallback<number> | 是 |回调函数。当获取应用基于音频流的最大音量成功,err为undefined,data为获取到的应用基于音频流的最大音量(音量范围0-1);否则为错误对象。| 7038 7039**示例:** 7040 7041```ts 7042import { BusinessError } from '@ohos.base'; 7043 7044audioRenderer.getMaxStreamVolume((err: BusinessError, maxVolume: number) => { 7045 if (err) { 7046 console.error(`getMaxStreamVolume Fail: ${err}`); 7047 } else { 7048 console.info(`getMaxStreamVolume Success! ${maxVolume}`); 7049 } 7050}); 7051``` 7052### getMaxStreamVolume<sup>10+</sup> 7053 7054getMaxStreamVolume(): Promise<number> 7055 7056获取应用基于音频流的最大音量。使用Promise异步回调。 7057 7058**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7059 7060**返回值:** 7061 7062| 类型 | 说明 | 7063| ------------------- | ----------------------------- | 7064| Promise<number>| Promise对象,返回音频流最大音量(音量范围0-1)。| 7065 7066**示例:** 7067 7068```ts 7069import { BusinessError } from '@ohos.base'; 7070 7071audioRenderer.getMaxStreamVolume().then((value: number) => { 7072 console.info(`Get max stream volume Success! ${value}`); 7073}).catch((err: BusinessError) => { 7074 console.error(`Get max stream volume Fail: ${err}`); 7075}); 7076``` 7077 7078### getMaxStreamVolumeSync<sup>10+</sup> 7079 7080getMaxStreamVolumeSync(): number 7081 7082获取应用基于音频流的最大音量,同步返回结果。 7083 7084**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7085 7086**返回值:** 7087 7088| 类型 | 说明 | 7089| ------------------- | ----------------------------- | 7090| number| 返回音频流最大音量(音量范围0-1)。| 7091 7092**示例:** 7093 7094```ts 7095import { BusinessError } from '@ohos.base'; 7096 7097try { 7098 let value: number = audioRenderer.getMaxStreamVolumeSync(); 7099 console.info(`Get max stream volume Success! ${value}`); 7100} catch (err) { 7101 let error = err as BusinessError; 7102 console.error(`Get max stream volume Fail: ${error}`); 7103} 7104``` 7105 7106### getUnderflowCount<sup>10+</sup> 7107 7108getUnderflowCount(callback: AsyncCallback<number>): void 7109 7110获取当前播放音频流的欠载音频帧数量。使用Callback回调返回。 7111 7112**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7113 7114**参数:** 7115 7116| 参数名 | 类型 | 必填 | 说明 | 7117| ------- | -----------| ------ | ------------------- | 7118|callback | AsyncCallback<number> | 是 |回调函数。当获取当前播放音频流的欠载音频帧数量成功,err为undefined,data为获取到的当前播放音频流的欠载音频帧数量;否则为错误对象。| 7119 7120**示例:** 7121 7122```ts 7123import { BusinessError } from '@ohos.base'; 7124 7125audioRenderer.getUnderflowCount((err: BusinessError, underflowCount: number) => { 7126 if (err) { 7127 console.error(`getUnderflowCount Fail: ${err}`); 7128 } else { 7129 console.info(`getUnderflowCount Success! ${underflowCount}`); 7130 } 7131}); 7132``` 7133### getUnderflowCount<sup>10+</sup> 7134 7135getUnderflowCount(): Promise<number> 7136 7137获取当前播放音频流的欠载音频帧数量。使用Promise异步回调。 7138 7139**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7140 7141**返回值:** 7142 7143| 类型 | 说明 | 7144| ------------------- | ----------------------------- | 7145| Promise<number>| Promise对象,返回音频流的欠载音频帧数量。| 7146 7147**示例:** 7148 7149```ts 7150import { BusinessError } from '@ohos.base'; 7151 7152audioRenderer.getUnderflowCount().then((value: number) => { 7153 console.info(`Get underflow count Success! ${value}`); 7154}).catch((err: BusinessError) => { 7155 console.error(`Get underflow count Fail: ${err}`); 7156}); 7157``` 7158 7159### getUnderflowCountSync<sup>10+</sup> 7160 7161getUnderflowCountSync(): number 7162 7163获取当前播放音频流的欠载音频帧数量,同步返回数据。 7164 7165**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7166 7167**返回值:** 7168 7169| 类型 | 说明 | 7170| ------------------- | ----------------------------- | 7171| number| 返回音频流的欠载音频帧数量。| 7172 7173**示例:** 7174 7175```ts 7176import { BusinessError } from '@ohos.base'; 7177 7178try { 7179 let value: number = audioRenderer.getUnderflowCountSync(); 7180 console.info(`Get underflow count Success! ${value}`); 7181} catch (err) { 7182 let error = err as BusinessError; 7183 console.error(`Get underflow count Fail: ${error}`); 7184} 7185``` 7186 7187### getCurrentOutputDevices<sup>10+</sup> 7188 7189getCurrentOutputDevices(callback: AsyncCallback<AudioDeviceDescriptors>): void 7190 7191获取音频流输出设备描述符。使用Callback回调返回。 7192 7193**系统能力:** SystemCapability.Multimedia.Audio.Device 7194 7195**参数:** 7196 7197| 参数名 | 类型 | 必填 | 说明 | 7198| ------- | -----------| ------ | ------------------- | 7199|callback | AsyncCallback\<[AudioDeviceDescriptors](#audiodevicedescriptors)>| 是 |回调函数。当获取音频流输出设备描述符成功,err为undefined,data为获取到的音频流输出设备描述符;否则为错误对象。| 7200 7201**示例:** 7202 7203```ts 7204import { BusinessError } from '@ohos.base'; 7205 7206audioRenderer.getCurrentOutputDevices((err: BusinessError, deviceInfo: audio.AudioDeviceDescriptors) => { 7207 if (err) { 7208 console.error(`getCurrentOutputDevices Fail: ${err}`); 7209 } else { 7210 for (let i = 0; i < deviceInfo.length; i++) { 7211 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7212 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7213 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7214 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7215 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7216 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7217 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7218 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7219 } 7220 } 7221}); 7222``` 7223### getCurrentOutputDevices<sup>10+</sup> 7224 7225getCurrentOutputDevices(): Promise<AudioDeviceDescriptors> 7226 7227获取音频流输出设备描述符。使用Promise异步回调。 7228 7229**系统能力:** SystemCapability.Multimedia.Audio.Device 7230 7231**返回值:** 7232 7233| 类型 | 说明 | 7234| ------------------- | ----------------------------- | 7235| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)>| Promise对象,返回音频流的输出设备描述信息 | 7236 7237**示例:** 7238 7239```ts 7240import { BusinessError } from '@ohos.base'; 7241 7242audioRenderer.getCurrentOutputDevices().then((deviceInfo: audio.AudioDeviceDescriptors) => { 7243 for (let i = 0; i < deviceInfo.length; i++) { 7244 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7245 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7246 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7247 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7248 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7249 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7250 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7251 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7252 } 7253}).catch((err: BusinessError) => { 7254 console.error(`Get current output devices Fail: ${err}`); 7255}); 7256``` 7257 7258### getCurrentOutputDevicesSync<sup>10+</sup> 7259 7260getCurrentOutputDevicesSync(): AudioDeviceDescriptors 7261 7262获取音频流输出设备描述符,同步返回结果。 7263 7264**系统能力:** SystemCapability.Multimedia.Audio.Device 7265 7266**返回值:** 7267 7268| 类型 | 说明 | 7269| ------------------- | ----------------------------- | 7270| [AudioDeviceDescriptors](#audiodevicedescriptors) | 返回音频流的输出设备描述信息 | 7271 7272**示例:** 7273 7274```ts 7275import { BusinessError } from '@ohos.base'; 7276 7277try { 7278 let deviceInfo: audio.AudioDeviceDescriptors = audioRenderer.getCurrentOutputDevicesSync(); 7279 for (let i = 0; i < deviceInfo.length; i++) { 7280 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 7281 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 7282 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 7283 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 7284 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 7285 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 7286 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 7287 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 7288 } 7289} catch (err) { 7290 let error = err as BusinessError; 7291 console.error(`Get current output devices Fail: ${error}`); 7292} 7293``` 7294 7295### on('audioInterrupt')<sup>9+</sup> 7296 7297on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 7298 7299监听音频中断事件,使用callback方式返回结果。 7300 7301与[on('interrupt')](#oninterrupt)一致,均用于监听焦点变化。AudioRenderer对象在start事件发生时会主动获取焦点,在pause、stop等事件发生时会主动释放焦点,不需要开发者主动发起获取焦点或释放焦点的申请。 7302 7303**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 7304 7305**参数:** 7306 7307| 参数名 | 类型 | 必填 | 说明 | 7308| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 7309| type | string | 是 | 事件回调类型,支持的事件为:'audioInterrupt'(中断事件被触发,音频渲染被中断。) | 7310| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回播放中断时,应用接收的中断事件信息。 | 7311 7312**错误码:** 7313 7314以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 7315 7316| 错误码ID | 错误信息 | 7317| ------- | --------------------------------------------| 7318| 6800101 | if input parameter value error | 7319 7320**示例:** 7321 7322```ts 7323import audio from '@ohos.multimedia.audio'; 7324 7325let isPlaying: boolean; // 标识符,表示是否正在渲染 7326let isDucked: boolean; // 标识符,表示是否被降低音量 7327onAudioInterrupt(); 7328 7329async function onAudioInterrupt(){ 7330 audioRenderer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 7331 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 7332 // 由系统进行操作,强制打断音频渲染,应用需更新自身状态及显示内容等 7333 switch (interruptEvent.hintType) { 7334 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 7335 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent 7336 console.info('Force paused. Update playing status and stop writing'); 7337 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 7338 break; 7339 case audio.InterruptHint.INTERRUPT_HINT_STOP: 7340 // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发 7341 console.info('Force stopped. Update playing status and stop writing'); 7342 isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 7343 break; 7344 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 7345 // 音频流已被降低音量渲染 7346 console.info('Force ducked. Update volume status'); 7347 isDucked = true; // 简化处理,代表应用更新音量状态的若干操作 7348 break; 7349 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 7350 // 音频流已被恢复正常音量渲染 7351 console.info('Force ducked. Update volume status'); 7352 isDucked = false; // 简化处理,代表应用更新音量状态的若干操作 7353 break; 7354 default: 7355 console.info('Invalid interruptEvent'); 7356 break; 7357 } 7358 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 7359 // 由应用进行操作,应用可以自主选择打断或忽略 7360 switch (interruptEvent.hintType) { 7361 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 7362 // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染) 7363 console.info('Resume force paused renderer or ignore'); 7364 // 若选择继续渲染,需在此处主动执行开始渲染的若干操作 7365 break; 7366 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 7367 // 建议应用暂停渲染 7368 console.info('Choose to pause or ignore'); 7369 // 若选择暂停渲染,需在此处主动执行暂停渲染的若干操作 7370 break; 7371 case audio.InterruptHint.INTERRUPT_HINT_STOP: 7372 // 建议应用停止渲染 7373 console.info('Choose to stop or ignore'); 7374 // 若选择停止渲染,需在此处主动执行停止渲染的若干操作 7375 break; 7376 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 7377 // 建议应用降低音量渲染 7378 console.info('Choose to duck or ignore'); 7379 // 若选择降低音量渲染,需在此处主动执行降低音量渲染的若干操作 7380 break; 7381 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 7382 // 建议应用恢复正常音量渲染 7383 console.info('Choose to unduck or ignore'); 7384 // 若选择恢复正常音量渲染,需在此处主动执行恢复正常音量渲染的若干操作 7385 break; 7386 default: 7387 break; 7388 } 7389 } 7390 }); 7391} 7392``` 7393 7394### on('markReach')<sup>8+</sup> 7395 7396on(type: 'markReach', frame: number, callback: Callback<number>): void 7397 7398订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,回调被调用,使用callback方式返回结果。 7399 7400**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7401 7402**参数:** 7403 7404| 参数名 | 类型 | 必填 | 说明 | 7405| :------- | :----------------------- | :--- | :---------------------------------------- | 7406| type | string | 是 | 事件回调类型,支持的事件为:'markReach'。 | 7407| frame | number | 是 | 触发事件的帧数。 该值必须大于 0。 | 7408| callback | Callback\<number> | 是 | 回调函数,返回frame 参数的值 | 7409 7410**示例:** 7411 7412```ts 7413audioRenderer.on('markReach', 1000, (position: number) => { 7414 if (position == 1000) { 7415 console.info('ON Triggered successfully'); 7416 } 7417}); 7418``` 7419 7420 7421### off('markReach') <sup>8+</sup> 7422 7423off(type: 'markReach'): void 7424 7425取消订阅标记事件。 7426 7427**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7428 7429**参数:** 7430 7431| 参数名 | 类型 | 必填 | 说明 | 7432| :----- | :----- | :--- | :------------------------------------------------ | 7433| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'markReach'。 | 7434 7435**示例:** 7436 7437```ts 7438audioRenderer.off('markReach'); 7439``` 7440 7441### on('periodReach') <sup>8+</sup> 7442 7443on(type: 'periodReach', frame: number, callback: Callback<number>): void 7444 7445订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,触发回调并返回设定的值,使用callback方式返回结果。 7446 7447**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7448 7449**参数:** 7450 7451| 参数名 | 类型 | 必填 | 说明 | 7452| :------- | :----------------------- | :--- | :------------------------------------------ | 7453| type | string | 是 | 事件回调类型,支持的事件为:'periodReach'。 | 7454| frame | number | 是 | 触发事件的帧数。 该值必须大于 0。 | 7455| callback | Callback\<number> | 是 | 回调函数,返回frame 参数的值 | 7456 7457**示例:** 7458 7459```ts 7460audioRenderer.on('periodReach', 1000, (position: number) => { 7461 if (position == 1000) { 7462 console.info('ON Triggered successfully'); 7463 } 7464}); 7465``` 7466 7467### off('periodReach') <sup>8+</sup> 7468 7469off(type: 'periodReach'): void 7470 7471取消订阅标记事件。 7472 7473**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7474 7475**参数:** 7476 7477| 参数名 | 类型 | 必填 | 说明 | 7478| :----- | :----- | :--- | :-------------------------------------------------- | 7479| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'periodReach'。 | 7480 7481**示例:** 7482 7483```ts 7484audioRenderer.off('periodReach'); 7485``` 7486 7487### on('stateChange') <sup>8+</sup> 7488 7489on(type: 'stateChange', callback: Callback<AudioState\>): void 7490 7491订阅监听状态变化,使用callback方式返回结果。 7492 7493**系统能力:** SystemCapability.Multimedia.Audio.Renderer 7494 7495**参数:** 7496 7497| 参数名 | 类型 | 必填 | 说明 | 7498| :------- | :------------------------- | :--- | :------------------------------------------ | 7499| type | string | 是 | 事件回调类型,支持的事件为:'stateChange'。 | 7500| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 7501 7502**示例:** 7503 7504```ts 7505audioRenderer.on('stateChange', (state: audio.AudioState) => { 7506 if (state == 1) { 7507 console.info('audio renderer state is: STATE_PREPARED'); 7508 } 7509 if (state == 2) { 7510 console.info('audio renderer state is: STATE_RUNNING'); 7511 } 7512}); 7513``` 7514 7515### on('outputDeviceChange') <sup>10+</sup> 7516 7517on(type: 'outputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 7518 7519订阅监听音频输出设备变化,使用callback方式返回结果。 7520 7521**系统能力:** SystemCapability.Multimedia.Audio.Device 7522 7523**参数:** 7524 7525| 参数名 | 类型 | 必填 | 说明 | 7526| :------- | :------------------------- | :--- | :------------------------------------------ | 7527| type | string | 是 | 事件回调类型,支持的事件为:'outputDeviceChange'。 | 7528| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调函数,返回当前音频流的输出设备描述信息。 | 7529 7530**错误码:** 7531 7532以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 7533 7534| 错误码ID | 错误信息 | 7535| ------- | --------------------------------------------| 7536| 6800101 | if input parameter value error. | 7537 7538**示例:** 7539 7540```ts 7541audioRenderer.on('outputDeviceChange', (deviceInfo: audio.AudioDeviceDescriptors) => { 7542 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 7543 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 7544 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 7545}); 7546``` 7547### off('outputDeviceChange') <sup>10+</sup> 7548 7549off(type: 'outputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 7550 7551取消订阅监听音频输出设备变化,使用callback方式返回结果。 7552 7553**系统能力:** SystemCapability.Multimedia.Audio.Device 7554 7555**参数:** 7556 7557| 参数名 | 类型 | 必填 | 说明 | 7558| :------- | :------------------------- | :--- | :------------------------------------------ | 7559| type | string | 是 | 事件回调类型,支持的事件为:'outputDeviceChange'。 | 7560| callback | Callback\<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 否 | 回调函数,返回当前音频流的输出设备描述信息。 | 7561 7562**错误码:** 7563 7564以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 7565 7566| 错误码ID | 错误信息 | 7567| ------- | --------------------------------------------| 7568| 6800101 | if input parameter value error. | 7569 7570**示例:** 7571 7572```ts 7573audioRenderer.off('outputDeviceChange', (deviceInfo: audio.AudioDeviceDescriptors) => { 7574 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 7575 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 7576 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 7577}); 7578``` 7579 7580## AudioCapturer<sup>8+</sup> 7581 7582提供音频采集的相关接口。在调用AudioCapturer的接口前,需要先通过[createAudioCapturer](#audiocreateaudiocapturer8)创建实例。 7583 7584### 属性 7585 7586**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7587 7588| 名称 | 类型 | 可读 | 可写 | 说明 | 7589| :---- | :------------------------- | :--- | :--- | :--------------- | 7590| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频采集器状态。 | 7591 7592**示例:** 7593 7594```ts 7595import audio from '@ohos.multimedia.audio'; 7596 7597let state: audio.AudioState = audioCapturer.state; 7598``` 7599 7600### getCapturerInfo<sup>8+</sup> 7601 7602getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void 7603 7604获取采集器信息。使用callback方式异步返回结果。 7605 7606**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7607 7608**参数:** 7609 7610| 参数名 | 类型 | 必填 | 说明 | 7611| :------- | :-------------------------------- | :--- | :----------------------------------- | 7612| callback | AsyncCallback<[AudioCapturerInfo](#audiocapturerinfo)\> | 是 | 回调函数。当获取采集器信息成功,err为undefined,data为获取到的采集器信息;否则为错误对象。 | 7613 7614**示例:** 7615 7616```ts 7617import { BusinessError } from '@ohos.base'; 7618 7619audioCapturer.getCapturerInfo((err: BusinessError, capturerInfo: audio.AudioCapturerInfo) => { 7620 if (err) { 7621 console.error('Failed to get capture info'); 7622 } else { 7623 console.info('Capturer getCapturerInfo:'); 7624 console.info(`Capturer source: ${capturerInfo.source}`); 7625 console.info(`Capturer flags: ${capturerInfo.capturerFlags}`); 7626 } 7627}); 7628``` 7629 7630 7631### getCapturerInfo<sup>8+</sup> 7632 7633getCapturerInfo(): Promise<AudioCapturerInfo\> 7634 7635获取采集器信息。使用Promise方式异步返回结果。 7636 7637**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7638 7639**返回值:** 7640 7641| 类型 | 说明 | 7642| :------------------------------------------------ | :---------------------------------- | 7643| Promise<[AudioCapturerInfo](#audiocapturerinfo)\> | Promise对象,返回采集器信息。 | 7644 7645**示例:** 7646 7647```ts 7648import { BusinessError } from '@ohos.base'; 7649 7650audioCapturer.getCapturerInfo().then((audioParamsGet: audio.AudioCapturerInfo) => { 7651 if (audioParamsGet != undefined) { 7652 console.info('AudioFrameworkRecLog: Capturer CapturerInfo:'); 7653 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 7654 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 7655 } else { 7656 console.info(`AudioFrameworkRecLog: audioParamsGet is : ${audioParamsGet}`); 7657 console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect'); 7658 } 7659}).catch((err: BusinessError) => { 7660 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${err}`); 7661}) 7662``` 7663 7664### getCapturerInfoSync<sup>10+</sup> 7665 7666getCapturerInfoSync(): AudioCapturerInfo 7667 7668获取采集器信息,同步返回结果。 7669 7670**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7671 7672**返回值:** 7673 7674| 类型 | 说明 | 7675| :------------------------------------------------ | :---------------------------------- | 7676| [AudioCapturerInfo](#audiocapturerinfo) | 返回采集器信息。 | 7677 7678**示例:** 7679 7680```ts 7681import { BusinessError } from '@ohos.base'; 7682 7683try { 7684 let audioParamsGet: audio.AudioCapturerInfo = audioCapturer.getCapturerInfoSync(); 7685 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 7686 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 7687} catch (err) { 7688 let error = err as BusinessError; 7689 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${error}`); 7690} 7691``` 7692 7693### getStreamInfo<sup>8+</sup> 7694 7695getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 7696 7697获取采集器流信息。使用callback方式异步返回结果。 7698 7699**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7700 7701**参数:** 7702 7703| 参数名 | 类型 | 必填 | 说明 | 7704| :------- | :--------------------------------------------------- | :--- | :------------------------------- | 7705| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调函数。当获取采集器流信息成功,err为undefined,data为获取到的采集器流信息;否则为错误对象。 | 7706 7707**示例:** 7708 7709```ts 7710import { BusinessError } from '@ohos.base'; 7711 7712audioCapturer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 7713 if (err) { 7714 console.error('Failed to get stream info'); 7715 } else { 7716 console.info('Capturer GetStreamInfo:'); 7717 console.info(`Capturer sampling rate: ${streamInfo.samplingRate}`); 7718 console.info(`Capturer channel: ${streamInfo.channels}`); 7719 console.info(`Capturer format: ${streamInfo.sampleFormat}`); 7720 console.info(`Capturer encoding type: ${streamInfo.encodingType}`); 7721 } 7722}); 7723``` 7724 7725### getStreamInfo<sup>8+</sup> 7726 7727getStreamInfo(): Promise<AudioStreamInfo\> 7728 7729获取采集器流信息。使用Promise方式异步返回结果。 7730 7731**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7732 7733**返回值:** 7734 7735| 类型 | 说明 | 7736| :--------------------------------------------- | :------------------------------ | 7737| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise对象,返回流信息。 | 7738 7739**示例:** 7740 7741```ts 7742import { BusinessError } from '@ohos.base'; 7743 7744audioCapturer.getStreamInfo().then((audioParamsGet: audio.AudioStreamInfo) => { 7745 console.info('getStreamInfo:'); 7746 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 7747 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 7748 console.info(`channels: ${audioParamsGet.channels}`); 7749 console.info(`encodingType: ${audioParamsGet.encodingType}`); 7750}).catch((err: BusinessError) => { 7751 console.error(`getStreamInfo :ERROR: ${err}`); 7752}); 7753``` 7754 7755### getStreamInfoSync<sup>10+</sup> 7756 7757getStreamInfoSync(): AudioStreamInfo 7758 7759获取采集器流信息,同步返回结果。 7760 7761**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7762 7763**返回值:** 7764 7765| 类型 | 说明 | 7766| :--------------------------------------------- | :------------------------------ | 7767| [AudioStreamInfo](#audiostreaminfo8) | 返回流信息。 | 7768 7769**示例:** 7770 7771```ts 7772import { BusinessError } from '@ohos.base'; 7773 7774try { 7775 let audioParamsGet: audio.AudioStreamInfo = audioCapturer.getStreamInfoSync(); 7776 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 7777 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 7778 console.info(`channels: ${audioParamsGet.channels}`); 7779 console.info(`encodingType: ${audioParamsGet.encodingType}`); 7780} catch (err) { 7781 let error = err as BusinessError; 7782 console.error(`getStreamInfo :ERROR: ${error}`); 7783} 7784``` 7785 7786### getAudioStreamId<sup>9+</sup> 7787 7788getAudioStreamId(callback: AsyncCallback<number\>): void 7789 7790获取音频流id,使用callback方式异步返回结果。 7791 7792**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7793 7794**参数:** 7795 7796| 参数名 | 类型 | 必填 | 说明 | 7797| :------- | :--------------------------------------------------- | :--- | :------------------- | 7798| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 7799 7800**示例:** 7801 7802```ts 7803import { BusinessError } from '@ohos.base'; 7804 7805audioCapturer.getAudioStreamId((err: BusinessError, streamId: number) => { 7806 console.info(`audioCapturer GetStreamId: ${streamId}`); 7807}); 7808``` 7809 7810### getAudioStreamId<sup>9+</sup> 7811 7812getAudioStreamId(): Promise<number\> 7813 7814获取音频流id,使用Promise方式异步返回结果。 7815 7816**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7817 7818**返回值:** 7819 7820| 类型 | 说明 | 7821| :----------------| :--------------------- | 7822| Promise<number\> | Promise对象,返回音频流id。 | 7823 7824**示例:** 7825 7826```ts 7827import { BusinessError } from '@ohos.base'; 7828 7829audioCapturer.getAudioStreamId().then((streamId: number) => { 7830 console.info(`audioCapturer getAudioStreamId: ${streamId}`); 7831}).catch((err: BusinessError) => { 7832 console.error(`ERROR: ${err}`); 7833}); 7834``` 7835 7836### getAudioStreamIdSync<sup>10+</sup> 7837 7838getAudioStreamIdSync(): number 7839 7840获取音频流id,同步返回结果。 7841 7842**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7843 7844**返回值:** 7845 7846| 类型 | 说明 | 7847| :----------------| :--------------------- | 7848| number | 返回音频流id。 | 7849 7850**示例:** 7851 7852```ts 7853import { BusinessError } from '@ohos.base'; 7854 7855try { 7856 let streamId: number = audioCapturer.getAudioStreamIdSync(); 7857 console.info(`audioCapturer getAudioStreamIdSync: ${streamId}`); 7858} catch (err) { 7859 let error = err as BusinessError; 7860 console.error(`ERROR: ${error}`); 7861} 7862``` 7863 7864### start<sup>8+</sup> 7865 7866start(callback: AsyncCallback<void\>): void 7867 7868启动音频采集器。使用callback方式异步返回结果。 7869 7870**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7871 7872**参数:** 7873 7874| 参数名 | 类型 | 必填 | 说明 | 7875| :------- | :------------------- | :--- | :----------------------------- | 7876| callback | AsyncCallback<void\> | 是 | 回调函数。当启动音频采集器成功,err为undefined,否则为错误对象。 | 7877 7878**示例:** 7879 7880```ts 7881import { BusinessError } from '@ohos.base'; 7882 7883audioCapturer.start((err: BusinessError) => { 7884 if (err) { 7885 console.error('Capturer start failed.'); 7886 } else { 7887 console.info('Capturer start success.'); 7888 } 7889}); 7890``` 7891 7892 7893### start<sup>8+</sup> 7894 7895start(): Promise<void\> 7896 7897启动音频采集器。使用Promise方式异步返回结果。 7898 7899**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7900 7901**返回值:** 7902 7903| 类型 | 说明 | 7904| :------------- | :---------------------------- | 7905| Promise<void\> | Promise对象,无返回结果。 | 7906 7907**示例:** 7908 7909```ts 7910import { BusinessError } from '@ohos.base'; 7911 7912audioCapturer.start().then(() => { 7913 console.info('AudioFrameworkRecLog: ---------START---------'); 7914 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 7915 console.info(`AudioFrameworkRecLog: AudioCapturer: STATE: ${audioCapturer.state}`); 7916 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 7917 if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) { 7918 console.info('AudioFrameworkRecLog: AudioCapturer is in Running State'); 7919 } 7920}).catch((err: BusinessError) => { 7921 console.error(`AudioFrameworkRecLog: Capturer start :ERROR : ${err}`); 7922}); 7923``` 7924 7925### stop<sup>8+</sup> 7926 7927stop(callback: AsyncCallback<void\>): void 7928 7929停止采集。使用callback方式异步返回结果。 7930 7931**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7932 7933**参数:** 7934 7935| 参数名 | 类型 | 必填 | 说明 | 7936| :------- | :------------------- | :--- | :----------------------------- | 7937| callback | AsyncCallback<void\> | 是 | 回调函数。当停止采集成功,err为undefined,否则为错误对象。 | 7938 7939**示例:** 7940 7941```ts 7942import { BusinessError } from '@ohos.base'; 7943 7944audioCapturer.stop((err: BusinessError) => { 7945 if (err) { 7946 console.error('Capturer stop failed'); 7947 } else { 7948 console.info('Capturer stopped.'); 7949 } 7950}); 7951``` 7952 7953 7954### stop<sup>8+</sup> 7955 7956stop(): Promise<void\> 7957 7958停止采集。使用Promise方式异步返回结果。 7959 7960**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7961 7962**返回值:** 7963 7964| 类型 | 说明 | 7965| :------------- | :---------------------------- | 7966| Promise<void\> | Promise对象,无返回结果。 | 7967 7968**示例:** 7969 7970```ts 7971import { BusinessError } from '@ohos.base'; 7972 7973audioCapturer.stop().then(() => { 7974 console.info('AudioFrameworkRecLog: ---------STOP RECORD---------'); 7975 console.info('AudioFrameworkRecLog: Capturer stopped: SUCCESS'); 7976 if ((audioCapturer.state == audio.AudioState.STATE_STOPPED)){ 7977 console.info('AudioFrameworkRecLog: State is Stopped:'); 7978 } 7979}).catch((err: BusinessError) => { 7980 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 7981}); 7982``` 7983 7984### release<sup>8+</sup> 7985 7986release(callback: AsyncCallback<void\>): void 7987 7988释放采集器。使用callback方式异步返回结果。 7989 7990**系统能力:** SystemCapability.Multimedia.Audio.Capturer 7991 7992**参数:** 7993 7994| 参数名 | 类型 | 必填 | 说明 | 7995| :------- | :------------------- | :--- | :---------------------------------- | 7996| callback | AsyncCallback<void\> | 是 | 回调函数。当释放采集器成功,err为undefined,否则为错误对象。 | 7997 7998**示例:** 7999 8000```ts 8001import { BusinessError } from '@ohos.base'; 8002 8003audioCapturer.release((err: BusinessError) => { 8004 if (err) { 8005 console.error('capturer release failed'); 8006 } else { 8007 console.info('capturer released.'); 8008 } 8009}); 8010``` 8011 8012 8013### release<sup>8+</sup> 8014 8015release(): Promise<void\> 8016 8017释放采集器。使用Promise方式异步返回结果。 8018 8019**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8020 8021**返回值:** 8022 8023| 类型 | 说明 | 8024| :------------- | :---------------------------- | 8025| Promise<void\> | Promise对象,无返回结果。 | 8026 8027**示例:** 8028 8029```ts 8030import { BusinessError } from '@ohos.base'; 8031 8032audioCapturer.release().then(() => { 8033 console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------'); 8034 console.info('AudioFrameworkRecLog: Capturer release : SUCCESS'); 8035 console.info(`AudioFrameworkRecLog: AudioCapturer : STATE : ${audioCapturer.state}`); 8036}).catch((err: BusinessError) => { 8037 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 8038}); 8039``` 8040 8041### read<sup>8+</sup> 8042 8043read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void 8044 8045读入缓冲区。使用callback方式异步返回结果。 8046 8047**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8048 8049**参数:** 8050 8051| 参数名 | 类型 | 必填 | 说明 | 8052| :------------- | :-------------------------- | :--- | :------------------------------- | 8053| size | number | 是 | 读入的字节数。 | 8054| isBlockingRead | boolean | 是 | 是否阻塞读操作 ,true阻塞,false不阻塞。 | 8055| callback | AsyncCallback<ArrayBuffer\> | 是 | 回调函数。当读入缓冲区成功,err为undefined,data为获取到的缓冲区;否则为错误对象。 | 8056 8057**示例:** 8058 8059```ts 8060import { BusinessError } from '@ohos.base'; 8061 8062let bufferSize: number = 0; 8063audioCapturer.getBufferSize().then((data: number) => { 8064 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 8065 bufferSize = data; 8066}).catch((err: BusinessError) => { 8067 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR: ${err}`); 8068}); 8069audioCapturer.read(bufferSize, true, (err: BusinessError, buffer: ArrayBuffer) => { 8070 if (!err) { 8071 console.info('Success in reading the buffer data'); 8072 } 8073}); 8074``` 8075 8076### read<sup>8+</sup> 8077 8078read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\> 8079 8080读入缓冲区。使用Promise方式异步返回结果。 8081 8082**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8083 8084**参数:** 8085 8086| 参数名 | 类型 | 必填 | 说明 | 8087| :------------- | :------ | :--- | :--------------- | 8088| size | number | 是 | 读入的字节数。 | 8089| isBlockingRead | boolean | 是 | 是否阻塞读操作 ,true阻塞,false不阻塞。 | 8090 8091**返回值:** 8092 8093| 类型 | 说明 | 8094| :-------------------- | :----------------------------------------------------- | 8095| Promise<ArrayBuffer\> | Promise对象,返回读取的缓冲区数据。 | 8096 8097**示例:** 8098 8099```ts 8100import { BusinessError } from '@ohos.base'; 8101 8102let bufferSize: number = 0; 8103audioCapturer.getBufferSize().then((data: number) => { 8104 console.info(`AudioFrameworkRecLog: getBufferSize: SUCCESS ${data}`); 8105 bufferSize = data; 8106}).catch((err: BusinessError) => { 8107 console.error(`AudioFrameworkRecLog: getBufferSize: ERROR ${err}`); 8108}); 8109console.info(`Buffer size: ${bufferSize}`); 8110audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 8111 console.info('buffer read successfully'); 8112}).catch((err: BusinessError) => { 8113 console.error(`ERROR : ${err}`); 8114}); 8115``` 8116 8117### getAudioTime<sup>8+</sup> 8118 8119getAudioTime(callback: AsyncCallback<number\>): void 8120 8121获取时间戳(从1970年1月1日开始),单位为纳秒。使用callback方式异步返回结果。 8122 8123**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8124 8125**参数:** 8126 8127| 参数名 | 类型 | 必填 | 说明 | 8128| :------- | :--------------------- | :--- | :----------------------------- | 8129| callback | AsyncCallback<number\> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 8130 8131**示例:** 8132 8133```ts 8134import { BusinessError } from '@ohos.base'; 8135 8136audioCapturer.getAudioTime((err: BusinessError, timestamp: number) => { 8137 console.info(`Current timestamp: ${timestamp}`); 8138}); 8139``` 8140 8141### getAudioTime<sup>8+</sup> 8142 8143getAudioTime(): Promise<number\> 8144 8145获取时间戳(从1970年1月1日开始),单位为纳秒。使用Promise方式异步返回结果。 8146 8147**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8148 8149**返回值:** 8150 8151| 类型 | 说明 | 8152| :--------------- | :---------------------------- | 8153| Promise<number\> | Promise对象,返回时间戳(从1970年1月1日开始),单位为纳秒。 | 8154 8155**示例:** 8156 8157```ts 8158import { BusinessError } from '@ohos.base'; 8159 8160audioCapturer.getAudioTime().then((audioTime: number) => { 8161 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTime : Success ${audioTime}`); 8162}).catch((err: BusinessError) => { 8163 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 8164}); 8165``` 8166 8167### getAudioTimeSync<sup>10+</sup> 8168 8169getAudioTimeSync(): number 8170 8171获取时间戳(从1970年1月1日开始),单位为纳秒,同步返回结果。 8172 8173**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8174 8175**返回值:** 8176 8177| 类型 | 说明 | 8178| :--------------- | :---------------------------- | 8179| number | 返回时间戳。 | 8180 8181**示例:** 8182 8183```ts 8184import { BusinessError } from '@ohos.base'; 8185 8186try { 8187 let audioTime: number = audioCapturer.getAudioTimeSync(); 8188 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : Success ${audioTime}`); 8189} catch (err) { 8190 let error = err as BusinessError; 8191 console.error(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : ERROR : ${error}`); 8192} 8193``` 8194 8195### getBufferSize<sup>8+</sup> 8196 8197getBufferSize(callback: AsyncCallback<number\>): void 8198 8199获取采集器合理的最小缓冲区大小。使用callback方式异步返回结果。 8200 8201**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8202 8203**参数:** 8204 8205| 参数名 | 类型 | 必填 | 说明 | 8206| :------- | :--------------------- | :--- | :----------------------------------- | 8207| callback | AsyncCallback<number\> | 是 | 回调函数。当获取采集器合理的最小缓冲区大小成功,err为undefined,data为获取到的采集器合理的最小缓冲区大小;否则为错误对象。 | 8208 8209**示例:** 8210 8211```ts 8212import { BusinessError } from '@ohos.base'; 8213 8214audioCapturer.getBufferSize((err: BusinessError, bufferSize: number) => { 8215 if (!err) { 8216 console.info(`BufferSize : ${bufferSize}`); 8217 audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 8218 console.info(`Buffer read is ${buffer.byteLength}`); 8219 }).catch((err: BusinessError) => { 8220 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 8221 }); 8222 } 8223}); 8224``` 8225 8226### getBufferSize<sup>8+</sup> 8227 8228getBufferSize(): Promise<number\> 8229 8230获取采集器合理的最小缓冲区大小。使用Promise方式异步返回结果。 8231 8232**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8233 8234**返回值:** 8235 8236| 类型 | 说明 | 8237| :--------------- | :---------------------------------- | 8238| Promise<number\> | Promise对象,返回缓冲区大小。 | 8239 8240**示例:** 8241 8242```ts 8243import { BusinessError } from '@ohos.base'; 8244 8245let bufferSize: number = 0; 8246audioCapturer.getBufferSize().then((data: number) => { 8247 console.info(`AudioFrameworkRecLog: getBufferSize :SUCCESS ${data}`); 8248 bufferSize = data; 8249}).catch((err: BusinessError) => { 8250 console.error(`AudioFrameworkRecLog: getBufferSize :ERROR : ${err}`); 8251}); 8252``` 8253 8254### getBufferSizeSync<sup>10+</sup> 8255 8256getBufferSizeSync(): number 8257 8258获取采集器合理的最小缓冲区大小,同步返回结果。 8259 8260**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8261 8262**返回值:** 8263 8264| 类型 | 说明 | 8265| :--------------- | :---------------------------------- | 8266| number | 返回缓冲区大小。 | 8267 8268**示例:** 8269 8270```ts 8271import { BusinessError } from '@ohos.base'; 8272 8273let bufferSize: number = 0; 8274try { 8275 bufferSize = audioCapturer.getBufferSizeSync(); 8276 console.info(`AudioFrameworkRecLog: getBufferSizeSync :SUCCESS ${bufferSize}`); 8277} catch (err) { 8278 let error = err as BusinessError; 8279 console.error(`AudioFrameworkRecLog: getBufferSizeSync :ERROR : ${error}`); 8280} 8281``` 8282 8283### on('audioInterrupt')<sup>10+</sup> 8284 8285on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 8286 8287监听音频中断事件,使用callback方式返回结果。 8288 8289与[on('interrupt')](#oninterrupt)一致,均用于监听焦点变化。AudioCapturer对象在start事件发生时会主动获取焦点,在pause、stop等事件发生时会主动释放焦点,不需要开发者主动发起获取焦点或释放焦点的申请。 8290 8291**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8292 8293**参数:** 8294 8295| 参数名 | 类型 | 必填 | 说明 | 8296| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 8297| type | string | 是 | 事件回调类型,支持的事件为:'audioInterrupt'(中断事件被触发,音频采集被中断。) | 8298| callback | Callback\<[InterruptEvent](#interruptevent9)\> | 是 | 回调函数,返回播放中断时,应用接收的中断事件信息。 | 8299 8300**错误码:** 8301 8302以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 8303 8304| 错误码ID | 错误信息 | 8305| ------- | --------------------------------------------| 8306| 6800101 | if input parameter value error | 8307 8308**示例:** 8309 8310```ts 8311import audio from '@ohos.multimedia.audio'; 8312 8313let isCapturing: boolean; // 标识符,表示是否正在采集 8314onAudioInterrupt(); 8315 8316async function onAudioInterrupt(){ 8317 audioCapturer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 8318 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 8319 // 由系统进行操作,强制打断音频采集,应用需更新自身状态及显示内容等 8320 switch (interruptEvent.hintType) { 8321 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 8322 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent 8323 console.info('Force paused. Update capturing status and stop reading'); 8324 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作 8325 break; 8326 case audio.InterruptHint.INTERRUPT_HINT_STOP: 8327 // 音频流已被停止,永久失去焦点,若想恢复采集,需用户主动触发 8328 console.info('Force stopped. Update capturing status and stop reading'); 8329 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作 8330 break; 8331 default: 8332 console.info('Invalid interruptEvent'); 8333 break; 8334 } 8335 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 8336 // 由应用进行操作,应用可以自主选择打断或忽略 8337 switch (interruptEvent.hintType) { 8338 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 8339 // 建议应用继续采集(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复采集) 8340 console.info('Resume force paused renderer or ignore'); 8341 // 若选择继续采集,需在此处主动执行开始采集的若干操作 8342 break; 8343 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 8344 // 建议应用暂停采集 8345 console.info('Choose to pause or ignore'); 8346 // 若选择暂停采集,需在此处主动执行暂停采集的若干操作 8347 break; 8348 case audio.InterruptHint.INTERRUPT_HINT_STOP: 8349 // 建议应用停止采集 8350 console.info('Choose to stop or ignore'); 8351 // 若选择停止采集,需在此处主动执行停止采集的若干操作 8352 break; 8353 default: 8354 break; 8355 } 8356 } 8357 }); 8358} 8359``` 8360 8361### off('audioInterrupt')<sup>10+</sup> 8362 8363off(type: 'audioInterrupt'): void 8364 8365取消订阅音频中断事件。 8366 8367**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 8368 8369**参数:** 8370 8371| 参数名 | 类型 | 必填 | 说明 | 8372| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 8373| type | string | 是 | 事件回调类型,支持的事件为:'audioInterrupt' | 8374 8375**错误码:** 8376 8377以下错误码的详细介绍请参见[音频错误码](../errorcodes/errorcode-audio.md)。 8378 8379| 错误码ID | 错误信息 | 8380| ------- | --------------------------------------------| 8381| 6800101 | if input parameter value error | 8382 8383**示例:** 8384 8385```ts 8386audioCapturer.off('audioInterrupt'); 8387``` 8388 8389 8390### on('markReach')<sup>8+</sup> 8391 8392on(type: 'markReach', frame: number, callback: Callback<number>): void 8393 8394订阅标记到达的事件。 当采集的帧数达到 frame 参数的值时,回调被触发,使用callback方式返回结果。 8395 8396**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8397 8398**参数:** 8399 8400| 参数名 | 类型 | 必填 | 说明 | 8401| :------- | :---------------------- | :--- | :----------------------------------------- | 8402| type | string | 是 | 事件回调类型,支持的事件为:'markReach'。 | 8403| frame | number | 是 | 触发事件的帧数。 该值必须大于0。 | 8404| callback | Callback\<number> | 是 | 回调函数,返回frame 参数的值 | 8405 8406**示例:** 8407 8408```ts 8409audioCapturer.on('markReach', 1000, (position: number) => { 8410 if (position == 1000) { 8411 console.info('ON Triggered successfully'); 8412 } 8413}); 8414``` 8415 8416### off('markReach')<sup>8+</sup> 8417 8418off(type: 'markReach'): void 8419 8420取消订阅标记到达的事件。 8421 8422**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8423 8424**参数:** 8425 8426| 参数名 | 类型 | 必填 | 说明 | 8427| :----- | :----- | :--- | :-------------------------------------------- | 8428| type | string | 是 | 取消事件回调类型,支持的事件为:'markReach'。 | 8429 8430**示例:** 8431 8432```ts 8433audioCapturer.off('markReach'); 8434``` 8435 8436### on('periodReach')<sup>8+</sup> 8437 8438on(type: 'periodReach', frame: number, callback: Callback<number>): void 8439 8440订阅到达标记的事件。 当采集的帧数达到 frame 参数的值时,触发回调并返回设定的值,使用callback方式返回结果。 8441 8442**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8443 8444**参数:** 8445 8446| 参数名 | 类型 | 必填 | 说明 | 8447| :------- | :----------------------- | :--- | :------------------------------------------ | 8448| type | string | 是 | 事件回调类型,支持的事件为:'periodReach'。 | 8449| frame | number | 是 | 触发事件的帧数。 该值必须大于0。 | 8450| callback | Callback\<number> | 是 |回调函数,返回frame 参数的值。 | 8451 8452**示例:** 8453 8454```ts 8455audioCapturer.on('periodReach', 1000, (position: number) => { 8456 if (position == 1000) { 8457 console.info('ON Triggered successfully'); 8458 } 8459}); 8460``` 8461 8462### off('periodReach')<sup>8+</sup> 8463 8464off(type: 'periodReach'): void 8465 8466取消订阅标记到达的事件。 8467 8468**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8469 8470**参数:** 8471 8472| 参数名 | 类型 | 必填 | 说明 | 8473| :----- | :----- | :--- | :---------------------------------------------- | 8474| type | string | 是 | 取消事件回调类型,支持的事件为:'periodReach'。 | 8475 8476**示例:** 8477 8478```ts 8479audioCapturer.off('periodReach'); 8480``` 8481 8482### on('stateChange') <sup>8+</sup> 8483 8484on(type: 'stateChange', callback: Callback<AudioState\>): void 8485 8486订阅监听状态变化,使用callback方式返回结果。 8487 8488**系统能力:** SystemCapability.Multimedia.Audio.Capturer 8489 8490**参数:** 8491 8492| 参数名 | 类型 | 必填 | 说明 | 8493| :------- | :------------------------- | :--- | :------------------------------------------ | 8494| type | string | 是 | 事件回调类型,支持的事件为:'stateChange'。 | 8495| callback | Callback\<[AudioState](#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 8496 8497**示例:** 8498 8499```ts 8500audioCapturer.on('stateChange', (state: audio.AudioState) => { 8501 if (state == 1) { 8502 console.info('audio capturer state is: STATE_PREPARED'); 8503 } 8504 if (state == 2) { 8505 console.info('audio capturer state is: STATE_RUNNING'); 8506 } 8507}); 8508``` 8509 8510## ToneType<sup>9+</sup> 8511 8512枚举,播放器的音调类型。 8513 8514**系统接口:** 该接口为系统接口 8515 8516**系统能力:** SystemCapability.Multimedia.Audio.Tone 8517 8518| 名称 | 值 | 说明 | 8519| :------------------------------------------------ | :----- | :----------------------------| 8520| TONE_TYPE_DIAL_0 | 0 | 键0的DTMF音。 | 8521| TONE_TYPE_DIAL_1 | 1 | 键1的DTMF音。 | 8522| TONE_TYPE_DIAL_2 | 2 | 键2的DTMF音。 | 8523| TONE_TYPE_DIAL_3 | 3 | 键3的DTMF音。 | 8524| TONE_TYPE_DIAL_4 | 4 | 键4的DTMF音。 | 8525| TONE_TYPE_DIAL_5 | 5 | 键5的DTMF音。 | 8526| TONE_TYPE_DIAL_6 | 6 | 键6的DTMF音。 | 8527| TONE_TYPE_DIAL_7 | 7 | 键7的DTMF音。 | 8528| TONE_TYPE_DIAL_8 | 8 | 键8的DTMF音。 | 8529| TONE_TYPE_DIAL_9 | 9 | 键9的DTMF音。 | 8530| TONE_TYPE_DIAL_S | 10 | 键*的DTMF音。 | 8531| TONE_TYPE_DIAL_P | 11 | 键#的DTMF音。 | 8532| TONE_TYPE_DIAL_A | 12 | 键A的DTMF音。 | 8533| TONE_TYPE_DIAL_B | 13 | 键B的DTMF音。 | 8534| TONE_TYPE_DIAL_C | 14 | 键C的DTMF音。 | 8535| TONE_TYPE_DIAL_D | 15 | 键D的DTMF音。 | 8536| TONE_TYPE_COMMON_SUPERVISORY_DIAL | 100 | 呼叫监管音调,拨号音。 | 8537| TONE_TYPE_COMMON_SUPERVISORY_BUSY | 101 | 呼叫监管音调,忙。 | 8538| TONE_TYPE_COMMON_SUPERVISORY_CONGESTION | 102 | 呼叫监管音调,拥塞。 | 8539| TONE_TYPE_COMMON_SUPERVISORY_RADIO_ACK | 103 | 呼叫监管音调,无线电 ACK。 | 8540| TONE_TYPE_COMMON_SUPERVISORY_RADIO_NOT_AVAILABLE | 104 | 呼叫监管音调,无线电不可用。 | 8541| TONE_TYPE_COMMON_SUPERVISORY_CALL_WAITING | 106 | 呼叫监管音调,呼叫等待。 | 8542| TONE_TYPE_COMMON_SUPERVISORY_RINGTONE | 107 | 呼叫监管音调,铃声。 | 8543| TONE_TYPE_COMMON_PROPRIETARY_BEEP | 200 | 专有声调,一般蜂鸣声。 | 8544| TONE_TYPE_COMMON_PROPRIETARY_ACK | 201 | 专有声调,ACK。 | 8545| TONE_TYPE_COMMON_PROPRIETARY_PROMPT | 203 | 专有声调,PROMPT。 | 8546| TONE_TYPE_COMMON_PROPRIETARY_DOUBLE_BEEP | 204 | 专有声调,双重蜂鸣声。 | 8547 8548## TonePlayer<sup>9+</sup> 8549 8550提供播放和管理DTMF(Dual Tone Multi Frequency,双音多频)音调的方法,包括各种系统监听音调、专有音调,如拨号音、通话回铃音等。 8551在调用TonePlayer的接口前,需要先通过[createTonePlayer](#audiocreatetoneplayer9)创建实例。 8552 8553**系统接口:** 该接口为系统接口 8554 8555### load<sup>9+</sup> 8556 8557load(type: ToneType, callback: AsyncCallback<void>): void 8558 8559加载DTMF音调配置。使用callback方式异步返回结果。 8560 8561**系统接口:** 该接口为系统接口 8562 8563**系统能力:** SystemCapability.Multimedia.Audio.Tone 8564 8565**参数:** 8566 8567| 参数名 | 类型 | 必填 | 说明 | 8568| :--------------| :-------------------------- | :-----| :------------------------------ | 8569| type | [ToneType](#tonetype9) | 是 | 配置的音调类型。 | 8570| callback | AsyncCallback<void\> | 是 | 回调函数。当加载DTMF音调配置成功,err为undefined,否则为错误对象。 | 8571 8572**示例:** 8573 8574```ts 8575import { BusinessError } from '@ohos.base'; 8576 8577tonePlayer.load(audio.ToneType.TONE_TYPE_DIAL_5, (err: BusinessError) => { 8578 if (err) { 8579 console.error(`callback call load failed error: ${err.message}`); 8580 return; 8581 } else { 8582 console.info('callback call load success'); 8583 } 8584}); 8585``` 8586 8587### load<sup>9+</sup> 8588 8589load(type: ToneType): Promise<void> 8590 8591加载DTMF音调配置。使用Promise方式异步返回结果。 8592 8593**系统接口:** 该接口为系统接口 8594 8595**系统能力:** SystemCapability.Multimedia.Audio.Tone 8596 8597**参数:** 8598 8599| 参数名 | 类型 | 必填 | 说明 | 8600| :------------- | :--------------------- | :--- | ---------------- | 8601| type | [ToneType](#tonetype9) | 是 | 配置的音调类型。 | 8602 8603**返回值:** 8604 8605| 类型 | 说明 | 8606| :--------------| :-------------------------- | 8607| Promise<void\> | Promise对象,无返回结果。 | 8608 8609**示例:** 8610 8611```ts 8612tonePlayer.load(audio.ToneType.TONE_TYPE_DIAL_1).then(() => { 8613 console.info('promise call load '); 8614}).catch(() => { 8615 console.error('promise call load fail'); 8616}); 8617``` 8618 8619### start<sup>9+</sup> 8620 8621start(callback: AsyncCallback<void>): void 8622 8623启动DTMF音调播放。使用callback方式异步返回结果。 8624 8625**系统接口:** 该接口为系统接口 8626 8627**系统能力:** SystemCapability.Multimedia.Audio.Tone 8628 8629**参数:** 8630 8631| 参数名 | 类型 | 必填 | 说明 | 8632| :------- | :------------------- | :--- | :----------------------------- | 8633| callback | AsyncCallback<void\> | 是 | 回调函数。当启动DTMF音调播放成功,err为undefined,否则为错误对象。 | 8634 8635**示例:** 8636 8637```ts 8638import { BusinessError } from '@ohos.base'; 8639 8640tonePlayer.start((err: BusinessError) => { 8641 if (err) { 8642 console.error(`callback call start failed error: ${err.message}`); 8643 return; 8644 } else { 8645 console.info('callback call start success'); 8646 } 8647}); 8648``` 8649 8650### start<sup>9+</sup> 8651 8652start(): Promise<void> 8653 8654启动DTMF音调播放。使用Promise方式异步返回结果。 8655 8656**系统接口:** 该接口为系统接口 8657 8658**系统能力:** SystemCapability.Multimedia.Audio.Tone 8659 8660**返回值:** 8661 8662| 类型 | 说明 | 8663| :------------- | :---------------------------- | 8664| Promise<void\> | Promise对象,无返回结果。 | 8665 8666**示例:** 8667 8668```ts 8669tonePlayer.start().then(() => { 8670 console.info('promise call start'); 8671}).catch(() => { 8672 console.error('promise call start fail'); 8673}); 8674``` 8675 8676### stop<sup>9+</sup> 8677 8678stop(callback: AsyncCallback<void>): void 8679 8680停止当前正在播放的音调。使用callback方式异步返回结果。 8681 8682**系统接口:** 该接口为系统接口 8683 8684**系统能力:** SystemCapability.Multimedia.Audio.Tone 8685 8686**参数:** 8687 8688| 参数名 | 类型 | 必填 | 说明 | 8689| :------- | :------------------- | :--- | :----------------------------- | 8690| callback | AsyncCallback<void\> | 是 | 回调函数。当停止当前正在播放的音调成功,err为undefined,否则为错误对象。 | 8691 8692**示例:** 8693 8694```ts 8695import { BusinessError } from '@ohos.base'; 8696 8697tonePlayer.stop((err: BusinessError) => { 8698 if (err) { 8699 console.error(`callback call stop error: ${err.message}`); 8700 return; 8701 } else { 8702 console.error('callback call stop success '); 8703 } 8704}); 8705``` 8706 8707### stop<sup>9+</sup> 8708 8709stop(): Promise<void> 8710 8711停止当前正在播放的音调。使用Promise方式异步返回结果。 8712 8713**系统接口:** 该接口为系统接口 8714 8715**系统能力:** SystemCapability.Multimedia.Audio.Tone 8716 8717**返回值:** 8718 8719| 类型 | 说明 | 8720| :------------- | :---------------------------- | 8721| Promise<void\> | Promise对象,无返回结果。 | 8722 8723**示例:** 8724 8725```ts 8726tonePlayer.stop().then(() => { 8727 console.info('promise call stop finish'); 8728}).catch(() => { 8729 console.error('promise call stop fail'); 8730}); 8731``` 8732 8733### release<sup>9+</sup> 8734 8735release(callback: AsyncCallback<void>): void 8736 8737释放与此TonePlayer对象关联的资源。使用callback方式异步返回结果。 8738 8739**系统接口:** 该接口为系统接口 8740 8741**系统能力:** SystemCapability.Multimedia.Audio.Tone 8742 8743**参数:** 8744 8745| 参数名 | 类型 | 必填 | 说明 | 8746| :------- | :------------------- | :--- | :---------------------------- | 8747| callback | AsyncCallback<void\> | 是 | 回调函数。当释放与此TonePlayer对象关联的资源成功,err为undefined,否则为错误对象。 | 8748 8749**示例:** 8750 8751```ts 8752import { BusinessError } from '@ohos.base'; 8753 8754tonePlayer.release((err: BusinessError) => { 8755 if (err) { 8756 console.error(`callback call release failed error: ${err.message}`); 8757 return; 8758 } else { 8759 console.info('callback call release success '); 8760 } 8761}); 8762``` 8763 8764### release<sup>9+</sup> 8765 8766release(): Promise<void> 8767 8768释放与此TonePlayer对象关联的资源。使用Promise方式异步返回结果。 8769 8770**系统接口:** 该接口为系统接口 8771 8772**系统能力:** SystemCapability.Multimedia.Audio.Tone 8773 8774**返回值:** 8775 8776| 类型 | 说明 | 8777| :------------- | :---------------------------- | 8778| Promise<void\> | Promise对象,无返回结果。 | 8779 8780**示例:** 8781 8782```ts 8783tonePlayer.release().then(() => { 8784 console.info('promise call release'); 8785}).catch(() => { 8786 console.error('promise call release fail'); 8787}); 8788``` 8789 8790## ActiveDeviceType<sup>(deprecated)</sup> 8791 8792枚举,活跃设备类型。 8793 8794> **说明:** 8795> 8796> 从 API version 9 开始废弃,建议使用[CommunicationDeviceType](#communicationdevicetype9)替代。 8797 8798**系统能力:** SystemCapability.Multimedia.Audio.Device 8799 8800| 名称 | 值 | 说明 | 8801| ------------- | ------ | ---------------------------------------------------- | 8802| SPEAKER | 2 | 扬声器。 | 8803| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 8804 8805## InterruptActionType<sup>(deprecated)</sup> 8806 8807枚举,中断事件返回类型。 8808 8809> **说明:** 8810> 8811> 从 API version 7 开始支持,从 API version 9 开始废弃。无替代接口,与中断事件配套使用。 8812 8813**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8814 8815| 名称 | 值 | 说明 | 8816| -------------- | ------ | ------------------ | 8817| TYPE_ACTIVATED | 0 | 表示触发焦点事件。 | 8818| TYPE_INTERRUPT | 1 | 表示音频打断事件。 | 8819 8820## AudioInterrupt<sup>(deprecated)</sup> 8821 8822音频监听事件传入的参数。 8823 8824> **说明:** 8825> 8826> 从 API version 7 开始支持,从 API version 9 开始废弃。无替代接口,与中断事件配套使用。 8827 8828**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8829 8830| 名称 | 类型 | 必填 | 说明 | 8831| --------------- | --------------------------- | ----| ------------------------------------------------------------ | 8832| streamUsage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 8833| contentType | [ContentType](#contenttypedeprecated) | 是 | 音频打断媒体类型。 | 8834| pauseWhenDucked | boolean | 是 | 音频打断时是否可以暂停音频播放(true表示音频播放可以在音频打断期间暂停,false表示相反)。 | 8835 8836## InterruptAction<sup>(deprecated)</sup> 8837 8838音频打断/获取焦点事件的回调方法。 8839 8840> **说明:** 8841> 8842> 从 API version 7 开始支持,从 API version 9 开始废弃。建议使用[InterruptEvent](#interruptevent9)替代。 8843 8844**系统能力:** SystemCapability.Multimedia.Audio.Renderer 8845 8846| 名称 | 类型 | 必填 | 说明 | 8847| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 8848| actionType | [InterruptActionType](#interruptactiontypedeprecated) | 是 | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。 | 8849| type | [InterruptType](#interrupttype) | 否 | 打断事件类型。 | 8850| hint | [InterruptHint](#interrupthint) | 否 | 打断事件提示。 | 8851| activated | boolean | 否 | 获得/释放焦点。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 | 8852