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