1# Interface (AudioCapturer) 2<!--Kit: Audio Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @songshenke--> 5<!--Designer: @caixuejiang; @hao-liangfei; @zhanganxiang--> 6<!--Tester: @Filger--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12> - 本Interface首批接口从API version 8开始支持。 13 14提供音频采集的相关接口。 15 16在使用AudioCapturer的接口之前,需先通过[createAudioCapturer](arkts-apis-audio-f.md#audiocreateaudiocapturer8)获取AudioCapturer实例。 17 18## 导入模块 19 20```ts 21import { audio } from '@kit.AudioKit'; 22``` 23 24## 属性 25 26**系统能力:** SystemCapability.Multimedia.Audio.Capturer 27 28| 名称 | 类型 | 只读 | 可选 | 说明 | 29| :---- | :------------------------- | :--- | :--- | :--------------- | 30| state<sup>8+</sup> | [AudioState](arkts-apis-audio-e.md#audiostate8) | 是 | 否 | 音频采集器状态。 | 31 32**示例:** 33 34```ts 35import { audio } from '@kit.AudioKit'; 36 37let state: audio.AudioState = audioCapturer.state; 38``` 39 40## getCapturerInfo<sup>8+</sup> 41 42getCapturerInfo(callback: AsyncCallback<AudioCapturerInfo\>): void 43 44获取音频采集器信息。使用callback异步回调。 45 46**系统能力:** SystemCapability.Multimedia.Audio.Capturer 47 48**参数:** 49 50| 参数名 | 类型 | 必填 | 说明 | 51| :------- | :-------------------------------- | :--- | :----------------------------------- | 52| callback | AsyncCallback<[AudioCapturerInfo](arkts-apis-audio-i.md#audiocapturerinfo8)\> | 是 | 回调函数。当获取音频采集器信息成功,err为undefined,data为获取到的音频采集器信息;否则为错误对象。 | 53 54**示例:** 55 56```ts 57import { BusinessError } from '@kit.BasicServicesKit'; 58 59audioCapturer.getCapturerInfo((err: BusinessError, capturerInfo: audio.AudioCapturerInfo) => { 60 if (err) { 61 console.error('Failed to get capture info'); 62 } else { 63 console.info('Capturer getCapturerInfo:'); 64 console.info(`Capturer source: ${capturerInfo.source}`); 65 console.info(`Capturer flags: ${capturerInfo.capturerFlags}`); 66 } 67}); 68``` 69 70 71## getCapturerInfo<sup>8+</sup> 72 73getCapturerInfo(): Promise<AudioCapturerInfo\> 74 75获取音频采集器信息。使用Promise异步回调。 76 77**系统能力:** SystemCapability.Multimedia.Audio.Capturer 78 79**返回值:** 80 81| 类型 | 说明 | 82| :------------------------------------------------ | :---------------------------------- | 83| Promise<[AudioCapturerInfo](arkts-apis-audio-i.md#audiocapturerinfo8)\> | Promise对象,返回音频采集器信息。 | 84 85**示例:** 86 87```ts 88import { BusinessError } from '@kit.BasicServicesKit'; 89 90audioCapturer.getCapturerInfo().then((audioParamsGet: audio.AudioCapturerInfo) => { 91 if (audioParamsGet != undefined) { 92 console.info('AudioFrameworkRecLog: Capturer CapturerInfo:'); 93 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 94 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 95 } else { 96 console.info(`AudioFrameworkRecLog: audioParamsGet is : ${audioParamsGet}`); 97 console.info('AudioFrameworkRecLog: audioParams getCapturerInfo are incorrect'); 98 } 99}).catch((err: BusinessError) => { 100 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${err}`); 101}) 102``` 103 104## getCapturerInfoSync<sup>10+</sup> 105 106getCapturerInfoSync(): AudioCapturerInfo 107 108获取音频采集器信息。同步返回结果。 109 110**系统能力:** SystemCapability.Multimedia.Audio.Capturer 111 112**返回值:** 113 114| 类型 | 说明 | 115| :------------------------------------------------ | :---------------------------------- | 116| [AudioCapturerInfo](arkts-apis-audio-i.md#audiocapturerinfo8) | 返回音频采集器信息。 | 117 118**示例:** 119 120```ts 121import { BusinessError } from '@kit.BasicServicesKit'; 122 123try { 124 let audioParamsGet: audio.AudioCapturerInfo = audioCapturer.getCapturerInfoSync(); 125 console.info(`AudioFrameworkRecLog: Capturer SourceType: ${audioParamsGet.source}`); 126 console.info(`AudioFrameworkRecLog: Capturer capturerFlags: ${audioParamsGet.capturerFlags}`); 127} catch (err) { 128 let error = err as BusinessError; 129 console.error(`AudioFrameworkRecLog: CapturerInfo :ERROR: ${error}`); 130} 131``` 132 133## getStreamInfo<sup>8+</sup> 134 135getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 136 137获取音频采集器流信息。使用callback异步回调。 138 139**系统能力:** SystemCapability.Multimedia.Audio.Capturer 140 141**参数:** 142 143| 参数名 | 类型 | 必填 | 说明 | 144| :------- | :--------------------------------------------------- | :--- | :------------------------------- | 145| callback | AsyncCallback<[AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8)\> | 是 | 回调函数。当获取音频采集器流信息成功,err为undefined,data为获取到的音频采集器流信息;否则为错误对象。 | 146 147**示例:** 148 149```ts 150import { BusinessError } from '@kit.BasicServicesKit'; 151 152audioCapturer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 153 if (err) { 154 console.error('Failed to get stream info'); 155 } else { 156 console.info('Capturer GetStreamInfo:'); 157 console.info(`Capturer sampling rate: ${streamInfo.samplingRate}`); 158 console.info(`Capturer channel: ${streamInfo.channels}`); 159 console.info(`Capturer format: ${streamInfo.sampleFormat}`); 160 console.info(`Capturer encoding type: ${streamInfo.encodingType}`); 161 } 162}); 163``` 164 165## getStreamInfo<sup>8+</sup> 166 167getStreamInfo(): Promise<AudioStreamInfo\> 168 169获取音频采集器流信息。使用Promise异步回调。 170 171**系统能力:** SystemCapability.Multimedia.Audio.Capturer 172 173**返回值:** 174 175| 类型 | 说明 | 176| :--------------------------------------------- | :------------------------------ | 177| Promise<[AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8)\> | Promise对象,返回音频流信息。 | 178 179**示例:** 180 181```ts 182import { BusinessError } from '@kit.BasicServicesKit'; 183 184audioCapturer.getStreamInfo().then((audioParamsGet: audio.AudioStreamInfo) => { 185 console.info('getStreamInfo:'); 186 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 187 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 188 console.info(`channels: ${audioParamsGet.channels}`); 189 console.info(`encodingType: ${audioParamsGet.encodingType}`); 190}).catch((err: BusinessError) => { 191 console.error(`getStreamInfo :ERROR: ${err}`); 192}); 193``` 194 195## getStreamInfoSync<sup>10+</sup> 196 197getStreamInfoSync(): AudioStreamInfo 198 199获取音频采集器流信息。同步返回结果。 200 201**系统能力:** SystemCapability.Multimedia.Audio.Capturer 202 203**返回值:** 204 205| 类型 | 说明 | 206| :--------------------------------------------- | :------------------------------ | 207| [AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8) | 返回音频流信息。 | 208 209**示例:** 210 211```ts 212import { BusinessError } from '@kit.BasicServicesKit'; 213 214try { 215 let audioParamsGet: audio.AudioStreamInfo = audioCapturer.getStreamInfoSync(); 216 console.info(`sampleFormat: ${audioParamsGet.sampleFormat}`); 217 console.info(`samplingRate: ${audioParamsGet.samplingRate}`); 218 console.info(`channels: ${audioParamsGet.channels}`); 219 console.info(`encodingType: ${audioParamsGet.encodingType}`); 220} catch (err) { 221 let error = err as BusinessError; 222 console.error(`getStreamInfo :ERROR: ${error}`); 223} 224``` 225 226## getAudioStreamId<sup>9+</sup> 227 228getAudioStreamId(callback: AsyncCallback<number\>): void 229 230获取音频流id。使用callback异步回调。 231 232**系统能力:** SystemCapability.Multimedia.Audio.Capturer 233 234**参数:** 235 236| 参数名 | 类型 | 必填 | 说明 | 237| :------- | :--------------------------------------------------- | :--- | :------------------- | 238| callback | AsyncCallback<number\> | 是 | 回调函数。当获取音频流id成功,err为undefined,data为获取到的音频流id;否则为错误对象。 | 239 240**示例:** 241 242```ts 243import { BusinessError } from '@kit.BasicServicesKit'; 244 245audioCapturer.getAudioStreamId((err: BusinessError, streamId: number) => { 246 console.info(`audioCapturer GetStreamId: ${streamId}`); 247}); 248``` 249 250## getAudioStreamId<sup>9+</sup> 251 252getAudioStreamId(): Promise<number\> 253 254获取音频流id。使用Promise异步回调。 255 256**系统能力:** SystemCapability.Multimedia.Audio.Capturer 257 258**返回值:** 259 260| 类型 | 说明 | 261| :----------------| :--------------------- | 262| Promise<number\> | Promise对象,返回音频流id。 | 263 264**示例:** 265 266```ts 267import { BusinessError } from '@kit.BasicServicesKit'; 268 269audioCapturer.getAudioStreamId().then((streamId: number) => { 270 console.info(`audioCapturer getAudioStreamId: ${streamId}`); 271}).catch((err: BusinessError) => { 272 console.error(`ERROR: ${err}`); 273}); 274``` 275 276## getAudioStreamIdSync<sup>10+</sup> 277 278getAudioStreamIdSync(): number 279 280获取音频流id。同步返回结果。 281 282**系统能力:** SystemCapability.Multimedia.Audio.Capturer 283 284**返回值:** 285 286| 类型 | 说明 | 287| :----------------| :--------------------- | 288| number | 返回音频流id。 | 289 290**示例:** 291 292```ts 293import { BusinessError } from '@kit.BasicServicesKit'; 294 295try { 296 let streamId: number = audioCapturer.getAudioStreamIdSync(); 297 console.info(`audioCapturer getAudioStreamIdSync: ${streamId}`); 298} catch (err) { 299 let error = err as BusinessError; 300 console.error(`ERROR: ${error}`); 301} 302``` 303 304## start<sup>8+</sup> 305 306start(callback: AsyncCallback<void\>): void 307 308启动音频采集器,开始获取音频数据。使用callback异步回调。 309 310**系统能力:** SystemCapability.Multimedia.Audio.Capturer 311 312**参数:** 313 314| 参数名 | 类型 | 必填 | 说明 | 315| :------- | :------------------- | :--- | :----------------------------- | 316| callback | AsyncCallback<void\> | 是 | 回调函数。当启动音频采集器成功,err为undefined,否则为错误对象。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 317 318**示例:** 319 320```ts 321import { BusinessError } from '@kit.BasicServicesKit'; 322 323audioCapturer.start((err: BusinessError) => { 324 if (err) { 325 console.error('Capturer start failed.'); 326 } else { 327 console.info('Capturer start success.'); 328 } 329}); 330``` 331 332 333## start<sup>8+</sup> 334 335start(): Promise<void\> 336 337启动音频采集器,开始获取音频数据。使用Promise异步回调。 338 339**系统能力:** SystemCapability.Multimedia.Audio.Capturer 340 341**返回值:** 342 343| 类型 | 说明 | 344| :------------- | :---------------------------- | 345| Promise<void\> | Promise对象,成功表示启动音频采集器成功。异常将返回error对象:<br>错误码6800301:表示包含状态检查异常、焦点抢占失败、系统处理异常(具体错误查看系统日志)。 | 346 347**示例:** 348 349```ts 350import { BusinessError } from '@kit.BasicServicesKit'; 351 352audioCapturer.start().then(() => { 353 console.info('Succeeded in doing start.'); 354 if (audioCapturer.state == audio.AudioState.STATE_RUNNING) { 355 console.info('AudioFrameworkRecLog: AudioCapturer is in Running State'); 356 } 357}).catch((err: BusinessError) => { 358 console.error(`Failed to start. Code: ${err.code}, message: ${err.message}`); 359}); 360``` 361 362## stop<sup>8+</sup> 363 364stop(callback: AsyncCallback<void\>): void 365 366停止音频采集器,停止输入音频流。使用callback异步回调。 367 368**系统能力:** SystemCapability.Multimedia.Audio.Capturer 369 370**参数:** 371 372| 参数名 | 类型 | 必填 | 说明 | 373| :------- | :------------------- | :--- | :----------------------------- | 374| callback | AsyncCallback<void\> | 是 | 回调函数。当停止音频采集成功,err为undefined,否则为错误对象。 | 375 376**示例:** 377 378```ts 379import { BusinessError } from '@kit.BasicServicesKit'; 380 381audioCapturer.stop((err: BusinessError) => { 382 if (err) { 383 console.error('Capturer stop failed'); 384 } else { 385 console.info('Capturer stopped.'); 386 } 387}); 388``` 389 390 391## stop<sup>8+</sup> 392 393stop(): Promise<void\> 394 395停止音频采集器,停止输入音频流。使用Promise异步回调。 396 397**系统能力:** SystemCapability.Multimedia.Audio.Capturer 398 399**返回值:** 400 401| 类型 | 说明 | 402| :------------- | :---------------------------- | 403| Promise<void\> | Promise对象。无返回结果的Promise对象。 | 404 405**示例:** 406 407```ts 408import { BusinessError } from '@kit.BasicServicesKit'; 409 410audioCapturer.stop().then(() => { 411 console.info('Succeeded in doing stop.'); 412 if (audioCapturer.state == audio.AudioState.STATE_STOPPED){ 413 console.info('AudioFrameworkRecLog: State is Stopped:'); 414 } 415}).catch((err: BusinessError) => { 416 console.error(`Failed to stop. Code: ${err.code}, message: ${err.message}`); 417}); 418``` 419 420## release<sup>8+</sup> 421 422release(callback: AsyncCallback<void\>): void 423 424释放音频采集器。使用callback异步回调。 425 426**系统能力:** SystemCapability.Multimedia.Audio.Capturer 427 428**参数:** 429 430| 参数名 | 类型 | 必填 | 说明 | 431| :------- | :------------------- | :--- | :---------------------------------- | 432| callback | AsyncCallback<void\> | 是 | 回调函数。当释放音频采集器成功,err为undefined,否则为错误对象。 | 433 434**示例:** 435 436```ts 437import { BusinessError } from '@kit.BasicServicesKit'; 438 439audioCapturer.release((err: BusinessError) => { 440 if (err) { 441 console.error('capturer release failed'); 442 } else { 443 console.info('capturer released.'); 444 } 445}); 446``` 447 448 449## release<sup>8+</sup> 450 451release(): Promise<void\> 452 453释放音频采集器。使用Promise异步回调。 454 455**系统能力:** SystemCapability.Multimedia.Audio.Capturer 456 457**返回值:** 458 459| 类型 | 说明 | 460| :------------- | :---------------------------- | 461| Promise<void\> | Promise对象。无返回结果的Promise对象。 | 462 463**示例:** 464 465```ts 466import { BusinessError } from '@kit.BasicServicesKit'; 467 468audioCapturer.release().then(() => { 469 console.info('AudioFrameworkRecLog: ---------RELEASE RECORD---------'); 470 console.info('AudioFrameworkRecLog: Capturer release : SUCCESS'); 471 console.info(`AudioFrameworkRecLog: AudioCapturer : STATE : ${audioCapturer.state}`); 472}).catch((err: BusinessError) => { 473 console.error(`AudioFrameworkRecLog: Capturer stop: ERROR: ${err}`); 474}); 475``` 476 477 478## getAudioTime<sup>8+</sup> 479 480getAudioTime(callback: AsyncCallback<number\>): void 481 482获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。使用callback异步回调。 483 484**系统能力:** SystemCapability.Multimedia.Audio.Capturer 485 486**参数:** 487 488| 参数名 | 类型 | 必填 | 说明 | 489| :------- | :--------------------- | :--- | :----------------------------- | 490| callback | AsyncCallback<number\> | 是 | 回调函数。当获取时间戳成功,err为undefined,data为获取到的时间戳;否则为错误对象。 | 491 492**示例:** 493 494```ts 495import { BusinessError } from '@kit.BasicServicesKit'; 496 497audioCapturer.getAudioTime((err: BusinessError, timestamp: number) => { 498 console.info(`Current timestamp: ${timestamp}`); 499}); 500``` 501 502## getAudioTime<sup>8+</sup> 503 504getAudioTime(): Promise<number\> 505 506获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。使用Promise异步回调。 507 508**系统能力:** SystemCapability.Multimedia.Audio.Capturer 509 510**返回值:** 511 512| 类型 | 说明 | 513| :--------------- | :---------------------------- | 514| Promise<number\> | Promise对象,返回时间戳(从1970年1月1日开始),单位为纳秒。 | 515 516**示例:** 517 518```ts 519import { BusinessError } from '@kit.BasicServicesKit'; 520 521audioCapturer.getAudioTime().then((audioTime: number) => { 522 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTime : Success ${audioTime}`); 523}).catch((err: BusinessError) => { 524 console.error(`AudioFrameworkRecLog: AudioCapturer Created : ERROR : ${err}`); 525}); 526``` 527 528## getAudioTimeSync<sup>10+</sup> 529 530getAudioTimeSync(): number 531 532获取当前录制位置的时间戳(从1970年1月1日开始),单位为纳秒。同步返回结果。 533 534**系统能力:** SystemCapability.Multimedia.Audio.Capturer 535 536**返回值:** 537 538| 类型 | 说明 | 539| :--------------- | :---------------------------- | 540| number | 返回时间戳。 | 541 542**示例:** 543 544```ts 545import { BusinessError } from '@kit.BasicServicesKit'; 546 547try { 548 let audioTime: number = audioCapturer.getAudioTimeSync(); 549 console.info(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : Success ${audioTime}`); 550} catch (err) { 551 let error = err as BusinessError; 552 console.error(`AudioFrameworkRecLog: AudioCapturer getAudioTimeSync : ERROR : ${error}`); 553} 554``` 555 556## getAudioTimestampInfo<sup>19+</sup> 557 558getAudioTimestampInfo(): Promise\<AudioTimestampInfo> 559 560获取输入音频流时间戳和当前数据帧位置信息。 561 562该接口可以获取到音频通道实际录制位置(framePos)以及录制到该位置时候的时间戳(timestamp),时间戳单位为纳秒。 563 564**系统能力:** SystemCapability.Multimedia.Audio.Capturer 565 566**返回值:** 567 568| 类型 | 说明 | 569|-------------------------------------------------------| ----------------------- | 570| Promise\<[AudioTimestampInfo](arkts-apis-audio-i.md#audiotimestampinfo19)> | Promise对象,返回音频流时间戳和当前数据帧位置信息。 | 571 572**错误码:** 573 574以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 575 576| 错误码ID | 错误信息 | 577| ------- | --------------------------------------------| 578| 6800103 | Operation not permit at current state. | 579 580**示例:** 581 582```ts 583import { BusinessError } from '@kit.BasicServicesKit'; 584 585audioCapturer.getAudioTimestampInfo().then((audioTimestampInfo: audio.AudioTimestampInfo) => { 586 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 587}).catch((err: BusinessError) => { 588 console.error(`ERROR: ${err}`); 589}); 590``` 591 592## getAudioTimestampInfoSync<sup>19+</sup> 593 594getAudioTimestampInfoSync(): AudioTimestampInfo 595 596获取音频流时间戳和当前数据帧位置信息。同步返回结果。 597 598**系统能力:** SystemCapability.Multimedia.Audio.Capturer 599 600**返回值:** 601 602| 类型 | 说明 | 603| ---------------- | ----------------------- | 604| [AudioTimestampInfo](arkts-apis-audio-i.md#audiotimestampinfo19) | 返回音频流时间戳和当前数据帧位置信息。 | 605 606**错误码:** 607 608以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 609 610| 错误码ID | 错误信息 | 611| ------- | --------------------------------------------| 612| 6800103 | Operation not permit at current state. | 613 614**示例:** 615 616```ts 617import { BusinessError } from '@kit.BasicServicesKit'; 618 619try { 620 let audioTimestampInfo: audio.AudioTimestampInfo = audioCapturer.getAudioTimestampInfoSync(); 621 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 622} catch (err) { 623 let error = err as BusinessError; 624 console.error(`ERROR: ${error}`); 625} 626``` 627 628## getBufferSize<sup>8+</sup> 629 630getBufferSize(callback: AsyncCallback<number\>): void 631 632获取采集器合理的最小缓冲区大小。使用callback异步回调。 633 634**系统能力:** SystemCapability.Multimedia.Audio.Capturer 635 636**参数:** 637 638| 参数名 | 类型 | 必填 | 说明 | 639| :------- | :--------------------- | :--- | :----------------------------------- | 640| callback | AsyncCallback<number\> | 是 | 回调函数。当获取采集器合理的最小缓冲区大小成功,err为undefined,data为获取到的采集器合理的最小缓冲区大小;否则为错误对象。 | 641 642**示例:** 643 644```ts 645import { BusinessError } from '@kit.BasicServicesKit'; 646 647audioCapturer.getBufferSize((err: BusinessError, bufferSize: number) => { 648 if (err) { 649 console.error(`Failed to get buffer size. Code: ${err.code}, message: ${err.message}`); 650 } else { 651 console.info(`Succeeded in getting buffer size, BufferSize: ${bufferSize}.`); 652 } 653}); 654``` 655 656## getBufferSize<sup>8+</sup> 657 658getBufferSize(): Promise<number\> 659 660获取采集器合理的最小缓冲区大小。使用Promise异步回调。 661 662**系统能力:** SystemCapability.Multimedia.Audio.Capturer 663 664**返回值:** 665 666| 类型 | 说明 | 667| :--------------- | :---------------------------------- | 668| Promise<number\> | Promise对象,返回缓冲区大小。 | 669 670**示例:** 671 672```ts 673import { BusinessError } from '@kit.BasicServicesKit'; 674 675audioCapturer.getBufferSize().then((bufferSize: number) => { 676 console.info(`Succeeded in getting buffer size, BufferSize: ${bufferSize}.`); 677}).catch((err: BusinessError) => { 678 console.error(`Failed to get buffer size. Code: ${err.code}, message: ${err.message}`); 679}); 680``` 681 682## getBufferSizeSync<sup>10+</sup> 683 684getBufferSizeSync(): number 685 686获取采集器合理的最小缓冲区大小。同步返回结果。 687 688**系统能力:** SystemCapability.Multimedia.Audio.Capturer 689 690**返回值:** 691 692| 类型 | 说明 | 693| :--------------- | :---------------------------------- | 694| number | 返回缓冲区大小。 | 695 696**示例:** 697 698```ts 699import { BusinessError } from '@kit.BasicServicesKit'; 700 701try { 702 let bufferSize = audioCapturer.getBufferSizeSync(); 703 console.info(`Succeeded in getting buffer size, BufferSize: ${bufferSize}.`); 704} catch (err) { 705 let error = err as BusinessError; 706 console.error(`Failed to get buffer size. Code: ${error.code}, message: ${error.message}`); 707} 708``` 709 710## getCurrentInputDevices<sup>11+</sup> 711 712getCurrentInputDevices(): AudioDeviceDescriptors 713 714获取录音流输入设备信息。同步返回结果。 715 716**系统能力:** SystemCapability.Multimedia.Audio.Device 717 718**返回值:** 719 720| 类型 | 说明 | 721| ---------------------- | ------------------------------------------------------ | 722| [AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors) | 同步接口,返回设备属性数组类型数据。 | 723 724**示例:** 725 726```ts 727let deviceDescriptors: audio.AudioDeviceDescriptors = audioCapturer.getCurrentInputDevices(); 728console.info(`Device id: ${deviceDescriptors[0].id}`); 729console.info(`Device type: ${deviceDescriptors[0].deviceType}`); 730console.info(`Device role: ${deviceDescriptors[0].deviceRole}`); 731console.info(`Device name: ${deviceDescriptors[0].name}`); 732console.info(`Device address: ${deviceDescriptors[0].address}`); 733console.info(`Device samplerates: ${deviceDescriptors[0].sampleRates[0]}`); 734console.info(`Device channelcounts: ${deviceDescriptors[0].channelCounts[0]}`); 735console.info(`Device channelmask: ${deviceDescriptors[0].channelMasks[0]}`); 736if (deviceDescriptors[0].encodingTypes) { 737 console.info(`Device encodingTypes: ${deviceDescriptors[0].encodingTypes[0]}`); 738} 739``` 740 741## getCurrentAudioCapturerChangeInfo<sup>11+</sup> 742 743getCurrentAudioCapturerChangeInfo(): AudioCapturerChangeInfo 744 745获取录音流配置。同步返回结果。 746 747**系统能力:** SystemCapability.Multimedia.Audio.Device 748 749**返回值:** 750 751| 类型 | 说明 | 752| :--------------- | :---------------------------------- | 753| [AudioCapturerChangeInfo](arkts-apis-audio-i.md#audiocapturerchangeinfo9) | 同步接口,返回描述音频采集器更改信息。 | 754 755**示例:** 756 757```ts 758let info: audio.AudioCapturerChangeInfo = audioCapturer.getCurrentAudioCapturerChangeInfo(); 759console.info(`Info streamId: ${info.streamId}`); 760console.info(`Info source: ${info.capturerInfo.source}`); 761console.info(`Info capturerFlags: ${info.capturerInfo.capturerFlags}`); 762console.info(`Info muted: ${info.muted}`); 763console.info(`Info type: ${info.deviceDescriptors[0].deviceType}`); 764console.info(`Info role: ${info.deviceDescriptors[0].deviceRole}`); 765console.info(`Info name: ${info.deviceDescriptors[0].name}`); 766console.info(`Info address: ${info.deviceDescriptors[0].address}`); 767console.info(`Info samplerates: ${info.deviceDescriptors[0].sampleRates[0]}`); 768console.info(`Info channelcounts: ${info.deviceDescriptors[0].channelCounts[0]}`); 769console.info(`Info channelmask: ${info.deviceDescriptors[0].channelMasks[0]}`); 770if (info.deviceDescriptors[0].encodingTypes) { 771 console.info(`Device encodingTypes: ${info.deviceDescriptors[0].encodingTypes[0]}`); 772} 773``` 774 775## on('audioInterrupt')<sup>10+</sup> 776 777on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 778 779监听音频中断事件(当音频焦点发生变化时触发)。使用callback异步回调。 780 781AudioCapturer对象在start事件时获取焦点,在pause、stop等事件时释放焦点,无需开发者主动申请。 782 783调用此方法后,如果AudioCapturer对象获取焦点失败或发生中断事件(如被其他音频打断等),会收到[InterruptEvent](arkts-apis-audio-i.md#interruptevent9)。建议应用根据InterruptEvent的信息进行进一步处理。更多信息请参阅文档[音频焦点和音频会话介绍](../../media/audio/audio-playback-concurrency.md)。 784 785**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 786 787**参数:** 788 789| 参数名 | 类型 | 必填 | 说明 | 790| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 791| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当音频焦点状态发生变化时,触发该事件。 | 792| callback | Callback\<[InterruptEvent](arkts-apis-audio-i.md#interruptevent9)\> | 是 | 回调函数,返回中断事件信息。 | 793 794**错误码:** 795 796以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 797 798| 错误码ID | 错误信息 | 799| ------- | --------------------------------------------| 800| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 801| 6800101 | Parameter verification failed. | 802 803**示例:** 804 805```ts 806import { audio } from '@kit.AudioKit'; 807 808let isCapturing: boolean = false; // 标识符,表示是否正在采集。 809 810audioCapturer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 811 // 在发生音频打断事件时,audioCapturer收到interruptEvent回调,此处根据其内容做相应处理。 812 // 1. 可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 813 // 注意:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 814 // 2. 必选:读取interruptEvent.hintType的类型,做出相应的处理。 815 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 816 // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等。 817 switch (interruptEvent.hintType) { 818 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 819 // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent。 820 console.info('Force paused. Update capturing status and stop reading'); 821 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 822 break; 823 case audio.InterruptHint.INTERRUPT_HINT_STOP: 824 // 音频流已被停止,永久失去焦点,若想恢复采集,需用户主动触发。 825 console.info('Force stopped. Update capturing status and stop reading'); 826 isCapturing = false; // 简化处理,代表应用切换至暂停状态的若干操作。 827 break; 828 default: 829 console.info('Invalid interruptEvent'); 830 break; 831 } 832 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 833 // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理。 834 switch (interruptEvent.hintType) { 835 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 836 // 建议应用继续采集(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复采集)。 837 // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型。 838 console.info('Resume force paused renderer or ignore'); 839 // 若选择继续采集,需在此处主动执行开始采集的若干操作。 840 break; 841 default: 842 console.info('Invalid interruptEvent'); 843 break; 844 } 845 } 846}); 847``` 848 849## off('audioInterrupt')<sup>10+</sup> 850 851off(type: 'audioInterrupt'): void 852 853取消监听音频中断事件。 854 855**系统能力:** SystemCapability.Multimedia.Audio.Interrupt 856 857**参数:** 858 859| 参数名 | 类型 | 必填 | 说明 | 860| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 861| type | string | 是 | 事件回调类型,支持的事件为'audioInterrupt',当取消监听音频中断事件时,触发该事件。 | 862 863**错误码:** 864 865以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 866 867| 错误码ID | 错误信息 | 868| ------- | --------------------------------------------| 869| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 870| 6800101 | Parameter verification failed. | 871 872**示例:** 873 874```ts 875audioCapturer.off('audioInterrupt'); 876``` 877 878## on('inputDeviceChange')<sup>11+</sup> 879 880on(type: 'inputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 881 882监听音频输入设备变化事件(当音频输入设备发生变化时触发)。使用callback异步回调。 883 884**系统能力:** SystemCapability.Multimedia.Audio.Device 885 886**参数:** 887 888| 参数名 | 类型 | 必填 | 说明 | 889| :------- | :------------------------- | :--- | :------------------------------------------ | 890| type | string | 是 | 事件回调类型,支持的事件为'inputDeviceChange',当音频输入设备发生变化时,触发该事件。 | 891| callback | Callback\<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors) > | 是 | 回调函数,返回监听的音频输入设备变化(返回数据为切换后的设备信息)。 | 892 893**错误码:** 894 895以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 896 897| 错误码ID | 错误信息 | 898| ------- | --------------------------------------------| 899| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 900| 6800101 | Parameter verification failed. | 901 902**示例:** 903 904```ts 905audioCapturer.on('inputDeviceChange', (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 906 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 907 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 908 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 909}); 910``` 911## off('inputDeviceChange')<sup>11+</sup> 912 913off(type: 'inputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 914 915取消监听音频输入设备更改事件。使用callback异步回调。 916 917**系统能力:** SystemCapability.Multimedia.Audio.Device 918 919**参数:** 920 921| 参数名 | 类型 | 必填 | 说明 | 922| :------- | :------------------------- | :--- |:-----------------------------------------| 923| type | string | 是 | 事件回调类型,支持的事件为'inputDeviceChange',当取消监听音频输入设备更改事件时,触发该事件。 | 924| callback | Callback\<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors) > | 否 | 回调函数,返回监听的音频输入设备信息。 | 925 926**错误码:** 927 928以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 929 930| 错误码ID | 错误信息 | 931| ------- | --------------------------------------------| 932| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 933| 6800101 | Parameter verification failed. | 934 935**示例:** 936 937```ts 938// 取消该事件的所有监听。 939audioCapturer.off('inputDeviceChange'); 940 941// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 942let inputDeviceChangeCallback = (deviceChangeInfo: audio.AudioDeviceDescriptors) => { 943 console.info(`inputDevice id: ${deviceChangeInfo[0].id}`); 944 console.info(`inputDevice deviceRole: ${deviceChangeInfo[0].deviceRole}`); 945 console.info(`inputDevice deviceType: ${deviceChangeInfo[0].deviceType}`); 946}; 947 948audioCapturer.on('inputDeviceChange', inputDeviceChangeCallback); 949 950audioCapturer.off('inputDeviceChange', inputDeviceChangeCallback); 951``` 952 953## on('audioCapturerChange')<sup>11+</sup> 954 955on(type: 'audioCapturerChange', callback: Callback\<AudioCapturerChangeInfo>): void 956 957监听录音流配置变化事件(当音频录制流状态变化、设备变化时触发)。使用callback异步回调。订阅内部是异步实现,是非精确回调,在录音流配置变化的同时注册回调,收到的返回结果存在变化可能性。 958 959**系统能力:** SystemCapability.Multimedia.Audio.Capturer 960 961**参数:** 962 963| 参数名 | 类型 | 必填 | 说明 | 964| :------- | :------------------------- | :--- | :------------------------------------------ | 965| type | string | 是 | 事件回调类型,支持的事件为'audioCapturerChange',当音频录制流状态变化、设备变化时,触发该事件。 | 966| callback | Callback\<[AudioCapturerChangeInfo](arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | 是 | 回调函数,录音流配置或状态变化时返回监听的录音流当前配置和状态信息。 | 967 968**错误码:** 969 970以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 971 972| 错误码ID | 错误信息 | 973| ------- | --------------------------------------------| 974| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 975| 6800101 | Parameter verification failed. | 976 977**示例:** 978 979```ts 980audioCapturer.on('audioCapturerChange', (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 981 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 982 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 983 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 984}); 985``` 986 987## off('audioCapturerChange')<sup>11+</sup> 988 989off(type: 'audioCapturerChange', callback?: Callback\<AudioCapturerChangeInfo>): void 990 991取消监听录音流配置变化事件。使用callback异步回调。 992 993**系统能力:** SystemCapability.Multimedia.Audio.Capturer 994 995**参数:** 996 997| 参数名 | 类型 | 必填 | 说明 | 998| :------- | :------------------------- | :--- | :------------------------------------------ | 999| type | string | 是 | 事件回调类型,支持的事件为'audioCapturerChange',当取消监听录音流配置变化事件时,触发该事件。 | 1000| callback | Callback\<[AudioCapturerChangeInfo](arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | 否 | 回调函数,返回取消监听的录音流配置或状态变化。 | 1001 1002**错误码:** 1003 1004以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 1005 1006| 错误码ID | 错误信息 | 1007| ------- | --------------------------------------------| 1008| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1009| 6800101 | Parameter verification failed. | 1010 1011**示例:** 1012 1013```ts 1014// 取消该事件的所有监听。 1015audioCapturer.off('audioCapturerChange'); 1016 1017// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1018let audioCapturerChangeCallback = (capturerChangeInfo: audio.AudioCapturerChangeInfo) => { 1019 console.info(`audioCapturerChange id: ${capturerChangeInfo[0].id}`); 1020 console.info(`audioCapturerChange deviceRole: ${capturerChangeInfo[0].deviceRole}`); 1021 console.info(`audioCapturerChange deviceType: ${capturerChangeInfo[0].deviceType}`); 1022}; 1023 1024audioCapturer.on('audioCapturerChange', audioCapturerChangeCallback); 1025 1026audioCapturer.off('audioCapturerChange', audioCapturerChangeCallback); 1027``` 1028 1029## on('markReach')<sup>8+</sup> 1030 1031on(type: 'markReach', frame: number, callback: Callback<number>): void 1032 1033监听标记到达事件(当采集的帧数达到frame参数的值时触发,仅调用一次)。使用callback异步回调。 1034 1035如果将frame设置为100,当采集帧数到达第100帧时,系统将上报信息。 1036 1037**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1038 1039**参数:** 1040 1041| 参数名 | 类型 | 必填 | 说明 | 1042| :------- | :---------------------- | :--- | :----------------------------------------- | 1043| type | string | 是 | 事件回调类型,支持的事件为'markReach',当采集的帧数达到frame参数的值时,触发该事件。 | 1044| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 1045| callback | Callback\<number> | 是 | 回调函数,返回frame参数的值。 | 1046 1047**示例:** 1048 1049```ts 1050audioCapturer.on('markReach', 1000, (position: number) => { 1051 if (position == 1000) { 1052 console.info('ON Triggered successfully'); 1053 } 1054}); 1055``` 1056 1057## off('markReach')<sup>8+</sup> 1058 1059off(type: 'markReach', callback?: Callback<number>): void 1060 1061取消监听标记到达事件。使用callback异步回调。 1062 1063**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1064 1065**参数:** 1066 1067| 参数名 | 类型 | 必填 | 说明 | 1068| :----- | :----- | :--- | :------------------------------------------------ | 1069| type | string | 是 | 事件回调类型,支持的事件为'markReach',当取消监听标记到达事件时,触发该事件。 | 1070| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 1071 1072**示例:** 1073 1074```ts 1075// 取消该事件的所有监听。 1076audioCapturer.off('markReach'); 1077 1078// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1079let markReachCallback = (position: number) => { 1080 if (position == 1000) { 1081 console.info('ON Triggered successfully'); 1082 } 1083}; 1084 1085audioCapturer.on('markReach', 1000, markReachCallback); 1086 1087audioCapturer.off('markReach', markReachCallback); 1088``` 1089 1090## on('periodReach')<sup>8+</sup> 1091 1092on(type: 'periodReach', frame: number, callback: Callback<number>): void 1093 1094监听标记到达事件(当采集的帧数达到frame参数的值时触发,即按周期上报信息)。使用callback异步回调。 1095 1096如果将frame设置为10,每渲染10帧数据均会上报信息(例如:第10帧、第20帧、第30帧......)。 1097 1098**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1099 1100**参数:** 1101 1102| 参数名 | 类型 | 必填 | 说明 | 1103| :------- | :----------------------- | :--- | :------------------------------------------ | 1104| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当采集的帧数达到frame参数的值时,触发该事件。 | 1105| frame | number | 是 | 触发事件的帧数。该值必须大于0。 | 1106| callback | Callback\<number> | 是 |回调函数,返回frame参数的值。 | 1107 1108**示例:** 1109 1110```ts 1111audioCapturer.on('periodReach', 1000, (position: number) => { 1112 if (position == 1000) { 1113 console.info('ON Triggered successfully'); 1114 } 1115}); 1116``` 1117 1118## off('periodReach')<sup>8+</sup> 1119 1120off(type: 'periodReach', callback?: Callback<number>): void 1121 1122取消监听标记到达事件。使用callback异步回调。 1123 1124**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1125 1126**参数:** 1127 1128| 参数名 | 类型 | 必填 | 说明 | 1129| :----- | :----- | :--- | :-------------------------------------------------- | 1130| type | string | 是 | 事件回调类型,支持的事件为'periodReach',当取消监听标记到达事件时,触发该事件。 | 1131| callback<sup>18+</sup> | Callback\<number> | 否 | 回调函数,返回frame参数的值。 | 1132 1133**示例:** 1134 1135```ts 1136// 取消该事件的所有监听。 1137audioCapturer.off('periodReach'); 1138 1139// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1140let periodReachCallback = (position: number) => { 1141 if (position == 1000) { 1142 console.info('ON Triggered successfully'); 1143 } 1144}; 1145 1146audioCapturer.on('periodReach', 1000, periodReachCallback); 1147 1148audioCapturer.off('periodReach', periodReachCallback); 1149``` 1150 1151## on('stateChange')<sup>8+</sup> 1152 1153on(type: 'stateChange', callback: Callback<AudioState\>): void 1154 1155监听状态变化事件(当AudioCapturer状态发生变化时触发)。使用callback异步回调。 1156 1157**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1158 1159**参数:** 1160 1161| 参数名 | 类型 | 必填 | 说明 | 1162| :------- | :------------------------- | :--- | :------------------------------------------ | 1163| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当AudioCapturer状态发生变化时,触发该事件。 | 1164| callback | Callback\<[AudioState](arkts-apis-audio-e.md#audiostate8)> | 是 | 回调函数,返回当前音频的状态。 | 1165 1166**示例:** 1167 1168```ts 1169audioCapturer.on('stateChange', (state: audio.AudioState) => { 1170 if (state == 1) { 1171 console.info('audio capturer state is: STATE_PREPARED'); 1172 } 1173 if (state == 2) { 1174 console.info('audio capturer state is: STATE_RUNNING'); 1175 } 1176}); 1177``` 1178 1179## off('stateChange')<sup>18+</sup> 1180 1181off(type: 'stateChange', callback?: Callback<AudioState>): void 1182 1183取消监听到达标记事件。使用callback异步回调。 1184 1185**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1186 1187**参数:** 1188 1189| 参数名 | 类型 | 必填 | 说明 | 1190| :----- | :----- | :--- | :-------------------------------------------------- | 1191| type | string | 是 | 事件回调类型,支持的事件为'stateChange',当取消监听到达标记事件时,触发该事件。 | 1192| callback | Callback\<[AudioState](arkts-apis-audio-e.md#audiostate8)> | 否 | 回调函数,返回当前音频的状态。 | 1193 1194**错误码:** 1195 1196以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 1197 1198| 错误码ID | 错误信息 | 1199| ------- | --------------------------------------------| 1200| 6800101 | Parameter verification failed. | 1201 1202**示例:** 1203 1204```ts 1205// 取消该事件的所有监听。 1206audioCapturer.off('stateChange'); 1207 1208// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1209let stateChangeCallback = (state: audio.AudioState) => { 1210 if (state == 1) { 1211 console.info('audio renderer state is: STATE_PREPARED'); 1212 } 1213 if (state == 2) { 1214 console.info('audio renderer state is: STATE_RUNNING'); 1215 } 1216}; 1217 1218audioCapturer.on('stateChange', stateChangeCallback); 1219 1220audioCapturer.off('stateChange', stateChangeCallback); 1221``` 1222 1223## on('readData')<sup>11+</sup> 1224 1225on(type: 'readData', callback: Callback\<ArrayBuffer>): void 1226 1227监听音频数据读取回调事件(当需要读取音频流数据时触发)。使用callback异步回调。 1228 1229回调函数仅用来读取音频数据,请勿在回调函数中调用AudioCapturer相关接口。 1230 1231**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1232 1233**参数:** 1234 1235| 参数名 | 类型 | 必填 | 说明 | 1236| :------- |:-----------------------| :--- |:--------------------------| 1237| type | string | 是 | 事件回调类型,支持的事件为'readData',当需要读取音频流数据时,触发该事件。 | 1238| callback | Callback\<ArrayBuffer> | 是 | 回调函数,返回读到的数据缓冲区。 | 1239 1240**错误码:** 1241 1242以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 1243 1244| 错误码ID | 错误信息 | 1245| ------- | --------------------------------------------| 1246| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1247| 6800101 | Parameter verification failed. | 1248 1249**示例:** 1250 1251```ts 1252import { BusinessError } from '@kit.BasicServicesKit'; 1253import { fileIo as fs } from '@kit.CoreFileKit'; 1254import { common } from '@kit.AbilityKit'; 1255 1256class Options { 1257 offset?: number; 1258 length?: number; 1259} 1260 1261let bufferSize: number = 0; 1262// 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext。 1263let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 1264let path = context.cacheDir; 1265// 确保该沙箱路径下存在该资源。 1266let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 1267let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 1268let readDataCallback = (buffer: ArrayBuffer) => { 1269 let options: Options = { 1270 offset: bufferSize, 1271 length: buffer.byteLength 1272 }; 1273 fs.writeSync(file.fd, buffer, options); 1274 bufferSize += buffer.byteLength; 1275} 1276 1277audioCapturer.on('readData', readDataCallback); 1278 1279audioCapturer.start((err: BusinessError) => { 1280 if (err) { 1281 console.error('Capturer start failed.'); 1282 } else { 1283 console.info('Capturer start success.'); 1284 } 1285}); 1286``` 1287 1288## off('readData')<sup>11+</sup> 1289 1290off(type: 'readData', callback?: Callback\<ArrayBuffer>): void 1291 1292取消监听音频数据读取回调事件。使用callback异步回调。 1293 1294**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1295 1296**参数:** 1297 1298| 参数名 | 类型 | 必填 | 说明 | 1299| :------- |:-----------------------| :--- |:-------------------------------------------| 1300| type | string | 是 | 事件回调类型,支持的事件为'readData',当取消监听音频数据读取回调事件时,触发该事件。 | 1301| callback | Callback\<ArrayBuffer> | 否 | 回调函数,返回读到的数据缓冲区。 | 1302 1303**错误码:** 1304 1305以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[Audio错误码](errorcode-audio.md)。 1306 1307| 错误码ID | 错误信息 | 1308| ------- | --------------------------------------------| 1309| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1310| 6800101 | Parameter verification failed. | 1311 1312**示例:** 1313 1314```ts 1315// 取消该事件的所有监听。 1316audioCapturer.off('readData'); 1317 1318// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听。 1319let readDataCallback = (data: ArrayBuffer) => { 1320 console.info(`read data: ${data}`); 1321}; 1322 1323audioCapturer.on('readData', readDataCallback); 1324 1325audioCapturer.off('readData', readDataCallback); 1326``` 1327 1328## getOverflowCount<sup>12+</sup> 1329 1330getOverflowCount(): Promise<number> 1331 1332获取当前录制音频流的过载音频帧数量。使用Promise异步回调。 1333 1334**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1335 1336**返回值:** 1337 1338| 类型 | 说明 | 1339| ------------------- | ----------------------------- | 1340| Promise<number>| Promise对象,返回音频流的过载音频帧数量。| 1341 1342**示例:** 1343 1344```ts 1345import { BusinessError } from '@kit.BasicServicesKit'; 1346 1347audioCapturer.getOverflowCount().then((value: number) => { 1348 console.info(`Get overflow count Success! ${value}`); 1349}).catch((err: BusinessError) => { 1350 console.error(`Get overflow count Fail: ${err}`); 1351}); 1352``` 1353 1354## getOverflowCountSync<sup>12+</sup> 1355 1356getOverflowCountSync(): number 1357 1358获取当前录制音频流的过载音频帧数量。同步返回数据。 1359 1360**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1361 1362**返回值:** 1363 1364| 类型 | 说明 | 1365| ------------------- | ----------------------------- | 1366| number| 返回音频流的过载音频帧数量。| 1367 1368**示例:** 1369 1370```ts 1371import { BusinessError } from '@kit.BasicServicesKit'; 1372 1373try { 1374 let value: number = audioCapturer.getOverflowCountSync(); 1375 console.info(`Get overflow count Success! ${value}`); 1376} catch (err) { 1377 let error = err as BusinessError; 1378 console.error(`Get overflow count Fail: ${error}`); 1379} 1380``` 1381 1382## setWillMuteWhenInterrupted<sup>20+</sup> 1383 1384setWillMuteWhenInterrupted(muteWhenInterrupted: boolean): Promise<void> 1385 1386设置当前录制音频流是否启用[静音打断模式](../../media/audio/using-audiocapturer-for-recording.md#设置静音打断模式)。使用Promise异步回调。 1387 1388**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1389 1390**参数:** 1391 1392| 参数名 | 类型 | 必填 | 说明 | 1393| ---------- |---------------- | ------ |---------------------------------------------------------| 1394| muteWhenInterrupted | boolean | 是 | 设置当前录制音频流是否启用静音打断模式, true表示启用,false表示不启用,保持为默认打断模式。 | 1395 1396**返回值:** 1397 1398| 类型 | 说明 | 1399| ------------------- | ----------------------------- | 1400| Promise<void>| Promise对象。无返回结果的Promise对象。| 1401 1402**错误码:** 1403 1404以下错误码的详细介绍请参见[Audio错误码](errorcode-audio.md)。 1405 1406| 错误码ID | 错误信息 | 1407| ------- | --------------------------------------------| 1408| 6800103 | Operation not permitted at current state. | 1409 1410**示例:** 1411 1412```ts 1413import { BusinessError } from '@kit.BasicServicesKit'; 1414 1415audioCapturer.setWillMuteWhenInterrupted(true).then(() => { 1416 console.info('setWillMuteWhenInterrupted Success!'); 1417}).catch((err: BusinessError) => { 1418 console.error(`setWillMuteWhenInterrupted Fail: ${err}`); 1419}); 1420``` 1421 1422## read<sup>(deprecated)</sup> 1423 1424read(size: number, isBlockingRead: boolean, callback: AsyncCallback<ArrayBuffer\>): void 1425 1426读入缓冲区。使用callback异步回调。 1427 1428> **说明:** 1429> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('readData')](#onreaddata11)替代。 1430 1431**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1432 1433**参数:** 1434 1435| 参数名 | 类型 | 必填 | 说明 | 1436| :------------- | :-------------------------- | :--- | :------------------------------- | 1437| size | number | 是 | 读入的字节数。 | 1438| isBlockingRead | boolean | 是 | 是否阻塞读操作。true表示阻塞,false表示不阻塞。 | 1439| callback | AsyncCallback<ArrayBuffer\> | 是 | 回调函数。当读入缓冲区成功,err为undefined,data为获取到的缓冲区;否则为错误对象。 | 1440 1441**示例:** 1442 1443```ts 1444import { BusinessError } from '@kit.BasicServicesKit'; 1445 1446audioCapturer.getBufferSize().then((bufferSize: number) => { 1447 console.info('Succeeded in doing getBufferSize.'); 1448 audioCapturer.read(bufferSize, true, (err: BusinessError, buffer: ArrayBuffer) => { 1449 if (err) { 1450 console.error(`Failed to read. Code: ${err.code}, message: ${err.message}`); 1451 return; 1452 } 1453 console.info('Succeeded in doing read.'); 1454 }); 1455}).catch((err: BusinessError) => { 1456 console.error(`Failed to getBufferSize. Code: ${err.code}, message: ${err.message}`); 1457}); 1458``` 1459 1460## read<sup>(deprecated)</sup> 1461 1462read(size: number, isBlockingRead: boolean): Promise<ArrayBuffer\> 1463 1464读入缓冲区。使用Promise异步回调。 1465 1466> **说明:** 1467> 从API version 8开始支持,从API version 11开始废弃,建议使用[on('readData')](#onreaddata11)替代。 1468 1469**系统能力:** SystemCapability.Multimedia.Audio.Capturer 1470 1471**参数:** 1472 1473| 参数名 | 类型 | 必填 | 说明 | 1474| :------------- | :------ | :--- | :--------------- | 1475| size | number | 是 | 读入的字节数。 | 1476| isBlockingRead | boolean | 是 | 是否阻塞读操作。true表示阻塞,false表示不阻塞。 | 1477 1478**返回值:** 1479 1480| 类型 | 说明 | 1481| :-------------------- | :----------------------------------------------------- | 1482| Promise<ArrayBuffer\> | Promise对象,返回读取的缓冲区数据。 | 1483 1484**示例:** 1485 1486```ts 1487import { BusinessError } from '@kit.BasicServicesKit'; 1488 1489audioCapturer.getBufferSize().then((bufferSize: number) => { 1490 console.info('Succeeded in doing getBufferSize.'); 1491 audioCapturer.read(bufferSize, true).then((buffer: ArrayBuffer) => { 1492 console.info('Succeeded in doing read.'); 1493 }).catch((err: BusinessError) => { 1494 console.error(`Failed to read. Code: ${err.code}, message: ${err.message}`); 1495 }); 1496}).catch((err: BusinessError) => { 1497 console.error(`Failed to getBufferSize. Code: ${err.code}, message: ${err.message}`); 1498}); 1499``` 1500