【vue】el-carousel实现视频自动播放与自动切换到下一个视频功能:

2023-10-20 05:36

本文主要是介绍【vue】el-carousel实现视频自动播放与自动切换到下一个视频功能:,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

        • 一、原因:
        • 二、实现代码:
        • 三、遇到的问题:
            • 【1】问题:el-carousel页面的视频不更新
            • 【2】问题:多按几次左按钮,其中跳过没有播放的视频没有销毁,造成再次自动播放时会跳页


一、原因:

由于后端无法实现将多条视频拼接为一条视频,所以更改为由前端实现页面视频自动播放功能和播放完后,自动切换到下一个视频功能

二、实现代码:
<template><div class="preview-content" v-loading="loading"><el-tabs v-model="activeName" @tab-click="tabClick"><!-- 视频预览 --><el-tab-pane v-if="DisplayList.includes('video')" name="video" :label="`视频预览【${videoList.length}`"><div v-if="videoList.length > 0" class="preview-content-box"><el-carousel ref="videoCarousel" @change="autoPlayVideo" :autoplay="false" :loop="false" style="width:100%"height="540px" :initial-index="0" :key="(Date.parse(new Date()) / 1000)" arrow="always"indicator-position="none"><el-carousel-item v-for="(item, index) in videoList" :key="index"><h2>{{ index + 1 }}/{{ videoList.length }}</h2><video autoplay :muted="false" controls preload :id="`video-${index}`" width=100% height="100%"><source :src="item" type="video/mp4" /><object type="application/x-shockwave-flash" data="myvideo.swf"><param name="movie" value="myvideo.swf" /><param name="flashvars" value="autostart=true&amp;file=myvideo.swf" /></object>当前浏览器不支持video直接播放,点击这里下载视频:<a :href="item">下载视频</a></video></el-carousel-item></el-carousel></div><div v-else class="noData">无视频</div></el-tab-pane></el-tabs></div>
</template><script>
export default {props: {DisplayList: {type: Array,default() {return ['img', 'video', 'imgBYWeight', 'imgBYCar', 'imgBYUpload']}},},data() {return {activeName: 'img',loading: false,videoList: [],//视频列表player: [],};},methods: {loadData(record, type) {const that = this;console.log('图片/视频预览:', record)that.activeName = (type && type != null && type != undefined && that.DisplayList.includes(type)) ? type : that.DisplayList.length > 0 ? that.DisplayList[0] : 'img'that.videoList = []             //视频列表if (!(typeof record == 'object') || Object.keys(record).length == 0) { return }that.loading = true// 视频列表if (that.DisplayList.includes('video')) {record.videoList && record.videoList.length > 0 ? that.videoList = [...that.pushArr(record.videoList)] : null;record.allVideoList && record.allVideoList.length > 0 ? that.videoList = [...that.videoList, ...that.pushArr(record.allVideoList)] : null;this.$nextTick(() => {that.videoList.length > 0 ? that.$refs.videoCarousel.setActiveItem(0) : null;that.player = []that.sleep(1000).then(() => { that.activeName == 'video' ? that.autoPlayVideo(0) : null; })})}that.loading = false;},// 数组遍历判断是否需要拼接地址pushArr(arr) {if (!Array.isArray(arr) || arr.length == 0) { return [] }let newArr = []for (let index = 0; index < arr.length; index++) {const item = arr[index];if ((typeof item == 'object') && item.url) {item.url.includes("http") || item.url.includes(";base64,") ? newArr.push(item.url) : newArr.push(process.env.VUE_APP_FILE_URL + item.url)} else {item.includes("http") || item.includes(";base64,") ? newArr.push(item) : newArr.push(process.env.VUE_APP_FILE_URL + item)}}return newArr},tabClick(tab, event) {const that = this;if (tab.name == 'video') {that.videoList.length > 0 ? that.$refs.videoCarousel.setActiveItem(0) : null;that.player = []that.autoPlayVideo(0)}},//设置播放点,续播autoPlayVideo(index, oldVal) {const that = this;let currVideo = document.getElementById(`video-${index}`);if (currVideo == null) { return }if (that.player.includes(currVideo)) {that.player[index].currentTime = 0, that.player[index].muted = false, that.player[index].autoplay = true, that.player[index].play().catch((err) => { });that.player[oldVal].currentTime = 0, that.player[oldVal].muted = true, that.player[oldVal].autoplay = false, that.player[oldVal].pause();} else {currVideo.currentTime = 0;//设置播放点currVideo.muted = false;//设置非静音that.sleep(100).then(() => {currVideo.play().catch((err) => { })that.player.includes(currVideo) ? null : that.player.push(currVideo);that.player.forEach((item, i) => { if (i != index) { item.autoplay = false, item.pause() } })that.player[index].addEventListener('ended', function (e) { //结束console.log("播放结束", e.target.id, that.player)that.$refs.videoCarousel.next()}, false);})}},},beforeDestroy() {this.videoList = []             //视频列表this.player = []},
}
</script>
三、遇到的问题:
【1】问题:el-carousel页面的视频不更新

文章链接:【vue】element强制刷新el-carousel的dom:

在这里插入图片描述

【2】问题:多按几次左按钮,其中跳过没有播放的视频没有销毁,造成再次自动播放时会跳页

将video存放到player里面,并修改video的播放状态
在这里插入图片描述

这篇关于【vue】el-carousel实现视频自动播放与自动切换到下一个视频功能:的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/245020

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、