js-components-media-video.md 5.7 KB
Newer Older
E
ester.zhou 已提交
1
# video
Z
zengyawen 已提交
2 3


E
ester.zhou 已提交
4
>  **NOTE**
E
ester.zhou 已提交
5
>
H
HelloCrease 已提交
6
>  - This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
7
>
H
HelloCrease 已提交
8 9 10 11 12 13 14 15 16
>  - Set **configChanges** under **abilities** in the **config.json** file to **orientation**.
>  ```
>  "abilities": [
>  {
>  "configChanges": ["orientation"],
>  ...
>  }
>  ]
>  ```
Z
zengyawen 已提交
17

E
ester.zhou 已提交
18
The **\<video>** component provides a video player.
Z
zengyawen 已提交
19

E
ester.zhou 已提交
20 21

## Child Components
Z
zengyawen 已提交
22 23 24

Not supported

Z
zengyawen 已提交
25

E
ester.zhou 已提交
26 27 28 29
## Attributes

In addition to the [universal attributes](../arkui-js/js-components-common-attributes.md), the following attributes are supported.

H
HelloCrease 已提交
30 31 32 33 34 35
| Name     | Type    | Default Value | Mandatory | Description                              |
| -------- | ------- | ------------- | --------- | ---------------------------------------- |
| muted    | boolean | false         | No        | Whether the video is muted.              |
| src      | string  | -             | No        | Path of the video content to play.       |
| autoplay | boolean | false         | No        | Whether the video is played automatically after being rendered. |
| controls | boolean | true          | No        | Whether the control bar is displayed during video playback. If the value is set to **false**, the control bar is not displayed. The default value is **true**, indicating that the platform can either show or hide the control bar. |
E
ester.zhou 已提交
36 37 38 39 40 41


## Styles

In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported.

H
HelloCrease 已提交
42 43 44
| Name       | Type   | Default Value | Mandatory | Description                              |
| ---------- | ------ | ------------- | --------- | ---------------------------------------- |
| object-fit | string | contain       | No        | Video scale type. If **poster** has been assigned a value, the setting of this style will affect the scaling type of the video poster. For details, see object-fit enums. |
E
ester.zhou 已提交
45 46 47

**Table 1** object-fit enums

H
HelloCrease 已提交
48 49 50
| Type | Description                              |
| ---- | ---------------------------------------- |
| fill | The image is resized to fill the display area, and its aspect ratio is not retained. |
E
ester.zhou 已提交
51 52 53 54 55 56


## Events

In addition to the [universal events](../arkui-js/js-components-common-events.md), the following events are supported.

H
HelloCrease 已提交
57 58 59 60 61 62 63 64 65 66
| Name       | Parameter                                | Description                              |
| ---------- | ---------------------------------------- | ---------------------------------------- |
| prepared   | {&nbsp;duration:&nbsp;value&nbsp;}<sup>5+</sup> | Triggered when the video preparation is complete. The video duration (in seconds) is obtained from **duration**. |
| start      | -                                        | Triggered when the video is played.      |
| pause      | -                                        | Triggered when the video playback is paused. |
| finish     | -                                        | Triggered when the video playback is finished. |
| error      | -                                        | Triggered when the video playback fails. |
| seeking    | {&nbsp;currenttime:&nbsp;value&nbsp;}    | Triggered to report the time (in seconds) when the progress bar is being dragged. |
| seeked     | {&nbsp;currenttime:&nbsp;value&nbsp;}    | Triggered to report the playback time (in seconds) when the user finishes dragging the progress bar. |
| timeupdate | {&nbsp;currenttime:&nbsp;value&nbsp;}    | Triggered once per 250 ms when the playback progress changes. The unit of the current playback time is second. |
E
ester.zhou 已提交
67 68 69 70 71 72


## Methods

In addition to the [universal methods](../arkui-js/js-components-common-methods.md), the following methods are supported.

H
HelloCrease 已提交
73 74 75 76 77
| Name           | Parameter                             | Description                              |
| -------------- | ------------------------------------- | ---------------------------------------- |
| start          | -                                     | Starts playing a video.                  |
| pause          | -                                     | Pauses a video.                          |
| setCurrentTime | {&nbsp;currenttime:&nbsp;value&nbsp;} | Sets the video playback position, in seconds. |
E
ester.zhou 已提交
78

E
ester.zhou 已提交
79 80
> **NOTE**
>
E
ester.zhou 已提交
81 82 83 84 85 86 87 88 89
> The methods in the above table can be called after the **attached** callback is invoked.

## Example

```html
<!-- xxx.hml -->
<div class="container">
  <video id='videoId' src='/common/myDeram.mp4' muted='false' autoplay='false'
         controls='true' onprepared='preparedCallback' onstart='startCallback'
H
HelloCrease 已提交
90
         onpause='pauseCallback' onfinish='finishCallback' onerror='errorCallback'
E
ester.zhou 已提交
91 92
         onseeking='seekingCallback' onseeked='seekedCallback' 
         ontimeupdate='timeupdateCallback'
E
ester.zhou 已提交
93
         style="object-fit:fill; width:80%; height:400px;"
E
ester.zhou 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
         onclick="change_start_pause">
   </video>
</div>
```

```css
/* xxx.css */
.container {
  justify-content: center;
  align-items: center;
}
```

```js
// xxx.js
export default {
  data: {
    event:'',
    seekingtime:'',
    timeupdatetime:'',
    seekedtime:'',
    isStart: true,
    duration: '',
  },
  preparedCallback:function(e){ this.event = 'Video successfully connected'; this.duration = e.duration;},
  startCallback:function(){this.event = 'Playback starts.';},
  pauseCallback:function(){this.event = 'Playback pauses.';},
  finishCallback:function(){this.event = 'Playback ends.';},
  errorCallback:function(){this.event = 'Playback error.';},
  seekingCallback:function(e){ this.seekingtime = e.currenttime; },
  timeupdateCallback:function(e){ this.timeupdatetime = e.currenttime;},
  change_start_pause: function() {
    if(this.isStart) {
      this.$element('videoId').pause();
      this.isStart = false;
    } else {
      this.$element('videoId').start();
      this.isStart = true; 
    }
  },
}
```