ts-media-components-video.md 7.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

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


## Child Components

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


## APIs

E
ester.zhou 已提交
30
Video(value: VideoOptions)
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32
- VideoOptions attributes
E
ester.zhou 已提交
33
  | Name| Type| Mandatory| Default Value| Description|
Z
zengyawen 已提交
34
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
35 36 37 38
  | 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 已提交
39 40 41


- PlaybackSpeed<sup>8+</sup>
E
ester.zhou 已提交
42
  | Name| Description|
Z
zengyawen 已提交
43
  | -------- | -------- |
E
ester.zhou 已提交
44 45 46 47 48
  | 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 已提交
49 50 51 52


## Attributes

E
ester.zhou 已提交
53
| Name| Type| Default Value| Description|
Z
zengyawen 已提交
54
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
55 56 57 58 59
| 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 已提交
60 61 62 63


## Events

E
ester.zhou 已提交
64
| Name| Description|
Z
zengyawen 已提交
65
| -------- | -------- |
E
ester.zhou 已提交
66 67 68 69 70 71 72 73
| 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 已提交
74 75


E
ester.zhou 已提交
76
## VideoController
Z
zengyawen 已提交
77

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

E
ester.zhou 已提交
80 81 82 83 84 85 86

### Objects to Import

```
controller: VideoController = new VideoController();
```

E
ester.zhou 已提交
87

E
ester.zhou 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
### start

start(): void

Starts playback.

### pause

pause(): void

Pauses playback.

### stop

stop(): void

Stops playback.

### setCurrentTime

setCurrentTime(value: number)

Sets the video playback position.

- Parameters
E
ester.zhou 已提交
113
  | Name| Type| Mandatory| Default Value| Description|
E
ester.zhou 已提交
114
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
115
  | value | number | Yes| - | Video playback position.|
E
ester.zhou 已提交
116 117 118 119 120 121 122 123

### requestFullscreen

requestFullscreen(value: boolean)

Requests full-screen mode.

- Parameters
E
ester.zhou 已提交
124
  | Name| Type| Mandatory| Default Value| Description|
E
ester.zhou 已提交
125
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
126
  | value | number | Yes| false | Whether the playback is in full-screen mode.|
E
ester.zhou 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140

### 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.

- Parameters
E
ester.zhou 已提交
141
  | Name| Type| Mandatory| Default Value| Description|
E
ester.zhou 已提交
142
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
143 144
  | value | number | Yes| - | Video playback position.|
  | seekMode | SeekMode | Yes| - | Seek mode.|
Z
zengyawen 已提交
145 146

- SeekMode<sup>8+</sup>
E
ester.zhou 已提交
147
  | Name| Description|
Z
zengyawen 已提交
148
  | -------- | -------- |
E
ester.zhou 已提交
149 150 151 152
  | 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 已提交
153 154 155



E
ester.zhou 已提交
156
## Example
Z
zengyawen 已提交
157

E
ester.zhou 已提交
158 159
```ts
// xxx.ets
Z
zengyawen 已提交
160 161 162
@Entry
@Component
struct VideoCreateComponent {
E
ester.zhou 已提交
163 164
  @State srcs: Resource = $rawfile('video1');
  @State previewUris: Resource = $r('app.media.img');
Z
zengyawen 已提交
165 166 167
  @State currentProgressRates: number = 1;
  @State autoPlays: boolean = false;
  @State controlsss: boolean = true;
E
ester.zhou 已提交
168
  controller: VideoController = new VideoController();
Z
zengyawen 已提交
169 170 171 172 173 174 175
  @State startStaus: boolean = true;
  build() {
    Column() {
      Video({
        src: this.srcs,
        previewUri: this.previewUris, 
        currentProgressRate: this.currentProgressRates,
E
ester.zhou 已提交
176
        controller: this.controller
Z
zengyawen 已提交
177 178 179 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
      }).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 已提交
206
            this.srcs = $rawfile('video2');
Z
zengyawen 已提交
207 208
        });
        Button("previewUri").onClick(() => {
E
ester.zhou 已提交
209
            this.previewUris = $r('app.media.img1');
Z
zengyawen 已提交
210 211 212 213 214 215 216 217
        });
        Button("controlsss").onClick(() => {
          this.controlsss = !this.controlsss;
        });
      }

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

      Row() {
        Button("setCurrentTime").onClick(() => {
E
ester.zhou 已提交
230
          this.controller.setCurrentTime(9, SeekMode.Accurate);
Z
zengyawen 已提交
231 232 233 234 235 236
        });
      }
    }
  }
}
```