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