ts-media-components-video.md 8.1 KB
Newer Older
Z
zengyawen 已提交
1 2
# Video

E
ester.zhou 已提交
3
>  **NOTE**<br>
Z
zengyawen 已提交
4 5
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

E
ester.zhou 已提交
6 7 8 9 10
The **\<Video>** component provides a video player.

## Required Permissions

To use online videos, you need to add the **ohos.permission.INTERNET** permission to the corresponding **abilities** in the **config.json** or **module.json** file, whichever is appropriate.
Z
zengyawen 已提交
11

Mr-YX's avatar
Mr-YX 已提交
12
```json
E
ester.zhou 已提交
13 14 15 16 17 18 19 20
"abilities":[
  {
    ...
    "permissions": ["ohos.permission.INTERNET],
    ...
  }
]
```
Z
zengyawen 已提交
21 22 23

## Child Components

E
ester.zhou 已提交
24
Not supported
Z
zengyawen 已提交
25 26 27

## APIs

E
ester.zhou 已提交
28
Video(value: VideoOptions)
Z
zengyawen 已提交
29

Mr-YX's avatar
Mr-YX 已提交
30
1. VideoOptions attributes
E
ester.zhou 已提交
31
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
32
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
33 34 35 36
  | src | string \| [Resource](../../ui/ts-types.md) | No| - | Path of the video source, which can be a local path or a URL.<br>The video resources can be stored in the **video** or **rawfile** folder under **resources**.<br>The path can include a **dataability://** prefix, which is used to access the video path provided by a Data ability. For details about the path, see [Data Ability Development](../../ability/fa-dataability.md).|
  | currentProgressRate | number \| PlaybackSpeed<sup>8+</sup> | No| 1.0 \| PlaybackSpeed.<br>Speed_Forward_1_00_X | 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>|
  | previewUri | string \| PixelMap<sup>8+</sup> \| [Resource](../../ui/ts-types.md) | No| - | Path of the preview image.|
  | controller | [VideoController](#videocontroller) | No| - | Controller.|
Z
zengyawen 已提交
37 38


Mr-YX's avatar
Mr-YX 已提交
39 40 41 42 43 44 45 46
2. PlaybackSpeed<sup>8+</sup>
  | Name                 | Description           |
  | -------------------- | --------------------- |
  | Speed_Forward_0_75_X | 0.75x playback speed. |
  | Speed_Forward_1_00_X | 1x playback speed.    |
  | Speed_Forward_1_25_X | 1.25x playback speed. |
  | Speed_Forward_1_75_X | 1.75x playback speed. |
  | Speed_Forward_2_00_X | 2x playback speed.    |
Z
zengyawen 已提交
47 48 49

## Attributes

Mr-YX's avatar
Mr-YX 已提交
50 51 52 53 54 55 56
| Name      | Type                                     | Default Value | Description                                        |
| --------- | ---------------------------------------- | ------------- | -------------------------------------------------- |
| muted     | boolean                                  | false         | Whether the video is muted.                        |
| autoPlay  | boolean                                  | false         | Whether to enable auto play.                       |
| controls  | boolean                                  | true          | Whether to display the video playback control bar. |
| objectFit | [ImageFit](ts-basic-components-image.md) | Cover         | Video scale type.                                  |
| loop      | boolean                                  | false         | Whether to repeat the video.                       |
Z
zengyawen 已提交
57 58 59 60


## Events

E
ester.zhou 已提交
61
| Name| Description|
Z
zengyawen 已提交
62
| -------- | -------- |
E
ester.zhou 已提交
63 64 65 66 67 68 69 70
| onStart() =&gt; void | Triggered when the video is played.|
| onPause() =&gt; void | Triggered when the video playback is paused.|
| onFinish() =&gt; void | Triggered when the video playback is finished.|
| onError() =&gt; void | Triggered when the video playback fails.|
| onPrepared(event?: { duration: number }) =&gt; void | Triggered when video preparation is complete. The video duration (in seconds) is obtained from **duration**.|
| onSeeking(event?: { time: number }) =&gt; void | Triggered to report the time (in seconds) when the progress bar is being dragged.|
| onSeeked(event?: { time: number }) =&gt; void | Triggered to report the playback time (in seconds) when the user finishes dragging the progress bar.|
| onUpdate(event?: { time: number }) =&gt; void | Triggered once per 250 ms when the playback progress changes. The unit of the current playback time is second.|
Z
zengyawen 已提交
71 72


E
ester.zhou 已提交
73
## VideoController
Z
zengyawen 已提交
74

E
ester.zhou 已提交
75
A **VideoController** object can control one or more videos.
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77 78 79

### Objects to Import

Mr-YX's avatar
Mr-YX 已提交
80
```ts
E
ester.zhou 已提交
81 82 83
controller: VideoController = new VideoController();
```

E
ester.zhou 已提交
84

E
ester.zhou 已提交
85 86
### start

Mr-YX's avatar
Mr-YX 已提交
87
```ts
E
ester.zhou 已提交
88 89 90
start(): void

Starts playback.
Mr-YX's avatar
Mr-YX 已提交
91
```
E
ester.zhou 已提交
92 93 94

### pause

Mr-YX's avatar
Mr-YX 已提交
95
```ts
E
ester.zhou 已提交
96 97 98
pause(): void

Pauses playback.
Mr-YX's avatar
Mr-YX 已提交
99
```
E
ester.zhou 已提交
100 101 102

### stop

Mr-YX's avatar
Mr-YX 已提交
103
```ts
E
ester.zhou 已提交
104 105 106
stop(): void

Stops playback.
Mr-YX's avatar
Mr-YX 已提交
107
```
E
ester.zhou 已提交
108 109 110

### setCurrentTime

Mr-YX's avatar
Mr-YX 已提交
111
```ts
E
ester.zhou 已提交
112 113 114
setCurrentTime(value: number)

Sets the video playback position.
Mr-YX's avatar
Mr-YX 已提交
115
```
E
ester.zhou 已提交
116 117

- Parameters
Mr-YX's avatar
Mr-YX 已提交
118 119 120
  | Name  | Type   | Mandatory | Default Value | Description              |
  | ----- | ------ | --------- | ------------- | ------------------------ |
  | value | number | Yes       | -             | Video playback position. |
E
ester.zhou 已提交
121 122 123 124 125 126 127 128

### requestFullscreen

requestFullscreen(value: boolean)

Requests full-screen mode.

- Parameters
Mr-YX's avatar
Mr-YX 已提交
129 130
  | Name  | Type   | Mandatory | Default Value | Description                                  |
  | ----- | ------ | --------- | ------------- | -------------------------------------------- |
C
caocan 已提交
131
  | value | boolean | Yes       | false         | Whether the playback is in full-screen mode. |
E
ester.zhou 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144

### exitFullscreen

exitFullscreen()

Exits full-screen mode.

### setCurrentTime<sup>8+</sup>

setCurrentTime(value: number, seekMode: SeekMode)

Sets the video playback position with the specified seek mode.

Mr-YX's avatar
Mr-YX 已提交
145 146 147 148 149
1. Parameters
  | Name     | Type     | Mandatory | Default Value | Description              |
  | -------- | -------- | --------- | ------------- | -----------------------  |
  | value    | number   | Yes       | -             | Video playback position. |
  | seekMode | SeekMode | Yes       | -             | Seek mode.               |
Z
zengyawen 已提交
150

Mr-YX's avatar
Mr-YX 已提交
151 152 153 154 155 156 157
2. SeekMode<sup>8+</sup>
  | Name             | Description                                                               |
  | ---------------- | ------------------------------------------------------------------------- |
  | PreviousKeyframe | Seeks to the nearest previous keyframe.                                   |
  | NextKeyframe     | Seeks to the nearest next keyframe.                                       |
  | ClosestKeyframe  | Seeks to the nearest keyframe.                                            |
  | Accurate         | Seeks to a specific frame, regardless of whether the frame is a keyframe. |
Z
zengyawen 已提交
158

E
ester.zhou 已提交
159
## Example
Z
zengyawen 已提交
160

E
ester.zhou 已提交
161 162
```ts
// xxx.ets
Z
zengyawen 已提交
163 164 165
@Entry
@Component
struct VideoCreateComponent {
E
ester.zhou 已提交
166 167
  @State srcs: Resource = $rawfile('video1');
  @State previewUris: Resource = $r('app.media.img');
Z
zengyawen 已提交
168 169 170
  @State currentProgressRates: number = 1;
  @State autoPlays: boolean = false;
  @State controlsss: boolean = true;
E
ester.zhou 已提交
171
  controller: VideoController = new VideoController();
Z
zengyawen 已提交
172 173 174 175 176 177 178
  @State startStaus: boolean = true;
  build() {
    Column() {
      Video({
        src: this.srcs,
        previewUri: this.previewUris, 
        currentProgressRate: this.currentProgressRates,
E
ester.zhou 已提交
179
        controller: this.controller
Z
zengyawen 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      }).width(700).height(500)
        .autoPlay(this.autoPlays)
        .controls(this.controlsss)
        .onStart(() => {
                  console.error('onStart');
                })
        .onPause(() => {
                  console.error('onPause');
                })
        .onFinish(() => {
                  console.error('onFinish');
                })
        .onError(() => {
                  console.error('onFinish');
                })
        .onPrepared((e) => {
                    console.error('onPrepared is ' + e.duration);
                })
        .onSeeking((e) => {
                    console.error('onSeeking is ' + e.time);
                })
        .onSeeked((e) => {
                    console.error('onSeekedis ' + e.time);
                })
        .onUpdate((e) => {
                    console.error('onUpdateis ' + e.time);
                })
      Row() {
        Button("src").onClick(() => {
E
ester.zhou 已提交
209
            this.srcs = $rawfile('video2');
Z
zengyawen 已提交
210 211
        });
        Button("previewUri").onClick(() => {
E
ester.zhou 已提交
212
            this.previewUris = $r('app.media.img1');
Z
zengyawen 已提交
213 214 215 216 217 218 219 220
        });
        Button("controlsss").onClick(() => {
          this.controlsss = !this.controlsss;
        });
      }

      Row() {
        Button("start").onClick(() => {
E
ester.zhou 已提交
221
          this.controller.start();
Z
zengyawen 已提交
222 223
        });
        Button("pause").onClick(() => {
E
ester.zhou 已提交
224
          this.controller.pause();
Z
zengyawen 已提交
225 226
        });
        Button("stop").onClick(() => {
E
ester.zhou 已提交
227
          this.controller.stop();
Z
zengyawen 已提交
228 229 230 231 232
        });
      }

      Row() {
        Button("setCurrentTime").onClick(() => {
E
ester.zhou 已提交
233
          this.controller.setCurrentTime(9, SeekMode.Accurate);
Z
zengyawen 已提交
234 235 236 237 238
        });
      }
    }
  }
}
Mr-YX's avatar
Mr-YX 已提交
239
```