最近在使用vue做仿网易云音乐的项目,遇到了圆形进度条实现音乐播放的功能,所以我在这里总结一下,分享给大家我的实现方法。我这里主要是通过SVG的方式实现的。

效果图:

实现过程

一、实现步骤

  • 圆形进度条的实现
  • 音乐播放/暂停的控制

二、步骤分解

这里先放一下 audio标签引入音频文件的代码:

<audio src="/static/audios/周兴哲 - 你,好不好?.mp3" @timeupdate="timeupdate" id="audio" @ended="ended"></audio>

/*
    timeupdate() 方法   实时获取 音频当前播放时长
    ended() 方法   播放结束的时候触发
*/

// 实时获取音频当前播放时长
    timeupdate(e) {
      // console.log("e===>", e.target.currentTime);
      this.current = e.target.currentTime;
      let duration = document.getElementsByTagName("audio")[0].duration;

      let percent = Math.min(1, this.current / duration);
      this.dashOffset = (1 - percent) * this.dashArray;
    },
    ended() {
      console.log("播放结束~~~");
      this.isStatus = false;
      // 初始化 圆形进度条的周长
      let circleWidth = document.getElementById("progressCircle").offsetWidth;
      this.dashArray = Math.PI * circleWidth;
      this.dashOffset = Math.PI * circleWidth;
    },

1. 圆形进度条的实现

这里通过SVG画两个一模一样的圆,设置一定的宽度,然后第二个圆使用stroke-dasharraystroke-dashoffset来动态控制进度变化情况。stroke-dashoffset的变化情况要结合音乐的时长来设置。

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" id="mySvg">
            <circle class="progress-background" cx="50%" cy="50%" r="50%" fill="transparent" />
            <circle
              class="progress-bar"
              cx="50%"
              cy="50%"
              r="50%"
              fill="transparent"
              :stroke-dasharray="dashArray"
              :stroke-dashoffset="dashOffset"
            />
        </svg>

        computed: {
            // 实时监听播放进度
            getDashOffset() {
              let percent = 0;
              if (document.getElementsByTagName("audio")[0]) {
                // 计算播放进度比例
                let duration = document.getElementsByTagName("audio")[0].duration;
                percent = Math.min(1, this.current / duration);
                this.dashOffset = (1 - percent) * this.dashArray;
              } else {
                this.dashOffset = (1 - 0) * this.dashArray;
              }
            }
          },
          mounted() {
            this.$nextTick(() => {
              // 初始化圆角周长
              let circleWidth = document.getElementById("progressCircle").offsetWidth;
              this.dashArray = Math.PI * circleWidth;
              this.dashOffset = Math.PI * circleWidth;
            });
          }

2. 音乐播放/暂停的控制

