uniapp video 让视频不能快进,右上角显示倒计时
时间: 2023-10-16 17:09:07 浏览: 200
您好,对于 UniApp 中的视频组件,您可以通过设置 `controls` 属性来控制视频是否显示控制条,通过设置 `disable-progress` 属性来禁用视频的快进功能,而右上角的倒计时则需要通过自定义组件来实现。
具体实现方式如下:
1. 在 UniApp 项目中创建一个自定义组件,命名为 `countdown`。
2. 在 `countdown` 组件的 `template` 中,使用 `setInterval` 定时器来实现倒计时的功能,并将当前剩余时间显示在组件中。
3. 在 `video` 组件的 `bindtimeupdate` 事件中,获取视频当前播放的时间和总时长,并计算出剩余时间,将剩余时间传递给 `countdown` 组件。
4. 在 `video` 组件中添加 `disable-progress` 属性来禁用快进功能,并将 `controls` 属性设置为 `false` 来隐藏控制条。
5. 在需要使用视频播放的页面中引入 `video` 和 `countdown` 组件,并将视频地址传递给 `video` 组件。
下面是 `countdown` 组件的示例代码:
```html
<template>
<div class="countdown">{{ countdownTime }}</div>
</template>
<script>
export default {
props: {
time: {
type: Number,
default: 0
}
},
data() {
return {
countdownTime: ""
};
},
mounted() {
this.startCountdown();
},
methods: {
startCountdown() {
let timeLeft = this.time;
this.countdownTime = this.formatTime(timeLeft);
setInterval(() => {
timeLeft--;
if (timeLeft >= 0) {
this.countdownTime = this.formatTime(timeLeft);
}
}, 1000);
},
formatTime(time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
return `${minutes}:${seconds < 10 ? "0" + seconds : seconds}`;
}
}
};
</script>
```
在 `video` 组件中,您需要监听 `bindtimeupdate` 事件,并获取视频当前播放的时间和总时长,并计算出剩余时间,将剩余时间传递给 `countdown` 组件,示例代码如下:
```html
<template>
<view>
<video
:src="videoUrl"
:disable-progress="true"
:controls="false"
@bindtimeupdate="onTimeUpdate"
></video>
<countdown :time="countdownTime"></countdown>
</view>
</template>
<script>
import Countdown from "@/components/countdown";
export default {
components: {
Countdown
},
data() {
return {
videoUrl: "your_video_url",
totalTime: 0,
currentTime: 0,
countdownTime: 0
};
},
mounted() {
this.initVideo();
},
methods: {
initVideo() {
this.$refs.videoCtx.play();
},
onTimeUpdate(event) {
this.totalTime = event.target.duration;
this.currentTime = event.target.currentTime;
this.countdownTime = Math.floor(this.totalTime - this.currentTime);
}
}
};
</script>
```
注意,在实际开发中,您需要将 `your_video_url` 替换为您的视频地址。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![js](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)