简单使用vue2和elementUI自定义audio支持拖拽进度

本文主要是介绍简单使用vue2和elementUI自定义audio支持拖拽进度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<template><div class="music-player"><audio ref="audio" :src="src" @timeupdate.debounce="updateTime" @loadedmetadata="loadedmetadata" @ended="onAudioEnded"></audio><!-- 播放暂停按钮 --><div class="cm-btn-left" @click="togglePlay"><img src="../../../../public/img/icon/play.png" v-show="!isPlaying"><img src="../../../../public/img/icon/pause.png" v-show="isPlaying"></div><!-- 时间 --><div class="cm-time"><span>{{ currentTimeShow }}</span> / <span>{{ durationShow }}</span></div><!-- 播放进度条 --><div class="cm-progress" @mousedown="handleMouseDown"><el-slider v-model="progress" :max="duration" :format-tooltip="formatTime" @change="progressChange"></el-slider></div><!-- 倍速控制 --><div class="playback-rate-wrapper"><el-select v-model="playbackRate" @change="changePlaybackRate"><el-optionv-for="item in speed":key="item.value":label="item.label":value="item.value"></el-option></el-select></div><!-- 音量控制 --><div class="volumn-wrapper"><el-popoverpopper-class="volumn-popover"placement="right"width="30px"trigger="hover"><el-sliderv-model="volumnValue"verticaltooltip-class="volumn-tooltip"height="80px"@change="volumnChange"></el-slider><el-button slot="reference" type="text"><img src="../../../../public/img/icon/volumn.png"></el-button></el-popover></div></div>
</template><script>
import { ref, computed } from 'vue';export default {props: {src: String,},data() {return {isPlaying: false,currentTime: 0,progress: 0,duration: 0,playbackRate: 1,speed: [{label: '0.5x',value: 0.5},{label: '0.75x',value: 0.75},{label: '1x',value: 1},{label: '1.5x',value: 1.5},{label: '2x',value: 2}],volumnValue: 50,lastTimeUpdate: null};},computed: {currentTimeShow: function () {return this.formatTime(this.currentTime);},durationShow: function () {return this.formatTime(this.duration);}},mounted() {},beforeDestroy() {},methods: {// 播放暂停按钮点击事件togglePlay() {if (this.isPlaying) {this.$refs.audio.pause();this.isPlaying = false;} else {this.$refs.audio.play();this.$refs.audio.volume = this.volumnValue / 100;this.isPlaying = true;}},// 格式化播放时间formatTime(seconds) {const minutes = Math.floor(seconds / 60);const secondsRemainder = Math.round(seconds % 60);return `${minutes}:${secondsRemainder < 10 ? '0' : ''}${secondsRemainder}`;},// 倍速控制changePlaybackRate() {this.$refs.audio.playbackRate = this.playbackRate;},// 更新播放时间(节约资源,手动控制一秒执行一次)updateTime() {const now = Date.now();if (!this.lastTimeUpdate || now - this.lastTimeUpdate >= 1000) {this.lastTimeUpdate = now;this.currentTime = this.$refs.audio.currentTime;this.progress = this.currentTime;}},// 获取播放总时长loadedmetadata() {this.duration = this.$refs.audio.duration;},// 播放结束onAudioEnded() {this.isPlaying = false;},// 进度条鼠标按下事件handleMouseDown() {this.$refs.audio.pause();},// 设置进度progressChange(e) {this.$refs.audio.currentTime = e;this.progress = e;this.$refs.audio.play();this.isPlaying = true;},// 设置音量volumnChange(e) {this.$refs.audio.volume = e / 100;},},
};
</script><style lang="scss" scoped>.music-player {display: flex;align-items: center;justify-content: space-between;padding: 0px 20px;background-color: #f5f5f5;border-radius: 27px;height: 54px;width: 100%;.cm-btn-left {cursor: pointer;width: 22px;height: 22px;img {width: 22px;height: 22px;}}.volumn-wrapper {img {width: 20px;height: 18px;}}.cm-time {font-size: 14px;color: #404040;margin: 0 10px;}.cm-progress {flex: 1;margin: 0 15px 0 10px;}.playback-rate-wrapper {margin-right: 10px;::v-deep .el-input__suffix {display: none;}::v-deep .el-input__inner {height: 28px;width: 45px;text-align: center;padding: 0px !important;font-size: 12px;}}}::v-deep .el-slider__button {width: 12px;height: 12px;}::v-deep .el-popover {min-width: auto !important;}
</style>

app.vue中或者其他全局变量中修改下样式

.volumn-popover {

  min-width: auto !important;

}

.volumn-tooltip {

  z-index: 99999 !important;

}

这篇关于简单使用vue2和elementUI自定义audio支持拖拽进度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

定价129元!支持双频 Wi-Fi 5的华为AX1路由器发布

《定价129元!支持双频Wi-Fi5的华为AX1路由器发布》华为上周推出了其最新的入门级Wi-Fi5路由器——华为路由AX1,建议零售价129元,这款路由器配置如何?详细请看下文介... 华为 Wi-Fi 5 路由 AX1 已正式开售,新品支持双频 1200 兆、配有四个千兆网口、提供可视化智能诊断功能,建

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.