uniapp完成视频试看功能

可以使用uniapp Video组件提供的@timeupdate方法去拿到当前播放到的时间

1. html代码

<template>
	<video src="/static/mda-pasuqi06z2ig11x2.mp4" danmu-btn :danmu-list="danmuList" @play="playVideo" id="myVideo"
		@timeupdate="videoTimeUpdateEvent">
	</video>
</template>

2. js代码

data() {
			return {
				videoContext: {}, // 用于绑定视频标签
				isPaused: false, // 用于记录是否已经暂停
				danmuList: [{
						text: '第 1s 出现的弹幕', // 弹幕内容
						color: '#ff0000', // 弹幕字体颜色
						time: 1, // 弹幕在视频的那个时间段显示 
					},
					{
						text: '第 2s 出现的弹幕',
						color: '#ff0000',
						time: 2
					},
					{
						text: '第 3s 出现的弹幕',
						color: '#ff00ff',
						time: 3
					}
				]
			}
		},
		created() {
			// 创建视频实例指向视频控件
			this.videoContext = uni.createVideoContext('myVideo');
		},
		methods: {
			videoTimeUpdateEvent(e) { // 播放进度改变
				let currentTime = Number(e.detail.currentTime);
				if (currentTime >= 6) { // 6 视频播放到6s的时候停止
					this.videoContext.pause();
					this.isPaused = true;
				} else { // 视频回退6s以后接着播放
					this.videoContext.play();
					this.isPaused = false;
				}
			},
			playVideo() { // 用户点击播放按钮时触发的方法
				if (this.isPaused) {
					this.videoContext.pause();
					alert('请先开通会员')
					return false
				} else {
					this.videoContext.play();
				}
			},
		}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值