1# 音频管理 2 3音频管理提供管理音频的一些基础能力,包括对音频音量、音频设备的管理,以及对音频数据的采集和渲染等。 4 5该模块提供以下音频相关的常用功能: 6 7- [AudioManager](#audiomanager):音频管理。 8- [AudioRenderer](#audiorenderer8):音频渲染,用于播放PCM(Pulse Code Modulation)音频数据。 9- [AudioCapturer](#audiocapturer8):音频采集,用于录制PCM音频数据。 10 11> **说明:** 12> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13> 14> API Version 9当前为Canary版本,仅供试用,不保证接口可稳定调用。 15 16## 导入模块 17 18``` 19import audio from '@ohos.multimedia.audio'; 20``` 21 22 23## audio.getAudioManager 24 25getAudioManager(): AudioManager 26 27获取音频管理器。 28 29**系统能力:** SystemCapability.Multimedia.Audio.Core 30 31**返回值:** 32| 类型 | 说明 | 33| ----------------------------- | ------------ | 34| [AudioManager](#audiomanager) | 音频管理类。 | 35 36**示例:** 37``` 38var audioManager = audio.getAudioManager(); 39``` 40 41## audio.createAudioRenderer<sup>8+</sup> 42 43createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void 44 45获取音频渲染器。使用callback方式异步返回结果。 46 47**系统能力:** SystemCapability.Multimedia.Audio.Renderer 48 49**参数**: 50 51| 参数名 | 类型 | 必填 | 说明 | 52| -------- | ----------------------------------------------- | ---- | ---------------- | 53| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 54| callback | AsyncCallback<[AudioRenderer](#audiorenderer8)> | 是 | 音频渲染器对象。 | 55 56**示例:** 57 58``` 59import audio from '@ohos.multimedia.audio'; 60var audioStreamInfo = { 61 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 62 channels: audio.AudioChannel.CHANNEL_1, 63 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 64 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 65} 66 67var audioRendererInfo = { 68 content: audio.ContentType.CONTENT_TYPE_SPEECH, 69 usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 70 rendererFlags: 1 71} 72 73var audioRendererOptions = { 74 streamInfo: audioStreamInfo, 75 rendererInfo: audioRendererInfo 76} 77 78audio.createAudioRenderer(audioRendererOptions,(err, data) => { 79 if (err) { 80 console.error(`AudioRenderer Created : Error: ${err.message}`); 81 } 82 else { 83 console.info('AudioRenderer Created : Success : SUCCESS'); 84 let audioRenderer = data; 85 } 86}); 87``` 88 89## audio.createAudioRenderer<sup>8+</sup> 90 91createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\> 92 93获取音频渲染器。使用Promise方式异步返回结果。 94 95**系统能力:** SystemCapability.Multimedia.Audio.Renderer 96 97**参数:** 98 99| 参数名 | 类型 | 必填 | 说明 | 100| :------ | :--------------------------------------------- | :--- | :----------- | 101| options | [AudioRendererOptions](#audiorendereroptions8) | 是 | 配置渲染器。 | 102 103**返回值:** 104 105| 类型 | 说明 | 106| ----------------------------------------- | ---------------- | 107| Promise<[AudioRenderer](#audiorenderer8)> | 音频渲染器对象。 | 108 109**示例:** 110 111``` 112import audio from '@ohos.multimedia.audio'; 113 114var audioStreamInfo = { 115 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 116 channels: audio.AudioChannel.CHANNEL_1, 117 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 118 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 119} 120 121var audioRendererInfo = { 122 content: audio.ContentType.CONTENT_TYPE_SPEECH, 123 usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 124 rendererFlags: 1 125} 126 127var audioRendererOptions = { 128 streamInfo: audioStreamInfo, 129 rendererInfo: audioRendererInfo 130} 131 132var audioRenderer; 133audio.createAudioRenderer(audioRendererOptions).then((data) => { 134 audioRenderer = data; 135 console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS'); 136}).catch((err) => { 137 console.info('AudioFrameworkRenderLog: AudioRenderer Created : ERROR : '+err.message); 138}); 139``` 140 141## audio.createAudioCapturer<sup>8+</sup> 142 143createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void 144 145获取音频采集器。使用callback方式异步返回结果。 146 147**系统能力:** SystemCapability.Multimedia.Audio.Capturer 148 149**参数:** 150 151| 参数名 | 类型 | 必填 | 说明 | 152| :------- | :---------------------------------------------- | :--- | :--------------- | 153| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 154| callback | AsyncCallback<[AudioCapturer](#audiocapturer8)> | 是 | 音频采集器对象。 | 155 156**示例:** 157 158``` 159import audio from '@ohos.multimedia.audio'; 160var audioStreamInfo = { 161 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 162 channels: audio.AudioChannel.CHANNEL_2, 163 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 164 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 165} 166 167var audioCapturerInfo = { 168 source: audio.SourceType.SOURCE_TYPE_MIC, 169 capturerFlags: 1 170} 171 172var audioCapturerOptions = { 173 streamInfo: audioStreamInfo, 174 capturerInfo: audioCapturerInfo 175} 176 177audio.createAudioCapturer(audioCapturerOptions,(err, data) => { 178 if (err) { 179 console.error(`AudioCapturer Created : Error: ${err.message}`); 180 } 181 else { 182 console.info('AudioCapturer Created : Success : SUCCESS'); 183 let audioCapturer = data; 184 } 185}); 186``` 187 188## audio.createAudioCapturer<sup>8+</sup> 189 190createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\> 191 192获取音频采集器。使用promise 方式异步返回结果。 193 194**系统能力:** SystemCapability.Multimedia.Audio.Capturer 195 196**参数:** 197 198| 参数名 | 类型 | 必填 | 说明 | 199| :------ | :--------------------------------------------- | :--- | :--------------- | 200| options | [AudioCapturerOptions](#audiocaptureroptions8) | 是 | 配置音频采集器。 | 201 202**返回值:** 203 204| 类型 | 说明 | 205| ----------------------------------------- | -------------- | 206| Promise<[AudioCapturer](#audiocapturer8)> | 音频采集器对象 | 207 208**示例:** 209 210``` 211import audio from '@ohos.multimedia.audio'; 212 213var audioStreamInfo = { 214 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 215 channels: audio.AudioChannel.CHANNEL_2, 216 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 217 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 218} 219 220var audioCapturerInfo = { 221 source: audio.SourceType.SOURCE_TYPE_MIC, 222 capturerFlags: 1 223} 224 225var audioCapturerOptions = { 226 streamInfo: audioStreamInfo, 227 capturerInfo: audioCapturerInfo 228} 229 230var audioCapturer; 231audio.createAudioCapturer(audioCapturerOptions).then((data) => { 232 audioCapturer = data; 233 console.info('AudioCapturer Created : Success : Stream Type: SUCCESS'); 234}).catch((err) => { 235 console.info('AudioCapturer Created : ERROR : '+err.message); 236}); 237``` 238 239## AudioVolumeType 240 241枚举,音频流类型。 242 243**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Volume 244 245| 名称 | 默认值 | 描述 | 246| ---------------------------- | ------ | ---------- | 247| VOICE_CALL<sup>8+</sup> | 0 | 语音电话。 | 248| RINGTONE | 2 | 铃声。 | 249| MEDIA | 3 | 媒体。 | 250| VOICE_ASSISTANT<sup>8+</sup> | 9 | 语音助手。 | 251 252 253## DeviceFlag 254 255枚举,可获取的设备种类。 256 257**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 258 259| 名称 | 默认值 | 描述 | 260| ------------------- | ------ | ---------- | 261| OUTPUT_DEVICES_FLAG | 1 | 输出设备。 | 262| INPUT_DEVICES_FLAG | 2 | 输入设备。 | 263| ALL_DEVICES_FLAG | 3 | 所有设备。 | 264 265 266## DeviceRole 267 268枚举,设备角色。 269 270**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 271 272| 名称 | 默认值 | 描述 | 273| ------------- | ------ | -------------- | 274| INPUT_DEVICE | 1 | 输入设备角色。 | 275| OUTPUT_DEVICE | 2 | 输出设备角色。 | 276 277 278## DeviceType 279 280枚举,设备类型。 281 282**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 283 284| 名称 | 默认值 | 描述 | 285| ---------------- | ------ | --------------------------------------------------------- | 286| INVALID | 0 | 无效设备。 | 287| EARPIECE | 1 | 听筒。 | 288| SPEAKER | 2 | 扬声器。 | 289| WIRED_HEADSET | 3 | 有线耳机,带麦克风。 | 290| WIRED_HEADPHONES | 4 | 有线耳机,无麦克风。 | 291| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 292| BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。 | 293| MIC | 15 | 麦克风。 | 294| USB_HEADSET | 22 | USB耳机,带麦克风。 | 295 296## ActiveDeviceType 297 298枚举,活跃设备类型。 299 300**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 301 302| 名称 | 默认值 | 描述 | 303| ------------- | ------ | ---------------------------------------------------- | 304| SPEAKER | 2 | 扬声器。 | 305| BLUETOOTH_SCO | 7 | 蓝牙设备SCO(Synchronous Connection Oriented)连接。 | 306 307## AudioRingMode 308 309枚举,铃声模式。 310 311**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Communication 312 313| 名称 | 默认值 | 描述 | 314| ------------------- | ------ | ---------- | 315| RINGER_MODE_SILENT | 0 | 静音模式。 | 316| RINGER_MODE_VIBRATE | 1 | 震动模式。 | 317| RINGER_MODE_NORMAL | 2 | 响铃模式。 | 318 319## AudioSampleFormat<sup>8+</sup> 320 321枚举,音频采样格式。 322 323**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 324 325| 名称 | 默认值 | 描述 | 326| --------------------- | ------ | -------------------------- | 327| SAMPLE_FORMAT_INVALID | -1 | 无效格式。 | 328| SAMPLE_FORMAT_U8 | 0 | 无符号8位整数。 | 329| SAMPLE_FORMAT_S16LE | 1 | 带符号的16位整数,小尾数。 | 330| SAMPLE_FORMAT_S24LE | 2 | 带符号的24位整数,小尾数。 | 331| SAMPLE_FORMAT_S32LE | 3 | 带符号的32位整数,小尾数。 | 332 333## AudioChannel<sup>8+</sup> 334 335枚举, 音频声道。 336 337**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 338 339| 名称 | 默认值 | 描述 | 340| --------- | -------- | -------- | 341| CHANNEL_1 | 0x1 << 0 | 单声道。 | 342| CHANNEL_2 | 0x1 << 1 | 双声道。 | 343 344## AudioSamplingRate<sup>8+</sup> 345 346枚举,音频采样率。 347 348**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 349 350| 名称 | 默认值 | 描述 | 351| ----------------- | ------ | --------------- | 352| SAMPLE_RATE_8000 | 8000 | 采样率为8000。 | 353| SAMPLE_RATE_11025 | 11025 | 采样率为11025。 | 354| SAMPLE_RATE_12000 | 12000 | 采样率为12000。 | 355| SAMPLE_RATE_16000 | 16000 | 采样率为16000。 | 356| SAMPLE_RATE_22050 | 22050 | 采样率为22050。 | 357| SAMPLE_RATE_24000 | 24000 | 采样率为24000。 | 358| SAMPLE_RATE_32000 | 32000 | 采样率为32000。 | 359| SAMPLE_RATE_44100 | 44100 | 采样率为44100。 | 360| SAMPLE_RATE_48000 | 48000 | 采样率为48000。 | 361| SAMPLE_RATE_64000 | 64000 | 采样率为64000。 | 362| SAMPLE_RATE_96000 | 96000 | 采样率为96000。 | 363 364## AudioEncodingType<sup>8+</sup> 365 366枚举,音频编码类型。 367 368**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 369 370| 名称 | 默认值 | 描述 | 371| --------------------- | ------ | --------- | 372| ENCODING_TYPE_INVALID | -1 | 无效。 | 373| ENCODING_TYPE_RAW | 0 | PCM编码。 | 374 375## ContentType 376 377枚举,音频内容类型。 378 379**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 380 381| 名称 | 默认值 | 描述 | 382| ---------------------------------- | ------ | ---------- | 383| CONTENT_TYPE_UNKNOWN | 0 | 未知类型。 | 384| CONTENT_TYPE_SPEECH | 1 | 语音。 | 385| CONTENT_TYPE_MUSIC | 2 | 音乐。 | 386| CONTENT_TYPE_MOVIE | 3 | 电影。 | 387| CONTENT_TYPE_SONIFICATION | 4 | 加密类型。 | 388| CONTENT_TYPE_RINGTONE<sup>8+</sup> | 5 | 铃声。 | 389 390## StreamUsage 391 392枚举,音频流使用类型。 393 394**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 395 396| 名称 | 默认值 | 描述 | 397| ---------------------------------- | ------ | ---------- | 398| STREAM_USAGE_UNKNOWN | 0 | 未知类型。 | 399| STREAM_USAGE_MEDIA | 1 | 音频。 | 400| STREAM_USAGE_VOICE_COMMUNICATION | 2 | 语音通信。 | 401| STREAM_USAGE_NOTIFICATION_RINGTONE | 6 | 通知铃声。 | 402 403## AudioState<sup>8+</sup> 404 405枚举,音频状态。 406 407**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 408 409| 名称 | 默认值 | 描述 | 410| -------------- | ------ | ---------------- | 411| STATE_INVALID | -1 | 无效状态。 | 412| STATE_NEW | 0 | 创建新实例状态。 | 413| STATE_PREPARED | 1 | 准备状态。 | 414| STATE_RUNNING | 2 | 可运行状态。 | 415| STATE_STOPPED | 3 | 停止状态。 | 416| STATE_RELEASED | 4 | 释放状态。 | 417| STATE_PAUSED | 5 | 暂停状态。 | 418 419## AudioRendererRate<sup>8+</sup> 420 421枚举,音频渲染速度。 422 423**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 424 425| 名称 | 默认值 | 描述 | 426| ------------------ | ------ | ---------- | 427| RENDER_RATE_NORMAL | 0 | 正常速度。 | 428| RENDER_RATE_DOUBLE | 1 | 2倍速。 | 429| RENDER_RATE_HALF | 2 | 0.5倍数。 | 430 431## InterruptType 432 433枚举,中断类型。 434 435**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 436 437| 名称 | 默认值 | 描述 | 438| -------------------- | ------ | ---------------------- | 439| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断事件开始。 | 440| INTERRUPT_TYPE_END | 2 | 音频播放中断事件结束。 | 441 442## InterruptForceType<sup>9+</sup> 443 444枚举,强制打断类型。 445 446**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 447 448| 名称 | 默认值 | 描述 | 449| --------------- | ------ | ------------------------------------ | 450| INTERRUPT_FORCE | 0 | 由系统进行操作,强制打断音频播放。 | 451| INTERRUPT_SHARE | 1 | 由应用进行操作,可以选择打断或忽略。 | 452 453## InterruptHint 454 455枚举,中断提示。 456 457**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 458 459| 名称 | 默认值 | 描述 | 460| ---------------------------------- | ------ | -------------------------------------------- | 461| INTERRUPT_HINT_NONE<sup>8+</sup> | 0 | 无提示。 | 462| INTERRUPT_HINT_RESUME | 1 | 提示音频恢复。 | 463| INTERRUPT_HINT_PAUSE | 2 | 提示音频暂停。 | 464| INTERRUPT_HINT_STOP | 3 | 提示音频停止。 | 465| INTERRUPT_HINT_DUCK | 4 | 提示音频躲避。(躲避:音量减弱,而不会停止) | 466| INTERRUPT_HINT_UNDUCK<sup>8+</sup> | 5 | 提示音量恢复。 | 467 468## InterruptActionType 469 470枚举,中断事件返回类型。 471 472**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 473 474| 名称 | 默认值 | 描述 | 475| -------------- | ------ | ------------------ | 476| TYPE_ACTIVATED | 0 | 表示触发焦点事件。 | 477| TYPE_INTERRUPT | 1 | 表示音频打断事件。 | 478 479## AudioStreamInfo<sup>8+</sup> 480 481音频流信息。 482 483**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 484 485| 名称 | 类型 | 必填 | 说明 | 486| ------------ | ---------------------------------------- | ---- | ------------------ | 487| samplingRate | [AudioSamplingRate](#audiosamplingrate8) | 是 | 音频文件的采样率。 | 488| channels | [AudioChannel](#audiochannel8) | 是 | 音频文件的通道数。 | 489| sampleFormat | [AudioSampleFormat](#audiosampleformat8) | 是 | 音频采样格式。 | 490| encodingType | [AudioEncodingType](#audioencodingtype8) | 是 | 音频编码格式。 | 491 492## AudioRendererInfo<sup>8+</sup> 493 494音频渲染器信息。 495 496**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 497 498| 名称 | 类型 | 必填 | 说明 | 499| ------------- | --------------------------- | ---- | ---------------- | 500| content | [ContentType](#contenttype) | 是 | 媒体类型。 | 501| usage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 502| rendererFlags | number | 是 | 音频渲染器标志。 | 503 504## AudioRendererOptions<sup>8+</sup> 505 506音频渲染器选项信息。 507 508**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 509 510| 名称 | 类型 | 必填 | 说明 | 511| ------------ | ---------------------------------------- | ---- | ---------------- | 512| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。 | 513| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 | 514 515## InterruptEvent<sup>9+</sup> 516 517播放中断时,应用接收的中断事件。 518 519**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 520 521| 名称 | 类型 | 必填 | 说明 | 522| --------- | ------------------------------------------ | ---- | ------------------------------------ | 523| eventType | [InterruptType](#interrupttype) | 是 | 中断事件类型,开始或是结束。 | 524| forceType | [InterruptForceType](#interruptforcetype9) | 是 | 操作是由系统执行或是由应用程序执行。 | 525| hintType | [InterruptHint](#interrupthint) | 是 | 中断提示。 | 526 527## AudioInterrupt 528 529音频监听事件传入的参数。 530 531**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 532 533| 名称 | 类型 | 必填 | 说明 | 534| --------------- | --------------------------- | ---- | ------------------------------------------------------------ | 535| streamUsage | [StreamUsage](#streamusage) | 是 | 音频流使用类型。 | 536| contentType | [ContentType](#contenttype) | 是 | 音频打断媒体类型。 | 537| pauseWhenDucked | boolean | 是 | 音频打断时是否可以暂停音频播放(true表示音频播放可以在音频打断期间暂停,false表示相反)。 | 538 539## InterruptAction 540 541音频打断/获取焦点事件的回调方法。 542 543**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer 544 545| 名称 | 类型 | 必填 | 说明 | 546| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 547| actionType | [InterruptActionType](#interruptactiontype) | 是 | 事件返回类型。TYPE_ACTIVATED为焦点触发事件,TYPE_INTERRUPT为音频打断事件。 | 548| type | [InterruptType](#interrupttype) | 否 | 打断事件类型。 | 549| hint | [InterruptHint](#interrupthint) | 否 | 打断事件提示。 | 550| activated | boolean | 否 | 获得/释放焦点。true表示焦点获取/释放成功,false表示焦点获得/释放失败。 | 551 552## VolumeEvent<sup>8+</sup> 553 554音量改变时,应用接收的事件。 555 556此接口为系统接口,三方应用不支持调用。 557 558**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Volume 559 560| 名称 | 类型 | 必填 | 说明 | 561| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 562| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 563| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 564| updateUi | boolean | 是 | 在UI中显示音量变化。 | 565 566## DeviceChangeAction 567 568描述设备连接状态变化和设备信息。 569 570**系统能力:**SystemCapability.Multimedia.Audio.Device 571 572| 名称 | 类型 | 必填 | 说明 | 573| :---------------- | :------------------------------------------------ | :--- | :----------------- | 574| type | [DeviceChangeType](#DeviceChangeType) | 是 | 设备连接状态变化。 | 575| deviceDescriptors | [AudioDeviceDescriptors](#AudioDeviceDescriptors) | 是 | 设备信息。 | 576 577## DeviceChangeType 578 579枚举,设备连接状态变化。 580 581**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 582 583| 名称 | 默认值 | 描述 | 584| :--------- | :----- | :------------- | 585| CONNECT | 0 | 设备连接。 | 586| DISCONNECT | 1 | 断开设备连接。 | 587 588## AudioCapturerOptions<sup>8+</sup> 589 590音频采集器选项信息。 591 592**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Capturer 593 594| 名称 | 类型 | 必填 | 说明 | 595| ------------ | --------------------------------------- | ---- | ---------------- | 596| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。 | 597| capturerInfo | [AudioCapturerInfo](#audiocapturerinfo) | 是 | 表示采集器信息。 | 598 599## AudioCapturerInfo<sup>8+</sup><a name="audiocapturerinfo"></a> 600 601描述音频采集器信息。 602 603**系统能力:** SystemCapability.Multimedia.Audio.Core 604 605| 名称 | 类型 | 必填 | 说明 | 606| :------------ | :------------------------ | :--- | :--------------- | 607| source | [SourceType](#sourcetype) | 是 | 音源类型。 | 608| capturerFlags | number | 是 | 音频采集器标志。 | 609 610## SourceType<sup>8+</sup><a name="sourcetype"></a> 611 612枚举,音源类型。 613 614**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Core 615 616| 名称 | 默认值 | 描述 | 617| :------------------------------ | :----- | :--------------------- | 618| SOURCE_TYPE_INVALID | -1 | 无效的音频源。 | 619| SOURCE_TYPE_MIC | 0 | Mic音频源。 | 620| SOURCE_TYPE_VOICE_COMMUNICATION | 7 | 语音通话场景的音频源。 | 621 622## AudioScene<sup>8+</sup><a name="audioscene"></a> 623 624枚举,音频场景。 625 626**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Communication 627 628| 名称 | 默认值 | 描述 | 629| :--------------------- | :----- | :-------------------------------------------- | 630| AUDIO_SCENE_DEFAULT | 0 | 默认音频场景。 | 631| AUDIO_SCENE_RINGING | 1 | 响铃模式。<br/>此接口为系统接口,三方应用不支持调用。 | 632| AUDIO_SCENE_PHONE_CALL | 2 | 电话模式。<br/>此接口为系统接口,三方应用不支持调用。 | 633| AUDIO_SCENE_VOICE_CHAT | 3 | 语音聊天模式。 | 634 635## AudioManager 636 637管理音频音量和音频设备。在调用AudioManager的接口前,需要先通过[getAudioManager](#audiogetaudiomanager)创建实例。 638 639### setVolume 640 641setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void 642 643设置指定流的音量,使用callback方式异步返回结果。 644 645**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 646 647**系统能力:** SystemCapability.Multimedia.Audio.Volume 648 649**参数:** 650 651| 参数名 | 类型 | 必填 | 说明 | 652| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 653| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 654| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 655| callback | AsyncCallback<void> | 是 | 回调表示成功还是失败。 | 656 657**示例:** 658 659``` 660audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err) => { 661 if (err) { 662 console.error('Failed to set the volume. ${err.message}'); 663 return; 664 } 665 console.log('Callback invoked to indicate a successful volume setting.'); 666}); 667``` 668 669### setVolume 670 671setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> 672 673设置指定流的音量,使用Promise方式异步返回结果。 674 675**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 676 677**系统能力:** SystemCapability.Multimedia.Audio.Volume 678 679**参数:** 680 681| 参数名 | 类型 | 必填 | 说明 | 682| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 683| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 684| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | 685 686**返回值:** 687 688| 类型 | 说明 | 689| ------------------- | ----------------------------- | 690| Promise<void> | Promise回调表示成功还是失败。 | 691 692**示例:** 693 694``` 695audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => { 696 console.log('Promise returned to indicate a successful volume setting.'); 697}); 698``` 699 700### getVolume 701 702getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 703 704获取指定流的音量,使用callback方式异步返回结果。 705 706**系统能力:** SystemCapability.Multimedia.Audio.Volume 707 708**参数:** 709 710| 参数名 | 类型 | 必填 | 说明 | 711| ---------- | ----------------------------------- | ---- | ------------------ | 712| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 713| callback | AsyncCallback<number> | 是 | 回调返回音量大小。 | 714 715**示例:** 716 717``` 718audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => { 719 if (err) { 720 console.error('Failed to obtain the volume. ${err.message}'); 721 return; 722 } 723 console.log('Callback invoked to indicate that the volume is obtained.'); 724}); 725``` 726 727### getVolume 728 729getVolume(volumeType: AudioVolumeType): Promise<number> 730 731获取指定流的音量,使用Promise方式异步返回结果。 732 733**系统能力:** SystemCapability.Multimedia.Audio.Volume 734 735**参数:** 736 737| 参数名 | 类型 | 必填 | 说明 | 738| ---------- | ----------------------------------- | ---- | ------------ | 739| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 740 741**返回值:** 742 743| 类型 | 说明 | 744| --------------------- | ------------------------- | 745| Promise<number> | Promise回调返回音量大小。 | 746 747**示例:** 748 749``` 750audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => { 751 console.log('Promise returned to indicate that the volume is obtained.' + value); 752}); 753``` 754 755### getMinVolume 756 757getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 758 759获取指定流的最小音量,使用callback方式异步返回结果。 760 761**系统能力:** SystemCapability.Multimedia.Audio.Volume 762 763**参数:** 764 765| 参数名 | 类型 | 必填 | 说明 | 766| ---------- | ----------------------------------- | ---- | ------------------ | 767| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 768| callback | AsyncCallback<number> | 是 | 回调返回最小音量。 | 769 770**示例:** 771 772``` 773audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => { 774 if (err) { 775 console.error('Failed to obtain the minimum volume. ${err.message}'); 776 return; 777 } 778 console.log('Callback invoked to indicate that the minimum volume is obtained.' + value); 779}); 780``` 781 782### getMinVolume 783 784getMinVolume(volumeType: AudioVolumeType): Promise<number> 785 786获取指定流的最小音量,使用Promise方式异步返回结果。 787 788**系统能力:** SystemCapability.Multimedia.Audio.Volume 789 790**参数:** 791 792| 参数名 | 类型 | 必填 | 说明 | 793| ---------- | ----------------------------------- | ---- | ------------ | 794| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 795 796**返回值:** 797 798| 类型 | 说明 | 799| --------------------- | ------------------------- | 800| Promise<number> | Promise回调返回最小音量。 | 801 802**示例:** 803 804``` 805audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => { 806 console.log('Promised returned to indicate that the minimum volume is obtained.' + value); 807}); 808``` 809 810### getMaxVolume 811 812getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void 813 814获取指定流的最大音量,使用callback方式异步返回结果。 815 816**系统能力:** SystemCapability.Multimedia.Audio.Volume 817 818**参数:** 819 820| 参数名 | 类型 | 必填 | 说明 | 821| ---------- | ----------------------------------- | ---- | ---------------------- | 822| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 823| callback | AsyncCallback<number> | 是 | 回调返回最大音量大小。 | 824 825**示例:** 826 827``` 828audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => { 829 if (err) { 830 console.error('Failed to obtain the maximum volume. ${err.message}'); 831 return; 832 } 833 console.log('Callback invoked to indicate that the maximum volume is obtained.' + value); 834}); 835``` 836 837### getMaxVolume 838 839getMaxVolume(volumeType: AudioVolumeType): Promise<number> 840 841获取指定流的最大音量,使用Promise方式异步返回结果。 842 843**系统能力:** SystemCapability.Multimedia.Audio.Volume 844 845**参数:** 846 847| 参数名 | 类型 | 必填 | 说明 | 848| ---------- | ----------------------------------- | ---- | ------------ | 849| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 850 851**返回值:** 852 853| 类型 | 说明 | 854| --------------------- | ----------------------------- | 855| Promise<number> | Promise回调返回最大音量大小。 | 856 857**示例:** 858 859``` 860audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => { 861 console.log('Promised returned to indicate that the maximum volume is obtained.'); 862}); 863``` 864 865### mute 866 867mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void 868 869设置指定音量流静音,使用callback方式异步返回结果。 870 871**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 872 873**系统能力:** SystemCapability.Multimedia.Audio.Volume 874 875**参数:** 876 877| 参数名 | 类型 | 必填 | 说明 | 878| ---------- | ----------------------------------- | ---- | ------------------------------------- | 879| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 880| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 881| callback | AsyncCallback<void> | 是 | 回调表示成功还是失败。 | 882 883**示例:** 884 885``` 886audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => { 887 if (err) { 888 console.error('Failed to mute the stream. ${err.message}'); 889 return; 890 } 891 console.log('Callback invoked to indicate that the stream is muted.'); 892}); 893``` 894 895### mute 896 897mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> 898 899设置指定音量流静音,使用Promise方式异步返回结果。 900 901**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。 902 903**系统能力:** SystemCapability.Multimedia.Audio.Volume 904 905**参数:** 906 907| 参数名 | 类型 | 必填 | 说明 | 908| ---------- | ----------------------------------- | ---- | ------------------------------------- | 909| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 910| mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | 911 912**返回值:** 913 914| 类型 | 说明 | 915| ------------------- | ----------------------------- | 916| Promise<void> | Promise回调表示成功还是失败。 | 917 918**示例:** 919 920 921``` 922audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => { 923 console.log('Promise returned to indicate that the stream is muted.'); 924}); 925``` 926 927 928### isMute 929 930isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 931 932获取指定音量流是否被静音,使用callback方式异步返回结果。 933 934**系统能力:** SystemCapability.Multimedia.Audio.Volume 935 936**参数:** 937 938| 参数名 | 类型 | 必填 | 说明 | 939| ---------- | ----------------------------------- | ---- | ----------------------------------------------- | 940| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 941| callback | AsyncCallback<boolean> | 是 | 回调返回流静音状态,true为静音,false为非静音。 | 942 943**示例:** 944 945``` 946audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => { 947 if (err) { 948 console.error('Failed to obtain the mute status. ${err.message}'); 949 return; 950 } 951 console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value); 952}); 953``` 954 955 956### isMute 957 958isMute(volumeType: AudioVolumeType): Promise<boolean> 959 960获取指定音量流是否被静音,使用Promise方式异步返回结果。 961 962**系统能力:** SystemCapability.Multimedia.Audio.Volume 963 964**参数:** 965 966| 参数名 | 类型 | 必填 | 说明 | 967| ---------- | ----------------------------------- | ---- | ------------ | 968| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 969 970**返回值:** 971 972| 类型 | 说明 | 973| ---------------------- | ------------------------------------------------------ | 974| Promise<boolean> | Promise回调返回流静音状态,true为静音,false为非静音。 | 975 976**示例:** 977 978``` 979audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => { 980 console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value); 981}); 982``` 983 984### isActive 985 986isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void 987 988获取指定音量流是否为活跃状态,使用callback方式异步返回结果。 989 990**系统能力:** SystemCapability.Multimedia.Audio.Volume 991 992**参数:** 993 994| 参数名 | 类型 | 必填 | 说明 | 995| ---------- | ----------------------------------- | ---- | ------------------------------------------------- | 996| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 997| callback | AsyncCallback<boolean> | 是 | 回调返回流的活跃状态,true为活跃,false为不活跃。 | 998 999**示例:** 1000 1001``` 1002audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => { 1003 if (err) { 1004 console.error('Failed to obtain the active status of the stream. ${err.message}'); 1005 return; 1006 } 1007 console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value); 1008}); 1009``` 1010 1011### isActive 1012 1013isActive(volumeType: AudioVolumeType): Promise<boolean> 1014 1015获取指定音量流是否为活跃状态,使用Promise方式异步返回结果。 1016 1017**系统能力:** SystemCapability.Multimedia.Audio.Volume 1018 1019**参数:** 1020 1021| 参数名 | 类型 | 必填 | 说明 | 1022| ---------- | ----------------------------------- | ---- | ------------ | 1023| volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | 1024 1025**返回值:** 1026 1027| 类型 | 说明 | 1028| ---------------------- | -------------------------------------------------------- | 1029| Promise<boolean> | Promise回调返回流的活跃状态,true为活跃,false为不活跃。 | 1030 1031**示例:** 1032 1033``` 1034audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => { 1035 console.log('Promise returned to indicate that the active status of the stream is obtained.' + value); 1036}); 1037``` 1038 1039### setRingerMode 1040 1041setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void 1042 1043设置铃声模式,使用callback方式异步返回结果。 1044 1045**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅在静音和非静音状态切换时需要该权限。 1046 1047**系统能力:** SystemCapability.Multimedia.Audio.Communication 1048 1049**参数:** 1050 1051| 参数名 | 类型 | 必填 | 说明 | 1052| -------- | ------------------------------- | ---- | ------------------------ | 1053| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1054| callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | 1055 1056**示例:** 1057 1058``` 1059audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => { 1060 if (err) { 1061 console.error('Failed to set the ringer mode. ${err.message}'); 1062 return; 1063 } 1064 console.log('Callback invoked to indicate a successful setting of the ringer mode.'); 1065}); 1066``` 1067 1068### setRingerMode 1069 1070setRingerMode(mode: AudioRingMode): Promise<void> 1071 1072设置铃声模式,使用Promise方式异步返回结果。 1073 1074**需要权限:** ohos.permission.ACCESS_NOTIFICATION_POLICY,仅在静音和非静音状态切换时需要该权限。 1075 1076**系统能力:** SystemCapability.Multimedia.Audio.Communication 1077 1078**参数:** 1079 1080| 参数名 | 类型 | 必填 | 说明 | 1081| ------ | ------------------------------- | ---- | -------------- | 1082| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | 1083 1084**返回值:** 1085 1086| 类型 | 说明 | 1087| ------------------- | ------------------------------- | 1088| Promise<void> | Promise回调返回设置成功或失败。 | 1089 1090**示例:** 1091 1092``` 1093audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => { 1094 console.log('Promise returned to indicate a successful setting of the ringer mode.'); 1095}); 1096``` 1097 1098 1099### getRingerMode 1100 1101getRingerMode(callback: AsyncCallback<AudioRingMode>): void 1102 1103获取铃声模式,使用callback方式异步返回结果。 1104 1105**系统能力:** SystemCapability.Multimedia.Audio.Communication 1106 1107**参数:** 1108 1109| 参数名 | 类型 | 必填 | 说明 | 1110| -------- | ---------------------------------------------------- | ---- | ------------------------ | 1111| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调返回系统的铃声模式。 | 1112 1113**示例:** 1114 1115``` 1116audioManager.getRingerMode((err, value) => { 1117 if (err) { 1118 console.error('Failed to obtain the ringer mode. ${err.message}'); 1119 return; 1120 } 1121 console.log('Callback invoked to indicate that the ringer mode is obtained.' + value); 1122}); 1123``` 1124 1125 1126### getRingerMode 1127 1128getRingerMode(): Promise<AudioRingMode> 1129 1130获取铃声模式,使用Promise方式异步返回结果。 1131 1132**系统能力:** SystemCapability.Multimedia.Audio.Communication 1133 1134**返回值:** 1135 1136| 类型 | 说明 | 1137| ---------------------------------------------- | ------------------------------- | 1138| Promise<[AudioRingMode](#audioringmode)> | Promise回调返回系统的铃声模式。 | 1139 1140**示例:** 1141 1142``` 1143audioManager.getRingerMode().then((value) => { 1144 console.log('Promise returned to indicate that the ringer mode is obtained.' + value); 1145}); 1146``` 1147 1148### setAudioParameter 1149 1150setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void 1151 1152音频参数设置,使用callback方式异步返回结果。 1153 1154本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1155 1156**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 1157 1158**系统能力:** SystemCapability.Multimedia.Audio.Core 1159 1160**参数:** 1161 1162| 参数名 | 类型 | 必填 | 说明 | 1163| -------- | ------------------------- | ---- | ------------------------ | 1164| key | string | 是 | 被设置的音频参数的键。 | 1165| value | string | 是 | 被设置的音频参数的值。 | 1166| callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | 1167 1168**示例:** 1169 1170``` 1171audioManager.setAudioParameter('key_example', 'value_example', (err) => { 1172 if (err) { 1173 console.error('Failed to set the audio parameter. ${err.message}'); 1174 return; 1175 } 1176 console.log('Callback invoked to indicate a successful setting of the audio parameter.'); 1177}); 1178``` 1179 1180### setAudioParameter 1181 1182setAudioParameter(key: string, value: string): Promise<void> 1183 1184音频参数设置,使用Promise方式异步返回结果。 1185 1186本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1187 1188**需要权限:** ohos.permission.MODIFY_AUDIO_SETTINGS 1189 1190**系统能力:** SystemCapability.Multimedia.Audio.Core 1191 1192**参数:** 1193 1194| 参数名 | 类型 | 必填 | 说明 | 1195| ------ | ------ | ---- | ---------------------- | 1196| key | string | 是 | 被设置的音频参数的键。 | 1197| value | string | 是 | 被设置的音频参数的值。 | 1198 1199**返回值:** 1200 1201| 类型 | 说明 | 1202| ------------------- | ------------------------------- | 1203| Promise<void> | Promise回调返回设置成功或失败。 | 1204 1205**示例:** 1206 1207``` 1208audioManager.setAudioParameter('key_example', 'value_example').then(() => { 1209 console.log('Promise returned to indicate a successful setting of the audio parameter.'); 1210}); 1211``` 1212 1213### getAudioParameter 1214 1215getAudioParameter(key: string, callback: AsyncCallback<string>): void 1216 1217获取指定音频参数值,使用callback方式异步返回结果。 1218 1219本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1220 1221**系统能力:** SystemCapability.Multimedia.Audio.Core 1222 1223**参数:** 1224 1225| 参数名 | 类型 | 必填 | 说明 | 1226| -------- | --------------------------- | ---- | ---------------------------- | 1227| key | string | 是 | 待获取的音频参数的键。 | 1228| callback | AsyncCallback<string> | 是 | 回调返回获取的音频参数的值。 | 1229 1230**示例:** 1231 1232``` 1233audioManager.getAudioParameter('key_example', (err, value) => { 1234 if (err) { 1235 console.error('Failed to obtain the value of the audio parameter. ${err.message}'); 1236 return; 1237 } 1238 console.log('Callback invoked to indicate that the value of the audio parameter is obtained.' + value); 1239}); 1240``` 1241 1242### getAudioParameter 1243 1244getAudioParameter(key: string): Promise<string> 1245 1246获取指定音频参数值,使用Promise方式异步返回结果。 1247 1248本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。 1249 1250**系统能力:** SystemCapability.Multimedia.Audio.Core 1251 1252**参数:** 1253 1254| 参数名 | 类型 | 必填 | 说明 | 1255| ------ | ------ | ---- | ---------------------- | 1256| key | string | 是 | 待获取的音频参数的键。 | 1257 1258**返回值:** 1259 1260| 类型 | 说明 | 1261| --------------------- | ----------------------------------- | 1262| Promise<string> | Promise回调返回获取的音频参数的值。 | 1263 1264**示例:** 1265 1266``` 1267audioManager.getAudioParameter('key_example').then((value) => { 1268 console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value); 1269}); 1270``` 1271 1272### getDevices 1273 1274getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void 1275 1276获取音频设备列表,使用callback方式异步返回结果。 1277 1278**系统能力:** SystemCapability.Multimedia.Audio.Device 1279 1280**参数:** 1281 1282| 参数名 | 类型 | 必填 | 说明 | 1283| ---------- | ------------------------------------------------------------ | ---- | -------------------- | 1284| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1285| callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调,返回设备列表。 | 1286 1287**示例:** 1288``` 1289audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value) => { 1290 if (err) { 1291 console.error('Failed to obtain the device list. ${err.message}'); 1292 return; 1293 } 1294 console.log('Callback invoked to indicate that the device list is obtained.'); 1295}); 1296``` 1297 1298### getDevices 1299 1300getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> 1301 1302获取音频设备列表,使用Promise方式异步返回结果。 1303 1304**系统能力:** SystemCapability.Multimedia.Audio.Device 1305 1306**参数:** 1307 1308| 参数名 | 类型 | 必填 | 说明 | 1309| ---------- | ------------------------- | ---- | ---------------- | 1310| deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | 1311 1312**返回值:** 1313 1314| 类型 | 说明 | 1315| ------------------------------------------------------------ | ------------------------- | 1316| Promise<[AudioDeviceDescriptors](#audiodevicedescriptors)> | Promise回调返回设备列表。 | 1317 1318**示例:** 1319 1320``` 1321audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 1322 console.log('Promise returned to indicate that the device list is obtained.'); 1323}); 1324``` 1325 1326### setDeviceActive 1327 1328setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void 1329 1330设置设备激活状态,使用callback方式异步返回结果。 1331 1332**系统能力:** SystemCapability.Multimedia.Audio.Device 1333 1334**参数:** 1335 1336| 参数名 | 类型 | 必填 | 说明 | 1337| ---------- | ------------------------------------- | ---- | ------------------------ | 1338| deviceType | [ActiveDeviceType](#activedevicetype) | 是 | 活跃音频设备类型。 | 1339| active | boolean | 是 | 设备激活状态。 | 1340| callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | 1341 1342**示例:** 1343 1344``` 1345audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err) => { 1346 if (err) { 1347 console.error('Failed to set the active status of the device. ${err.message}'); 1348 return; 1349 } 1350 console.log('Callback invoked to indicate that the device is set to the active status.'); 1351}); 1352``` 1353 1354### setDeviceActive 1355 1356setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void> 1357 1358设置设备激活状态,使用Promise方式异步返回结果。 1359 1360**系统能力:** SystemCapability.Multimedia.Audio.Device 1361 1362**参数:** 1363 1364| 参数名 | 类型 | 必填 | 说明 | 1365| ---------- | ------------------------------------- | ---- | ------------------ | 1366| deviceType | [ActiveDeviceType](#activedevicetype) | 是 | 活跃音频设备类型。 | 1367| active | boolean | 是 | 设备激活状态。 | 1368 1369**返回值:** 1370 1371| 类型 | 说明 | 1372| ------------------- | ------------------------------- | 1373| Promise<void> | Promise回调返回设置成功或失败。 | 1374 1375**示例:** 1376 1377 1378``` 1379audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(() => { 1380 console.log('Promise returned to indicate that the device is set to the active status.'); 1381}); 1382``` 1383 1384### isDeviceActive 1385 1386isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void 1387 1388获取指定设备的激活状态,使用callback方式异步返回结果。 1389 1390**系统能力:** SystemCapability.Multimedia.Audio.Device 1391 1392**参数:** 1393 1394| 参数名 | 类型 | 必填 | 说明 | 1395| ---------- | ------------------------------------- | ---- | ------------------------ | 1396| deviceType | [ActiveDeviceType](#activedevicetype) | 是 | 活跃音频设备类型。 | 1397| callback | AsyncCallback<boolean> | 是 | 回调返回设备的激活状态。 | 1398 1399**示例:** 1400 1401``` 1402audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err, value) => { 1403 if (err) { 1404 console.error('Failed to obtain the active status of the device. ${err.message}'); 1405 return; 1406 } 1407 console.log('Callback invoked to indicate that the active status of the device is obtained.'); 1408}); 1409``` 1410 1411 1412### isDeviceActive 1413 1414isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean> 1415 1416获取指定设备的激活状态,使用Promise方式异步返回结果。 1417 1418**系统能力:** SystemCapability.Multimedia.Audio.Device 1419 1420**参数:** 1421 1422| 参数名 | 类型 | 必填 | 说明 | 1423| ---------- | ------------------------------------- | ---- | ------------------ | 1424| deviceType | [ActiveDeviceType](#activedevicetype) | 是 | 活跃音频设备类型。 | 1425 1426**返回值:** 1427 1428| Type | Description | 1429| ---------------------- | ------------------------------- | 1430| Promise<boolean> | Promise回调返回设备的激活状态。 | 1431 1432**示例:** 1433 1434``` 1435audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value) => { 1436 console.log('Promise returned to indicate that the active status of the device is obtained.' + value); 1437}); 1438``` 1439 1440### setMicrophoneMute 1441 1442setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void 1443 1444设置麦克风静音状态,使用callback方式异步返回结果。 1445 1446**需要权限:** ohos.permission.MICROPHONE 1447 1448**系统能力:** SystemCapability.Multimedia.Audio.Device 1449 1450**参数:** 1451 1452| 参数名 | 类型 | 必填 | 说明 | 1453| -------- | ------------------------- | ---- | --------------------------------------------- | 1454| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 1455| callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | 1456 1457**示例:** 1458 1459``` 1460audioManager.setMicrophoneMute(true, (err) => { 1461 if (err) { 1462 console.error('Failed to mute the microphone. ${err.message}'); 1463 return; 1464 } 1465 console.log('Callback invoked to indicate that the microphone is muted.'); 1466}); 1467``` 1468 1469### setMicrophoneMute 1470 1471setMicrophoneMute(mute: boolean): Promise<void> 1472 1473设置麦克风静音状态,使用Promise方式异步返回结果。 1474 1475**需要权限:** ohos.permission.MICROPHONE 1476 1477**系统能力:** SystemCapability.Multimedia.Audio.Device 1478 1479**参数:** 1480 1481| 参数名 | 类型 | 必填 | 说明 | 1482| ------ | ------- | ---- | --------------------------------------------- | 1483| mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | 1484 1485**返回值:** 1486 1487| 类型 | 说明 | 1488| ------------------- | ------------------------------- | 1489| Promise<void> | Promise回调返回设置成功或失败。 | 1490 1491**示例:** 1492 1493``` 1494audioManager.setMicrophoneMute(true).then(() => { 1495 console.log('Promise returned to indicate that the microphone is muted.'); 1496}); 1497``` 1498 1499### isMicrophoneMute 1500 1501isMicrophoneMute(callback: AsyncCallback<boolean>): void 1502 1503获取麦克风静音状态,使用callback方式异步返回结果。 1504 1505**需要权限:** ohos.permission.MICROPHONE 1506 1507**系统能力:** SystemCapability.Multimedia.Audio.Device 1508 1509**参数:** 1510 1511| 参数名 | 类型 | 必填 | 说明 | 1512| -------- | ---------------------------- | ---- | ------------------------------------------------------- | 1513| callback | AsyncCallback<boolean> | 是 | 回调返回系统麦克风静音状态,true为静音,false为非静音。 | 1514 1515**示例:** 1516 1517``` 1518audioManager.isMicrophoneMute((err, value) => { 1519 if (err) { 1520 console.error('Failed to obtain the mute status of the microphone. ${err.message}'); 1521 return; 1522 } 1523 console.log('Callback invoked to indicate that the mute status of the microphone is obtained.' + value); 1524}); 1525``` 1526 1527### isMicrophoneMute 1528 1529isMicrophoneMute(): Promise<boolean> 1530 1531获取麦克风静音状态,使用Promise方式异步返回结果。 1532 1533**需要权限:** ohos.permission.MICROPHONE 1534 1535**系统能力:** SystemCapability.Multimedia.Audio.Device 1536 1537**返回值:** 1538 1539| 类型 | 说明 | 1540| ---------------------- | ------------------------------------------------------------ | 1541| Promise<boolean> | Promise回调返回系统麦克风静音状态,true为静音,false为非静音。 | 1542 1543**示例:** 1544 1545 1546``` 1547audioManager.isMicrophoneMute().then((value) => { 1548 console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value); 1549}); 1550``` 1551 1552### on('volumeChange')<sup>8+</sup> 1553 1554on(type: 'volumeChange', callback: Callback\<VolumeEvent>): void 1555 1556监听系统音量变化事件。 1557 1558此接口为系统接口,三方应用不支持调用。 1559 1560**系统能力:** SystemCapability.Multimedia.Audio.Volume 1561 1562**参数:** 1563 1564| 参数名 | 类型 | 必填 | 说明 | 1565| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 1566| type | string | 是 | 事件回调类型,支持的事件为:'volumeChange'(系统音量变化事件,检测到系统音量改变时,触发该事件)。 | 1567| callback | Callback<[VolumeEvent](#volumeevent8)> | 是 | 回调方法。 | 1568 1569**示例:** 1570 1571``` 1572audioManager.on('volumeChange', (volumeEvent) => { 1573 console.log('VolumeType of stream: ' + volumeEvent.volumeType); 1574 console.log('Volume level: ' + volumeEvent.volume); 1575 console.log('Whether to updateUI: ' + volumeEvent.updateUi); 1576}); 1577``` 1578 1579### on('ringerModeChange')<sup>8+</sup> 1580 1581on(type: 'ringerModeChange', callback: Callback\<AudioRingMode>): void 1582 1583监听铃声模式变化事件。 1584 1585此接口为系统接口,三方应用不支持调用。 1586 1587**系统能力:** SystemCapability.Multimedia.Audio.Communication 1588 1589**参数:** 1590 1591| 参数名 | 类型 | 必填 | 说明 | 1592| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 1593| type | string | 是 | 事件回调类型,支持的事件为:'ringerModeChange'(铃声模式变化事件,检测到铃声模式改变时,触发该事件)。 | 1594| callback | Callback<[AudioRingMode](#audioringmode)> | 是 | 回调方法。 | 1595 1596**示例:** 1597 1598``` 1599audioManager.on('ringerModeChange', (ringerMode) => { 1600 console.log('Updated ringermode: ' + ringerMode); 1601}); 1602``` 1603 1604### on('deviceChange') 1605 1606on(type: 'deviceChange', callback: Callback<DeviceChangeAction\>): void 1607 1608设备更改。音频设备连接状态变化。 1609 1610**系统能力:** SystemCapability.Multimedia.Audio.Device 1611 1612**参数:** 1613 1614| 参数名 | 类型 | 必填 | 说明 | 1615| :------- | :--------------------------------------------------- | :--- | :----------------------------------------- | 1616| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 1617| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)\> | 是 | 获取设备更新详情。 | 1618 1619**示例:** 1620 1621``` 1622audioManager.on('deviceChange', (deviceChanged) => { 1623 console.info("device change type : " + deviceChanged.type); 1624 console.info("device descriptor size : " + deviceChanged.deviceDescriptors.length); 1625 console.info("device change descriptor : " + deviceChanged.deviceDescriptors[0].deviceRole); 1626 console.info("device change descriptor : " + deviceChanged.deviceDescriptors[0].deviceType); 1627}); 1628``` 1629 1630### off('deviceChange') 1631 1632off(type: 'deviceChange', callback?: Callback<DeviceChangeAction\>): void 1633 1634取消订阅音频设备连接变化事件。 1635 1636**系统能力:** SystemCapability.Multimedia.Audio.Device 1637 1638**参数:** 1639 1640| 参数名 | 类型 | 必填 | 说明 | 1641| -------- | --------------------------------------------------- | ---- | ------------------------------------------ | 1642| type | string | 是 | 订阅的事件的类型。支持事件:'deviceChange' | 1643| callback | Callback<[DeviceChangeAction](#DeviceChangeAction)> | 否 | 获取设备更新详情。 | 1644 1645**示例:** 1646 1647``` 1648audioManager.off('deviceChange', (deviceChanged) => { 1649 console.log("Should be no callback."); 1650}); 1651``` 1652 1653### on('interrupt') 1654 1655on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback\<InterruptAction>): void 1656 1657请求焦点并开始监听音频打断事件(当应用程序的音频被另一个播放事件中断,回调通知此应用程序) 1658 1659**系统能力:** SystemCapability.Multimedia.Audio.Renderer 1660 1661**参数:** 1662 1663| 参数名 | 类型 | 必填 | 说明 | 1664| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 1665| type | string | 是 | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 | 1666| interrupt | AudioInterrupt | 是 | 音频打断事件类型的参数。 | 1667| callback | Callback<[InterruptAction](#interruptaction)> | 是 | 音频打断事件回调方法。 | 1668 1669**示例:** 1670 1671``` 1672var interAudioInterrupt = { 1673 streamUsage:2, 1674 contentType:0, 1675 pauseWhenDucked:true 1676}; 1677audioManager.on('interrupt', interAudioInterrupt, (InterruptAction) => { 1678 if (InterruptAction.actionType === 0) { 1679 console.log("An event to gain the audio focus starts."); 1680 console.log("Focus gain event:" + JSON.stringify(InterruptAction)); 1681 } 1682 if (InterruptAction.actionType === 1) { 1683 console.log("An audio interruption event starts."); 1684 console.log("Audio interruption event:" + JSON.stringify(InterruptAction)); 1685 } 1686}); 1687``` 1688 1689### off('interrupt') 1690 1691off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback\<InterruptAction>): void 1692 1693取消监听音频打断事件(删除监听事件,取消打断) 1694 1695**系统能力:** SystemCapability.Multimedia.Audio.Renderer 1696 1697**参数:** 1698 1699| 参数名 | 类型 | 必填 | 说明 | 1700| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 1701| type | string | 是 | 音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。 | 1702| interrupt | AudioInterrupt | 是 | 音频打断事件类型的参数。 | 1703| callback | Callback<[InterruptAction](#interruptaction)> | 否 | 音频打断事件回调方法。 | 1704 1705**示例:** 1706 1707``` 1708var interAudioInterrupt = { 1709 streamUsage:2, 1710 contentType:0, 1711 pauseWhenDucked:true 1712}; 1713audioManager.off('interrupt', interAudioInterrupt, (InterruptAction) => { 1714 if (InterruptAction.actionType === 0) { 1715 console.log("An event to release the audio focus starts."); 1716 console.log("Focus release event:" + JSON.stringify(InterruptAction)); 1717 } 1718}); 1719``` 1720 1721### setAudioScene<sup>8+</sup> 1722 1723setAudioScene\(scene: AudioScene, callback: AsyncCallback<void\>\): void 1724 1725设置音频场景模式,使用callback方式异步返回结果。 1726 1727此接口为系统接口,三方应用不支持调用。 1728 1729**系统能力:** SystemCapability.Multimedia.Audio.Communication 1730 1731**参数:** 1732 1733| 参数名 | 类型 | 必填 | 说明 | 1734| :------- | :----------------------------------- | :--- | :------------------- | 1735| scene | <a href="#audioscene">AudioScene</a> | 是 | 音频场景模式。 | 1736| callback | AsyncCallback<void\> | 是 | 用于返回结果的回调。 | 1737 1738**示例:** 1739 1740``` 1741audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL, (err) => { 1742 if (err) { 1743 console.error('Failed to set the audio scene mode. ${err.message}'); 1744 return; 1745 } 1746 console.log('Callback invoked to indicate a successful setting of the audio scene mode.'); 1747}); 1748``` 1749 1750### setAudioScene<sup>8+</sup> 1751 1752setAudioScene\(scene: AudioScene\): Promise<void\> 1753 1754设置音频场景模式,使用Promise方式返回异步结果。 1755 1756此接口为系统接口,三方应用不支持调用。 1757 1758**系统能力:** SystemCapability.Multimedia.Audio.Communication 1759 1760**参数:** 1761 1762| 参数名 | 类型 | 必填 | 说明 | 1763| :----- | :----------------------------------- | :--- | :------------- | 1764| scene | <a href="#audioscene">AudioScene</a> | 是 | 音频场景模式。 | 1765 1766**返回值:** 1767 1768| 类型 | 说明 | 1769| :------------- | :------------------- | 1770| Promise<void\> | 用于返回结果的回调。 | 1771 1772**示例:** 1773 1774``` 1775audioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_PHONE_CALL).then(() => { 1776 console.log('Promise returned to indicate a successful setting of the audio scene mode.'); 1777}).catch ((err) => { 1778 console.log('Failed to set the audio scene mode'); 1779}); 1780``` 1781 1782### getAudioScene<sup>8+</sup> 1783 1784getAudioScene\(callback: AsyncCallback<AudioScene\>\): void 1785 1786获取音频场景模式,使用callback方式返回异步结果。 1787 1788**系统能力:** SystemCapability.Multimedia.Audio.Communication 1789 1790**参数:** 1791 1792| 参数名 | 类型 | 必填 | 说明 | 1793| :------- | :-------------------------------------------------- | :--- | :--------------------------- | 1794| callback | AsyncCallback<<a href="#audioscene">AudioScene</a>> | 是 | 用于返回音频场景模式的回调。 | 1795 1796**示例:** 1797 1798``` 1799audioManager.getAudioScene((err, value) => { 1800 if (err) { 1801 console.error('Failed to obtain the audio scene mode. ${err.message}'); 1802 return; 1803 } 1804 console.log('Callback invoked to indicate that the audio scene mode is obtained.' + value); 1805}); 1806``` 1807 1808 1809### getAudioScene<sup>8+</sup> 1810 1811getAudioScene\(\): Promise<AudioScene\> 1812 1813获取音频场景模式,使用Promise方式返回异步结果。 1814 1815**系统能力:** SystemCapability.Multimedia.Audio.Communication 1816 1817**返回值:** 1818 1819| 类型 | 说明 | 1820| :-------------------------------------------- | :--------------------------- | 1821| Promise<<a href="#audioscene">AudioScene</a>> | 用于返回音频场景模式的回调。 | 1822 1823**示例:** 1824 1825``` 1826audioManager.getAudioScene().then((value) => { 1827 console.log('Promise returned to indicate that the audio scene mode is obtained.' + value); 1828}).catch ((err) => { 1829 console.log('Failed to obtain the audio scene mode'); 1830}); 1831``` 1832 1833## AudioDeviceDescriptor 1834 1835描述音频设备。 1836 1837**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Device 1838 1839| 名称 | 类型 | 可读 | 可写 | 说明 | 1840| ---------- | ------------------------- | ---- | ---- | ---------- | 1841| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 | 1842| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 | 1843 1844## AudioDeviceDescriptors 1845 1846设备属性数组类型,为[AudioDeviceDescriptor](#audiodevicedescriptor)的数组,只读。 1847 1848**示例:** 1849 1850``` 1851import audio from '@ohos.multimedia.audio'; 1852 1853function displayDeviceProp(value) { 1854 deviceRoleValue = value.deviceRole; 1855 deviceTypeValue = value.deviceType; 1856 1857} 1858 1859var deviceRoleValue = null; 1860var deviceTypeValue = null; 1861const promise = audio.getAudioManager().getDevices(1); 1862promise.then(function (value) { 1863 console.info('AudioFrameworkTest: Promise: getDevices OUTPUT_DEVICES_FLAG'); 1864 value.forEach(displayDeviceProp); 1865 if (deviceTypeValue != null && deviceRoleValue != null){ 1866 console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : PASS'); 1867 } 1868 else{ 1869 console.info('AudioFrameworkTest: Promise: getDevices : OUTPUT_DEVICES_FLAG : FAIL'); 1870 } 1871}); 1872``` 1873 1874## AudioRenderer<sup>8+</sup> 1875 1876提供音频渲染的相关接口。在调用AudioRenderer的接口前,需要先通过[createAudioRenderer](#audiocreateaudiorenderer8)创建实例。 1877 1878### 属性 1879 1880**系统能力:** SystemCapability.Multimedia.Audio.Renderer 1881 1882| 名称 | 类型 | 可读 | 可写 | 说明 | 1883| ----- | -------------------------- | ---- | ---- | ------------------ | 1884| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频渲染器的状态。 | 1885 1886**示例:** 1887 1888``` 1889var state = audioRenderer.state; 1890``` 1891 1892### getRendererInfo<sup>8+</sup> 1893 1894getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void 1895 1896获取当前被创建的音频渲染器的信息,使用callback方式异步返回结果。 1897 1898**系统能力**: SystemCapability.Multimedia.Audio.Renderer 1899 1900**参数:** 1901 1902| 参数名 | 类型 | 必填 | 说明 | 1903| :------- | :------------------------------------------------------- | :--- | :--------------------- | 1904| callback | AsyncCallback<[AudioRendererInfo](#audiorendererinfo8)\> | 是 | 返回音频渲染器的信息。 | 1905 1906**示例:** 1907 1908``` 1909audioRenderer.getRendererInfo((err, rendererInfo) => { 1910 console.log('Renderer GetRendererInfo:'); 1911 console.log('Renderer content:' + rendererInfo.content); 1912 console.log('Renderer usage:' + rendererInfo.usage); 1913 console.log('Renderer flags:' + rendererInfo.rendererFlags); 1914}); 1915``` 1916 1917### getRendererInfo<sup>8+</sup> 1918 1919getRendererInfo(): Promise<AudioRendererInfo\> 1920 1921获取当前被创建的音频渲染器的信息,使用Promise方式异步返回结果。 1922 1923**系统能力**: SystemCapability.Multimedia.Audio.Renderer 1924 1925**返回值:** 1926 1927| 类型 | 说明 | 1928| -------------------------------------------------- | ------------------------------- | 1929| Promise<[AudioRendererInfo](#audiorendererinfo8)\> | Promise用于返回音频渲染器信息。 | 1930 1931**示例:** 1932 1933``` 1934var resultFlag = true; 1935audioRenderer.getRendererInfo().then((rendererInfo) => { 1936 console.log('Renderer GetRendererInfo:'); 1937 console.log('Renderer content:' + rendererInfo.content); 1938 console.log('Renderer usage:' + rendererInfo.usage); 1939 console.log('Renderer flags:' + rendererInfo.rendererFlags); 1940}).catch((err) => { 1941 console.log('AudioFrameworkRenderLog: RendererInfo :ERROR: '+err.message); 1942 resultFlag = false; 1943}); 1944``` 1945 1946### getStreamInfo<sup>8+</sup> 1947 1948getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 1949 1950获取音频流信息,使用callback方式异步返回结果。 1951 1952**系统能力**: SystemCapability.Multimedia.Audio.Renderer 1953 1954**参数:** 1955 1956| 参数名 | 类型 | 必填 | 说明 | 1957| :------- | :--------------------------------------------------- | :--- | :------------------- | 1958| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 回调返回音频流信息。 | 1959 1960**示例:** 1961 1962``` 1963audioRenderer.getStreamInfo((err, streamInfo) => { 1964 console.log('Renderer GetStreamInfo:'); 1965 console.log('Renderer sampling rate:' + streamInfo.samplingRate); 1966 console.log('Renderer channel:' + streamInfo.channels); 1967 console.log('Renderer format:' + streamInfo.sampleFormat); 1968 console.log('Renderer encoding type:' + streamInfo.encodingType); 1969}); 1970``` 1971 1972### getStreamInfo<sup>8+</sup> 1973 1974getStreamInfo(): Promise<AudioStreamInfo\> 1975 1976获取音频流信息,使用Promise方式异步返回结果。 1977 1978**系统能力**: SystemCapability.Multimedia.Audio.Renderer 1979 1980**返回值:** 1981 1982| 类型 | 说明 | 1983| :--------------------------------------------- | :--------------------- | 1984| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | Promise返回音频流信息. | 1985 1986**示例:** 1987 1988``` 1989audioRenderer.getStreamInfo().then((streamInfo) => { 1990 console.log('Renderer GetStreamInfo:'); 1991 console.log('Renderer sampling rate:' + streamInfo.samplingRate); 1992 console.log('Renderer channel:' + streamInfo.channels); 1993 console.log('Renderer format:' + streamInfo.sampleFormat); 1994 console.log('Renderer encoding type:' + streamInfo.encodingType); 1995}).catch((err) => { 1996 console.log('ERROR: '+err.message); 1997}); 1998``` 1999 2000### start<sup>8+</sup> 2001 2002start(callback: AsyncCallback<void\>): void 2003 2004启动音频渲染器。使用callback方式异步返回结果。 2005 2006**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2007 2008**参数:** 2009 2010| 参数名 | 类型 | 必填 | 说明 | 2011| -------- | -------------------- | ---- | ---------- | 2012| callback | AsyncCallback\<void> | 是 | 回调函数。 | 2013 2014**示例:** 2015 2016``` 2017audioRenderer.start((err) => { 2018 if (err) { 2019 console.error('Renderer start failed.'); 2020 } else { 2021 console.info('Renderer start success.'); 2022 } 2023}); 2024``` 2025 2026### start<sup>8+</sup> 2027 2028start(): Promise<void\> 2029 2030启动音频渲染器。使用Promise方式异步返回结果。 2031 2032**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2033 2034**返回值:** 2035 2036| 类型 | 说明 | 2037| -------------- | ------------------------- | 2038| Promise\<void> | Promise方式异步返回结果。 | 2039 2040**示例:** 2041 2042``` 2043audioRenderer.start().then(() => { 2044 console.log('Renderer started'); 2045}).catch((err) => { 2046 console.log('ERROR: '+err.message); 2047}); 2048``` 2049 2050### pause<sup>8+</sup> 2051 2052pause(callback: AsyncCallback\<void>): void 2053 2054暂停渲染。使用callback方式异步返回结果。 2055 2056**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2057 2058**参数:** 2059 2060| 参数名 | 类型 | 必填 | 说明 | 2061| -------- | -------------------- | ---- | ---------------- | 2062| callback | AsyncCallback\<void> | 是 | 返回回调的结果。 | 2063 2064**示例:** 2065 2066``` 2067audioRenderer.pause((err) => { 2068 if (err) { 2069 console.error('Renderer pause failed'); 2070 } else { 2071 console.log('Renderer paused.'); 2072 } 2073}); 2074``` 2075 2076### pause<sup>8+</sup> 2077 2078pause(): Promise\<void> 2079 2080暂停渲染。使用Promise方式异步返回结果。 2081 2082**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2083 2084**返回值:** 2085 2086| 类型 | 说明 | 2087| -------------- | ------------------------- | 2088| Promise\<void> | Promise方式异步返回结果。 | 2089 2090**示例:** 2091 2092``` 2093audioRenderer.pause().then(() => { 2094 console.log('Renderer paused'); 2095}).catch((err) => { 2096 console.log('ERROR: '+err.message); 2097}); 2098``` 2099 2100### drain<sup>8+</sup> 2101 2102drain(callback: AsyncCallback\<void>): void 2103 2104检查缓冲区是否已被耗尽。使用callback方式异步返回结果。 2105 2106**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2107 2108**参数:** 2109 2110| 参数名 | 类型 | 必填 | 说明 | 2111| -------- | -------------------- | ---- | ---------------- | 2112| callback | AsyncCallback\<void> | 是 | 返回回调的结果。 | 2113 2114**示例:** 2115 2116``` 2117audioRenderer.drain((err) => { 2118 if (err) { 2119 console.error('Renderer drain failed'); 2120 } else { 2121 console.log('Renderer drained.'); 2122 } 2123}); 2124``` 2125 2126### drain<sup>8+</sup> 2127 2128drain(): Promise\<void> 2129 2130检查缓冲区是否已被耗尽。使用Promise方式异步返回结果。 2131 2132**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2133 2134**返回值:** 2135 2136| 类型 | 说明 | 2137| -------------- | ------------------------- | 2138| Promise\<void> | Promise方式异步返回结果。 | 2139 2140**示例:** 2141 2142``` 2143audioRenderer.drain().then(() => { 2144 console.log('Renderer drained successfully'); 2145}).catch((err) => { 2146 console.log('ERROR: '+err.message); 2147}); 2148``` 2149 2150### stop<sup>8+</sup> 2151 2152stop(callback: AsyncCallback\<void>): void 2153 2154停止渲染。使用callback方式异步返回结果。 2155 2156**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2157 2158**参数:** 2159 2160| 参数名 | 类型 | 必填 | 说明 | 2161| -------- | -------------------- | ---- | ---------------- | 2162| callback | AsyncCallback\<void> | 是 | 返回回调的结果。 | 2163 2164**示例:** 2165 2166``` 2167audioRenderer.stop((err) => { 2168 if (err) { 2169 console.error('Renderer stop failed'); 2170 } else { 2171 console.log('Renderer stopped.'); 2172 } 2173}); 2174``` 2175 2176### stop<sup>8+</sup> 2177 2178stop(): Promise\<void> 2179 2180停止渲染。使用Promise方式异步返回结果。 2181 2182**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2183 2184**返回值:** 2185 2186| 类型 | 说明 | 2187| -------------- | ------------------------- | 2188| Promise\<void> | Promise方式异步返回结果。 | 2189 2190**示例:** 2191 2192``` 2193audioRenderer.stop().then(() => { 2194 console.log('Renderer stopped successfully'); 2195}).catch((err) => { 2196 console.log('ERROR: '+err.message); 2197}); 2198``` 2199 2200### release<sup>8+</sup> 2201 2202release(callback: AsyncCallback\<void>): void 2203 2204释放音频渲染器。使用callback方式异步返回结果。 2205 2206**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2207 2208**参数:** 2209 2210| 参数名 | 类型 | 必填 | 说明 | 2211| -------- | -------------------- | ---- | ---------------- | 2212| callback | AsyncCallback\<void> | 是 | 返回回调的结果。 | 2213 2214**示例:** 2215 2216``` 2217audioRenderer.release((err) => { 2218 if (err) { 2219 console.error('Renderer release failed'); 2220 } else { 2221 console.log('Renderer released.'); 2222 } 2223}); 2224``` 2225 2226### release<sup>8+</sup> 2227 2228release(): Promise\<void> 2229 2230释放渲染器。使用Promise方式异步返回结果。 2231 2232**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2233 2234**返回值:** 2235 2236| 类型 | 说明 | 2237| -------------- | ------------------------- | 2238| Promise\<void> | Promise方式异步返回结果。 | 2239 2240**示例:** 2241 2242``` 2243audioRenderer.release().then(() => { 2244 console.log('Renderer released successfully'); 2245}).catch((err) => { 2246 console.log('ERROR: '+err.message); 2247}); 2248``` 2249 2250### write<sup>8+</sup> 2251 2252write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void 2253 2254写入缓冲区。使用callback方式异步返回结果。 2255 2256**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2257 2258**参数:** 2259 2260| 参数名 | 类型 | 必填 | 说明 | 2261| -------- | ---------------------- | ---- | --------------------------------------------------- | 2262| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | 2263| callback | AsyncCallback\<number> | 是 | 回调如果成功,返回写入的字节数,否则返回errorcode。 | 2264 2265**示例:** 2266 2267``` 2268import audio from '@ohos.multimedia.audio'; 2269import fileio from '@ohos.fileio'; 2270import featureAbility from '@ohos.ability.featureAbility' 2271 2272var audioStreamInfo = { 2273 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, 2274 channels: audio.AudioChannel.CHANNEL_2, 2275 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S32LE, 2276 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 2277} 2278 2279var audioRendererInfo = { 2280 content: audio.ContentType.CONTENT_TYPE_SPEECH, 2281 usage: audio.streamUsage.STREAM_USAGE_VOICE_COMMUNICATION 2282 rendererFlags: 1 2283} 2284 2285var audioRendererOptions = { 2286 streamInfo: audioStreamInfo, 2287 rendererInfo: audioRendererInfo 2288} 2289var audioRenderer; 2290audio.createAudioRenderer(audioRendererOptions).then((data)=> { 2291 audioRenderer = data; 2292 console.info('AudioFrameworkRenderLog: AudioRenderer Created: SUCCESS'); 2293 }).catch((err) => { 2294 console.info('AudioFrameworkRenderLog: AudioRenderer Created: ERROR: '+err.message); 2295 }); 2296var bufferSize; 2297audioRenderer.getBufferSize().then((data)=> { 2298 console.info('AudioFrameworkRenderLog: getBufferSize: SUCCESS '+data); 2299 bufferSize = data; 2300 }).catch((err) => { 2301 console.info.('AudioFrameworkRenderLog: getBufferSize: ERROR: '+err.message); 2302 }); 2303console.info('Buffer size:'+bufferSize); 2304var context = featureAbility.getContext(); 2305var path = await context.getCacheDir(); 2306var filePath = path+"/StarWars10s-2C-48000-4SW.wav" 2307let ss = fileio.createStreamSync(filePath, 'r'); 2308let buf = new ArrayBuffer(bufferSize); 2309ss.readSync(buf); 2310audioRenderer.write(buf, (err, writtenbytes) => { 2311 if (writtenbytes < 0) { 2312 console.error('write failed.'); 2313 } else { 2314 console.log('Actual written bytes: ' + writtenbytes); 2315 } 2316}); 2317``` 2318 2319### write<sup>8+</sup> 2320 2321write(buffer: ArrayBuffer): Promise\<number> 2322 2323写入缓冲区。使用Promise方式异步返回结果。 2324 2325**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2326 2327**返回值:** 2328 2329| 类型 | 说明 | 2330| ---------------- | ------------------------------------------------------------ | 2331| Promise\<number> | Promise返回结果,如果成功,返回写入的字节数,否则返回errorcode。 | 2332 2333**示例:** 2334 2335``` 2336import audio from '@ohos.multimedia.audio'; 2337import fileio from '@ohos.fileio'; 2338import featureAbility from '@ohos.ability.featureAbility' 2339 2340var audioStreamInfo = { 2341 samplingRate:audio.AudioSamplingRate.SAMPLE_RATE_48000, 2342 channels:audio.AudioChannel.CHANNEL_2, 2343 sampleFormat.audio.AudioSampleFormat.SAMPLE_FORMAT_S32LE, 2344 encodingType.audio.AudioEncodingType.ENCODING_TYPE_RAW 2345} 2346 2347var audioRendererInfo = { 2348 content: audio.ContentType.CONTENT_TYPE_SPEECH, 2349 usage: audio.streamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 2350 rendererFlags: 1 2351} 2352 2353var audioRendererOptions = { 2354 streamInfo: audioStreamInfo, 2355 rendererInfo: audioRendererInfo 2356} 2357var audioRenderer; 2358audio.createAudioRenderer(audioRendererOptions).then((data) => { 2359 audioRenderer = data; 2360 console.info('AudioFrameworkRenderLog: AudioRenderer Created: SUCCESS'); 2361 }).catch((err) => { 2362 console.info('AudioFrameworkRenderLog: AudioRenderer Created: ERROR: '+err.message); 2363 }); 2364var bufferSize; 2365audioRenderer.getBufferSize().then((data) => { 2366 console.info('AudioFrameworkRenderLog: getBufferSize: SUCCESS '+data); 2367 bufferSize = data; 2368 }).catch((err) => { 2369 console.info('AudioFrameworkRenderLog: getBufferSize: ERROR: '+err.message); 2370 }); 2371console.info('BufferSize: ' + bufferSize); 2372var context = featureAbility.getContext(); 2373var path = await context.getCacheDir(); 2374var filePath = 'data/StarWars10s-2C-48000-4SW.wav'; 2375let ss = fileio.createStreamSync(filePath, 'r'); 2376let buf = new ArrayBuffer(bufferSize); 2377ss.readSync(buf); 2378audioRenderer.write(buf).then((writtenbytes) => { 2379 if (writtenbytes < 0) { 2380 console.error('write failed.'); 2381 } else { 2382 console.log('Actual written bytes: ' + writtenbytes); 2383 } 2384}).catch((err) => { 2385 console.log('ERROR: '+err.message); 2386}); 2387``` 2388 2389### getAudioTime<sup>8+</sup> 2390 2391getAudioTime(callback: AsyncCallback\<number>): void 2392 2393获取时间戳(从 1970 年 1 月 1 日开始)。使用callback方式异步返回结果。 2394 2395**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2396 2397**参数:** 2398 2399| 参数名 | 类型 | 必填 | 说明 | 2400| -------- | ---------------------- | ---- | ---------------- | 2401| callback | AsyncCallback\<number> | 是 | 回调返回时间戳。 | 2402 2403**示例:** 2404 2405``` 2406audioRenderer.getAudioTime((err, timestamp) => { 2407 console.log('Current timestamp: ' + timestamp); 2408}); 2409``` 2410 2411### getAudioTime<sup>8+</sup> 2412 2413getAudioTime(): Promise\<number> 2414 2415获取时间戳(从 1970 年 1 月 1 日开始)。使用Promise方式异步返回结果。 2416 2417**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2418 2419**返回值:** 2420 2421| 类型 | 描述 | 2422| ---------------- | ----------------------- | 2423| Promise\<number> | Promise回调返回时间戳。 | 2424 2425**示例:** 2426 2427``` 2428audioRenderer.getAudioTime().then((timestamp) => { 2429 console.log('Current timestamp: ' + timestamp); 2430}).catch((err) => { 2431 console.log('ERROR: '+err.message); 2432}); 2433``` 2434 2435### getBufferSize<sup>8+</sup> 2436 2437getBufferSize(callback: AsyncCallback\<number>): void 2438 2439获取音频渲染器的最小缓冲区大小。使用callback方式异步返回结果。 2440 2441**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2442 2443**参数:** 2444 2445| 参数名 | 类型 | 必填 | 说明 | 2446| -------- | ---------------------- | ---- | -------------------- | 2447| callback | AsyncCallback\<number> | 是 | 回调返回缓冲区大小。 | 2448 2449**示例:** 2450 2451``` 2452var bufferSize = audioRenderer.getBufferSize(async(err, bufferSize) => { 2453 if (err) { 2454 console.error('getBufferSize error'); 2455 } 2456}); 2457``` 2458 2459### getBufferSize<sup>8+</sup> 2460 2461getBufferSize(): Promise\<number> 2462 2463获取音频渲染器的最小缓冲区大小。使用Promise方式异步返回结果。 2464 2465**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2466 2467**返回值:** 2468 2469| 类型 | 说明 | 2470| ---------------- | --------------------------- | 2471| Promise\<number> | promise回调返回缓冲区大小。 | 2472 2473**示例:** 2474 2475``` 2476import audio from '@ohos.multimedia.audio'; 2477import fileio from '@ohos.fileio'; 2478 2479var audioStreamInfo = { 2480 samplingRate:audio.AudioSamplingRate.SAMPLE_RATE_48000, 2481 channels:audio.AudioChannel.CHANNEL_2, 2482 sampleFormat.audio.AudioSampleFormat.SAMPLE_FORMAT_S32LE, 2483 encodingType.audio.AudioEncodingType.ENCODING_TYPE_RAW 2484} 2485 2486var audioRendererInfo = { 2487 content: audio.ContentType.CONTENT_TYPE_SPEECH, 2488 usage: audio.streamUsage.STREAM_USAGE_VOICE_COMMUNICATION, 2489 rendererFlags: 1 2490} 2491 2492var audioRendererOptions = { 2493 streamInfo: audioStreamInfo, 2494 rendererInfo: audioRendererInfo 2495} 2496var audioRenderer; 2497audio.createAudioRenderer(audioRendererOptions).then((data) => { 2498 audioRenderer = data; 2499 console.info('AudioFrameworkRenderLog: AudioRenderer Created: SUCCESS'); 2500 }).catch((err) => { 2501 console.info('AudioFrameworkRenderLog: AudioRenderer Created: ERROR: '+err.message); 2502 }); 2503var bufferSize; 2504audioRenderer.getBufferSize().then((data) => { 2505 console.info('AudioFrameworkRenderLog: getBufferSize: SUCCESS '+data); 2506 bufferSize=data; 2507}).catch((err) => { 2508 console.info('AudioFrameworkRenderLog: getBufferSize: ERROR: '+err.message); 2509}); 2510``` 2511 2512### setRenderRate<sup>8+</sup> 2513 2514setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void 2515 2516设置音频渲染速率。使用callback方式异步返回结果。 2517 2518**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2519 2520**参数:** 2521 2522| 参数名 | 类型 | 必填 | 说明 | 2523| -------- | ---------------------------------------- | ---- | ------------------------ | 2524| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 2525| callback | AsyncCallback\<void> | 是 | 用于返回执行结果的回调。 | 2526 2527**示例:** 2528 2529``` 2530audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err) => { 2531 if (err) { 2532 console.error('Failed to set params'); 2533 } else { 2534 console.log('Callback invoked to indicate a successful render rate setting.'); 2535 } 2536}); 2537``` 2538 2539### setRenderRate<sup>8+</sup> 2540 2541setRenderRate(rate: AudioRendererRate): Promise\<void> 2542 2543设置音频渲染速率。使用Promise方式异步返回结果。 2544 2545**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2546 2547**参数:** 2548 2549| 参数名 | 类型 | 必填 | 说明 | 2550| ------ | ---------------------------------------- | ---- | ------------ | 2551| rate | [AudioRendererRate](#audiorendererrate8) | 是 | 渲染的速率。 | 2552 2553**返回值:** 2554 2555| 类型 | 说明 | 2556| -------------- | ------------------------- | 2557| Promise\<void> | Promise用于返回执行结果。 | 2558 2559**示例:** 2560 2561``` 2562audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => { 2563 console.log('setRenderRate SUCCESS'); 2564}).catch((err) => { 2565 console.log('ERROR: '+err.message); 2566}); 2567``` 2568 2569### getRenderRate<sup>8+</sup> 2570 2571getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void 2572 2573获取当前渲染速率。使用callback方式异步返回结果。 2574 2575**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2576 2577**参数:** 2578 2579| 参数名 | 类型 | 必填 | 说明 | 2580| -------- | ------------------------------------------------------- | ---- | ------------------ | 2581| callback | AsyncCallback<[AudioRendererRate](#audiorendererrate8)> | 是 | 回调返回渲染速率。 | 2582 2583**示例:** 2584 2585``` 2586audioRenderer.getRenderRate((err, renderrate) => { 2587 console.log('getRenderRate: ' + renderrate); 2588}); 2589``` 2590 2591### getRenderRate<sup>8+</sup> 2592 2593getRenderRate(): Promise\<AudioRendererRate> 2594 2595获取当前渲染速率。使用Promise方式异步返回结果。 2596 2597**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2598 2599**返回值:** 2600 2601| 类型 | 说明 | 2602| ------------------------------------------------- | ------------------------- | 2603| Promise<[AudioRendererRate](#audiorendererrate8)> | Promise回调返回渲染速率。 | 2604 2605**示例:** 2606 2607``` 2608audioRenderer.getRenderRate().then((renderRate) => { 2609 console.log('getRenderRate: ' + renderRate); 2610}).catch((err) => { 2611 console.log('ERROR: '+err.message); 2612}); 2613``` 2614 2615### on('interrupt')<sup>9+</sup> 2616 2617on(type: 'interrupt', callback: Callback\<InterruptEvent>): void 2618 2619监听音频中断事件。使用callback获取中断事件。 2620 2621**系统能力**: SystemCapability.Multimedia.Audio.Renderer 2622 2623**参数:** 2624 2625| 参数名 | 类型 | 必填 | 说明 | 2626| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2627| type | string | 是 | 事件回调类型,支持的事件为:'interrupt'(中断事件被触发,音频播放被中断。) | 2628| callback | Callback<[InterruptEvent](#interruptevent9)> | 是 | 被监听的中断事件的回调。 | 2629 2630**示例:** 2631 2632``` 2633var isPlay; 2634var started; 2635audioRenderer.on('interrupt', async(interruptEvent) => { 2636 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 2637 switch (interruptEvent.hintType) { 2638 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 2639 console.log('Force paused. Stop writing'); 2640 isPlay = false; 2641 break; 2642 case audio.InterruptHint.INTERRUPT_HINT_STOP: 2643 console.log('Force stopped. Stop writing'); 2644 isPlay = false; 2645 break; 2646 } 2647 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 2648 switch (interruptEvent.hintType) { 2649 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 2650 console.log('Resume force paused renderer or ignore'); 2651 await audioRenderer.start().then(async function () { 2652 console.info('AudioInterruptMusic: renderInstant started :SUCCESS '); 2653 started = true; 2654 }).catch((err) => { 2655 console.info('AudioInterruptMusic: renderInstant start :ERROR : '+err.message); 2656 started = false; 2657 }); 2658 if (started) { 2659 isPlay = true; 2660 console.info('AudioInterruptMusic Renderer started : isPlay : '+isPlay); 2661 } else { 2662 console.error('AudioInterruptMusic Renderer start failed'); 2663 } 2664 break; 2665 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 2666 console.log('Choose to pause or ignore'); 2667 if (isPlay == true) { 2668 isPlay == false; 2669 console.info('AudioInterruptMusic: Media PAUSE : TRUE'); 2670 } 2671 else { 2672 isPlay = true; 2673 console.info('AudioInterruptMusic: Media PLAY : TRUE'); 2674 } 2675 break; 2676 } 2677 } 2678}); 2679``` 2680 2681### on('markReach')<sup>8+</sup> 2682 2683on(type: 'markReach', frame: number, callback: (position: number) => {}): void 2684 2685订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,回调被调用。 2686 2687**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2688 2689**参数:** 2690 2691| 参数名 | 类型 | 必填 | 说明 | 2692| :------- | :----------------------- | :--- | :---------------------------------------- | 2693| type | string | 是 | 事件回调类型,支持的事件为:'markReach'。 | 2694| frame | number | 是 | 触发事件的帧数。 该值必须大于 0。 | 2695| callback | (position: number) => {} | 是 | 触发事件时调用的回调。 | 2696 2697**示例:** 2698 2699``` 2700audioRenderer.on('markReach', 1000, (position) => { 2701 if (position == 1000) { 2702 console.log('ON Triggered successfully'); 2703 } 2704}); 2705``` 2706 2707 2708### off('markReach') <sup>8+</sup> 2709 2710off(type: 'markReach'): void 2711 2712取消订阅标记事件。 2713 2714**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2715 2716**参数:** 2717 2718| 参数名 | 类型 | 必填 | 说明 | 2719| :----- | :----- | :--- | :------------------------------------------------ | 2720| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'markReach'。 | 2721 2722**示例:** 2723 2724``` 2725audioRenderer.off('markReach'); 2726``` 2727 2728### on('periodReach') <sup>8+</sup> 2729 2730on(type: "periodReach", frame: number, callback: (position: number) => {}): void 2731 2732订阅到达标记的事件。 当渲染的帧数达到 frame 参数的值时,回调被循环调用。 2733 2734**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2735 2736**参数:** 2737 2738| 参数名 | 类型 | 必填 | 说明 | 2739| :------- | :----------------------- | :--- | :------------------------------------------ | 2740| type | string | 是 | 事件回调类型,支持的事件为:'periodReach'。 | 2741| frame | number | 是 | 触发事件的帧数。 该值必须大于 0。 | 2742| callback | (position: number) => {} | 是 | 触发事件时调用的回调。 | 2743 2744**示例:** 2745 2746``` 2747audioRenderer.on('periodReach', 1000, (position) => { 2748 if (position == 1000) { 2749 console.log('ON Triggered successfully'); 2750 } 2751}); 2752``` 2753 2754### off('periodReach') <sup>8+</sup> 2755 2756off(type: 'periodReach'): void 2757 2758取消订阅标记事件。 2759 2760**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2761 2762**参数:** 2763 2764| 参数名 | 类型 | 必填 | 说明 | 2765| :----- | :----- | :--- | :-------------------------------------------------- | 2766| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'periodReach'。 | 2767 2768**示例:** 2769 2770``` 2771audioRenderer.off('periodReach') 2772``` 2773 2774### on('stateChange') <sup>8+</sup> 2775 2776on(type: 'stateChange', callback: Callback<AudioState\>): void 2777 2778订阅监听状态变化。 2779 2780**系统能力:** SystemCapability.Multimedia.Audio.Renderer 2781 2782**参数:** 2783 2784| 参数名 | 类型 | 必填 | 说明 | 2785| :------- | :------------------------- | :--- | :------------------------------------------ | 2786| type | string | 是 | 事件回调类型,支持的事件为:'stateChange'。 | 2787| callback | [AudioState](#audiostate8) | 是 | 返回监听的状态。 | 2788 2789**示例:** 2790 2791``` 2792audioRenderer.on('stateChange', (state) => { 2793 if (state == 1) { 2794 console.log("audio renderer state is: STATE_PREPARED"); 2795 } 2796 if (state == 2) { 2797 console.log("audio renderer state is: STATE_RUNNING"); 2798 } 2799}); 2800``` 2801 2802## AudioCapturer<sup>8+</sup> 2803 2804提供音频采集的相关接口。在调用AudioCapturer的接口前,需要先通过[createAudioCapturer](#audiocreateaudiocapturer8)创建实例。 2805 2806### 属性 2807 2808**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2809 2810| 名称 | 类型 | 可读 | 可写 | 说明 | 2811| :---- | :------------------------- | :--- | :--- | :--------------- | 2812| state<sup>8+</sup> | [AudioState](#audiostate8) | 是 | 否 | 音频采集器状态。 | 2813 2814**示例:** 2815 2816``` 2817var state = audioCapturer.state; 2818``` 2819 2820### getCapturerInfo<sup>8+</sup> 2821 2822getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void 2823 2824获取采集器信息。使用callback方式异步返回结果。 2825 2826**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2827 2828**参数:** 2829 2830| 参数名 | 类型 | 必填 | 说明 | 2831| :------- | :-------------------------------- | :--- | :----------------------------------- | 2832| callback | AsyncCallback<AudioCapturerInfo\> | 是 | 使用callback方式异步返回采集器信息。 | 2833 2834**示例:** 2835 2836``` 2837audioCapturer.getCapturerInfo((err, capturerInfo) => { 2838 if (err) { 2839 console.error('Failed to get capture info'); 2840 } else { 2841 console.log('Capturer getCapturerInfo:'); 2842 console.log('Capturer source:' + capturerInfo.source); 2843 console.log('Capturer flags:' + capturerInfo.capturerFlags); 2844 } 2845}); 2846``` 2847 2848 2849### getCapturerInfo<sup>8+</sup> 2850 2851getCapturerInfo(): Promise<AudioCapturerInfo\> 2852 2853获取采集器信息。使用Promise方式异步返回结果。 2854 2855**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2856 2857**返回值:** 2858 2859| 类型 | 说明 | 2860| :------------------------------------------------ | :---------------------------------- | 2861| Promise<[AudioCapturerInfo](#audiocapturerinfo)\> | 使用Promise方式异步返回采集器信息。 | 2862 2863**示例:** 2864 2865``` 2866audioCapturer.getCapturerInfo().then((audioParamsGet) => { 2867 if (audioParamsGet != undefined) { 2868 console.info('AudioFrameworkRecLog: Capturer CapturerInfo:'); 2869 console.info('AudioFrameworkRecLog: Capturer SourceType:' + audioParamsGet.source); 2870 console.info('AudioFrameworkRecLog: Capturer capturerFlags:' + audioParamsGet.capturerFlags); 2871 }else { 2872 console.info('AudioFrameworkRecLog: audioParamsGet is : '+audioParamsGet); 2873 console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect: '); 2874 } 2875}).catch((err) => { 2876 console.log('AudioFrameworkRecLog: CapturerInfo :ERROR: '+err.message); 2877}); 2878``` 2879 2880### getStreamInfo<sup>8+</sup> 2881 2882getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 2883 2884获取采集器流信息。使用callback方式异步返回结果。 2885 2886**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2887 2888**参数:** 2889 2890| 参数名 | 类型 | 必填 | 说明 | 2891| :------- | :--------------------------------------------------- | :--- | :------------------------------- | 2892| callback | AsyncCallback<[AudioStreamInfo](#audiostreaminfo8)\> | 是 | 使用callback方式异步返回流信息。 | 2893 2894**示例:** 2895 2896``` 2897audioCapturer.getStreamInfo((err, streamInfo) => { 2898 if (err) { 2899 console.error('Failed to get stream info'); 2900 } else { 2901 console.log('Capturer GetStreamInfo:'); 2902 console.log('Capturer sampling rate:' + streamInfo.samplingRate); 2903 console.log('Capturer channel:' + streamInfo.channels); 2904 console.log('Capturer format:' + streamInfo.sampleFormat); 2905 console.log('Capturer encoding type:' + streamInfo.encodingType); 2906 } 2907}); 2908``` 2909 2910### getStreamInfo<sup>8+</sup> 2911 2912getStreamInfo(): Promise<AudioStreamInfo\> 2913 2914获取采集器流信息。使用Promise方式异步返回结果。 2915 2916**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2917 2918**返回值:** 2919 2920| 类型 | 说明 | 2921| :--------------------------------------------- | :------------------------------ | 2922| Promise<[AudioStreamInfo](#audiostreaminfo8)\> | 使用Promise方式异步返回流信息。 | 2923 2924**示例:** 2925 2926``` 2927audioCapturer.getStreamInfo().then((audioParamsGet) => { 2928 console.info('getStreamInfo:'); 2929 console.info('sampleFormat:' + audioParamsGet.sampleFormat); 2930 console.info('samplingRate:' + audioParamsGet.samplingRate); 2931 console.info('channels:' + audioParamsGet.channels); 2932 console.info('encodingType:' + audioParamsGet.encodingType); 2933}).catch((err) => { 2934 console.log('getStreamInfo :ERROR: ' + err.message); 2935}); 2936``` 2937 2938### start<sup>8+</sup> 2939 2940start(callback: AsyncCallback<void\>): void 2941 2942启动音频采集器。使用callback方式异步返回结果。 2943 2944**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2945 2946**参数** 2947 2948| 参数名 | 类型 | 必填 | 说明 | 2949| :------- | :------------------- | :--- | :----------------------------- | 2950| callback | AsyncCallback<void\> | 是 | 使用callback方式异步返回结果。 | 2951 2952**示例:** 2953 2954``` 2955audioCapturer.start((err) => { 2956 if (err) { 2957 console.error('Capturer start failed.'); 2958 } else { 2959 console.info('Capturer start success.'); 2960 } 2961}); 2962``` 2963 2964 2965### start<sup>8+</sup> 2966 2967start(): Promise<void\> 2968 2969启动音频采集器。使用Promise方式异步返回结果。 2970 2971**系统能力:** SystemCapability.Multimedia.Audio.Capturer 2972 2973**返回值:** 2974 2975| 类型 | 说明 | 2976| :------------- | :---------------------------- | 2977| Promise<void\> | 使用Promise方式异步返回结果。 | 2978 2979**示例:** 2980 2981``` 2982import audio from '@ohos.multimedia.audio'; 2983import fileio from '@ohos.fileio'; 2984 2985var audioStreamInfo = { 2986 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 2987 channels: audio.AudioChannel.CHANNEL_2, 2988 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 2989 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 2990} 2991 2992var audioCapturerInfo = { 2993 source: audio.SourceType.SOURCE_TYPE_MIC, 2994 capturerFlags = 1 2995} 2996 2997var audioCapturer; 2998audio.createAudioCapturer(audioCapturerOptions).then((data) => { 2999 audioCapturer = data; 3000 console.info('AudioFrameworkRecLog: AudioCapturer Created: SUCCESS'); 3001 }).catch((err) => { 3002 console.info('AudioFrameworkRecLog: AudioCapturer Created: ERROR: '+err.message); 3003 }); 3004audioCapturer.start().then(() => { 3005 console.info('AudioFrameworkRecLog: ---------START---------'); 3006 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS'); 3007 console.info('AudioFrameworkRecLog: AudioCapturer: STATE: '+audioCapturer.state); 3008 console.info('AudioFrameworkRecLog: Capturer started: SUCCESS '); 3009 if ((audioCapturer.state == audio.AudioState.STATE_RUNNING)) { 3010 console.info('AudioFrameworkRecLog: AudioCapturer is in Running State'); 3011 } 3012}).catch((err) => { 3013 console.info('AudioFrameworkRecLog: Capturer start :ERROR : '+err.message); 3014 stateFlag=false; 3015}); 3016``` 3017 3018### stop<sup>8+</sup> 3019 3020stop(callback: AsyncCallback<void\>): void 3021 3022停止采集。使用callback方式异步返回结果。 3023 3024**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3025 3026**参数:** 3027 3028| 参数名 | 类型 | 必填 | 说明 | 3029| :------- | :------------------- | :--- | :----------------------------- | 3030| callback | AsyncCallback<void\> | 是 | 使用callback方式异步返回结果。 | 3031 3032**示例:** 3033 3034``` 3035audioCapturer.stop((err) => { 3036 if (err) { 3037 console.error('Capturer stop failed'); 3038 } else { 3039 console.log('Capturer stopped.'); 3040 } 3041}); 3042``` 3043 3044 3045### stop<sup>8+</sup> 3046 3047stop(): Promise<void\> 3048 3049停止采集。使用Promise方式异步返回结果。 3050 3051**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3052 3053**返回值:** 3054 3055| 类型 | 说明 | 3056| :------------- | :---------------------------- | 3057| Promise<void\> | 使用Promise方式异步返回结果。 | 3058 3059**示例:** 3060 3061``` 3062audioCapturer.stop().then(() => { 3063 console.info('AudioFrameworkRecLog: ---------STOP RECORD---------'); 3064 console.info('AudioFrameworkRecLog: Capturer stopped: SUCCESS'); 3065 if ((audioCapturer.state == audio.AudioState.STATE_STOPPED)){ 3066 console.info('AudioFrameworkRecLog: State is Stopped': '); 3067 } 3068}).catch((err) => { 3069 console.info('AudioFrameworkRecLog: Capturer stop: ERROR: '+err.message); 3070}); 3071``` 3072 3073### release<sup>8+</sup> 3074 3075release(callback: AsyncCallback<void\>): void 3076 3077释放采集器。使用callback方式异步返回结果。 3078 3079**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3080 3081**参数:** 3082 3083| 参数名 | 类型 | 必填 | 说明 | 3084| :------- | :------------------- | :--- | :---------------------------------- | 3085| callback | AsyncCallback<void\> | 是 | Callback used to return the result. | 3086 3087**示例:** 3088 3089``` 3090audioCapturer.release((err) => { 3091 if (err) { 3092 console.error('capturer release failed'); 3093 } else { 3094 console.log('capturer released.'); 3095 } 3096}); 3097``` 3098 3099 3100### release<sup>8+</sup> 3101 3102release(): Promise<void\> 3103 3104释放采集器。使用Promise方式异步返回结果。 3105 3106**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3107 3108**返回值:** 3109 3110| 类型 | 说明 | 3111| :------------- | :---------------------------- | 3112| Promise<void\> | 使用Promise方式异步返回结果。 | 3113 3114**示例:** 3115 3116``` 3117audioCapturer.release().then(() => { 3118 console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------'); 3119 console.info('AudioFrameworkRecLog: Capturer release : SUCCESS'); 3120 console.info('AudioFrameworkRecLog: AudioCapturer : STATE : '+audioCapturer.state); 3121 console.info('AudioFrameworkRecLog: stateFlag : '+stateFlag); 3122}).catch((err) => { 3123 console.info('AudioFrameworkRecLog: Capturer stop: ERROR: '+err.message); 3124}); 3125``` 3126 3127 3128### read<sup>8+</sup> 3129 3130read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void 3131 3132读入缓冲区。使用callback方式异步返回结果。 3133 3134**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3135 3136**参数** 3137 3138| 参数名 | 类型 | 必填 | 说明 | 3139| :------------- | :-------------------------- | :--- | :------------------------------- | 3140| size | number | 是 | 读入的字节数。 | 3141| isBlockingRead | boolean | 是 | 是否阻塞读操作。 | 3142| callback | AsyncCallback<ArrayBuffer\> | 是 | 使用callback方式异步返回缓冲区。 | 3143 3144**示例:** 3145 3146``` 3147var bufferSize; 3148audioCapturer.getBufferSize().then((data) => { 3149 console.info('AudioFrameworkRecLog: getBufferSize: SUCCESS '+data); 3150 bufferSize = data; 3151 }).catch((err) => { 3152 console.info('AudioFrameworkRecLog: getBufferSize: EROOR: '+err.message); 3153 }); 3154audioCapturer.read(bufferSize, true, async(err, buffer) => { 3155 if (!err) { 3156 console.log("Success in reading the buffer data"); 3157 } 3158}); 3159``` 3160 3161 3162### read<sup>8+</sup> 3163 3164read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\> 3165 3166读入缓冲区。使用Promise方式异步返回结果。 3167 3168**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3169 3170**参数:** 3171 3172| 参数名 | 类型 | 必填 | 说明 | 3173| :------------- | :------ | :--- | :--------------- | 3174| size | number | 是 | 读入的字节数。 | 3175| isBlockingRead | boolean | 是 | 是否阻塞读操作。 | 3176 3177**返回值:** 3178 3179| 类型 | 说明 | 3180| :-------------------- | :----------------------------------------------------- | 3181| Promise<ArrayBuffer\> | 如果操作成功,返回读取的缓冲区数据;否则返回错误代码。 | 3182 3183**示例:** 3184 3185``` 3186var bufferSize; 3187audioCapturer.getBufferSize().then((data) => { 3188 console.info('AudioFrameworkRecLog: getBufferSize: SUCCESS '+data); 3189 bufferSize = data; 3190 }).catch((err) => { 3191 console.info('AudioFrameworkRecLog: getBufferSize: ERROR '+err.message); 3192 }); 3193console.info('Buffer size: ' + bufferSize); 3194audioCapturer.read(bufferSize, true).then((buffer) => { 3195 console.info('buffer read successfully'); 3196}).catch((err) => { 3197 console.info('ERROR : '+err.message); 3198}); 3199``` 3200 3201 3202### getAudioTime<sup>8+</sup> 3203 3204getAudioTime(callback: AsyncCallback<number\>): void 3205 3206获取时间戳(从1970年1月1日开始),单位为纳秒。使用callback方式异步返回结果。 3207 3208**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3209 3210**参数:** 3211 3212| 参数名 | 类型 | 必填 | 说明 | 3213| :------- | :--------------------- | :--- | :----------------------------- | 3214| callback | AsyncCallback<number\> | 是 | 使用callback方式异步返回结果。 | 3215 3216**示例:** 3217 3218``` 3219audioCapturer.getAudioTime((err, timestamp) => { 3220 console.log('Current timestamp: ' + timestamp); 3221}); 3222``` 3223 3224 3225### getAudioTime<sup>8+</sup> 3226 3227getAudioTime(): Promise<number\> 3228 3229获取时间戳(从1970年1月1日开始),单位为纳秒。使用Promise方式异步返回结果。 3230 3231**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3232 3233**返回值:** 3234 3235| 类型 | 说明 | 3236| :--------------- | :---------------------------- | 3237| Promise<number\> | 使用Promise方式异步返回结果。 | 3238 3239**示例:** 3240 3241``` 3242audioCapturer.getAudioTime().then((audioTime) => { 3243 console.info('AudioFrameworkRecLog: AudioCapturer getAudioTime : Success' + audioTime ); 3244}).catch((err) => { 3245 console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message); 3246}); 3247``` 3248 3249 3250### getBufferSize<sup>8+</sup> 3251 3252getBufferSize(callback: AsyncCallback<number\>): void 3253 3254获取采集器合理的最小缓冲区大小。使用callback方式异步返回结果。 3255 3256**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3257 3258**参数:** 3259 3260| 参数名 | 类型 | 必填 | 说明 | 3261| :------- | :--------------------- | :--- | :----------------------------------- | 3262| callback | AsyncCallback<number\> | 是 | 使用callback方式异步返回缓冲区大小。 | 3263 3264**示例:** 3265 3266``` 3267audioCapturer.getBufferSize((err, bufferSize) => { 3268 if (!err) { 3269 console.log('BufferSize : ' + bufferSize); 3270 audioCapturer.read(bufferSize, true).then((buffer) => { 3271 console.info('Buffer read is ' + buffer ); 3272 }).catch((err) => { 3273 console.info('AudioFrameworkRecLog: AudioCapturer Created : ERROR : '+err.message); 3274 }); 3275 } 3276}); 3277``` 3278 3279 3280### getBufferSize<sup>8+</sup> 3281 3282getBufferSize(): Promise<number\> 3283 3284获取采集器合理的最小缓冲区大小。使用Promise方式异步返回结果。 3285 3286**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3287 3288**返回值:** 3289 3290| 类型 | 说明 | 3291| :--------------- | :---------------------------------- | 3292| Promise<number\> | 使用Promise方式异步返回缓冲区大小。 | 3293 3294**示例:** 3295 3296``` 3297var bufferSize; 3298audioCapturer.getBufferSize().then((data) => { 3299 console.info('AudioFrameworkRecLog: getBufferSize :SUCCESS '+ data); 3300 bufferSize = data; 3301}).catch((err) => { 3302 console.info('AudioFrameworkRecLog: getBufferSize :ERROR : '+ err.message); 3303}); 3304``` 3305 3306 3307### on('markReach')<sup>8+</sup> 3308 3309on(type: 'markReach', frame: number, callback: (position: number) => {}): void 3310 3311订阅标记到达的事件。 当采集的帧数达到 frame 参数的值时,回调被触发。 3312 3313**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3314 3315**参数:** 3316 3317| 参数名 | 类型 | 必填 | 说明 | 3318| :------- | :---------------------- | :--- | :----------------------------------------- | 3319| type | string | 是 | 事件回调类型,支持的事件为:'markReach'。 | 3320| frame | number | 是 | 触发事件的帧数。 该值必须大于0。 | 3321| callback | position: number) => {} | 是 | 使用callback方式异步返回被触发事件的回调。 | 3322 3323**示例:** 3324 3325``` 3326audioCapturer.on('markReach', 1000, (position) => { 3327 if (position == 1000) { 3328 console.log('ON Triggered successfully'); 3329 } 3330}); 3331``` 3332 3333### off('markReach')<sup>8+</sup> 3334 3335off(type: 'markReach'): void 3336 3337取消订阅标记到达的事件。 3338 3339**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3340 3341**参数:** 3342 3343| 参数名 | 类型 | 必填 | 说明 | 3344| :----- | :----- | :--- | :-------------------------------------------- | 3345| type | string | 是 | 取消事件回调类型,支持的事件为:'markReach'。 | 3346 3347**示例:** 3348 3349``` 3350audioCapturer.off('markReach'); 3351``` 3352 3353### on('periodReach')<sup>8+</sup> 3354 3355on(type: "periodReach", frame: number, callback: (position: number) => {}): void 3356 3357订阅到达标记的事件。 当采集的帧数达到 frame 参数的值时,回调被循环调用。 3358 3359**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3360 3361**参数:** 3362 3363| 参数名 | 类型 | 必填 | 说明 | 3364| :------- | :----------------------- | :--- | :------------------------------------------ | 3365| type | string | 是 | 事件回调类型,支持的事件为:'periodReach'。 | 3366| frame | number | 是 | 触发事件的帧数。 该值必须大于0。 | 3367| callback | (position: number) => {} | 是 | 使用callback方式异步返回被触发事件的回调 | 3368 3369**示例:** 3370 3371``` 3372audioCapturer.on('periodReach', 1000, (position) => { 3373 if (position == 1000) { 3374 console.log('ON Triggered successfully'); 3375 } 3376}); 3377``` 3378 3379### off('periodReach')<sup>8+</sup> 3380 3381off(type: 'periodReach'): void 3382 3383取消订阅标记到达的事件。 3384 3385**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3386 3387**参数:** 3388 3389| 参数名 | 类型 | 必填 | 说明 | 3390| :----- | :----- | :--- | :---------------------------------------------- | 3391| type | string | Yes | 取消事件回调类型,支持的事件为:'periodReach'。 | 3392 3393**示例:** 3394 3395``` 3396audioCapturer.off('periodReach') 3397``` 3398 3399### on('stateChange') <sup>8+</sup> 3400 3401on(type: 'stateChange', callback: Callback<AudioState\>): void 3402 3403订阅监听状态变化。 3404 3405**系统能力:** SystemCapability.Multimedia.Audio.Capturer 3406 3407**参数:** 3408 3409| 参数名 | 类型 | 必填 | 说明 | 3410| :------- | :------------------------- | :--- | :------------------------------------------ | 3411| type | string | 是 | 事件回调类型,支持的事件为:'stateChange'。 | 3412| callback | [AudioState](#audiostate8) | 是 | 返回监听的状态。 | 3413 3414**示例:** 3415 3416``` 3417audioCapturer.on('stateChange', (state) => { 3418 if (state == 1) { 3419 console.log("audio capturer state is: STATE_PREPARED"); 3420 } 3421 if (state == 2) { 3422 console.log("audio capturer state is: STATE_RUNNING"); 3423 } 3424}); 3425```