vue中audio实现微信语音播放动画

预览

在这里插入图片描述

思路

拿到时长做倒计时,时长 = ( 时长 + 1 ) * 100; destroy的时候清除一下

代码

<div style="display:none">
  <audio controls="controls" ref="audio">
    <source :src="url || ''" type="audio/mpeg" />
  </audio>
</div>
<div class="voice-bg" @click="handlePlay">
  <img
    v-if="isPlaying"
    src="./../../assets/images/goods/voice-play.gif"
    alt=""
  />
  <img
    v-else
    src="./../../assets/images/goods/voice.png"
    alt=""
  />
  <span>{{ duration || 0 }}"</span>
</div>
data() {
  return {
    url: '',
    timer: null,
    isPlaying: false,
    currentTime: 0,

  }
},
methods:{
  handlePlay() {
    let audio = this.$refs.audio;
    if (!this.isPlaying) {
      audio.play();
      this.isPlaying = true;
      this.watchEnd();
    } else {
      audio.pause();
      clearTimeout(this.timer);
      this.isPlaying = false;
      audio.currentTime = 0;
    }
  },
  watchEnd() {
    let that = this;
    this.timer = setTimeout(() => {
      that.isPlaying = false;
    }, (that.duration + 1) * 1000);
  },
},
beforeDestroy() {
  clearTimeout(this.timer);
}

资料

关于获取audio信息接口

七牛云????

this.$axios.get(`${this.info.url}?avinfo`).then(res => {
  let { streams } = res.data;
  this.duration = parseInt((streams && streams[0].duration) || 0);
});

UI图片

下载地址
isPlaying gif
isPause png

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值