音乐的暂停和播放状态可以通过document.getElementById("audio").paused来判断,如果返回false则说明当前是播放状态,我们需要触发 document.getElementById("audio").pause()方法实现暂停操作,反之,触发document.getElementById("audio&quot;).play()方法实现播放操作。

// 操作音乐播放/暂停
  let audio = document.getElementById("audio");
  console.log("this.audio.paused===>", audio.paused);

  if (audio.paused) {
    audio.play();
  } else {
    audio.pause();
  }

下面方法全部的代码,大家可直接复制到自己的编辑器中运行(记得修改音频路径)

<template>
  <div class="playmusic">
    <div class="bottom-music">
      <div class="music-lt">
        <div class="lt-avator">
          <img src="/static/img/avator.jpg" alt />
        </div>
        <div class="lt-title">
          <span class="title">你,好不好?</span>
          <span class="name">周兴哲</span>
        </div>
      </div>
      <div class="music-rt">
        <div class="progress-circle" id="progressCircle" @click="operation">
          <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" id="mySvg">
            <circle class="progress-background" cx="50%" cy="50%" r="50%" fill="transparent" />
            <circle
              class="progress-bar"
              cx="50%"
              cy="50%"
              r="50%"
              fill="transparent"
              :stroke-dasharray="dashArray"
              :stroke-dashoffset="dashOffset"
            />
          </svg>
          <span
            :class="isStatus?'iconfont icon-bofang3 icons':'iconfont icon-bofang2 icons icons1'"
          ></span>
        </div>
        <span class="iconfont icon-yinleliebiao menu"></span>
        <audio src="/static/audios/周兴哲 - 你,好不好?.mp3" @timeupdate="timeupdate" id="audio" @ended="ended"></audio>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dashArray: 0,
      current: 0,
      dashOffset: 0,
      isStatus: false // 播放状态  false 暂停  true 播放
    };
  },
  computed: {
    // 实时监听播放进度条
    getDashOffset() {
      let percent = 0;
      if (document.getElementsByTagName("audio")[0]) {
        // 计算播放进度比例
        let duration = document.getElementsByTagName("audio")[0].duration;
        percent = Math.min(1, this.current / duration);
        this.dashOffset = (1 - percent) * this.dashArray;
      } else {
        this.dashOffset = (1 - 0) * this.dashArray;
      }
    }
  },
  methods: {
    // 实时获取音频当前播放时长
    timeupdate(e) {
      // console.log("e===>", e.target.currentTime);
      this.current = e.target.currentTime;
      let duration = document.getElementsByTagName("audio")[0].duration;

      let percent = Math.min(1, this.current / duration);
      this.dashOffset = (1 - percent) * this.dashArray;
    },
    ended() {
      console.log("播放结束~~~");
      this.isStatus = false;
      // 初始化 圆形进度条的周长
      let circleWidth = document.getElementById("progressCircle").offsetWidth;
      this.dashArray = Math.PI * circleWidth;
      this.dashOffset = Math.PI * circleWidth;
    },
    // 操作音乐播放/暂停
    operation() {
      console.log("播放/暂停音乐");
      let audio = document.getElementById("audio");
      console.log("this.audio.paused===>", audio.paused);

      if (audio.paused) {
        audio.play();
        this.isStatus = true;
      } else {
        audio.pause();
        this.isStatus = false;
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 初始化圆的周长
      let circleWidth = document.getElementById("progressCircle").offsetWidth;
      this.dashArray = Math.PI * circleWidth;
      this.dashOffset = Math.PI * circleWidth;
    });
  }
};
</script>
<style lang="css" scoped>
.font {
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
}
.playmusic {
  position: relative;
  height: 100vh;
  width: 100%;
}
.playmusic .bottom-music {
  box-sizing: border-box;
  padding: 0 0.64rem;
  z-index: 999;
  box-shadow: 5px 5px 5px 5px #efefef, -5px 5px 5px 5px rgba(255, 255, 255, 0.5);
}
.playmusic .bottom-music {
  height: 3.41333333rem;
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #ffffff;
}
.playmusic .bottom-music .music-lt {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: start;
  width: 70%;
  overflow: hidden;
}
.playmusic .bottom-music .music-lt .lt-avator {
  width: 2.13333333rem;
  height: 2.13333333rem;
  border-radius: 50%;
  overflow: hidden;
}
.playmusic .bottom-music .music-lt .lt-avator img {
  width: 100%;
  height: 100%;
  display: block;
}
.playmusic .bottom-music .music-lt .lt-title {
  padding: 0 0.42666667rem;
  width: 80%;
}
.playmusic .bottom-music .music-lt .lt-title span {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
}
.playmusic .bottom-music .music-lt .lt-title .title {
  font-size: 0.59733333rem;
  font-weight: 600;
  color: #333;
}
.playmusic .bottom-music .music-lt .lt-title .name {
  font-size: 0.46933333rem;
  color: #666;
}
.playmusic .bottom-music .music-rt {
  display: flex;
  justify-content: end;
  align-items: center;
}
.playmusic .bottom-music .music-rt .progress-circle {
  width: 1.92rem;
  height: 1.92rem;
  position: relative;
}
.playmusic .bottom-music .music-rt .progress-circle circle {
  stroke-width: 0.14933333rem;
  transform-origin: center;
}
.playmusic .bottom-music .music-rt .progress-circle .progress-background {
  transform: scale(0.9);
  stroke: rgba(212, 68, 57, 0.5);
}
.playmusic .bottom-music .music-rt .progress-circle .progress-bar {
  transform: scale(0.9) rotate(-90deg);
  stroke: #d44439;
}
.playmusic .bottom-music .music-rt .progress-circle .icons {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #d44439;
}
.playmusic .bottom-music .music-rt .progress-circle .icons {
  font-size: 1.17333333rem;
}
.playmusic .bottom-music .music-rt .progress-circle .icons1 {
  font-size: 0.96rem;
}
.playmusic .bottom-music .music-rt .menu {
  font-size: 1.70666667rem;
  color: #d44439;
  font-weight: 500;
}
.playmusic .bottom-music .music-rt .menu {
  padding-left: 0.85333333rem;
}

</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

vue使用SVG实现圆形进度条音乐播放的更多相关文章

  1. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  2. vue自定义加载指令v-loading占位图指令v-showimg

    这篇文章主要为大家介绍了vue自定义加载指令和v-loading占位图指令v-showimg的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  3. vue使用动画实现滚动表格效果

    这篇文章主要为大家详细介绍了vue使用动画实现滚动表格效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. 关于Vue 监控数组的问题

    这篇文章主要介绍了Vue 监控数组的示例,主要包括Vue 是如何追踪数据发生变化,Vue 如何更新数组以及为什么有些数组的数据变更不能被 Vue 监测到,对vue监控数组知识是面试比较常见的问题,感兴趣的朋友一起看看吧

  5. Vue子组件props从父组件接收数据并存入data

    这篇文章主要介绍了Vue子组件props从父组件接收数据并存入data的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. Vue h函数的使用详解

    本文主要介绍了Vue h函数的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  7. VUE响应式原理的实现详解

    这篇文章主要为大家详细介绍了VUE响应式原理的实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

  8. vue+Element ui实现照片墙效果

    这篇文章主要为大家详细介绍了vue+Element ui实现照片墙效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. vue+elemet实现表格手动合并行列

    这篇文章主要为大家详细介绍了vue+elemet实现表格手动合并行列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. iview+vue实现导入EXCEL预览功能

    这篇文章主要为大家详细介绍了iview+vue实现导入EXCEL预览功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部