目录

video

Represents a component used to play video files and control their playback states.

Since: 1.0.0

Related document: has.createVideoContext

Important notes:

When the HarmonyOS SDK version is 6.1.0 (23) or later and the ROM version is 6.1.0 or later, the menu bar is hidden when the video component is in full-screen mode.

Since: 1.0.22

Attributes

Name Type Default Value Mandatory Description
src string - Yes

Video data source.

The supported format is mp4. Since version 1.0.16, mpeg-ts and mkv are also supported.

Both network video URLs and local video URLs (starting with internal://) are supported.

Video URLs in the code package are supported since version 1.0.20.

duration number - No

Video duration, in seconds.

Since: 1.0.16

controls boolean true No

Whether to display default playback components.

The options include the play/pause button, playback progress bar, time display, mute button, and full-screen button. Since version 1.0.16, the casting button is additionally supported.

autoplay boolean false No

Whether to enable autoplay.

In version 1.0.15 and earlier versions, autoplay must be enabled in muted mode.

Since version 1.0.16, this restriction no longer applies.

loop boolean false No Whether to play in a loop.
muted boolean false No Whether to play in mute mode.
initial-time number 0 No

Initial video playing position, in seconds.

Since: 1.0.16

object-fit string contain No

Display mode of the video when its size does not match the video component size.

- contain: maintains the aspect ratio and scales down or up to fit within the display boundaries.

- cover: maintains the aspect ratio and scales down or up so that both sides are greater than or equal to the display boundaries.

- fill: scales to fill the display boundaries without maintaining the aspect ratio.

Since: 1.0.3

poster string - No

Path of the preview image when the video is not playing. By default, the first frame of the video is displayed. Network images are supported.

If the poster value fails to load:

In version 1.0.15 and earlier versions, the video cover appears black.

Since version 1.0.16, the first frame of the video is displayed by default.

direction number - No

Video orientation in full-screen mode. If not specified, the orientation is automatically determined by the aspect ratio.

0: upright portrait.

90: landscape with 90° counterclockwise rotation.

-90: landscape with 90° clockwise rotation.

Since: 1.0.16

show-casting-button boolean false No

Whether to display the casting button. This button only appears after the video starts playing and is supported solely for network videos.

Required permissions: To enable the casting button, perform the following operations:

Declare the ohos.permission.KEEP_BACKGROUND_RUNNING permission in module.json5.

Declare the backgroundModes configuration item in module.json5.

"module": {"abilities": [{"backgroundModes": ["audioPlayback"]}]}

Configure the requiredBackgroundModes attribute in app.json.

"requiredBackgroundModes": ["audio"].

Since: 1.0.16

show-fullscreen-btn boolean true No

Whether to display the full-screen button.

Since: 1.0.16

show-play-btn boolean true No

Whether to display the play button on the bottom control bar.

Since: 1.0.16

play-btn-position string bottom No

Position of the play button.

- bottom: bottom control bar.

- center: video center.

Since: 1.0.16

title string - No

Title of the video, which is displayed at the top in full-screen mode.

Since: 1.0.16

show-screen-lock-button boolean false No

Whether to display the lock screen button. This button is displayed only in full-screen mode.

Since: 1.0.16

enable-auto-rotation boolean false No

Whether to enable auto-rotation in full screen when the phone is in landscape mode. The setting takes effect when system auto-rotation is enabled.

Since: 1.0.16

enable-progress-gesture boolean true No

Whether to enable progress control gestures.

Since: 1.0.16

vslide-gesture-in-fullscreen boolean true No

Whether to enable the brightness and volume adjustment gestures in full-screen mode.

Since: 1.0.16

vslide-gesture boolean false No

Whether to enable the brightness and volume adjustment gestures in non-full-screen mode.

Since: 1.0.16

show-mute-btn boolean false No

Whether to display the mute button.

Since: 1.0.16

enable-play-gesture boolean false No

Whether to enable playback control gestures so that users can double-tap the screen to play or pause the video.

Since: 1.0.16

bindplay eventhandle - No Event triggered when the playback starts or resumes.
bindpause eventhandle - No Event triggered when the playback is paused.
bindended eventhandle - No Event triggered when the playback reaches the end.
bindtimeupdate eventhandle - No

Event triggered when the playback progress changes, with event.detail = {currentTime, duration}, where the unit of currentTime and duration is second. The event is triggered every 200 ms.

Since: 1.0.16

bindfullscreenchange eventhandle - No

Event triggered when the video enters or exits the full-screen mode. In event.detail = {fullScreen, direction}, the value of direction can be vertical or horizontal.

Since: 1.0.16

bindwaiting eventhandle - No

Event triggered when a video is being buffered.

Since: 1.0.16

binderror eventhandle - No

Event triggered when an error occurs during video playback.

Since: 1.0.16

bindprogress eventhandle - No

Event triggered when the loading progress changes. Only single-segment loading is supported. In event.detail = {buffered}, buffered is a percentage value.

Since: 1.0.16

bindloadedmetadata eventhandle - No

Event triggered when the video metadata loading is complete. In event.detail = {width, height, duration}, the unit of duration is second.

Since: 1.0.16

bindcontrolstoggle eventhandle - No

Event triggered when the components are displayed or hidden, with event.detail = {show}.

Since: 1.0.16

bindseekcomplete eventhandle - No

Event triggered when the seek operation is complete. In event.detail = {position}, the unit of position is second.

Since: 1.0.16

bindcastinguserselect eventhandle - No

Event triggered when a user selects a casting device, with detail = { state: "success"/"fail" }.

Since: 1.0.16

bindcastingstatechange eventhandle - No

Event triggered when the casting succeeds or fails. In detail = { type, state: "success"/"fail" }, type is fixed at connect.

Since: 1.0.16

bindcastinginterrupt eventhandle - No

Event triggered when the casting is interrupted.

Since: 1.0.16

Casting restrictions:

Example:

HXML file:

<video
  src="{{ videoSrc }}"
  controls="{{ controls }}"
  autoplay="{{ autoplay }}"
  loop="{{ loop }}"
  muted="{{ muted }}"
  object-fit="{{ objectFit }}"
  poster="{{ posterSrc }}"
  bindplay="onPlay"
  bindpause="onPause"
  bindended="onEnded"
  binderror="onError"
/>

JavaScript file:

Page({
  data: {
    videoSrc: 'https://www.example.com/example.mp4', // Replace the example with the actual video resource address.
    posterSrc: 'https://www.example.com/example.jpg', // Replace the example with the actual image resource address.
    controls: true,
    autoplay: true,
    loop: true,
    muted: true,
    objectFit: 'fill',
  },
  onPlay(e) {
    console.info('video start play', e);
  },
  onPause(e) {
    console.info('video pause', e);
  },
  onEnded(e) {
    console.info('video end', e);
  },
  onError(e) {
    console.error('video error', e.detail);
  }
});