1# Interface (AVRecorder) 2<!--Kit: Media Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @shiwei75--> 5<!--Designer: @HmQQQ--> 6<!--Tester: @xdlinc--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12> - 本Interface首批接口从API version 9开始支持。 13 14音视频录制管理类,用于音视频媒体录制。在调用AVRecorder的方法前,需要先通过[createAVRecorder()](arkts-apis-media-f.md#mediacreateavrecorder9)构建一个AVRecorder实例。 15 16音视频录制demo可参考:[音频录制开发指导](../../media/media/using-avrecorder-for-recording.md)、[视频录制开发指导](../../media/media/video-recording.md)。 17 18> **说明:** 19> 20> 使用相机进行视频录制时,需要与相机模块配合,相机模块接口的使用详情见[相机管理](../apis-camera-kit/arkts-apis-camera.md)。 21 22## 导入模块 23 24```ts 25import { media } from '@kit.MediaKit'; 26``` 27 28## 属性 29 30**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 31 32| 名称 | 类型 | 只读 | 可选 | 说明 | 33| ------- | ------------------------------------ | ---- | ---- | ------------------ | 34| state9+ | [AVRecorderState](arkts-apis-media-t.md#avrecorderstate9) | 是 | 否 | 音视频录制的状态。<br>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 | 35 36## prepare<sup>9+</sup> 37 38prepare(config: AVRecorderConfig, callback: AsyncCallback\<void>): void 39 40异步方式进行音视频录制的参数设置。通过注册回调函数获取返回值。 41 42**需要权限:** ohos.permission.MICROPHONE 43 44不涉及音频录制时,可以不需要获取ohos.permission.MICROPHONE权限。 45 46使用相机视频录制还需要与相机模块配合,相机模块接口的使用详情见[相机管理](../apis-camera-kit/arkts-apis-camera.md)。 47 48**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 49 50**参数:** 51 52| 参数名 | 类型 | 必填 | 说明 | 53| -------- | -------------------------------------- | ---- | ------------------------------------- | 54| config | [AVRecorderConfig](arkts-apis-media-i.md#avrecorderconfig9) | 是 | 配置音视频录制的相关参数。 | 55| callback | AsyncCallback\<void> | 是 | 回调函数。当prepare方法成功,err为undefined,否则为错误对象。 | 56 57**错误码:** 58 59以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[媒体错误码](errorcode-media.md)。 60 61| 错误码ID | 错误信息 | 62| -------- | --------------------------------------- | 63| 201 | Permission denied. Return by callback. | 64| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 65| 5400102 | Operate not permit. Return by callback. | 66| 5400105 | Service died. Return by callback. | 67 68**示例:** 69 70```ts 71import { BusinessError } from '@kit.BasicServicesKit'; 72 73// 配置参数以实际硬件设备支持的范围为准。 74let avRecorderProfile: media.AVRecorderProfile = { 75 audioBitrate : 48000, 76 audioChannels : 2, 77 audioCodec : media.CodecMimeType.AUDIO_AAC, 78 audioSampleRate : 48000, 79 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 80 videoBitrate : 2000000, 81 videoCodec : media.CodecMimeType.VIDEO_AVC, 82 videoFrameWidth : 640, 83 videoFrameHeight : 480, 84 videoFrameRate : 30 85}; 86let videoMetaData: media.AVMetadata = { 87 videoOrientation: '0' // 合理值0、90、180、270,非合理值prepare接口报错。 88}; 89let avRecorderConfig: media.AVRecorderConfig = { 90 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 91 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 92 profile : avRecorderProfile, 93 url : 'fd://', // 文件需先由调用者创建,赋予读写权限,将文件fd传给此参数,eg.fd://45 94 metadata: videoMetaData, 95 location : { latitude : 30, longitude : 130 } 96}; 97 98avRecorder.prepare(avRecorderConfig, (err: BusinessError) => { 99 if (err) { 100 console.error('Failed to prepare and error is: ' + err.message); 101 } else { 102 console.info('Succeeded in preparing'); 103 } 104}); 105``` 106 107## prepare<sup>9+</sup> 108 109prepare(config: AVRecorderConfig): Promise\<void> 110 111异步方式进行音视频录制的参数设置。通过Promise获取返回值。 112 113**需要权限:** ohos.permission.MICROPHONE 114 115不涉及音频录制时,可以不需要获ohos.permission.MICROPHONE权限。 116 117使用相机视频录制还需要与相机模块配合,相机模块接口的使用详情见[相机管理](../apis-camera-kit/arkts-apis-camera.md)。 118 119**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 120 121**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 122 123**参数:** 124 125| 参数名 | 类型 | 必填 | 说明 | 126| ------ | -------------------------------------- | ---- | -------------------------- | 127| config | [AVRecorderConfig](arkts-apis-media-i.md#avrecorderconfig9) | 是 | 配置音视频录制的相关参数。 | 128 129**返回值:** 130 131| 类型 | 说明 | 132| -------------- | ------------------------------------------ | 133| Promise\<void> | 异步音视频录制prepare方法的Promise返回值。 | 134 135**错误码:** 136 137以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[媒体错误码](errorcode-media.md)。 138 139| 错误码ID | 错误信息 | 140| -------- | -------------------------------------- | 141| 201 | Permission denied. Return by promise. | 142| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 143| 5400102 | Operate not permit. Return by promise. | 144| 5400105 | Service died. Return by promise. | 145 146**示例:** 147 148```ts 149import { BusinessError } from '@kit.BasicServicesKit'; 150 151// 配置参数以实际硬件设备支持的范围为准。 152let avRecorderProfile: media.AVRecorderProfile = { 153 audioBitrate : 48000, 154 audioChannels : 2, 155 audioCodec : media.CodecMimeType.AUDIO_AAC, 156 audioSampleRate : 48000, 157 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 158 videoBitrate : 2000000, 159 videoCodec : media.CodecMimeType.VIDEO_AVC, 160 videoFrameWidth : 640, 161 videoFrameHeight : 480, 162 videoFrameRate : 30 163}; 164let videoMetaData: media.AVMetadata = { 165 videoOrientation: '0' // 合理值0、90、180、270,非合理值prepare接口报错。 166}; 167let avRecorderConfig: media.AVRecorderConfig = { 168 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 169 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 170 profile : avRecorderProfile, 171 url : 'fd://', // 文件需先由调用者创建,赋予读写权限,将文件fd传给此参数,eg.fd://45 172 metadata : videoMetaData, 173 location : { latitude : 30, longitude : 130 } 174}; 175 176avRecorder.prepare(avRecorderConfig).then(() => { 177 console.info('Succeeded in preparing'); 178}).catch((err: Error) => { 179 let error: BusinessError = err as BusinessError; 180 console.error('Failed to prepare and error is: ' + error.message); 181}); 182``` 183 184## getInputSurface<sup>9+</sup> 185 186getInputSurface(callback: AsyncCallback\<string>): void 187 188异步方式获得录制需要的surface。通过注册回调函数获取返回值。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的视频数据。 189 190应当注意,填入的视频数据需要携带时间戳(单位ns)和buffersize。时间戳的起始时间请以系统启动时间为基准。 191 192需在[prepare()](#prepare9)事件成功触发后,才能调用getInputSurface()方法。 193 194**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 195 196**参数:** 197 198| 参数名 | 类型 | 必填 | 说明 | 199| -------- | ---------------------- | ---- | --------------------------- | 200| callback | AsyncCallback\<string> | 是 | 回调函数。当获取surface成功,err为undefined,data为获取到的surfaceId,否则为错误对象。 | 201 202**错误码:** 203 204以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 205 206| 错误码ID | 错误信息 | 207| -------- | --------------------------------------- | 208| 5400102 | Operate not permit. Return by callback. | 209| 5400103 | IO error. Return by callback. | 210| 5400105 | Service died. Return by callback. | 211 212**示例:** 213 214```ts 215import { BusinessError } from '@kit.BasicServicesKit'; 216let surfaceID: string; // 该surfaceID用于传递给相机接口创造videoOutput。 217 218avRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 219 if (err) { 220 console.error('Failed to do getInputSurface and error is: ' + err.message); 221 } else { 222 console.info('Succeeded in doing getInputSurface'); 223 surfaceID = surfaceId; 224 } 225}); 226 227``` 228 229## getInputSurface<sup>9+</sup> 230 231getInputSurface(): Promise\<string> 232 233异步方式获得录制需要的surface。通过Promise获取返回值。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的视频数据。 234 235应当注意,填入的视频数据需要携带时间戳(单位ns)和buffersize。时间戳的起始时间请以系统启动时间为基准。 236 237需在[prepare()](#prepare9-1)事件成功触发后,才能调用getInputSurface方法。 238 239**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 240 241**返回值:** 242 243| 类型 | 说明 | 244| ---------------- | -------------------------------- | 245| Promise\<string> | 异步获得surface的Promise返回值。 | 246 247**错误码:** 248 249以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 250 251| 错误码ID | 错误信息 | 252| -------- | -------------------------------------- | 253| 5400102 | Operate not permit. Return by promise. | 254| 5400103 | IO error. Return by promise. | 255| 5400105 | Service died. Return by promise. | 256 257**示例:** 258 259```ts 260import { BusinessError } from '@kit.BasicServicesKit'; 261let surfaceID: string; // 该surfaceID用于传递给相机接口创造videoOutput。 262 263avRecorder.getInputSurface().then((surfaceId: string) => { 264 console.info('Succeeded in getting InputSurface'); 265 surfaceID = surfaceId; 266}).catch((err: Error) => { 267 let error: BusinessError = err as BusinessError; 268 console.error('Failed to get InputSurface and error is: ' + error.message); 269}); 270``` 271 272## updateRotation<sup>12+</sup> 273 274updateRotation(rotation: number): Promise\<void> 275 276更新视频旋转角度。通过Promise获取返回值。 277 278当且仅当[prepare()](#prepare9-1)事件成功触发后,且在[start()](#start9)之前,才能调用updateRotation方法。 279 280**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 281 282**参数:** 283 284| 参数名 | 类型 | 必填 | 说明 | 285| -------- | -------------------- | ---- | --------------------------- | 286| rotation | number | 是 | 旋转角度,取值仅支持0、90、180、270度。 | 287 288**返回值:** 289 290| 类型 | 说明 | 291| ---------------- | -------------------------------- | 292| Promise\<void> | 异步返回函数执行结果。 | 293 294**错误码:** 295 296以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[媒体错误码](errorcode-media.md)。 297 298| 错误码ID | 错误信息 | 299| -------- | -------------------------------------- | 300| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 301| 5400102 | Operation not allowed. Return by promise. | 302| 5400103 | IO error. Return by promise. | 303| 5400105 | Service died. Return by promise. | 304 305**示例:** 306 307```ts 308import { BusinessError } from '@kit.BasicServicesKit'; 309 310let rotation = 90; 311 312avRecorder.updateRotation(rotation).then(() => { 313 console.info('Succeeded in doing updateRotation'); 314}).catch((err: Error) => { 315 let error: BusinessError = err as BusinessError; 316 console.error('Failed to do updateRotation and error is: ' + error.message); 317}); 318``` 319 320## setWillMuteWhenInterrupted<sup>20+</sup> 321 322setWillMuteWhenInterrupted(muteWhenInterrupted: boolean): Promise<void> 323 324设置当前录制音频流是否启用静音打断模式。使用Promise异步回调。 325 326**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 327 328**参数:** 329 330| 参数名 | 类型 | 必填 | 说明 | 331| ---------- |---------------- | ------ |---------------------------------------------------------| 332| muteWhenInterrupted | boolean | 是 | 设置当前录制音频流是否启用静音打断模式, true表示启用,false表示不启用,保持为默认打断模式。 | 333 334**返回值:** 335 336| 类型 | 说明 | 337| ------------------- | ----------------------------- | 338| Promise<void>| Promise对象。无返回结果的Promise对象。| 339 340**错误码:** 341 342以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 343 344| 错误码ID | 错误信息 | 345| -------- | -------------------------------------- | 346| 5400102 | Operation not allowed. Return by promise. | 347| 5400105 | Service died. Return by promise. | 348 349**示例:** 350 351```ts 352import { BusinessError } from '@kit.BasicServicesKit'; 353 354avRecorder.setWillMuteWhenInterrupted(true).then(() => { 355 console.info('Succeeded in doing setWillMuteWhenInterrupted'); 356}).catch((err: Error) => { 357 let error: BusinessError = err as BusinessError; 358 console.error('Failed to do setWillMuteWhenInterrupted and error is: ' + error.message); 359}); 360``` 361 362## start<sup>9+</sup> 363 364start(callback: AsyncCallback\<void>): void 365 366异步方式开始视频录制。通过注册回调函数获取返回值。 367 368纯音频录制需在[prepare()](#prepare9)事件成功触发后,才能调用start方法。纯视频录制,音视频录制需在[getInputSurface()](#getinputsurface9)事件成功触发后,才能调用start方法。 369 370**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| -------- | -------------------- | ---- | ---------------------------- | 376| callback | AsyncCallback\<void> | 是 |回调函数。当开始录制视频成功,err为undefined,否则为错误对象。 | 377 378**错误码:** 379 380以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 381 382| 错误码ID | 错误信息 | 383| -------- | --------------------------------------- | 384| 5400102 | Operate not permit. Return by callback. | 385| 5400103 | IO error. Return by callback. | 386| 5400105 | Service died. Return by callback. | 387 388**示例:** 389 390```ts 391import { BusinessError } from '@kit.BasicServicesKit'; 392 393avRecorder.start((err: BusinessError) => { 394 if (err) { 395 console.error('Failed to start AVRecorder and error is: ' + err.message); 396 } else { 397 console.info('Succeeded in starting AVRecorder'); 398 } 399}); 400``` 401 402## start<sup>9+</sup> 403 404start(): Promise\<void> 405 406异步方式开始视频录制。通过Promise获取返回值。 407 408纯音频录制需在[prepare()](#prepare9-1)事件成功触发后,才能调用start方法。纯视频录制,音视频录制需在[getInputSurface()](#getinputsurface9-1)事件成功触发后,才能调用start方法。 409 410**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 411 412**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 413 414**返回值:** 415 416| 类型 | 说明 | 417| -------------- | ------------------------------------- | 418| Promise\<void> | 异步开始视频录制方法的Promise返回值。 | 419 420**错误码:** 421 422以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 423 424| 错误码ID | 错误信息 | 425| -------- | -------------------------------------- | 426| 5400102 | Operate not permit. Return by promise. | 427| 5400103 | IO error. Return by promise. | 428| 5400105 | Service died. Return by promise. | 429 430**示例:** 431 432```ts 433import { BusinessError } from '@kit.BasicServicesKit'; 434 435avRecorder.start().then(() => { 436 console.info('Succeeded in starting AVRecorder'); 437}).catch((err: Error) => { 438 let error: BusinessError = err as BusinessError; 439 console.error('Failed to start AVRecorder and error is: ' + error.message); 440}); 441``` 442 443## pause<sup>9+</sup> 444 445pause(callback: AsyncCallback\<void>): void 446 447异步方式暂停视频录制。通过注册回调函数获取返回值。 448 449需要[start()](#start9)事件成功触发后,才能调用pause方法,可以通过调用[resume()](#resume9)接口来恢复录制。 450 451**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 452 453**参数:** 454 455| 参数名 | 类型 | 必填 | 说明 | 456| -------- | -------------------- | ---- | --------------------------- | 457| callback | AsyncCallback\<void> | 是 | 回调函数。当暂停视频录制成功,err为undefined,否则为错误对象。 | 458 459**错误码:** 460 461以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 462 463| 错误码ID | 错误信息 | 464| -------- | --------------------------------------- | 465| 5400102 | Operate not permit. Return by callback. | 466| 5400103 | IO error. Return by callback. | 467| 5400105 | Service died. Return by callback. | 468 469**示例:** 470 471```ts 472import { BusinessError } from '@kit.BasicServicesKit'; 473 474avRecorder.pause((err: BusinessError) => { 475 if (err) { 476 console.error('Failed to pause AVRecorder and error is: ' + err.message); 477 } else { 478 console.info('Succeeded in pausing'); 479 } 480}); 481``` 482 483## pause<sup>9+</sup> 484 485pause(): Promise\<void> 486 487异步方式暂停视频录制。通过Promise获取返回值。 488 489需要[start()](#start9-1)事件成功触发后,才能调用pause方法,可以通过调用[resume()](#resume9-1)接口来恢复录制。 490 491**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 492 493**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 494 495**返回值:** 496 497| 类型 | 说明 | 498| -------------- | ------------------------------------- | 499| Promise\<void> | 异步暂停视频录制方法的Promise返回值。 | 500 501**错误码:** 502 503以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 504 505| 错误码ID | 错误信息 | 506| -------- | -------------------------------------- | 507| 5400102 | Operate not permit. Return by promise. | 508| 5400103 | IO error. Return by promise. | 509| 5400105 | Service died. Return by promise. | 510 511**示例:** 512 513```ts 514import { BusinessError } from '@kit.BasicServicesKit'; 515 516avRecorder.pause().then(() => { 517 console.info('Succeeded in pausing'); 518}).catch((err: Error) => { 519 let error: BusinessError = err as BusinessError; 520 console.error('Failed to pause AVRecorder and error is: ' + error.message); 521}); 522``` 523 524## resume<sup>9+</sup> 525 526resume(callback: AsyncCallback\<void>): void 527 528异步方式恢复视频录制。通过注册回调函数获取返回值。 529 530需要在[pause()](#pause9)事件成功触发后,才能调用resume方法。 531 532**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 533 534**参数:** 535 536| 参数名 | 类型 | 必填 | 说明 | 537| -------- | -------------------- | ---- | ---------------------------- | 538| callback | AsyncCallback\<void> | 是 | 回调函数。当恢复视频录制成功,err为undefined,否则为错误对象。 | 539 540**错误码:** 541 542以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 543 544| 错误码ID | 错误信息 | 545| -------- | --------------------------------------- | 546| 5400102 | Operate not permit. Return by callback. | 547| 5400103 | IO error. Return by callback. | 548| 5400105 | Service died. Return by callback. | 549 550**示例:** 551 552```ts 553import { BusinessError } from '@kit.BasicServicesKit'; 554 555avRecorder.resume((err: BusinessError) => { 556 if (err) { 557 console.error('Failed to resume AVRecorder and error is: ' + err.message); 558 } else { 559 console.info('Succeeded in resuming AVRecorder'); 560 } 561}); 562``` 563 564## resume<sup>9+</sup> 565 566resume(): Promise\<void> 567 568异步方式恢复视频录制。通过Promise获取返回值。 569 570需要在[pause()](#pause9-1)事件成功触发后,才能调用resume方法。 571 572**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 573 574**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 575 576**返回值:** 577 578| 类型 | 说明 | 579| -------------- | ------------------------------------- | 580| Promise\<void> | 异步恢复视频录制方法的Promise返回值。 | 581 582**错误码:** 583 584以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 585 586| 错误码ID | 错误信息 | 587| -------- | -------------------------------------- | 588| 5400102 | Operate not permit. Return by promise. | 589| 5400103 | IO error. Return by promise. | 590| 5400105 | Service died. Return by promise. | 591 592**示例:** 593 594```ts 595import { BusinessError } from '@kit.BasicServicesKit'; 596 597avRecorder.resume().then(() => { 598 console.info('Succeeded in resuming AVRecorder'); 599}).catch((err: Error) => { 600 let error: BusinessError = err as BusinessError; 601 console.error('Failed to resume AVRecorder failed and error is: ' + error.message); 602}); 603``` 604 605## stop<sup>9+</sup> 606 607stop(callback: AsyncCallback\<void>): void 608 609异步方式停止视频录制。通过注册回调函数获取返回值。 610 611需要在[start()](#start9)或[pause()](#pause9)事件成功触发后,才能调用stop方法。 612 613纯音频录制时,需要重新调用[prepare()](#prepare9)接口才能重新录制。纯视频录制,音视频录制时,需要重新调用[prepare()](#prepare9)和[getInputSurface()](#getinputsurface9)接口才能重新录制。 614 615**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 616 617**参数:** 618 619| 参数名 | 类型 | 必填 | 说明 | 620| -------- | -------------------- | ---- | ---------------------------- | 621| callback | AsyncCallback\<void> | 是 | 回调函数。当停止视频录制成功,err为undefined,否则为错误对象。 | 622 623**错误码:** 624 625以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 626 627| 错误码ID | 错误信息 | 628| -------- | --------------------------------------- | 629| 5400102 | Operate not permit. Return by callback. | 630| 5400103 | IO error. Return by callback. | 631| 5400105 | Service died. Return by callback. | 632 633**示例:** 634 635```ts 636import { BusinessError } from '@kit.BasicServicesKit'; 637 638avRecorder.stop((err: BusinessError) => { 639 if (err) { 640 console.error('Failed to stop AVRecorder and error is: ' + err.message); 641 } else { 642 console.info('Succeeded in stopping AVRecorder'); 643 } 644}); 645``` 646 647## stop<sup>9+</sup> 648 649stop(): Promise\<void> 650 651异步方式停止视频录制。通过Promise获取返回值。 652 653需要在[start()](#start9-1)或[pause()](#pause9-1)事件成功触发后,才能调用stop方法。 654 655纯音频录制时,需要重新调用[prepare()](#prepare9-1)接口才能重新录制。纯视频录制,音视频录制时,需要重新调用[prepare()](#prepare9-1)和[getInputSurface()](#getinputsurface9-1)接口才能重新录制。 656 657**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 658 659**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 660 661**返回值:** 662 663| 类型 | 说明 | 664| -------------- | ------------------------------------- | 665| Promise\<void> | 异步停止视频录制方法的Promise返回值。 | 666 667**错误码:** 668 669以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 670 671| 错误码ID | 错误信息 | 672| -------- | -------------------------------------- | 673| 5400102 | Operate not permit. Return by promise. | 674| 5400103 | IO error. Return by promise. | 675| 5400105 | Service died. Return by promise. | 676 677**示例:** 678 679```ts 680import { BusinessError } from '@kit.BasicServicesKit'; 681 682avRecorder.stop().then(() => { 683 console.info('Succeeded in stopping AVRecorder'); 684}).catch((err: Error) => { 685 let error: BusinessError = err as BusinessError; 686 console.error('Failed to stop AVRecorder and error is: ' + error.message); 687}); 688``` 689 690## reset<sup>9+</sup> 691 692reset(callback: AsyncCallback\<void>): void 693 694异步方式重置音视频录制。通过注册回调函数获取返回值。 695 696纯音频录制时,需要重新调用[prepare()](#prepare9)接口才能重新录制。纯视频录制,音视频录制时,需要重新调用[prepare()](#prepare9)和[getInputSurface()](#getinputsurface9)接口才能重新录制。 697 698**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 699 700**参数:** 701 702| 参数名 | 类型 | 必填 | 说明 | 703| -------- | -------------------- | ---- | ------------------------------ | 704| callback | AsyncCallback\<void> | 是 | 回调函数。当重置音视频录制成功,err为undefined,否则为错误对象。 | 705 706**错误码:** 707 708以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 709 710| 错误码ID | 错误信息 | 711| -------- | --------------------------------- | 712| 5400103 | IO error. Return by callback. | 713| 5400105 | Service died. Return by callback. | 714 715**示例:** 716 717```ts 718import { BusinessError } from '@kit.BasicServicesKit'; 719 720avRecorder.reset((err: BusinessError) => { 721 if (err) { 722 console.error('Failed to reset AVRecorder and error is: ' + err.message); 723 } else { 724 console.info('Succeeded in resetting AVRecorder'); 725 } 726}); 727``` 728 729## reset<sup>9+</sup> 730 731reset(): Promise\<void> 732 733异步方式重置音视频录制。通过Promise获取返回值。 734 735纯音频录制时,需要重新调用[prepare()](#prepare9-1)接口才能重新录制。纯视频录制,音视频录制时,需要重新调用[prepare()](#prepare9-1)和[getInputSurface()](#getinputsurface9-1)接口才能重新录制。 736 737**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 738 739**返回值:** 740 741| 类型 | 说明 | 742| -------------- | --------------------------------------- | 743| Promise\<void> | 异步重置音视频录制方法的Promise返回值。 | 744 745**错误码:** 746 747以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 748 749| 错误码ID | 错误信息 | 750| -------- | -------------------------------- | 751| 5400103 | IO error. Return by promise. | 752| 5400105 | Service died. Return by promise. | 753 754**示例:** 755 756```ts 757import { BusinessError } from '@kit.BasicServicesKit'; 758 759avRecorder.reset().then(() => { 760 console.info('Succeeded in resetting AVRecorder'); 761}).catch((err: Error) => { 762 let error: BusinessError = err as BusinessError; 763 console.error('Failed to reset AVRecorder and error is: ' + error.message); 764}); 765``` 766 767## release<sup>9+</sup> 768 769release(callback: AsyncCallback\<void>): void 770 771异步方式释放音视频录制资源。通过注册回调函数获取返回值。 772 773释放音视频录制资源之后,该AVRecorder实例不能再进行任何操作。 774 775**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 776 777**参数:** 778 779| 参数名 | 类型 | 必填 | 说明 | 780| -------- | -------------------- | ---- | ---------------------------------- | 781| callback | AsyncCallback\<void> | 是 | 回调函数。当释放音视频录制资源成功,err为undefined,否则为错误对象。 | 782 783**错误码:** 784 785以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 786 787| 错误码ID | 错误信息 | 788| -------- | --------------------------------- | 789| 5400105 | Service died. Return by callback. | 790 791**示例:** 792 793```ts 794import { BusinessError } from '@kit.BasicServicesKit'; 795 796avRecorder.release((err: BusinessError) => { 797 if (err) { 798 console.error('Failed to release AVRecorder and error is: ' + err.message); 799 } else { 800 console.info('Succeeded in releasing AVRecorder'); 801 } 802}); 803``` 804 805## release<sup>9+</sup> 806 807release(): Promise\<void> 808 809异步方式释放音视频录制资源。通过Promise获取返回值。 810 811释放音视频录制资源之后,该AVRecorder实例不能再进行任何操作。 812 813**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。 814 815**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 816 817**返回值:** 818 819| 类型 | 说明 | 820| -------------- | ------------------------------------------- | 821| Promise\<void> | 异步释放音视频录制资源方法的Promise返回值。 | 822 823**错误码:** 824 825以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 826 827| 错误码ID | 错误信息 | 828| -------- | --------------------------------- | 829| 5400105 | Service died. Return by callback. | 830 831**示例:** 832 833```ts 834import { BusinessError } from '@kit.BasicServicesKit'; 835 836avRecorder.release().then(() => { 837 console.info('Succeeded in releasing AVRecorder'); 838}).catch((err: Error) => { 839 let error: BusinessError = err as BusinessError; 840 console.error('Failed to release AVRecorder and error is: ' + error.message); 841}); 842``` 843 844## getCurrentAudioCapturerInfo<sup>11+</sup> 845 846getCurrentAudioCapturerInfo(callback: AsyncCallback\<audio.AudioCapturerChangeInfo>): void 847 848异步方式获取当前音频采集参数。通过注册回调函数获取返回值。 849 850在prepare()成功触发后,才能调用此方法。在stop()成功触发后,调用此方法会报错。 851 852**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 853 854**参数**: 855 856| 参数名 | 类型 | 必填 | 说明 | 857| -------- | ------------------------------------------------------------ | ---- | ------------------------------------ | 858| callback | AsyncCallback\<[audio.AudioCapturerChangeInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | 是 | 回调函数。当获取音频采集参数成功时,err为undefined,data为获取到的audio.AudioCapturerChangeInfo,否则为错误对象。 | 859 860**错误码**: 861 862以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 863 864| 错误码ID | 错误信息 | 865| -------- | ------------------------------------------ | 866| 5400102 | Operation not allowed. | 867| 5400103 | I/O error. | 868| 5400105 | Service died. Return by callback. | 869 870**示例**: 871 872```ts 873import { BusinessError } from '@kit.BasicServicesKit'; 874import { audio } from '@kit.AudioKit'; 875 876let currentCapturerInfo: audio.AudioCapturerChangeInfo; 877 878avRecorder.getCurrentAudioCapturerInfo((err: BusinessError, capturerInfo: audio.AudioCapturerChangeInfo) => { 879 if (err) { 880 console.error('Failed to get CurrentAudioCapturerInfo and error is: ' + err.message); 881 } else { 882 console.info('Succeeded in getting CurrentAudioCapturerInfo'); 883 currentCapturerInfo = capturerInfo; 884 } 885}); 886``` 887 888## getCurrentAudioCapturerInfo<sup>11+</sup> 889 890getCurrentAudioCapturerInfo(): Promise\<audio.AudioCapturerChangeInfo> 891 892异步方式获取当前音频采集参数。通过Promise获取返回值。 893 894在prepare()成功触发后,才能调用此方法。在stop()成功触发后,调用此方法会报错。 895 896**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 897 898**返回值**: 899 900| 类型 | 说明 | 901| ------------------------------------------------------------ | ------------------------------------------------- | 902| Promise\<[audio.AudioCapturerChangeInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | Promise对象,返回获取的当前音频采集参数。 | 903 904**错误码**: 905 906以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 907 908| 错误码ID | 错误信息 | 909| -------- | -------------------------------- | 910| 5400102 | Operation not allowed. | 911| 5400103 | I/O error. | 912| 5400105 | Service died. Return by promise. | 913 914**示例**: 915 916```ts 917import { BusinessError } from '@kit.BasicServicesKit'; 918import { audio } from '@kit.AudioKit'; 919 920let currentCapturerInfo: audio.AudioCapturerChangeInfo; 921 922avRecorder.getCurrentAudioCapturerInfo().then((capturerInfo: audio.AudioCapturerChangeInfo) => { 923 console.info('Succeeded in getting CurrentAudioCapturerInfo'); 924 currentCapturerInfo = capturerInfo; 925}).catch((err: Error) => { 926 let error: BusinessError = err as BusinessError; 927 console.error('Failed to get CurrentAudioCapturerInfo and error is: ' + error.message); 928}); 929``` 930 931## getAudioCapturerMaxAmplitude<sup>11+</sup> 932 933getAudioCapturerMaxAmplitude(callback: AsyncCallback\<number>): void 934 935异步方式获取当前音频最大振幅。通过注册回调函数获取返回值。 936 937在prepare()成功触发后,才能调用此方法。在stop()成功触发后,调用此方法会报错。 938 939调用接口时,获取到的返回值是上一次获取最大振幅的时刻到当前这段区间内的音频最大振幅。即,如果在1s的时刻获取了一次最大振幅,在2s时再获取到的最大振幅时1-2s这个区间里面的最大值。 940 941**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 942 943**参数**: 944 945| 参数名 | 类型 | 必填 | 说明 | 946| -------- | ---------------------- | ---- | ------------------------------------ | 947| callback | AsyncCallback\<number> | 是 | 回调函数。获取当前音频最大振幅成功时,err为undefined,data为获取到的最大振幅,否则为错误对象。 | 948 949**错误码**: 950 951以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 952 953| 错误码ID | 错误信息 | 954| -------- | ------------------------------------------ | 955| 5400102 | Operation not allowed. | 956| 5400105 | Service died. Return by callback. | 957 958**示例**: 959 960```ts 961import { BusinessError } from '@kit.BasicServicesKit'; 962 963let maxAmplitude: number; 964 965avRecorder.getAudioCapturerMaxAmplitude((err: BusinessError, amplitude: number) => { 966 if (err) { 967 console.error('Failed to get AudioCapturerMaxAmplitude and error is: ' + err.message); 968 } else { 969 console.info('Succeeded in getting AudioCapturerMaxAmplitude'); 970 maxAmplitude = amplitude; 971 } 972}); 973``` 974 975## getAudioCapturerMaxAmplitude<sup>11+</sup> 976 977getAudioCapturerMaxAmplitude(): Promise\<number> 978 979异步方式获取当前音频最大振幅参数。通过Promise获取返回值。 980 981在prepare()成功触发后,才能调用此方法。在stop()成功触发后,调用此方法会报错。 982 983调用接口时,获取到的返回值是上一次获取最大振幅的时刻到当前这段区间内的音频最大振幅。即,如果在1s的时刻获取了一次最大振幅,在2s时再获取到的最大振幅时1-2s这个区间里面的最大值。 984 985**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 986 987**返回值**: 988 989| 类型 | 说明 | 990| ---------------- | ------------------------------------------------- | 991| Promise\<number> | Promise对象,返回获取的当前音频最大振幅。 | 992 993**错误码**: 994 995以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 996 997| 错误码ID | 错误信息 | 998| -------- | -------------------------------- | 999| 5400102 | Operation not allowed. | 1000| 5400105 | Service died. Return by promise. | 1001 1002**示例**: 1003 1004```ts 1005import { BusinessError } from '@kit.BasicServicesKit'; 1006 1007let maxAmplitude: number; 1008 1009avRecorder.getAudioCapturerMaxAmplitude().then((amplitude: number) => { 1010 console.info('Succeeded in getting AudioCapturerMaxAmplitude'); 1011 maxAmplitude = amplitude; 1012}).catch((err: Error) => { 1013 let error: BusinessError = err as BusinessError; 1014 console.error('Failed to get AudioCapturerMaxAmplitude and error is: ' + error.message); 1015}); 1016``` 1017 1018## getAvailableEncoder<sup>11+</sup> 1019 1020getAvailableEncoder(callback: AsyncCallback\<Array\<EncoderInfo>>): void 1021 1022异步方式获取可用的编码器参数。通过注册回调函数获取返回值。 1023 1024**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 1025 1026**参数**: 1027 1028| 参数名 | 类型 | 必填 | 说明 | 1029| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 1030| callback | AsyncCallback\<Array\<[EncoderInfo](arkts-apis-media-i.md#encoderinfo11)>> | 是 | 回调函数。获取可用的编码器参数成功时,err为undefined,data为获取到的编码器参数,否则为错误对象。 | 1031 1032**错误码**: 1033 1034以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1035 1036| 错误码ID | 错误信息 | 1037| -------- | ------------------------------------------ | 1038| 5400102 | Operation not allowed. | 1039| 5400105 | Service died. Return by callback. | 1040 1041**示例**: 1042 1043```ts 1044import { BusinessError } from '@kit.BasicServicesKit'; 1045 1046let encoderInfo: media.EncoderInfo; 1047 1048avRecorder.getAvailableEncoder((err: BusinessError, info: media.EncoderInfo[]) => { 1049 if (err) { 1050 console.error('Failed to get AvailableEncoder and error is: ' + err.message); 1051 } else { 1052 console.info('Succeeded in getting AvailableEncoder'); 1053 if (info.length > 0) { 1054 encoderInfo = info[0]; 1055 } else { 1056 console.error('No available encoder'); 1057 } 1058 } 1059}); 1060``` 1061 1062## getAvailableEncoder<sup>11+</sup> 1063 1064getAvailableEncoder(): Promise\<Array\<EncoderInfo>> 1065 1066异步方式获取可用的编码器参数。通过Promise获取返回值。 1067 1068**系统能力**:SystemCapability.Multimedia.Media.AVRecorder 1069 1070**返回值**: 1071 1072| 类型 | 说明 | 1073| ----------------------------------------------- | ----------------------------------------------- | 1074| Promise\<Array\<[EncoderInfo](arkts-apis-media-i.md#encoderinfo11)>> | Promise对象,返回获取的可用的编码器参数。 | 1075 1076**错误码**: 1077 1078以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1079 1080| 错误码ID | 错误信息 | 1081| -------- | -------------------------------- | 1082| 5400102 | Operation not allowed. | 1083| 5400105 | Service died. Return by promise. | 1084 1085**示例**: 1086 1087```ts 1088import { BusinessError } from '@kit.BasicServicesKit'; 1089 1090let encoderInfo: media.EncoderInfo; 1091 1092avRecorder.getAvailableEncoder().then((info: media.EncoderInfo[]) => { 1093 console.info('Succeeded in getting AvailableEncoder'); 1094 if (info.length > 0) { 1095 encoderInfo = info[0]; 1096 } else { 1097 console.error('No available encoder'); 1098 } 1099}).catch((err: Error) => { 1100 let error: BusinessError = err as BusinessError; 1101 console.error('Failed to get AvailableEncoder and error is: ' + error.message); 1102}); 1103``` 1104 1105## getAVRecorderConfig<sup>11+</sup> 1106 1107getAVRecorderConfig(callback: AsyncCallback\<AVRecorderConfig>): void 1108 1109异步方式获取实时的配置参数。通过注册回调函数获取返回值。 1110 1111只能在[prepare()](#prepare9)接口调用后调用。 1112 1113**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1114 1115**参数:** 1116 1117| 参数名 | 类型 | 必填 | 说明 | 1118| -------- | ---------------------- | ---- | --------------------------- | 1119| callback | AsyncCallback\<[AVRecorderConfig](arkts-apis-media-i.md#avrecorderconfig9)> | 是 | 回调函数。获取实时配置的参数成功时,err为undefined,data为获取到的配置参数,否则为错误对象。 | 1120 1121**错误码:** 1122 1123以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1124 1125| 错误码ID | 错误信息 | 1126| -------- | ------------------------------------------ | 1127| 5400102 | Operate not permit. Return by callback. | 1128| 5400103 | IO error. Return by callback. | 1129| 5400105 | Service died. Return by callback. | 1130 1131**示例:** 1132 1133```ts 1134import { BusinessError } from '@kit.BasicServicesKit'; 1135 1136let avConfig: media.AVRecorderConfig; 1137 1138avRecorder.getAVRecorderConfig((err: BusinessError, config: media.AVRecorderConfig) => { 1139 if (err) { 1140 console.error('Failed to get avConfig and error is: ' + err.message); 1141 } else { 1142 console.info('Succeeded in getting AVRecorderConfig'); 1143 avConfig = config; 1144 } 1145}); 1146``` 1147 1148## getAVRecorderConfig<sup>11+</sup> 1149 1150getAVRecorderConfig(): Promise\<AVRecorderConfig>; 1151 1152异步方式获取实时的配置参数。通过Promise获取返回值。 1153 1154只能在[prepare()](#prepare9-1)接口调用后调用。 1155 1156**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1157 1158**返回值:** 1159 1160| 类型 | 说明 | 1161| ---------------- | -------------------------------- | 1162| Promise\<[AVRecorderConfig](arkts-apis-media-i.md#avrecorderconfig9)> | 异步获得实时配置参数的回调方法。 | 1163 1164**错误码:** 1165 1166以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1167 1168| 错误码ID | 错误信息 | 1169| -------- | ----------------------------------------- | 1170| 5400102 | Operate not permit. Return by promise. | 1171| 5400103 | IO error. Return by promise. | 1172| 5400105 | Service died. Return by promise. | 1173 1174**示例:** 1175 1176```ts 1177import { BusinessError } from '@kit.BasicServicesKit'; 1178 1179let avConfig: media.AVRecorderConfig; 1180 1181avRecorder.getAVRecorderConfig().then((config: media.AVRecorderConfig) => { 1182 console.info('Succeeded in getting AVRecorderConfig'); 1183 avConfig = config; 1184}).catch((err: Error) => { 1185 let error: BusinessError = err as BusinessError; 1186 console.error('Failed to get AVRecorderConfig and error is: ' + error.message); 1187}); 1188``` 1189 1190## on('stateChange')<sup>9+</sup> 1191 1192on(type: 'stateChange', callback: OnAVRecorderStateChangeHandler): void 1193 1194订阅录制状态机AVRecorderState切换的事件,当 AVRecorderState状态机发生变化时,会通过订阅的回调方法通知用户。用户只能订阅一个状态机切换事件的回调方法,当用户重复订阅时,以最后一次订阅的回调接口为准。 1195 1196**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1197 1198**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1199 1200**参数:** 1201 1202| 参数名 | 类型 | 必填 | 说明 | 1203| -------- | -------- | ---- | ------------------------------------------------------------ | 1204| type | string | 是 | 状态机切换事件回调类型,支持的事件:'stateChange',用户操作和系统都会触发此事件。 | 1205| callback | [OnAVRecorderStateChangeHandler](arkts-apis-media-t.md#onavrecorderstatechangehandler12) | 是 | 状态机切换事件回调方法。 | 1206 1207**错误码:** 1208 1209以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1210 1211| 错误码ID | 错误信息 | 1212| -------- | --------------------------------- | 1213| 5400103 | IO error. Return by callback. | 1214| 5400105 | Service died. Return by callback. | 1215 1216**示例:** 1217 1218```ts 1219avRecorder.on('stateChange', async (state: media.AVRecorderState, reason: media.StateChangeReason) => { 1220 console.info('case state has changed, new state is: ' + state + ', and reason is: ' + reason); 1221}); 1222``` 1223 1224## off('stateChange')<sup>9+</sup> 1225 1226off(type: 'stateChange', callback?: OnAVRecorderStateChangeHandler): void 1227 1228取消订阅播放状态机[AVRecorderState](arkts-apis-media-t.md#avrecorderstate9)切换的事件。 1229 1230**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1231 1232**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1233 1234**参数:** 1235 1236| 参数名 | 类型 | 必填 | 说明 | 1237| ------ | ------ | ---- | ------------------------------------------------------------ | 1238| type | string | 是 | 状态机切换事件回调类型,支持的事件:'stateChange',用户操作和系统都会触发此事件。 | 1239| callback | [OnAVRecorderStateChangeHandler](arkts-apis-media-t.md#onavrecorderstatechangehandler12) | 否 | 状态机切换事件回调方法。<br/>从API version 12开始支持此参数。 | 1240 1241**示例:** 1242 1243```ts 1244avRecorder.off('stateChange'); 1245``` 1246 1247## on('error')<sup>9+</sup> 1248 1249on(type: 'error', callback: ErrorCallback): void 1250 1251订阅AVRecorder的错误事件,该事件仅用于错误提示,不需要用户停止播控动作。如果此时[AVRecorderState](arkts-apis-media-t.md#avrecorderstate9)也切至error状态,用户需要通过[reset()](#reset9)或者[release()](#release9)退出录制操作。 1252 1253用户只能订阅一个错误事件的回调方法,当用户重复订阅时,以最后一次订阅的回调接口为准。 1254 1255**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1256 1257**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1258 1259**参数:** 1260 1261| 参数名 | 类型 | 必填 | 说明 | 1262| -------- | ------------- | ---- | ------------------------------------------------------------ | 1263| type | string | 是 | 录制错误事件回调类型'error'。 <br>- 'error':录制过程中发生错误,触发该事件。 | 1264| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 录制错误事件回调方法。 | 1265 1266**错误码:** 1267 1268以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[媒体错误码](errorcode-media.md)。 1269 1270| 错误码ID | 错误信息 | 1271| -------- | ------------------------------------------ | 1272| 201 | Permission denied. | 1273| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 1274| 801 | Capability not supported. | 1275| 5400101 | No memory. | 1276| 5400102 | Operation not allowed. | 1277| 5400103 | I/O error. | 1278| 5400104 | Time out. | 1279| 5400105 | Service died. | 1280| 5400106 | Unsupported format. | 1281| 5400107 | Audio interrupted. | 1282 1283**示例:** 1284 1285```ts 1286import { BusinessError } from '@kit.BasicServicesKit'; 1287 1288avRecorder.on('error', (err: BusinessError) => { 1289 console.info('case avRecorder.on(error) called, errMessage is ' + err.message); 1290}); 1291``` 1292 1293## off('error')<sup>9+</sup> 1294 1295off(type: 'error', callback?: ErrorCallback): void 1296 1297取消订阅录制错误事件,取消后不再接收到AVRecorder的错误事件。 1298 1299**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1300 1301**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1302 1303**参数:** 1304 1305| 参数名 | 类型 | 必填 | 说明 | 1306| ------ | ------ | ---- | ------------------------------------------------------------ | 1307| type | string | 是 | 录制错误事件回调类型'error'。 <br>- 'error':录制过程中发生错误,触发该事件。 | 1308| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 录制错误事件回调方法。<br/>从API version 12开始支持此参数。 | 1309 1310**示例:** 1311 1312```ts 1313avRecorder.off('error'); 1314``` 1315 1316## on('audioCapturerChange')<sup>11+</sup> 1317 1318on(type: 'audioCapturerChange', callback: Callback<audio.AudioCapturerChangeInfo>): void 1319 1320订阅录音配置变化的回调,任意录音配置的变化会触发变化后的录音配置全量信息回调。 1321 1322当用户重复订阅时,以最后一次订阅的回调接口为准。 1323 1324**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1325 1326**参数:** 1327 1328| 参数名 | 类型 | 必填 | 说明 | 1329| -------- | -------- | ---- | ------------------------------------------------------------ | 1330| type | string | 是 |录音配置变化的回调类型,支持的事件:'audioCapturerChange'。 | 1331| callback | Callback<[audio.AudioCapturerChangeInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | 是 | 变化后的录音配置全量信息。| 1332 1333**错误码:** 1334 1335以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1336 1337| 错误码ID | 错误信息 | 1338| -------- | ------------------------------------------ | 1339| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 1340 1341**示例:** 1342 1343```ts 1344import { audio } from '@kit.AudioKit' 1345 1346let capturerChangeInfo: audio.AudioCapturerChangeInfo; 1347 1348avRecorder.on('audioCapturerChange', (audioCapturerChangeInfo: audio.AudioCapturerChangeInfo) => { 1349 console.info('audioCapturerChange called'); 1350 capturerChangeInfo = audioCapturerChangeInfo; 1351}); 1352``` 1353 1354## off('audioCapturerChange')<sup>11+</sup> 1355 1356off(type: 'audioCapturerChange', callback?: Callback<audio.AudioCapturerChangeInfo>): void 1357 1358取消订阅录音变化的回调事件。 1359 1360**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1361 1362**参数:** 1363 1364| 参数名 | 类型 | 必填 | 说明 | 1365| ------ | ------ | ---- | ------------------------------------------------------------ | 1366| type | string | 是 | 录音配置变化的回调类型,支持的事件:'audioCapturerChange'。 | 1367| callback | Callback<[audio.AudioCapturerChangeInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiocapturerchangeinfo9)> | 否 | 变化后的录音配置全量信息。<br/>从API version 12开始支持此参数。| 1368 1369**示例:** 1370 1371```ts 1372avRecorder.off('audioCapturerChange'); 1373``` 1374 1375## on('photoAssetAvailable')<sup>12+</sup> 1376 1377on(type: 'photoAssetAvailable', callback: Callback\<photoAccessHelper.PhotoAsset>): void 1378 1379订阅媒体资源回调事件,当[FileGenerationMode](arkts-apis-media-e.md#filegenerationmode12)枚举设置为系统创建媒体文件时,会在[stop](#stop9)操作结束后把[PhotoAsset](../apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAsset.md)对象回调给应用。 1380 1381当用户重复订阅时,以最后一次订阅的回调接口为准。 1382 1383**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1384 1385**参数:** 1386 1387| 参数名 | 类型 | 必填 | 说明 | 1388| -------- | -------- | ---- | ------------------------------------------------------------ | 1389| type | string | 是 |录像资源的回调类型,支持的事件:'photoAssetAvailable'。 | 1390| callback | Callback<[photoAccessHelper.PhotoAsset](../apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAsset.md)> | 是 | 系统创建的资源文件对应的PhotoAsset对象。| 1391 1392**错误码:** 1393 1394以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1395 1396| 错误码ID | 错误信息 | 1397| -------- | ------------------------------------------ | 1398| 5400103 | IO error. Return by callback. | 1399| 5400105 | Service died. Return by callback. | 1400 1401**示例:** 1402 1403<!--code_no_check--> 1404```ts 1405import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1406import { common } from '@kit.AbilityKit' 1407let photoAsset: photoAccessHelper.PhotoAsset; 1408let context: Context | undefined; 1409constructor(context: Context) { 1410 this.context = context; // this.getUIContext().getHostContext(); 1411} 1412 1413// 例:处理photoAsset回调,保存video。 1414async function saveVideo(asset: photoAccessHelper.PhotoAsset) { 1415 console.info("saveVideo called"); 1416 if (!this.context) { 1417 console.error('context is undefined'); 1418 return; 1419 } 1420 try { 1421 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context); 1422 let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(asset); 1423 assetChangeRequest.saveCameraPhoto(); 1424 await phAccessHelper.applyChanges(assetChangeRequest); 1425 console.info('apply saveVideo successfully'); 1426 } catch (err) { 1427 console.error(`apply saveVideo failed with error: ${err.code}, ${err.message}`); 1428 } 1429} 1430// 注册photoAsset监听。 1431avRecorder.on('photoAssetAvailable', (asset: photoAccessHelper.PhotoAsset) => { 1432 console.info('photoAssetAvailable called'); 1433 if (asset != undefined) { 1434 photoAsset = asset; 1435 // 处理photoAsset回调。 1436 // 例:this.saveVideo(asset); 1437 } else { 1438 console.error('photoAsset is undefined'); 1439 } 1440}); 1441``` 1442 1443## off('photoAssetAvailable')<sup>12+</sup> 1444 1445off(type: 'photoAssetAvailable', callback?: Callback<photoAccessHelper.PhotoAsset>): void 1446 1447取消订阅媒体资源的回调类型。 1448 1449**系统能力:** SystemCapability.Multimedia.Media.AVRecorder 1450 1451**参数:** 1452 1453| 参数名 | 类型 | 必填 | 说明 | 1454| ------ | ------ | ---- | ------------------------------------------------------------ | 1455| type | string | 是 | 录音配置变化的回调类型,支持的事件:'photoAssetAvailable'。 | 1456| callback | Callback<[photoAccessHelper.PhotoAsset](../apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAsset.md)> | 否 | 系统创建的资源文件对应的PhotoAsset对象。| 1457 1458**示例:** 1459 1460```ts 1461avRecorder.off('photoAssetAvailable'); 1462```