1e41f4b71Sopenharmony_ci# Video Playback (Video) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThe **\<Video>** component is used to play a video and control its playback. It is usually used in video players and video list pages within applications. A video automatically plays once fully loaded. When the user clicks the video area, the video is paused and the playback progress bar is displayed. The user can drag the progress bar to the desired position. For details, see [Video](../reference/apis-arkui/arkui-ts/ts-media-components-video.md). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Creating a \<Video> Component 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciYou can create a **\<Video>** component by calling the following API: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciVideo(value: VideoOptions) 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciA **VideoOptions** object contains the **src**, **currentProgressRate**, **previewUri**, and **controller** parameters. In this API, **src** indicates the path of the video source, **currentProgressRate** indicates the video playback speed, **previewUri** indicates the path of the preview image, and **controller** indicates the video controller . For details about how to load a video, see [Loading Video](#loading-video). For details about **VideoOptions**, see [VideoOptions](../reference/apis-arkui/arkui-ts/ts-media-components-video.md#videooptions). 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Loading Video 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciThe **\<Video>** component supports both local and online videos. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### Loading a Local Video 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci- Common local video 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci To load a local video, specify the corresponding video file in the local **rawfile** directory, as shown in the following figure. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci  28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci Use **$rawfile()** to reference the video resource. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci ```ts 32e41f4b71Sopenharmony_ci @Component 33e41f4b71Sopenharmony_ci export struct VideoPlayer{ 34e41f4b71Sopenharmony_ci private controller:VideoController | undefined; 35e41f4b71Sopenharmony_ci private previewUris: Resource = $r ('app.media.preview'); 36e41f4b71Sopenharmony_ci private innerResource: Resource = $rawfile('videoTest.mp4'); 37e41f4b71Sopenharmony_ci build(){ 38e41f4b71Sopenharmony_ci Column() { 39e41f4b71Sopenharmony_ci Video({ 40e41f4b71Sopenharmony_ci src: this.innerResource, 41e41f4b71Sopenharmony_ci previewUri: this.previewUris, 42e41f4b71Sopenharmony_ci controller: this.controller 43e41f4b71Sopenharmony_ci }) 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci } 46e41f4b71Sopenharmony_ci } 47e41f4b71Sopenharmony_ci ``` 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci- Video provided by a [DataAbility](../application-models/dataability-overview.md), whose path contains the **dataability://** prefix<br>Ensure that the corresponding video resource exists. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci ```ts 53e41f4b71Sopenharmony_ci @Component 54e41f4b71Sopenharmony_ci export struct VideoPlayer{ 55e41f4b71Sopenharmony_ci private controller:VideoController | undefined; 56e41f4b71Sopenharmony_ci private previewUris: Resource = $r ('app.media.preview'); 57e41f4b71Sopenharmony_ci private videoSrc: string = 'dataability://device_id/com.domainname.dataability.videodata/video/10' 58e41f4b71Sopenharmony_ci build(){ 59e41f4b71Sopenharmony_ci Column() { 60e41f4b71Sopenharmony_ci Video({ 61e41f4b71Sopenharmony_ci src: this.videoSrc, 62e41f4b71Sopenharmony_ci previewUri: this.previewUris, 63e41f4b71Sopenharmony_ci controller: this.controller 64e41f4b71Sopenharmony_ci }) 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci ``` 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci### Loading a Video in the Application Sandbox 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciTo load a video in the application sandbox, use a string with the **file:///data/storage** prefix. Ensure that there are files in the specified path and the application has the read permission to the files. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci```ts 75e41f4b71Sopenharmony_ci@Component 76e41f4b71Sopenharmony_ciexport struct VideoPlayer { 77e41f4b71Sopenharmony_ci private controller: VideoController | undefined; 78e41f4b71Sopenharmony_ci private videoSrc: string = 'file:///data/storage/el2/base/haps/entry/files/show.mp4' 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci build() { 81e41f4b71Sopenharmony_ci Column() { 82e41f4b71Sopenharmony_ci Video({ 83e41f4b71Sopenharmony_ci src: this.videoSrc, 84e41f4b71Sopenharmony_ci controller: this.controller 85e41f4b71Sopenharmony_ci }) 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci } 88e41f4b71Sopenharmony_ci} 89e41f4b71Sopenharmony_ci``` 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci### Loading an Online Video 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciTo load online videos, you must apply for the **ohos.permission.INTERNET** permission. For details about how to apply for the permission, see [Declaring Permissions](../security/AccessToken/declare-permissions.md). In this scenario, the **src** attribute indicates the URL of the online video. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci```ts 98e41f4b71Sopenharmony_ci@Component 99e41f4b71Sopenharmony_ciexport struct VideoPlayer{ 100e41f4b71Sopenharmony_ci private controller:VideoController | undefined; 101e41f4b71Sopenharmony_ci private previewUris: Resource = $r ('app.media.preview'); 102e41f4b71Sopenharmony_ci private videoSrc: string= 'https://www.example.com/example.mp4' // Replace the URL with that of the actual video to load. 103e41f4b71Sopenharmony_ci build(){ 104e41f4b71Sopenharmony_ci Column() { 105e41f4b71Sopenharmony_ci Video({ 106e41f4b71Sopenharmony_ci src: this.videoSrc, 107e41f4b71Sopenharmony_ci previewUri: this.previewUris, 108e41f4b71Sopenharmony_ci controller: this.controller 109e41f4b71Sopenharmony_ci }) 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci} 113e41f4b71Sopenharmony_ci``` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci## Adding Attributes 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ciUse the [attributes](../reference/apis-arkui/arkui-ts/ts-media-components-video.md#attributes) of the **\<Video>** component to control video playback. For example, you can set whether to mute the video and whether to display the video playback control bar. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci```ts 122e41f4b71Sopenharmony_ci@Component 123e41f4b71Sopenharmony_ciexport struct VideoPlayer { 124e41f4b71Sopenharmony_ci private controller: VideoController | undefined; 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci build() { 127e41f4b71Sopenharmony_ci Column() { 128e41f4b71Sopenharmony_ci Video({ 129e41f4b71Sopenharmony_ci controller: this.controller 130e41f4b71Sopenharmony_ci }) 131e41f4b71Sopenharmony_ci .muted(false) // Set whether to mute the video. 132e41f4b71Sopenharmony_ci .controls(false) // Set whether to display the video playback control bar. 133e41f4b71Sopenharmony_ci .autoPlay(false) // Set whether to enable auto play. 134e41f4b71Sopenharmony_ci .loop(false) // Set whether to repeat the video. 135e41f4b71Sopenharmony_ci .objectFit(ImageFit.Contain) // Set the video scale type. 136e41f4b71Sopenharmony_ci } 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci} 139e41f4b71Sopenharmony_ci``` 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci## Adding Events 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci The **\<Video>** component supports various callback events in addition to the universal events. For details, see [Events](../reference/apis-arkui/arkui-ts/ts-media-components-video.md#events). 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci```ts 147e41f4b71Sopenharmony_ci@Entry 148e41f4b71Sopenharmony_ci@Component 149e41f4b71Sopenharmony_cistruct VideoPlayer{ 150e41f4b71Sopenharmony_ci private controller:VideoController | undefined; 151e41f4b71Sopenharmony_ci private previewUris: Resource = $r ('app.media.preview'); 152e41f4b71Sopenharmony_ci private innerResource: Resource = $rawfile('videoTest.mp4'); 153e41f4b71Sopenharmony_ci build(){ 154e41f4b71Sopenharmony_ci Column() { 155e41f4b71Sopenharmony_ci Video({ 156e41f4b71Sopenharmony_ci src: this.innerResource, 157e41f4b71Sopenharmony_ci previewUri: this.previewUris, 158e41f4b71Sopenharmony_ci controller: this.controller 159e41f4b71Sopenharmony_ci }) 160e41f4b71Sopenharmony_ci .onUpdate((event) => { // Triggered when the playback progress changes. 161e41f4b71Sopenharmony_ci console.info("Video update."); 162e41f4b71Sopenharmony_ci }) 163e41f4b71Sopenharmony_ci .onPrepared((event) => { // Triggered when video preparation is complete. 164e41f4b71Sopenharmony_ci console.info("Video prepared."); 165e41f4b71Sopenharmony_ci }) 166e41f4b71Sopenharmony_ci .onError(() => { // Triggered when the video playback fails. 167e41f4b71Sopenharmony_ci console.info("Video error."); 168e41f4b71Sopenharmony_ci }) 169e41f4b71Sopenharmony_ci .onStop(() => { // Stop event callback. 170e41f4b71Sopenharmony_ci console.info("Video stoped."); 171e41f4b71Sopenharmony_ci }) 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci} 175e41f4b71Sopenharmony_ci``` 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## Using the Video Controller 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ciThe video controller is used to control video playback. For details, see [VideoController](../reference/apis-arkui/arkui-ts/ts-media-components-video.md#videocontroller). 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci- Default controller 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci The default controller supports four basic features: start playback, pause playback, set the video playback position, and play the video in full screen. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci ```ts 187e41f4b71Sopenharmony_ci @Entry 188e41f4b71Sopenharmony_ci @Component 189e41f4b71Sopenharmony_ci struct VideoGuide { 190e41f4b71Sopenharmony_ci @State videoSrc: Resource = $rawfile('videoTest.mp4') 191e41f4b71Sopenharmony_ci @State previewUri: string = 'common/videoIcon.png' 192e41f4b71Sopenharmony_ci @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X 193e41f4b71Sopenharmony_ci build() { 194e41f4b71Sopenharmony_ci Row() { 195e41f4b71Sopenharmony_ci Column() { 196e41f4b71Sopenharmony_ci Video({ 197e41f4b71Sopenharmony_ci src: this.videoSrc, 198e41f4b71Sopenharmony_ci previewUri: this.previewUri, 199e41f4b71Sopenharmony_ci currentProgressRate: this.curRate 200e41f4b71Sopenharmony_ci }) 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci .width('100%') 203e41f4b71Sopenharmony_ci } 204e41f4b71Sopenharmony_ci .height('100%') 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci } 207e41f4b71Sopenharmony_ci ``` 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci- Custom controller 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci To use a custom controller, disable the default controller, and then use components such as \<Button> and \<Slider> to customize the control and display. This type of controller is applicable to scenarios where customization requirements are involved. 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci ```ts 214e41f4b71Sopenharmony_ci @Entry 215e41f4b71Sopenharmony_ci @Component 216e41f4b71Sopenharmony_ci struct VideoGuide1 { 217e41f4b71Sopenharmony_ci @State videoSrc: Resource = $rawfile('videoTest.mp4') 218e41f4b71Sopenharmony_ci @State previewUri: string = 'common/videoIcon.png' 219e41f4b71Sopenharmony_ci @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X 220e41f4b71Sopenharmony_ci @State isAutoPlay: boolean = false 221e41f4b71Sopenharmony_ci @State showControls: boolean = true 222e41f4b71Sopenharmony_ci @State sliderStartTime: string = ''; 223e41f4b71Sopenharmony_ci @State currentTime: number = 0; 224e41f4b71Sopenharmony_ci @State durationTime: number = 0; 225e41f4b71Sopenharmony_ci @State durationStringTime: string =''; 226e41f4b71Sopenharmony_ci controller: VideoController = new VideoController() 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci build() { 229e41f4b71Sopenharmony_ci Row() { 230e41f4b71Sopenharmony_ci Column() { 231e41f4b71Sopenharmony_ci Video({ 232e41f4b71Sopenharmony_ci src: this.videoSrc, 233e41f4b71Sopenharmony_ci previewUri: this.previewUri, 234e41f4b71Sopenharmony_ci currentProgressRate: this.curRate, 235e41f4b71Sopenharmony_ci controller: this.controller 236e41f4b71Sopenharmony_ci }).controls(false).autoPlay(true) 237e41f4b71Sopenharmony_ci .onPrepared((event)=>{ 238e41f4b71Sopenharmony_ci if(event){ 239e41f4b71Sopenharmony_ci this.durationTime = event.duration 240e41f4b71Sopenharmony_ci } 241e41f4b71Sopenharmony_ci }) 242e41f4b71Sopenharmony_ci .onUpdate((event)=>{ 243e41f4b71Sopenharmony_ci if(event){ 244e41f4b71Sopenharmony_ci this.currentTime =event.time 245e41f4b71Sopenharmony_ci } 246e41f4b71Sopenharmony_ci }) 247e41f4b71Sopenharmony_ci Row() { 248e41f4b71Sopenharmony_ci Text(JSON.stringify(this.currentTime) + 's') 249e41f4b71Sopenharmony_ci Slider({ 250e41f4b71Sopenharmony_ci value: this.currentTime, 251e41f4b71Sopenharmony_ci min: 0, 252e41f4b71Sopenharmony_ci max: this.durationTime 253e41f4b71Sopenharmony_ci }) 254e41f4b71Sopenharmony_ci .onChange((value: number, mode: SliderChangeMode) => { 255e41f4b71Sopenharmony_ci this.controller.setCurrentTime(value); 256e41f4b71Sopenharmony_ci }).width("90%") 257e41f4b71Sopenharmony_ci Text(JSON.stringify(this.durationTime) + 's') 258e41f4b71Sopenharmony_ci } 259e41f4b71Sopenharmony_ci .opacity(0.8) 260e41f4b71Sopenharmony_ci .width("100%") 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci .width('100%') 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci .height('40%') 265e41f4b71Sopenharmony_ci } 266e41f4b71Sopenharmony_ci } 267e41f4b71Sopenharmony_ci ``` 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci## Remarks 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ciThe **\<Video>** component has encapsulated the basic capabilities of video playback. You do not need to create video instances or set and obtain video information. Simply set the data source and basic information to play videos. To customize video playback, see [Video Playback](../media/media/video-playback.md). 273