1# Video 2 3The **Video** component is used to play a video and control its playback. 4 5> **NOTE** 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8> 9> The **Video** component provides only simple video playback features. For complex video playback control scenarios, consider using the [AVPlayer](../../apis-media-kit/arkts-apis-media-AVPlayer.md) APIs in conjunction with the [XComponent](ts-basic-components-xcomponent.md) component. 10> 11> When using **expandSafeArea** to extend into safe areas, the **Video** component's content display area does not support expansion. 12 13## Required Permissions 14 15To use online videos, you must apply for the ohos.permission.INTERNET permission. For details about how to apply for a permission, see [Declaring Permissions](../../../security/AccessToken/declare-permissions.md). 16 17 18## Child Components 19 20Not supported 21 22 23## APIs 24 25### Video 26 27Video(value: VideoOptions) 28 29**Atomic service API**: This API can be used in atomic services since API version 11. 30 31**System capability**: SystemCapability.ArkUI.ArkUI.Full 32 33**Parameters** 34 35| Name| Type| Mandatory| Description| 36| -------- | -------- | -------- | -------- | 37| value | [VideoOptions](#videooptions) | Yes| Video information.| 38 39## VideoOptions 40 41Defines the options of the **Video** component. 42 43**System capability**: SystemCapability.ArkUI.ArkUI.Full 44 45| Name | Type | Mandatory| Description | 46| ------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 47| src | string \| [Resource](ts-types.md#resource) | No | Video source, which can be either a local or a network video.<br>The Resource type allows cross-package and cross-module access to resource files and is commonly used for accessing local videos.<br>- Only resources in the rawfile folder are supported, which means that you can reference video files only with **$rawfile**.<br>The string type is used for loading local videos and, more frequently, network videos.<br>- Network video URLs are supported.<br>- Strings with the **file://** prefix, that is, [application sandbox URIs](../../apis-core-file-kit/js-apis-file-fileuri.md#constructor10): **file://\<bundleName>/\<sandboxPath>**, are supported. They are used to access resources in the application sandbox path. Ensure that the application has the read permission to the files in the specified path.<br>**NOTE**<br>The supported video formats are MP4, MKV, and TS.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 48| currentProgressRate | number \| string \| [PlaybackSpeed<sup>8+</sup>](#playbackspeed8) | No | Video playback speed.<br>**NOTE**<br>The value of the number type can only be **0.75**, **1.0**, **1.25**, **1.75**, or **2.0**.<br>For the string type, numeric string values, for example **0.75**, **1.0**, **1.25**, **1.75**, and **2.0**, are supported.<br>Default value: 1.0 \| PlaybackSpeed.Speed_Forward_1_00_X<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 49| previewUri | string \| [PixelMap](../../apis-image-kit/arkts-apis-image-PixelMap.md) \| [Resource](ts-types.md#resource) | No | Path of the preview image displayed before the video playback starts. By default, no preview image is displayed.<br>The string type can be used to load online images and local images.<br>- URLs are supported for loading online images.<br>- Relative paths are supported for loading local images, for example, **previewUri: "common/test.jpg"**. When using an image referenced using a relative path, the component cannot be called across bundles or modules.<br>- Strings with the **file://** prefix, that is, [application sandbox URIs](../../apis-core-file-kit/js-apis-file-fileuri.md#constructor10): **file://\<bundleName>/\<sandboxPath>**, are supported. They are used to access resources in the application sandbox path. Ensure that the application has the read permission to the files in the specified path.<br>The Resource type allows cross-package and cross-module access to resource files.<br>- Resources in the **rawfile** folder are supported, which means that you can reference video files with **\\$rawfile**.<br>- You can use **\\$r** to reference images in system or application resources.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 50| controller | [VideoController](#videocontroller) | No | Video controller to control the video playback status.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 51| imageAIOptions<sup>12+</sup> | [ImageAIOptions](ts-image-common.md#imageaioptions) | No | AI image analysis options. You can configure the analysis type or bind an analyzer controller through this parameter.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 52| posterOptions<sup>18+</sup> | [PosterOptions](#posteroptions18) | No | Display options for the first frame of the video.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 53 54## PlaybackSpeed<sup>8+</sup> 55 56Enumerates video playback speed options. 57 58**Atomic service API**: This API can be used in atomic services since API version 11. 59 60**System capability**: SystemCapability.ArkUI.ArkUI.Full 61 62| Name | Description | 63| -------------------- | -------------- | 64| Speed_Forward_0_75_X | 0.75x playback speed.| 65| Speed_Forward_1_00_X | 1x playback speed. | 66| Speed_Forward_1_25_X | 1.25x playback speed.| 67| Speed_Forward_1_75_X | 1.75x playback speed.| 68| Speed_Forward_2_00_X | 2x playback speed. | 69 70## Attributes 71 72In addition to the [universal attributes](ts-component-general-attributes.md), the following attributes are supported. 73 74### muted 75 76muted(value: boolean) 77 78Sets whether to mute the video. 79 80**Atomic service API**: This API can be used in atomic services since API version 11. 81 82**System capability**: SystemCapability.ArkUI.ArkUI.Full 83 84**Parameters** 85 86| Name| Type | Mandatory| Description | 87| ------ | ------- | ---- | ---------------------------- | 88| value | boolean | Yes | Whether to mute the video.<br>**true**: Mute the video.<br>**false**: Unmute the video.<br>Default value: **false**.| 89 90> **NOTE** 91> 92> When not muted, the **Video** component will acquire audio focus when playback starts. To prevent it from interrupting other audio streams, make sure it is muted before initiating playback. 93 94### autoPlay 95 96autoPlay(value: boolean) 97 98Specifies whether to enable autoplay. 99 100**Atomic service API**: This API can be used in atomic services since API version 11. 101 102**System capability**: SystemCapability.ArkUI.ArkUI.Full 103 104**Parameters** 105 106| Name| Type | Mandatory| Description | 107| ------ | ------- | ---- | -------------------------------- | 108| value | boolean | Yes | Whether to enable autoplay.<br>**true**: Enable autoplay.<br>**false**: Disable autoplay.<br>Default value: **false**.| 109 110### controls 111 112controls(value: boolean) 113 114Specifies whether to display the video playback control bar. 115 116**Atomic service API**: This API can be used in atomic services since API version 11. 117 118**System capability**: SystemCapability.ArkUI.ArkUI.Full 119 120**Parameters** 121 122| Name| Type | Mandatory| Description | 123| ------ | ------- | ---- | ----------------------------------------------- | 124| value | boolean | Yes | Whether to display the video playback control bar.<br>**true**: Display the video playback control bar.<br>**false**: Do not display the video playback control bar.<br>Default value: **true**| 125 126> **NOTE** 127> 128> The **Video** component comes with a built-in controller that cannot be customized. If you require different functionality, you can hide the default controller and implement your own custom controller. 129 130### objectFit 131 132objectFit(value: ImageFit) 133 134Sets the fill mode for the video content. 135 136**Atomic service API**: This API can be used in atomic services since API version 11. 137 138**System capability**: SystemCapability.ArkUI.ArkUI.Full 139 140**Parameters** 141 142| Name| Type | Mandatory| Description | 143| ------ | ----------------------------------------- | ---- | -------------------------------- | 144| value | [ImageFit](ts-appendix-enums.md#imagefit) | Yes | Fill mode of the video content.<br>Default value: **Cover**<br>Constraints: The enumerated value **Matrix** in **ImageFit** is not supported and will behave as **Cover**.<br>Invalid values, including **undefined**, **null**, and values outside the [ImageFit](ts-appendix-enums.md#imagefit) enumeration range, will result in an effect the same as **Cover**.| 145 146### loop 147 148loop(value: boolean) 149 150Specifies whether to repeat the video. 151 152**Atomic service API**: This API can be used in atomic services since API version 11. 153 154**System capability**: SystemCapability.ArkUI.ArkUI.Full 155 156**Parameters** 157 158| Name| Type | Mandatory| Description | 159| ------ | ------- | ---- | ---------------------------------------- | 160| value | boolean | Yes | Whether to repeat the video.<br>**true**: Repeat the video.<br>**false**: Do not repeat the video.<br>Default value: **false**.| 161 162### enableAnalyzer<sup>12+</sup> 163 164enableAnalyzer(enable: boolean) 165 166Sets whether to enable the AI image analyzer, which supports subject recognition, text recognition, and object lookup. This attribute can be dynamically set using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier). 167After this feature is enabled, the video automatically enters an analysis state to process the current frame when playback is paused, and exits the analysis state when playback is resumed. 168Note that if this attribute and the [overlay](ts-universal-attributes-overlay.md) attribute are both set, [CustomBuilder](ts-types.md#custombuilder8) specified in [overlay](ts-universal-attributes-overlay.md) has no effect. 169 170**Atomic service API**: This API can be used in atomic services since API version 12. 171 172**System capability**: SystemCapability.ArkUI.ArkUI.Full 173 174**Parameters** 175 176| Name| Type| Mandatory| Description| 177| -------- | -------- | -------- | -------- | 178| enable | boolean | Yes| Whether to enable the AI image analyzer.<br>**true**: Enable the AI image analyzer.<br>**false**: Disable the AI imageanalyzer.<br>Default value: **false**.| 179 180> **NOTE** 181> 182> This feature is available only when the custom control bar is used (that is, when the [controls](#controls) attribute is set to **false**). 183> 184> This feature depends on device capabilities. 185 186### analyzerConfig<sup>12+</sup> 187 188analyzerConfig(config: ImageAnalyzerConfig) 189 190Sets the AI image analysis types, including subject recognition, text recognition, and object lookup. This attribute can be dynamically set using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier). 191 192**Atomic service API**: This API can be used in atomic services since API version 12. 193 194**System capability**: SystemCapability.ArkUI.ArkUI.Full 195 196| Name| Type| Mandatory| Description| 197| -------- | -------- | -------- | -------- | 198| config | [ImageAnalyzerConfig](ts-image-common.md#imageanalyzerconfig) | Yes| AI image analysis types.| 199 200### enableShortcutKey<sup>15+</sup> 201 202enableShortcutKey(enabled: boolean) 203 204Sets whether the component responds to keyboard shortcuts when it has focus. 205 206Currently, the component can respond to the following keys when it is in focus: spacebar for playing or pausing the video, up or down arrow key for adjusting the video volume, and left or right arrow key for fast forwarding or rewinding the video. 207 208**Atomic service API**: This API can be used in atomic services since API version 15. 209 210**System capability**: SystemCapability.ArkUI.ArkUI.Full 211 212| Name | Type | Mandatory| Description | 213| ------- | ------- | ---- | -------------------------------------- | 214| enabled | boolean | Yes | Whether the component responds to keyboard shortcuts when it has focus.<br>**true**: The component responds to keyboard shortcuts when it has focus.<br>**false**: The component does not respond to keyboard shortcuts when it has focus.<br>Default value: **false**.| 215 216## Events 217 218In addition to the [universal events](ts-component-general-events.md), the following events are supported. 219 220### onStart 221 222onStart(event: VoidCallback) 223 224Triggered when video playback starts. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 225 226**Atomic service API**: This API can be used in atomic services since API version 11. 227 228**System capability**: SystemCapability.ArkUI.ArkUI.Full 229 230**Parameters** 231 232| Name| Type | Mandatory| Description | 233| ------ | --------------------------------------------- | ---- | ----------------------------------- | 234| event | [VoidCallback](ts-types.md#voidcallback12) | Yes | Callback invoked when video playback starts. | 235 236### onPause 237 238onPause(event: VoidCallback) 239 240Triggered when video playback is paused. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 241 242**Atomic service API**: This API can be used in atomic services since API version 11. 243 244**System capability**: SystemCapability.ArkUI.ArkUI.Full 245 246**Parameters** 247 248| Name| Type | Mandatory| Description | 249| ------ | --------------------------------------------- | ---- | ----------------------------------- | 250| event | [VoidCallback](ts-types.md#voidcallback12) | Yes | Callback invoked when video playback is paused. | 251 252### onFinish 253 254onFinish(event: VoidCallback) 255 256Triggered when video playback is finished. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 257 258**Atomic service API**: This API can be used in atomic services since API version 11. 259 260**System capability**: SystemCapability.ArkUI.ArkUI.Full 261 262**Parameters** 263 264| Name| Type | Mandatory| Description | 265| ------ | --------------------------------------------- | ---- | ----------------------------------- | 266| event | [VoidCallback](ts-types.md#voidcallback12) | Yes | Callback invoked when video playback is finished. | 267 268### onError 269 270onError(event: VoidCallback | ErrorCallback) 271 272Triggered when video playback fails. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 273 274**Atomic service API**: This API can be used in atomic services since API version 11. 275 276**System capability**: SystemCapability.ArkUI.ArkUI.Full 277 278**Parameters** 279 280| Name| Type | Mandatory| Description | 281| ------ | --------------------------------------------- | ---- | ----------------------------------- | 282| event | [VoidCallback](ts-types.md#voidcallback12) \| [ErrorCallback](../../apis-basic-services-kit/js-apis-base.md#errorcallback)<sup>20+</sup> | Yes | Callback invoked when video playback fails. The [ErrorCallback](../../apis-basic-services-kit/js-apis-base.md#errorcallback) parameter is used to receive error information.| 283 284For details about the following error codes, see [Video Component Error Codes](../errorcode-video.md). For details about other error codes, see [Media Error Codes](../../apis-media-kit/errorcode-media.md). 285 286|Error Code|Error Message| 287|--|--| 288|103601 |Failed to create the media player.| 289|103602 |Not a valid source.| 290 291### onStop<sup>12+</sup> 292 293onStop(event: Callback<void>) 294 295Triggered when video playback is stopped (after **stop()** is called). Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 296 297**Atomic service API**: This API can be used in atomic services since API version 12. 298 299**System capability**: SystemCapability.ArkUI.ArkUI.Full 300 301### onPrepared 302 303onPrepared(callback: Callback\<PreparedInfo>) 304 305Triggered when video preparation is complete. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 306 307**Atomic service API**: This API can be used in atomic services since API version 11. 308 309**System capability**: SystemCapability.ArkUI.ArkUI.Full 310 311**Parameters** 312 313| Name | Type | Mandatory| Description | 314| -------- | ------ | ---- | -------------------------- | 315| callback | Callback\<[PreparedInfo](#preparedinfo18)> | Yes | Duration of the video.| 316 317### onSeeking 318 319onSeeking(callback: Callback\<PlaybackInfo>) 320 321Triggered to report the time information while seeking is in progress (the progress bar is being dragged). Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 322 323**Atomic service API**: This API can be used in atomic services since API version 11. 324 325**System capability**: SystemCapability.ArkUI.ArkUI.Full 326 327**Parameters** 328 329| Name| Type | Mandatory| Description | 330| ------ | ------ | ---- | ------------------------------ | 331| callback | Callback\<[PlaybackInfo](#playbackinfo18)> | Yes | Callback that provides the current playback progress.| 332 333### onSeeked 334 335onSeeked(callback: Callback\<PlaybackInfo>) 336 337Triggered to report the time information while seeking is completed. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 338 339**Atomic service API**: This API can be used in atomic services since API version 11. 340 341**System capability**: SystemCapability.ArkUI.ArkUI.Full 342 343**Parameters** 344 345| Name| Type | Mandatory| Description | 346| ------ | ------ | ---- | ------------------------------ | 347| callback | Callback\<[PlaybackInfo](#playbackinfo18)> | Yes | Callback that provides the current playback progress.| 348 349### onUpdate 350 351onUpdate(callback: Callback\<PlaybackInfo>) 352 353Triggered when playback progress changes. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 354 355**Atomic service API**: This API can be used in atomic services since API version 11. 356 357**System capability**: SystemCapability.ArkUI.ArkUI.Full 358 359**Parameters** 360 361| Name| Type | Mandatory| Description | 362| ------ | ------ | ---- | ------------------------------ | 363| callback | Callback\<[PlaybackInfo](#playbackinfo18)> | Yes | Callback that provides the current playback progress.| 364 365### onFullscreenChange 366 367onFullscreenChange(callback: Callback\<FullscreenInfo>) 368 369Triggered when video playback is switched between full-screen mode and non-full-screen mode. Dynamic property modification using [attributeModifier](ts-universal-attributes-attribute-modifier.md#attributemodifier) is supported. 370 371**Atomic service API**: This API can be used in atomic services since API version 11. 372 373**System capability**: SystemCapability.ArkUI.ArkUI.Full 374 375**Parameters** 376 377| Name | Type | Mandatory| Description | 378| ---------- | ------- | ---- | ----------------------------------------------------- | 379| callback | Callback\<[FullscreenInfo](#fullscreeninfo18)> | Yes | Callback that indicates whether the video playback is in full-screen mode.| 380 381## FullscreenInfo<sup>18+</sup> 382 383Describes whether the video is in full-screen playback mode. 384 385> **NOTE** 386> 387> To standardize anonymous object definitions, the element definitions here have been revised in API version 18. While historical version information is preserved for anonymous objects, there may be cases where the outer element's @since version number is higher than inner elements'. This does not affect interface usability. 388 389**Atomic service API**: This API can be used in atomic services since API version 18. 390 391**System capability**: SystemCapability.ArkUI.ArkUI.Full 392 393| Name | Type | Read Only| Optional| Description | 394| ----------- | ------- | ---- | ---- | ---------------------------- | 395| fullscreen<sup>10+</sup> | boolean | No| No | Callback that indicates whether the video playback is in full-screen mode.<br>**true**: The video playback is in full-screen mode.<br>**false**: The video playback is not in full-screen mode.<br>Default value: **false**<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 396 397## PreparedInfo<sup>18+</sup> 398 399Describes the duration of the video. 400 401> **NOTE** 402> 403> To standardize anonymous object definitions, the element definitions here have been revised in API version 18. While historical version information is preserved for anonymous objects, there may be cases where the outer element's @since version number is higher than inner elements'. This does not affect interface usability. 404 405**Atomic service API**: This API can be used in atomic services since API version 18. 406 407**System capability**: SystemCapability.ArkUI.ArkUI.Full 408 409| Name | Type | Read Only| Optional| Description | 410| ----------- | ------- | ---- | ---- | ---------------------------- | 411| duration<sup>10+</sup> | number | No| No | Duration of the video.<br>Unit: second<br>Value range: [0, +∞)<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 412 413## PlaybackInfo<sup>18+</sup> 414 415Describes the current progress of video playback. 416 417> **NOTE** 418> 419> To standardize anonymous object definitions, the element definitions here have been revised in API version 18. While historical version information is preserved for anonymous objects, there may be cases where the outer element's @since version number is higher than inner elements'. This does not affect interface usability. 420 421**Atomic service API**: This API can be used in atomic services since API version 18. 422 423**System capability**: SystemCapability.ArkUI.ArkUI.Full 424 425| Name | Type | Read Only| Optional| Description | 426| ----------- | ------- | ---- | ---- | ---------------------------- | 427| time<sup>10+</sup> | number | No| No | Callback that provides the current playback progress.<br>Unit: second<br>Value range: [0, +∞)<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 428 429## PosterOptions<sup>18+</sup> 430 431Defines display options for the first frame of the video. 432 433**Atomic service API**: This API can be used in atomic services since API version 18. 434 435**System capability**: SystemCapability.ArkUI.ArkUI.Full 436 437| Name | Type | Read Only| Optional| Description | 438| ----------- | ------- | ---- | ---- | ---------------------------- | 439| showFirstFrame | boolean | No| Yes| Whether to enable first frame display, showing the first frame of the video as a preview.<br>**true**: Enable first frame display.<br>**false**: Disable first frame display.<br>Default value: **false**. | 440 441## VideoController 442 443Defines a **VideoController** object to control one or more **Video** components. For details about available video playback examples, see [@ohos.multimedia.media](../../apis-media-kit/arkts-apis-media.md). 444 445**Atomic service API**: This API can be used in atomic services since API version 11. 446 447**System capability**: SystemCapability.ArkUI.ArkUI.Full 448 449### Objects to Import 450 451```ts 452let controller: VideoController = new VideoController() 453``` 454 455### constructor 456 457constructor() 458 459A constructor used to create a **VideoController** object. 460 461**Atomic service API**: This API can be used in atomic services since API version 11. 462 463**System capability**: SystemCapability.ArkUI.ArkUI.Full 464 465### start 466 467start() 468 469Starts playback. 470 471**Atomic service API**: This API can be used in atomic services since API version 11. 472 473**System capability**: SystemCapability.ArkUI.ArkUI.Full 474 475### pause 476 477pause() 478 479Pauses playback. The current frame is then displayed, and playback will be resumed from this paused position. 480 481**Atomic service API**: This API can be used in atomic services since API version 11. 482 483**System capability**: SystemCapability.ArkUI.ArkUI.Full 484 485### stop 486 487stop() 488 489Stops playback. The current frame is then displayed, and playback will restart from the very beginning. 490 491**Atomic service API**: This API can be used in atomic services since API version 11. 492 493**System capability**: SystemCapability.ArkUI.ArkUI.Full 494 495### reset<sup>12+</sup> 496 497reset(): void 498 499Resets the **AVPlayer** instance of this component, which displays the current frame and sets the playback to start from the beginning for subsequent playbacks. 500 501**Atomic service API**: This API can be used in atomic services since API version 12. 502 503**System capability**: SystemCapability.ArkUI.ArkUI.Full 504 505### setCurrentTime 506 507setCurrentTime(value: number) 508 509Sets the video playback position. 510 511> **NOTE** 512> 513> To start playback from a specific position, disable autoplay, wait for video preparation to complete, and then seek to the target position. 514 515**Atomic service API**: This API can be used in atomic services since API version 11. 516 517**System capability**: SystemCapability.ArkUI.ArkUI.Full 518 519**Parameters** 520 521| Name | Type | Mandatory | Description | 522| ----- | ------ | ---- | -------------- | 523| value | number | Yes | Video playback position.<br>Value range: [0, [duration](ts-media-components-video.md#preparedinfo18)], in seconds<br>Since API version 8, seek mode configuration is supported. For details, see [setCurrentTime<sup>8+</sup>](#setcurrenttime8).| 524 525### requestFullscreen 526 527requestFullscreen(value: boolean) 528 529Requests full-screen playback. 530 531**Atomic service API**: This API can be used in atomic services since API version 11. 532 533**System capability**: SystemCapability.ArkUI.ArkUI.Full 534 535**Parameters** 536 537| Name| Type| Mandatory| Description | 538| ------ | -------- | ---- | -------------------------------- | 539| value | boolean | Yes | Whether to request full-screen playback (filling the application window).<br>**true**: Request full-screen playback.<br>**false**: Do not request full-screen playback.<br>Default value: **false**.| 540 541> **NOTE** 542> 543> The built-in full-screen feature of the **Video** component only sets the video content to full screen and displays the default controller. It does not support displaying a custom title or controller. If additional functionality is required, implement custom full-screen features. 544 545### exitFullscreen 546 547exitFullscreen() 548 549Exits full-screen mode. 550 551**Atomic service API**: This API can be used in atomic services since API version 11. 552 553**System capability**: SystemCapability.ArkUI.ArkUI.Full 554 555### setCurrentTime<sup>8+</sup> 556 557setCurrentTime(value: number, seekMode: SeekMode) 558 559Sets the video playback position with the specified seek mode. 560 561**Atomic service API**: This API can be used in atomic services since API version 11. 562 563**System capability**: SystemCapability.ArkUI.ArkUI.Full 564 565**Parameters** 566 567| Name | Type | Mandatory | Description | 568| -------- | -------- | ---- | -------------- | 569| value | number | Yes | Video playback position, in seconds.| 570| seekMode | [SeekMode](#seekmode8) | Yes | Seek mode. | 571 572## SeekMode<sup>8+</sup> 573 574Enumerates video seek modes. 575 576**Atomic service API**: This API can be used in atomic services since API version 11. 577 578**System capability**: SystemCapability.ArkUI.ArkUI.Full 579 580| Name | Description | 581| ---------------- | ---------------------------- | 582| PreviousKeyframe | Seek to the nearest previous keyframe. | 583| NextKeyframe | Seek to the nearest next keyframe. | 584| ClosestKeyframe | Seek to the nearest keyframe. | 585| Accurate | Seek to a specific frame, regardless of whether the frame is a keyframe.| 586 587## Example 588 589### Example 1: Implementing Basic Video Playback Features 590 591This example covers the basic features of video playback, including how to manage the control bar, use preview images, handle autoplay, adjust the playback speed, respond to keyboard shortcuts, and operate the controller for playback control. Additionally, it demonstrates how to implement first frame display and the callbacks for various playback states. 592 593```ts 594// xxx.ets 595@Entry 596@Component 597struct VideoCreateComponent { 598 @State videoSrc: Resource = $rawfile('video1.mp4'); 599 @State previewUri: Resource = $r('app.media.poster1'); 600 @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X; 601 @State isAutoPlay: boolean = false; 602 @State showControls: boolean = true; 603 @State isShortcutKeyEnabled: boolean = false; 604 @State showFirstFrame: boolean = false; 605 controller: VideoController = new VideoController(); 606 607 build() { 608 Column() { 609 Video({ 610 src: this.videoSrc, 611 previewUri: this.previewUri, // Set the preview image. 612 currentProgressRate: this.curRate, // Set the playback speed. 613 controller: this.controller, 614 posterOptions: { showFirstFrame: this.showFirstFrame } // Disable first frame display. 615 }) 616 .width('100%') 617 .height(600) 618 .autoPlay(this.isAutoPlay) 619 .controls(this.showControls) 620 .enableShortcutKey(this.isShortcutKeyEnabled) 621 .onStart(() => { 622 console.info('onStart') 623 }) 624 .onPause(() => { 625 console.info('onPause') 626 }) 627 .onFinish(() => { 628 console.info('onFinish') 629 }) 630 .onError(() => { 631 console.info('onError') 632 }) 633 .onStop(() => { 634 console.info('onStop') 635 }) 636 .onPrepared((e?: DurationObject) => { 637 if (e != undefined) { 638 console.info('onPrepared is ' + e.duration) 639 } 640 }) 641 .onSeeking((e?: TimeObject) => { 642 if (e != undefined) { 643 console.info('onSeeking is ' + e.time) 644 } 645 }) 646 .onSeeked((e?: TimeObject) => { 647 if (e != undefined) { 648 console.info('onSeeked is ' + e.time) 649 } 650 }) 651 .onUpdate((e?: TimeObject) => { 652 if (e != undefined) { 653 console.info('onUpdate is ' + e.time) 654 } 655 }) 656 .onFullscreenChange((e?: FullscreenObject) => { 657 if (e != undefined) { 658 console.info('onFullscreenChange is ' + e.fullscreen) 659 } 660 }) 661 662 Row() { 663 Button('src').onClick(() => { 664 this.videoSrc = $rawfile('video2.mp4') // Switch the video source. 665 }).margin(5) 666 Button('previewUri').onClick(() => { 667 this.previewUri = $r('app.media.poster2') // Switch the preview image. 668 }).margin(5) 669 Button('controls').onClick(() => { 670 this.showControls = !this.showControls // Specify whether to show the control bar. 671 }).margin(5) 672 } 673 674 Row() { 675 Button('start').onClick(() => { 676 this.controller.start() // Start playback. 677 }).margin(2) 678 Button('pause').onClick(() => { 679 this.controller.pause() // Pause playback. 680 }).margin(2) 681 Button('stop').onClick(() => { 682 this.controller.stop() // Stop playback. 683 }).margin(2) 684 Button('reset').onClick(() => { 685 this.controller.reset () // Reset the AVPlayer instance. 686 }).margin(2) 687 Button('setTime').onClick(() => { 688 this.controller.setCurrentTime(10, SeekMode.Accurate) // Seek to the 10s position of the video. 689 }).margin(2) 690 } 691 692 Row() { 693 Button('rate 0.75').onClick(() => { 694 this.curRate = PlaybackSpeed.Speed_Forward_0_75_X // Play the video at the 0.75x speed. 695 }).margin(5) 696 Button('rate 1').onClick(() => { 697 this.curRate = PlaybackSpeed.Speed_Forward_1_00_X // Play the video at the 1x speed. 698 }).margin(5) 699 Button('rate 2').onClick(() => { 700 this.curRate = PlaybackSpeed.Speed_Forward_2_00_X // Play the video at the 2x speed. 701 }).margin(5) 702 } 703 } 704 } 705} 706 707interface DurationObject { 708 duration: number; 709} 710 711interface TimeObject { 712 time: number; 713} 714 715interface FullscreenObject { 716 fullscreen: boolean; 717} 718``` 719 720### Example 2: Enabling AI Image Analyzer 721 722This example shows how to use the **enableAnalyzer** attribute to enable AI image analyzer. 723 724```ts 725// xxx.ets 726@Entry 727@Component 728struct ImageAnalyzerExample { 729 @State videoSrc: Resource = $rawfile('video1.mp4'); 730 @State previewUri: Resource = $r('app.media.poster1'); 731 controller: VideoController = new VideoController(); 732 config: ImageAnalyzerConfig = { 733 types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT] 734 } 735 private aiController: ImageAnalyzerController = new ImageAnalyzerController(); 736 private options: ImageAIOptions = { 737 types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT], 738 aiController: this.aiController 739 } 740 741 build() { 742 Column() { 743 Video({ 744 src: this.videoSrc, 745 previewUri: this.previewUri, 746 controller: this.controller, 747 imageAIOptions: this.options // Set the AI image analysis options. 748 }) 749 .width('100%') 750 .height(600) 751 .controls(false) 752 .enableAnalyzer(true) 753 .analyzerConfig(this.config) 754 .onStart(() => { 755 console.info('onStart') 756 }) 757 .onPause(() => { 758 console.info('onPause') 759 }) 760 761 Row() { 762 Button('start').onClick(() => { 763 this.controller.start() // Start playback. 764 }).margin(5) 765 Button('pause').onClick(() => { 766 this.controller.pause() // Pause playback. 767 }).margin(5) 768 Button('getTypes').onClick(() => { 769 this.aiController.getImageAnalyzerSupportTypes() 770 }).margin(5) 771 } 772 } 773 } 774} 775``` 776 777### Example 3: Playing a Dragged-in Video 778 779This example demonstrates how to enable the **Video** component to play a video that is dragged into it. 780 781```ts 782// xxx.ets 783import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData'; 784 785@Entry 786@Component 787struct Index { 788 @State videoSrc: Resource | string = $rawfile('video1.mp4'); 789 private controller: VideoController = new VideoController(); 790 791 build() { 792 Column() { 793 Video({ 794 src: this.videoSrc, 795 controller: this.controller 796 }) 797 .width('100%') 798 .height(600) 799 .onPrepared(() => { 800 // Start playing the video when it is ready. 801 this.controller.start(); 802 }) 803 .onDrop((e: DragEvent) => { 804 // Handle the drop event when a video is dragged into the component. 805 // The DragEvent contains the information about the dragged-in video source. After the information is obtained, assign a value to the state variable videoSrc to change the video source of the video. 806 let record = e.getData().getRecords()[0]; 807 if (record.getType() == uniformTypeDescriptor.UniformDataType.VIDEO) { 808 let videoInfo = record as unifiedDataChannel.Video; 809 this.videoSrc = videoInfo.videoUri; 810 } 811 }) 812 } 813 } 814} 815``` 816### Example 4: Setting the Video Fill Mode 817 818This example shows how to set the video fill mode using the **objectFit** attribute. 819 820```ts 821// xxx.ets 822@Entry 823@Component 824struct VideoObject { 825 @State videoSrc: Resource = $rawfile('rabbit.mp4'); 826 @State previewUri: Resource = $r('app.media.tree'); 827 @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X; 828 @State showControls: boolean = true; 829 controller: VideoController = new VideoController(); 830 831 build() { 832 Column() { 833 Text("ImageFit.Contain").fontSize(12) 834 Video({ 835 src: this.videoSrc, 836 previewUri: this.previewUri, 837 currentProgressRate: this.curRate, 838 controller: this.controller 839 }) 840 .width(350) 841 .height(230) 842 .controls(this.showControls) 843 .objectFit(ImageFit.Contain)// Set the video fill mode to ImageFit.Contain. 844 .margin(5) 845 846 Text("ImageFit.Fill").fontSize(12) 847 Video({ 848 src: this.videoSrc, 849 previewUri: this.previewUri, 850 currentProgressRate: this.curRate, 851 controller: this.controller 852 }) 853 .width(350) 854 .height(230) 855 .controls(this.showControls) 856 .objectFit(ImageFit.Fill)// Set the video fill mode to ImageFit.Fill. 857 .margin(5) 858 859 Text("ImageFit.START").fontSize(12) 860 Video({ 861 src: this.videoSrc, 862 previewUri: this.previewUri, 863 currentProgressRate: this.curRate, 864 controller: this.controller 865 }) 866 .width(350) 867 .height(230) 868 .controls(this.showControls) 869 .objectFit(ImageFit.START)// Set the video fill mode to ImageFit.START. 870 .margin(5) 871 }.width('100%').alignItems(HorizontalAlign.Center) 872 } 873} 874``` 875 876 877### Example 5: Handling Errors with onError 878 879The following example uses an invalid video resource path to demonstrate how the **Video** component can obtain error codes through the **onError** event. 880 881```ts 882// xxx.ets 883@Entry 884@Component 885struct VideoErrorComponent { 886 @State videoSrc: string = "video.mp4"; // Enter an invalid video resource path. 887 @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X; 888 @State isAutoPlay: boolean = false; 889 @State showControls: boolean = true; 890 @State showFirstFrame: boolean = false; 891 controller: VideoController = new VideoController(); 892 @State errorMessage: string = ""; 893 894 build() { 895 Column() { 896 Video({ 897 src: this.videoSrc, 898 currentProgressRate: this.curRate, 899 controller: this.controller, 900 }) 901 .width(200) 902 .height(120) 903 .margin(5) 904 .autoPlay(this.isAutoPlay) 905 .controls(this.showControls) 906 .onError((err) => { 907 // Obtain the error code through the onError event, where code indicates the error code, and message indicates the error message. 908 console.error(`code is ${err.code}, message is ${err.message}`); 909 this.errorMessage = `code is ${err.code}, message is ${err.message}`; 910 }) 911 // Pass in an invalid video resource path. Expected result: "code is 103602, message is Not a valid source." 912 Text(this.errorMessage) 913 } 914 .width("100%") 915 .height("100%") 916 .backgroundColor('rgb(213,213,213)') 917 } 918} 919``` 920 921 922 923### Example 6: Dynamically Setting Attributes and Methods of the Video Component Using attributeModifier 924 925The following example demonstrates how to use **attributeModifier** to dynamically set the attributes and methods of the **Video** component, including AI image analysis features and playback event methods. 926 927```ts 928// xxx.ets 929class MyVideoModifier implements AttributeModifier<VideoAttribute> { 930 applyNormalAttribute(instance: VideoAttribute): void { 931 // Enable the AI image analyzer, which can be triggered by a long press. 932 instance.enableAnalyzer(true) 933 let config: ImageAnalyzerConfig = { 934 types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT] 935 } 936 instance.analyzerConfig(config) 937 instance.onStart(() => { 938 console.info('video: onStart') 939 }) 940 instance.onPause(() => { 941 console.info('video: onPause') 942 }) 943 instance.onFinish(() => { 944 console.info('video: onFinish') 945 }) 946 instance.onError((err) => { 947 console.error('video: onError is code = ' + err.code + ', message = ' + err.message) 948 }) 949 instance.onStop(() => { 950 console.info('video: onStop') 951 }) 952 instance.onPrepared((e?: DurationObject) => { 953 if (e != undefined) { 954 console.info('video: onPrepared is ' + e.duration) 955 } 956 }) 957 instance.onSeeking((e?: TimeObject) => { 958 if (e != undefined) { 959 console.info('video: onSeeking is ' + e.time) 960 } 961 }) 962 instance.onSeeked((e?: TimeObject) => { 963 if (e != undefined) { 964 console.info('video: onSeeked is ' + e.time) 965 } 966 }) 967 instance.onUpdate((e?: TimeObject) => { 968 if (e != undefined) { 969 console.info('video: onUpdate is ' + e.time) 970 } 971 }) 972 instance.onFullscreenChange((e?: FullscreenObject) => { 973 if (e != undefined) { 974 console.info('video: onFullscreenChange is ' + e.fullscreen) 975 } 976 }) 977 } 978} 979 980@Entry 981@Component 982struct VideoModifierDemo { 983 @State videoSrc: Resource = $rawfile('video.mp4'); 984 @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X; 985 @State isAutoPlay: boolean = false; 986 @State showControls: boolean = false; 987 controller: VideoController = new VideoController(); 988 @State modifier: MyVideoModifier = new MyVideoModifier(); 989 990 build() { 991 Column() { 992 Video({ 993 src: this.videoSrc, 994 currentProgressRate: this.curRate, // Set the playback speed. 995 controller: this.controller 996 }) 997 .width(300) 998 .height(180) 999 .autoPlay(this.isAutoPlay) 1000 .controls(this.showControls) 1001 .attributeModifier(this.modifier) 1002 Row() { 1003 Button('start').onClick(() => { 1004 this.controller.start() // Start playback. 1005 }).margin(2) 1006 Button('pause').onClick(() => { 1007 this.controller.pause() // Pause playback. 1008 }).margin(2) 1009 Button('stop').onClick(() => { 1010 this.controller.stop() // Stop playback. 1011 }).margin(2) 1012 Button('reset').onClick(() => { 1013 this.controller.reset () // Reset the AVPlayer instance. 1014 }).margin(2) 1015 } 1016 1017 Row() { 1018 Button('Fullscreen').onClick(() => { 1019 this.controller.requestFullscreen(true) // Enable full-screen mode. 1020 }).margin(2) 1021 Button('showControls').onClick(() => { 1022 this.showControls = !this.showControls // Specify whether to show the control bar. 1023 }).margin(2) 1024 } 1025 } 1026 } 1027} 1028 1029interface DurationObject { 1030 duration: number; 1031} 1032 1033interface TimeObject { 1034 time: number; 1035} 1036 1037interface FullscreenObject { 1038 fullscreen: boolean; 1039} 1040``` 1041 1042 1043<!--no_check-->