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