Understanding the Uni Video Component
When working with uni-app, the video component is a powerful tool for embedding and controlling video content within your applications. This article will delve into the details of the uni video component, providing you with a comprehensive guide to its usage and capabilities.
Basic Usage
The uni video component is straightforward to use. You can embed a video by simply adding the <video>
tag to your HTML and setting the appropriate attributes. For instance, you can specify the video source using the src
attribute and control the video controls using the controls
attribute.
Attribute | Description |
---|---|
src | URL of the video source |
controls | Enables or disables video controls |
autoplay | Automatically plays the video when the page loads |
loop | Repeats the video indefinitely |
Controlling the Video
Uni-app provides a variety of methods to control the video component. You can use these methods to play, pause, seek, and more. Here’s a brief overview of some of the most commonly used methods:
Method | Description |
---|---|
uni.createVideoContext | Creates a video context object that allows you to control the video |
videoContext.play | Plays the video |
videoContext.pause | Pauses the video |
videoContext.seek | Seeks to a specific time in the video |
Handling Events
Uni-app also allows you to handle various events that occur during video playback. For example, you can listen for the play
event to know when the video starts playing, or the timeupdate
event to keep track of the current playback time.
Event | Description |
---|---|
play | Fires when the video starts playing |
pause | Fires when the video is paused |
timeupdate | Fires when the playback time changes |
Handling Errors
When working with video content, it’s important to handle errors gracefully. Uni-app provides the error
event, which you can use to handle errors that occur during video playback.
Here’s an example of how you can handle the error
event:
video.on('error', function(error) { console.error('Video error:', error);});
Responsive Design
The uni video component is designed to be responsive, meaning it will adjust to the size of its container. However, you may want to control the aspect ratio of the video to ensure it looks good on all devices. You can do this by setting the object-fit
property on the video element.
Here’s an example of how you can set the aspect ratio to 16:9:
video.style.objectFit = '16:9';
Advanced Features
In addition to the basic features mentioned above, the uni video component also supports a variety of advanced features, such as:
- Fullscreen: You can use the
uni.createVideo