简单使用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

相关文章

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2