1# @ohos.multimedia.media (媒体服务)(系统接口) 2 3媒体子系统为开发者提供一套简单且易于理解的接口,使得开发者能够方便接入系统并使用系统的媒体资源。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.multimedia.media (媒体服务)](js-apis-media.md)。 9 10## 导入模块 11 12```ts 13import { media } from '@kit.MediaKit'; 14``` 15 16## media.createVideoRecorder<sup>9+</sup> 17 18createVideoRecorder(callback: AsyncCallback\<VideoRecorder>): void 19 20异步方式创建视频录制实例。通过注册回调函数获取返回值。 21一台设备只允许创建一个录制实例。 22 23**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 24 25**系统接口:** 该接口为系统接口 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 31| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | 是 | 回调函数。异步返回VideoRecorder实例,失败时返回null。可用于录制视频媒体。 | 32 33**错误码:** 34 35以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 36 37| 错误码ID | 错误信息 | 38| -------- | ------------------------------ | 39| 5400101 | No memory. Return by callback. | 40 41**示例:** 42 43```ts 44import { BusinessError } from '@kit.BasicServicesKit'; 45 46let videoRecorder: media.VideoRecorder; 47media.createVideoRecorder((error: BusinessError, video: media.VideoRecorder) => { 48 if (video != null) { 49 videoRecorder = video; 50 console.info('video createVideoRecorder success'); 51 } else { 52 console.error(`video createVideoRecorder fail, error message:${error.message}`); 53 } 54}); 55``` 56 57## media.createVideoRecorder<sup>9+</sup> 58 59createVideoRecorder(): Promise\<VideoRecorder> 60 61异步方式创建视频录制实例。通过Promise获取返回值。 62一台设备只允许创建一个录制实例。 63 64**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 65 66**系统接口:** 该接口为系统接口 67 68**返回值:** 69 70| 类型 | 说明 | 71| ----------------------------------------- | ------------------------------------------------------------ | 72| Promise<[VideoRecorder](#videorecorder9)> | Promise对象。异步返回VideoRecorder实例,失败时返回null。可用于录制视频媒体。 | 73 74**错误码:** 75 76以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 77 78| 错误码ID | 错误信息 | 79| -------- | ----------------------------- | 80| 5400101 | No memory. Return by promise. | 81 82**示例:** 83 84```ts 85import { BusinessError } from '@kit.BasicServicesKit'; 86 87let videoRecorder: media.VideoRecorder; 88media.createVideoRecorder().then((video: media.VideoRecorder) => { 89 if (video != null) { 90 videoRecorder = video; 91 console.info('video createVideoRecorder success'); 92 } else { 93 console.error('video createVideoRecorder fail'); 94 } 95}).catch((error: BusinessError) => { 96 console.error(`video catchCallback, error message:${error.message}`); 97}); 98``` 99 100## media.reportAVScreenCaptureUserChoice<sup>12+</sup> 101 102reportAVScreenCaptureUserChoice(sessionId: number, choice: string): Promise\<void> 103 104上报录屏隐私弹窗的选择结果到ScreenCapture的服务端,用于判断是否开始录屏。如果用户选择“取消”则不进行录屏,如果用户选择“确定”则开始录屏。 105 106此接口提供给创建弹窗的系统应用调用。 107 108**系统能力:** SystemCapability.Multimedia.Media.AVScreenCapture 109 110**系统接口:** 该接口为系统接口 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| --------- | ------ | ---- | ------------------------------------------------------------ | 116| sessionId | number | 是 | AVScreenCapture服务会话Id,会由AVScreenCapture拉起隐私弹窗时传给应用。 | 117| choice | string | 是 | 用户的选择内容,包含是否同意录屏、选择的屏幕Id和窗口Id。可见示例中JsonData样例。| 118 119**错误码:** 120 121| 错误码ID | 错误信息 | 122| -------- | ------------------------------------------- | 123| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 124| 5400101 | No memory. Return by promise. | 125 126**示例:** 127 128```ts 129import { BusinessError } from '@kit.BasicServicesKit'; 130import { media } from '@kit.MediaKit'; 131 132class JsonData { 133 public choice: string = 'true' 134 public displayId: number | null = -1 135 public missionId: number | null = -1 136} 137let sessionId: number = 0; // 替换成拉起此进程的sessionId 138 139try { 140 const jsonData: JsonData = { 141 choice: 'true', // 替换成用户的选择内容 142 displayId: -1, // 替换成用户选择的屏幕Id 143 missionId: -1, // 替换成用户选择的窗口Id 144 } 145 await media.reportAVScreenCaptureUserChoice(sessionId, JSON.stringify(jsonData)); 146} catch (error: BusinessError) { 147 console.error(`reportAVScreenCaptureUserChoice error, error message: ${error.message}`); 148} 149``` 150 151## PixelMapParams<sup>11+</sup> 152 153获取视频缩略图时,输出缩略图的格式参数。 154 155**系统能力:** SystemCapability.Multimedia.Media.AVImageGenerator 156 157| 名称 | 类型 | 可读 | 可写 | 说明 | 158| -------- | ------ | ------| ------ | ---------------------- | 159| colorFormat | [PixelFormat](#pixelformat11) | 是 | 是 | 输出的缩略图颜色格式<br>**系统接口:** 该接口为系统接口 | 160 161## PixelFormat<sup>11+</sup> 162 163获取视频缩略图时,输出的缩略图采用的颜色格式枚举。 164 165**系统能力:** SystemCapability.Multimedia.Media.AVImageGenerator 166 167**系统接口:** 该接口为系统接口 168 169| 名称 | 值 | 说明 | 170| ------------------------ | --------------- | ------------------------------------------------------------ | 171| RGB_565 | 2 | 表示RGB_565颜色格式。 | 172| RGBA_8888 | 3 | 表示RGBA_8888颜色格式。 | 173| RGB_888 | 5 | 表示RGB_888颜色格式。 | 174 175## AvPlayer<sup>9+</sup> 176> **说明:** 177> 播放管理类,用于管理和播放媒体资源。在调用AVPlayer的方法前,需要先通过[createAVPlayer()](js-apis-media.md#mediacreateavplayer9)构建一个[AVPlayer](js-apis-media.md#avplayer9)实例。 178 179### setPlaybackRange<sup>12+</sup> 180 181setPlaybackRange(startTimeMs: number, endTimeMs: number, mode?: SeekMode) : Promise\<void> 182 183设置播放区间,并通过指定的[SeekMode](js-apis-media.md#seekmode8)跳转到区间开始位置。设置之后,只播放音视频文件设定区间内的内容。该方法异步方式返回执行结果,通过Promise获取返回值。可在**initialized**/**prepared**/**paused**/**stopped**/**completed**状态下使用。 184 185**系统能力:** SystemCapability.Multimedia.Media.AvPlayer 186 187**系统接口:** 该接口为系统接口 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| -------- | ---------------------- | ---- | --------------------------- | 193| startTimeMs | number | 是 | 区间开始位置,单位ms,取值[0, duration)。可以设置-1值,系统将会从0位置开始播放。| 194| endTimeMs | number | 是 | 区间结束位置,单位ms,取值(startTimeMs, duration]。可以设置-1值,系统将会播放到资源末尾。| 195| mode | [SeekMode](js-apis-media.md#seekmode8) | 否 | 支持SeekMode.SEEK_PREV_SYNC和SeekMode.SEEK_CLOSEST, <br/>默认值: SeekMode.SEEK_PREV_SYNC。| 196 197**错误码:** 198 199以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 200 201| 错误码ID | 错误信息 | 202| -------- | ------------------------------------------ | 203| 202 | Called from Non-System applications. Return by promise. | 204| 401 | The parameter check failed. Return by promise. | 205| 5400102 | Operation not allowed. Return by promise. | 206 207**示例:** 208 209```ts 210import { media } from '@kit.MediaKit'; 211import { BusinessError } from '@kit.BasicServicesKit'; 212 213avPlayer.setPlaybackRange(0, 6000, media.SeekMode.SEEK_CLOSEST).then(() => { 214 console.info('Succeeded setPlaybackRange'); 215}).catch((err: BusinessError) => { 216 console.error('Failed to setPlaybackRange' + err.message); 217}); 218``` 219 220## AVMetadataExtractor<sup>11+</sup> 221> **说明:** 222> 元数据获取类,用于从媒体资源中获取元数据。在调用AVMetadataExtractor的方法前,需要先通过[createAVMetadataExtractor()](js-apis-media.md#mediacreateavmetadataextractor11)构建一个AVMetadataExtractor实例。 223 224### getTimeByFrameIndex<sup>12+</sup> 225 226getTimeByFrameIndex(index: number): Promise\<number> 227 228获取目标视频帧号对应的视频时间戳。仅支持MP4视频文件。 229 230**系统能力:** SystemCapability.Multimedia.Media.AVMetadataExtractor 231 232**系统接口:** 该接口为系统接口 233 234**参数:** 235 236| 参数名 | 类型 | 必填 | 说明 | 237| ------ | ------ | ---- | ---------- | 238| index | number | 是 | 视频帧号。 | 239 240**返回值:** 241 242| 类型 | 说明 | 243| ---------------- | ----------------------------------- | 244| Promise\<number> | 时间戳的Promise返回值。单位是微秒。 | 245 246**错误码:** 247 248以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 249 250| 错误码ID | 错误信息 | 251| -------- | ---------------------------------------------- | 252| 401 | The parameter check failed. Return by promise. | 253| 5400102 | Operation not allowed. Returned by promise. | 254| 5400106 | Unsupported format. Returned by promise. | 255 256**示例:** 257 258```ts 259import { media } from '@kit.MediaKit'; 260import { BusinessError } from '@kit.BasicServicesKit'; 261 262avMetadataExtractor.getTimeByFrameIndex(0).then((timeUs: number) => { 263 console.info(`Succeeded getTimeByFrameIndex timeUs: ${timeUs}`); 264}).catch((err: BusinessError) => { 265 console.error(`Failed to getTimeByFrameIndex ${err.message}`); 266}) 267``` 268 269### getFrameIndexByTime<sup>12+</sup> 270 271getFrameIndexByTime(timeUs: number): Promise\<number> 272 273获取目标视频时间戳对应的视频帧号。仅支持MP4视频文件。 274 275**系统能力:** SystemCapability.Multimedia.Media.AVMetadataExtractor 276 277**系统接口:** 该接口为系统接口 278 279**参数:** 280 281| 参数名 | 类型 | 必填 | 说明 | 282| ------ | ------ | ---- | ------------------------ | 283| timeUs | number | 是 | 视频时间戳,单位:微秒。 | 284 285**返回值:** 286 287| 类型 | 说明 | 288| ---------------- | ------------------------- | 289| Promise\<number> | 视频帧号的Promise返回值。 | 290 291**错误码:** 292 293以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 294 295| 错误码ID | 错误信息 | 296| -------- | ---------------------------------------------- | 297| 401 | The parameter check failed. Return by promise. | 298| 5400102 | Operation not allowed. Returned by promise. | 299| 5400106 | Unsupported format. Returned by promise. | 300 301**示例:** 302 303```ts 304import { media } from '@kit.MediaKit'; 305import { BusinessError } from '@kit.BasicServicesKit'; 306 307avMetadataExtractor.getFrameIndexByTime(0).then((index: number) => { 308 console.info(`Succeeded getFrameIndexByTime index: ${index}`); 309}).catch((err: BusinessError) => { 310 console.error(`Failed to getFrameIndexByTime ${err.message}`); 311}) 312``` 313 314## VideoRecorder<sup>9+</sup> 315 316> **说明:** 317> AVRecorder<sup>9+</sup>发布后,VideoRecorder停止维护,建议使用[AVRecorder](js-apis-media.md#avrecorder9)替代。 318 319视频录制管理类,用于录制视频媒体。在调用VideoRecorder的方法前,需要先通过[createVideoRecorder()](#mediacreatevideorecorder9)构建一个[VideoRecorder](#videorecorder9)实例。 320 321### 属性 322 323**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 324 325**系统接口:** 该接口为系统接口 326 327| 名称 | 类型 | 可读 | 可写 | 说明 | 328| ------------------ | -------------------------------------- | ---- | ---- | ---------------- | 329| state<sup>9+</sup> | [VideoRecordState](#videorecordstate9) | 是 | 否 | 视频录制的状态。 | 330 331### prepare<sup>9+</sup> 332 333prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void 334 335异步方式进行视频录制的参数设置。通过注册回调函数获取返回值。 336 337**需要权限:** ohos.permission.MICROPHONE 338 339**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 340 341**系统接口:** 该接口为系统接口 342 343**参数:** 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | -------------------------------------------- | ---- | ----------------------------------- | 347| config | [VideoRecorderConfig](#videorecorderconfig9) | 是 | 配置视频录制的相关参数。 | 348| callback | AsyncCallback\<void> | 是 | 异步视频录制prepare方法的回调方法。 | 349 350**错误码:** 351 352以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 353 354| 错误码ID | 错误信息 | 355| -------- | ------------------------------------------ | 356| 201 | Permission denied. Return by callback. | 357| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 358| 5400102 | Operation not allowed. Return by callback. | 359| 5400105 | Service died. Return by callback. | 360 361**示例:** 362 363```ts 364import { BusinessError } from '@kit.BasicServicesKit'; 365 366// 配置参数以实际硬件设备支持的范围为准 367let videoProfile: media.VideoRecorderProfile = { 368 audioBitrate : 48000, 369 audioChannels : 2, 370 audioCodec : media.CodecMimeType.AUDIO_AAC, 371 audioSampleRate : 48000, 372 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 373 videoBitrate : 2000000, 374 videoCodec : media.CodecMimeType.VIDEO_AVC, 375 videoFrameWidth : 640, 376 videoFrameHeight : 480, 377 videoFrameRate : 30 378} 379 380let videoConfig: media.VideoRecorderConfig = { 381 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 382 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 383 profile : videoProfile, 384 url : 'fd://xx', // 文件需先由调用者创建,并给予适当的权限 385 rotation : 0, 386 location : { latitude : 30, longitude : 130 } 387} 388 389// asyncallback 390videoRecorder.prepare(videoConfig, (err: BusinessError) => { 391 if (err == null) { 392 console.info('prepare success'); 393 } else { 394 console.error('prepare failed and error is ' + err.message); 395 } 396}) 397``` 398 399### prepare<sup>9+</sup> 400 401prepare(config: VideoRecorderConfig): Promise\<void> 402 403异步方式进行视频录制的参数设置。通过Promise获取返回值。 404 405**需要权限:** ohos.permission.MICROPHONE 406 407**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 408 409**系统接口:** 该接口为系统接口 410 411**参数:** 412 413| 参数名 | 类型 | 必填 | 说明 | 414| ------ | -------------------------------------------- | ---- | ------------------------ | 415| config | [VideoRecorderConfig](#videorecorderconfig9) | 是 | 配置视频录制的相关参数。 | 416 417**返回值:** 418 419| 类型 | 说明 | 420| -------------- | ---------------------------------------- | 421| Promise\<void> | 异步视频录制prepare方法的Promise返回值。 | 422 423**错误码:** 424 425以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 426 427| 错误码ID | 错误信息 | 428| -------- | ----------------------------------------- | 429| 201 | Permission denied. Return by promise. | 430| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 431| 5400102 | Operation not allowed. Return by promise. | 432| 5400105 | Service died. Return by promise. | 433 434**示例:** 435 436```ts 437import { BusinessError } from '@kit.BasicServicesKit'; 438 439// 配置参数以实际硬件设备支持的范围为准 440let videoProfile: media.VideoRecorderProfile = { 441 audioBitrate : 48000, 442 audioChannels : 2, 443 audioCodec : media.CodecMimeType.AUDIO_AAC, 444 audioSampleRate : 48000, 445 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 446 videoBitrate : 2000000, 447 videoCodec : media.CodecMimeType.VIDEO_AVC, 448 videoFrameWidth : 640, 449 videoFrameHeight : 480, 450 videoFrameRate : 30 451} 452 453let videoConfig: media.VideoRecorderConfig = { 454 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 455 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 456 profile : videoProfile, 457 url : 'fd://xx', // 文件需先由调用者创建,并给予适当的权限 458 rotation : 0, 459 location : { latitude : 30, longitude : 130 } 460} 461 462// promise 463videoRecorder.prepare(videoConfig).then(() => { 464 console.info('prepare success'); 465}).catch((err: BusinessError) => { 466 console.error('prepare failed and catch error is ' + err.message); 467}); 468``` 469 470### getInputSurface<sup>9+</sup> 471 472getInputSurface(callback: AsyncCallback\<string>): void 473 474异步方式获得录制需要的surface。通过注册回调函数获取返回值。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的数据。 475 476应当注意,填入的视频数据需要携带时间戳(单位ns),buffersize。时间戳的起始时间请以系统启动时间为基准。 477 478只能在[prepare()](#prepare9)接口调用后调用。 479 480**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 481 482**系统接口:** 该接口为系统接口 483 484**参数:** 485 486| 参数名 | 类型 | 必填 | 说明 | 487| -------- | ---------------------- | ---- | --------------------------- | 488| callback | AsyncCallback\<string> | 是 | 异步获得surface的回调方法。 | 489 490**错误码:** 491 492以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 493 494| 错误码ID | 错误信息 | 495| -------- | ------------------------------------------ | 496| 5400102 | Operation not allowed. Return by callback. | 497| 5400103 | I/O error. Return by callback. | 498| 5400105 | Service died. Return by callback. | 499 500**示例:** 501 502```ts 503import { BusinessError } from '@kit.BasicServicesKit'; 504 505// asyncallback 506let surfaceID: string; // 传递给外界的surfaceID 507videoRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 508 if (err == null) { 509 console.info('getInputSurface success'); 510 surfaceID = surfaceId; 511 } else { 512 console.error('getInputSurface failed and error is ' + err.message); 513 } 514}); 515``` 516 517### getInputSurface<sup>9+</sup> 518 519getInputSurface(): Promise\<string>; 520 521 异步方式获得录制需要的surface。通过Promise获取返回值。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的数据。 522 523应当注意,填入的视频数据需要携带时间戳(单位ns),buffersize。时间戳的起始时间请以系统启动时间为基准。 524 525只能在[prepare()](#prepare9-1)接口调用后调用。 526 527**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 528 529**系统接口:** 该接口为系统接口 530 531**返回值:** 532 533| 类型 | 说明 | 534| ---------------- | -------------------------------- | 535| Promise\<string> | 异步获得surface的Promise返回值。 | 536 537**错误码:** 538 539以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 540 541| 错误码ID | 错误信息 | 542| -------- | ----------------------------------------- | 543| 5400102 | Operation not allowed. Return by promise. | 544| 5400103 | I/O error. Return by promise. | 545| 5400105 | Service died. Return by promise. | 546 547**示例:** 548 549```ts 550import { BusinessError } from '@kit.BasicServicesKit'; 551 552// promise 553let surfaceID: string; // 传递给外界的surfaceID 554videoRecorder.getInputSurface().then((surfaceId: string) => { 555 console.info('getInputSurface success'); 556 surfaceID = surfaceId; 557}).catch((err: BusinessError) => { 558 console.error('getInputSurface failed and catch error is ' + err.message); 559}); 560``` 561 562### start<sup>9+</sup> 563 564start(callback: AsyncCallback\<void>): void 565 566异步方式开始视频录制。通过注册回调函数获取返回值。 567 568在[prepare()](#prepare9)和[getInputSurface()](#getinputsurface9)后调用,需要依赖数据源先给surface传递数据。 569 570**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 571 572**系统接口:** 该接口为系统接口 573 574**参数:** 575 576| 参数名 | 类型 | 必填 | 说明 | 577| -------- | -------------------- | ---- | ---------------------------- | 578| callback | AsyncCallback\<void> | 是 | 异步开始视频录制的回调方法。 | 579 580**错误码:** 581 582以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 583 584| 错误码ID | 错误信息 | 585| -------- | ------------------------------------------ | 586| 5400102 | Operation not allowed. Return by callback. | 587| 5400103 | I/O error. Return by callback. | 588| 5400105 | Service died. Return by callback. | 589 590**示例:** 591 592```ts 593import { BusinessError } from '@kit.BasicServicesKit'; 594 595// asyncallback 596videoRecorder.start((err: BusinessError) => { 597 if (err == null) { 598 console.info('start videorecorder success'); 599 } else { 600 console.error('start videorecorder failed and error is ' + err.message); 601 } 602}); 603``` 604 605### start<sup>9+</sup> 606 607start(): Promise\<void> 608 609异步方式开始视频录制。通过Promise获取返回值。 610 611在[prepare()](#prepare9-1)和[getInputSurface()](#getinputsurface9-1)后调用,需要依赖数据源先给surface传递数据。 612 613**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 614 615**系统接口:** 该接口为系统接口 616 617**返回值:** 618 619| 类型 | 说明 | 620| -------------- | ------------------------------------- | 621| Promise\<void> | 异步开始视频录制方法的Promise返回值。 | 622 623**错误码:** 624 625以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 626 627| 错误码ID | 错误信息 | 628| -------- | ----------------------------------------- | 629| 5400102 | Operation not allowed. Return by promise. | 630| 5400103 | I/O error. Return by promise. | 631| 5400105 | Service died. Return by promise. | 632 633**示例:** 634 635```ts 636import { BusinessError } from '@kit.BasicServicesKit'; 637 638// promise 639videoRecorder.start().then(() => { 640 console.info('start videorecorder success'); 641}).catch((err: BusinessError) => { 642 console.error('start videorecorder failed and catch error is ' + err.message); 643}); 644``` 645 646### pause<sup>9+</sup> 647 648pause(callback: AsyncCallback\<void>): void 649 650异步方式暂停视频录制。通过注册回调函数获取返回值。 651 652在[start()](#start9)后调用。可以通过调用[resume()](#resume9)接口来恢复录制。 653 654**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 655 656**系统接口:** 该接口为系统接口 657 658**参数:** 659 660| 参数名 | 类型 | 必填 | 说明 | 661| -------- | -------------------- | ---- | ---------------------------- | 662| callback | AsyncCallback\<void> | 是 | 异步暂停视频录制的回调方法。 | 663 664**错误码:** 665 666以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 667 668| 错误码ID | 错误信息 | 669| -------- | ------------------------------------------ | 670| 5400102 | Operation not allowed. Return by callback. | 671| 5400103 | I/O error. Return by callback. | 672| 5400105 | Service died. Return by callback. | 673 674**示例:** 675 676```ts 677import { BusinessError } from '@kit.BasicServicesKit'; 678 679// asyncallback 680videoRecorder.pause((err: BusinessError) => { 681 if (err == null) { 682 console.info('pause videorecorder success'); 683 } else { 684 console.error('pause videorecorder failed and error is ' + err.message); 685 } 686}); 687``` 688 689### pause<sup>9+</sup> 690 691pause(): Promise\<void> 692 693异步方式暂停视频录制。通过Promise获取返回值。 694 695在[start()](#start9-1)后调用。可以通过调用[resume()](#resume9-1)接口来恢复录制。 696 697**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 698 699**系统接口:** 该接口为系统接口 700 701**返回值:** 702 703| 类型 | 说明 | 704| -------------- | ------------------------------------- | 705| Promise\<void> | 异步暂停视频录制方法的Promise返回值。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 710 711| 错误码ID | 错误信息 | 712| -------- | ----------------------------------------- | 713| 5400102 | Operation not allowed. Return by promise. | 714| 5400103 | I/O error. Return by promise. | 715| 5400105 | Service died. Return by promise. | 716 717**示例:** 718 719```ts 720import { BusinessError } from '@kit.BasicServicesKit'; 721 722// promise 723videoRecorder.pause().then(() => { 724 console.info('pause videorecorder success'); 725}).catch((err: BusinessError) => { 726 console.error('pause videorecorder failed and catch error is ' + err.message); 727}); 728``` 729 730### resume<sup>9+</sup> 731 732resume(callback: AsyncCallback\<void>): void 733 734异步方式恢复视频录制。通过注册回调函数获取返回值。 735 736**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 737 738**系统接口:** 该接口为系统接口 739 740**参数:** 741 742| 参数名 | 类型 | 必填 | 说明 | 743| -------- | -------------------- | ---- | ---------------------------- | 744| callback | AsyncCallback\<void> | 是 | 异步恢复视频录制的回调方法。 | 745 746**错误码:** 747 748以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 749 750| 错误码ID | 错误信息 | 751| -------- | ------------------------------------------ | 752| 5400102 | Operation not allowed. Return by callback. | 753| 5400103 | I/O error. Return by callback. | 754| 5400105 | Service died. Return by callback. | 755 756**示例:** 757 758```ts 759import { BusinessError } from '@kit.BasicServicesKit'; 760 761// asyncallback 762videoRecorder.resume((err: BusinessError) => { 763 if (err == null) { 764 console.info('resume videorecorder success'); 765 } else { 766 console.error('resume videorecorder failed and error is ' + err.message); 767 } 768}); 769``` 770 771### resume<sup>9+</sup> 772 773resume(): Promise\<void> 774 775异步方式恢复视频录制。通过Promise获取返回值。 776 777**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 778 779**系统接口:** 该接口为系统接口 780 781**返回值:** 782 783| 类型 | 说明 | 784| -------------- | ------------------------------------- | 785| Promise\<void> | 异步恢复视频录制方法的Promise返回值。 | 786 787**错误码:** 788 789以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 790 791| 错误码ID | 错误信息 | 792| -------- | ----------------------------------------- | 793| 5400102 | Operation not allowed. Return by promise. | 794| 5400103 | I/O error. Return by promise. | 795| 5400105 | Service died. Return by promise. | 796 797**示例:** 798 799```ts 800import { BusinessError } from '@kit.BasicServicesKit'; 801 802// promise 803videoRecorder.resume().then(() => { 804 console.info('resume videorecorder success'); 805}).catch((err: BusinessError) => { 806 console.error('resume videorecorder failed and catch error is ' + err.message); 807}); 808``` 809 810### stop<sup>9+</sup> 811 812stop(callback: AsyncCallback\<void>): void 813 814异步方式停止视频录制。通过注册回调函数获取返回值。 815 816需要重新调用[prepare()](#prepare9)和[getInputSurface()](#getinputsurface9)接口才能重新录制。 817 818**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 819 820**系统接口:** 该接口为系统接口 821 822**参数:** 823 824| 参数名 | 类型 | 必填 | 说明 | 825| -------- | -------------------- | ---- | ---------------------------- | 826| callback | AsyncCallback\<void> | 是 | 异步停止视频录制的回调方法。 | 827 828**错误码:** 829 830以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 831 832| 错误码ID | 错误信息 | 833| -------- | ------------------------------------------ | 834| 5400102 | Operation not allowed. Return by callback. | 835| 5400103 | I/O error. Return by callback. | 836| 5400105 | Service died. Return by callback. | 837 838**示例:** 839 840```ts 841import { BusinessError } from '@kit.BasicServicesKit'; 842 843// asyncallback 844videoRecorder.stop((err: BusinessError) => { 845 if (err == null) { 846 console.info('stop videorecorder success'); 847 } else { 848 console.error('stop videorecorder failed and error is ' + err.message); 849 } 850}); 851``` 852 853### stop<sup>9+</sup> 854 855stop(): Promise\<void> 856 857异步方式停止视频录制。通过Promise获取返回值。 858 859需要重新调用[prepare()](#prepare9-1)和[getInputSurface()](#getinputsurface9-1)接口才能重新录制。 860 861**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 862 863**系统接口:** 该接口为系统接口 864 865**返回值:** 866 867| 类型 | 说明 | 868| -------------- | ------------------------------------- | 869| Promise\<void> | 异步停止视频录制方法的Promise返回值。 | 870 871**错误码:** 872 873以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 874 875| 错误码ID | 错误信息 | 876| -------- | ----------------------------------------- | 877| 5400102 | Operation not allowed. Return by promise. | 878| 5400103 | I/O error. Return by promise. | 879| 5400105 | Service died. Return by promise. | 880 881**示例:** 882 883```ts 884import { BusinessError } from '@kit.BasicServicesKit'; 885 886// promise 887videoRecorder.stop().then(() => { 888 console.info('stop videorecorder success'); 889}).catch((err: BusinessError) => { 890 console.error('stop videorecorder failed and catch error is ' + err.message); 891}); 892``` 893 894### release<sup>9+</sup> 895 896release(callback: AsyncCallback\<void>): void 897 898异步方式释放视频录制资源。通过注册回调函数获取返回值。 899 900**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 901 902**系统接口:** 该接口为系统接口 903 904**参数:** 905 906| 参数名 | 类型 | 必填 | 说明 | 907| -------- | -------------------- | ---- | -------------------------------- | 908| callback | AsyncCallback\<void> | 是 | 异步释放视频录制资源的回调方法。 | 909 910**错误码:** 911 912以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 913 914| 错误码ID | 错误信息 | 915| -------- | --------------------------------- | 916| 5400105 | Service died. Return by callback. | 917 918**示例:** 919 920```ts 921import { BusinessError } from '@kit.BasicServicesKit'; 922 923// asyncallback 924videoRecorder.release((err: BusinessError) => { 925 if (err == null) { 926 console.info('release videorecorder success'); 927 } else { 928 console.error('release videorecorder failed and error is ' + err.message); 929 } 930}); 931``` 932 933### release<sup>9+</sup> 934 935release(): Promise\<void> 936 937异步方式释放视频录制资源。通过Promise获取返回值。 938 939**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 940 941**系统接口:** 该接口为系统接口 942 943**返回值:** 944 945| 类型 | 说明 | 946| -------------- | ----------------------------------------- | 947| Promise\<void> | 异步释放视频录制资源方法的Promise返回值。 | 948 949**错误码:** 950 951以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 952 953| 错误码ID | 错误信息 | 954| -------- | --------------------------------- | 955| 5400105 | Service died. Return by callback. | 956 957**示例:** 958 959```ts 960import { BusinessError } from '@kit.BasicServicesKit'; 961 962// promise 963videoRecorder.release().then(() => { 964 console.info('release videorecorder success'); 965}).catch((err: BusinessError) => { 966 console.error('release videorecorder failed and catch error is ' + err.message); 967}); 968``` 969 970### reset<sup>9+</sup> 971 972reset(callback: AsyncCallback\<void>): void 973 974异步方式重置视频录制。通过注册回调函数获取返回值。 975 976需要重新调用[prepare()](#prepare9)和[getInputSurface()](#getinputsurface9)接口才能重新录制。 977 978**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 979 980**系统接口:** 该接口为系统接口 981 982**参数:** 983 984| 参数名 | 类型 | 必填 | 说明 | 985| -------- | -------------------- | ---- | ---------------------------- | 986| callback | AsyncCallback\<void> | 是 | 异步重置视频录制的回调方法。 | 987 988**错误码:** 989 990以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 991 992| 错误码ID | 错误信息 | 993| -------- | --------------------------------- | 994| 5400103 | I/O error. Return by callback. | 995| 5400105 | Service died. Return by callback. | 996 997**示例:** 998 999```ts 1000import { BusinessError } from '@kit.BasicServicesKit'; 1001 1002// asyncallback 1003videoRecorder.reset((err: BusinessError) => { 1004 if (err == null) { 1005 console.info('reset videorecorder success'); 1006 } else { 1007 console.error('reset videorecorder failed and error is ' + err.message); 1008 } 1009}); 1010``` 1011 1012### reset<sup>9+</sup> 1013 1014reset(): Promise\<void> 1015 1016异步方式重置视频录制。通过Promise获取返回值。 1017 1018需要重新调用[prepare()](#prepare9-1)和[getInputSurface()](#getinputsurface9-1)接口才能重新录制。 1019 1020**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 1021 1022**系统接口:** 该接口为系统接口 1023 1024**返回值:** 1025 1026| 类型 | 说明 | 1027| -------------- | ------------------------------------- | 1028| Promise\<void> | 异步重置视频录制方法的Promise返回值。 | 1029 1030**错误码:** 1031 1032以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 1033 1034| 错误码ID | 错误信息 | 1035| -------- | -------------------------------- | 1036| 5400103 | I/O error. Return by promise. | 1037| 5400105 | Service died. Return by promise. | 1038 1039**示例:** 1040 1041```ts 1042import { BusinessError } from '@kit.BasicServicesKit'; 1043 1044// promise 1045videoRecorder.reset().then(() => { 1046 console.info('reset videorecorder success'); 1047}).catch((err: BusinessError) => { 1048 console.error('reset videorecorder failed and catch error is ' + err.message); 1049}); 1050``` 1051 1052### on('error')<sup>9+</sup> 1053 1054on(type: 'error', callback: ErrorCallback): void 1055 1056开始订阅视频录制错误事件,当上报error错误事件后,用户需处理error事件,退出录制操作。 1057 1058**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 1059 1060**系统接口:** 该接口为系统接口 1061 1062**参数:** 1063 1064| 参数名 | 类型 | 必填 | 说明 | 1065| -------- | ------------- | ---- | ------------------------------------------------------------ | 1066| type | string | 是 | 录制错误事件回调类型'error'。<br/>- 'error':视频录制过程中发生错误,触发该事件。 | 1067| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 录制错误事件回调方法。 | 1068 1069**错误码:** 1070 1071以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md) 1072 1073| 错误码ID | 错误信息 | 1074| -------- | --------------------------------- | 1075| 5400103 | I/O error. Return by callback. | 1076| 5400105 | Service died. Return by callback. | 1077 1078**示例:** 1079 1080```ts 1081import { BusinessError } from '@kit.BasicServicesKit'; 1082 1083// 当获取videoRecordState接口出错时通过此订阅事件上报 1084videoRecorder.on('error', (error: BusinessError) => { // 设置'error'事件回调 1085 console.error(`audio error called, error: ${error}`); 1086}) 1087``` 1088 1089## VideoRecordState<sup>9+</sup> 1090 1091视频录制的状态机。可通过state属性获取当前状态。 1092 1093**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 1094 1095**系统接口:** 该接口为系统接口 1096 1097| 名称 | 类型 | 说明 | 1098| -------- | ------ | ---------------------- | 1099| idle | string | 视频录制空闲。 | 1100| prepared | string | 视频录制参数设置完成。 | 1101| playing | string | 视频正在录制。 | 1102| paused | string | 视频暂停录制。 | 1103| stopped | string | 视频录制停止。 | 1104| error | string | 错误状态。 | 1105 1106## VideoRecorderConfig<sup>9+</sup> 1107 1108表示视频录制的参数设置。 1109 1110通过audioSourceType和videoSourceType区分纯视频录制和音视频录制(纯音频录制请使用[AVRecorder](js-apis-media.md#avrecorder9)或[AudioRecorder](js-apis-media.md#audiorecorderdeprecated))。纯视频录制时,仅需要设置videoSourceType;音视频录制时,audioSourceType和videoSourceType均需要设置。 1111 1112**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 1113 1114**系统接口:** 该接口为系统接口 1115 1116| 名称 | 类型 | 必填 | 说明 | 1117| --------------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 1118| audioSourceType | [AudioSourceType](js-apis-media.md#audiosourcetype9) | 否 | 视频录制的音频源类型,选择音频录制时必填。 | 1119| videoSourceType | [VideoSourceType](js-apis-media.md#videosourcetype9) | 是 | 视频录制的视频源类型。 | 1120| profile | [VideoRecorderProfile](#videorecorderprofile9) | 是 | 视频录制的profile。 | 1121| rotation | number | 否 | 录制的视频旋转角度,仅支持0,90,180,270,默认值为0。 | 1122| location | [Location](js-apis-media.md#location) | 否 | 录制视频的地理位置,默认不记录地理位置信息。 | 1123| url | string | 是 | 视频输出URL:fd://xx (fd number)<br/> | 1124 1125## VideoRecorderProfile<sup>9+</sup> 1126 1127视频录制的配置文件。 1128 1129**系统能力:** SystemCapability.Multimedia.Media.VideoRecorder 1130 1131**系统接口:** 该接口为系统接口 1132 1133| 名称 | 类型 | 必填 | 说明 | 1134| ---------------- | -------------------------------------------- | ---- | ---------------- | 1135| audioBitrate | number | 否 | 音频编码比特率,选择音频录制时必填。 | 1136| audioChannels | number | 否 | 音频采集声道数,选择音频录制时必填。 | 1137| audioCodec | [CodecMimeType](js-apis-media.md#codecmimetype8) | 否 | 音频编码格式,选择音频录制时必填。 | 1138| audioSampleRate | number | 否 | 音频采样率,选择音频录制时必填。 | 1139| fileFormat | [ContainerFormatType](js-apis-media.md#containerformattype8) | 是 | 文件的容器格式。 | 1140| videoBitrate | number | 是 | 视频编码比特率。 | 1141| videoCodec | [CodecMimeType](js-apis-media.md#codecmimetype8) | 是 | 视频编码格式。 | 1142| videoFrameWidth | number | 是 | 录制视频帧的宽。 | 1143| videoFrameHeight | number | 是 | 录制视频帧的高。 | 1144| videoFrameRate | number | 是 | 录制视频帧率。 | 1145