预览

在这里插入图片描述

思路

拿到时长做倒计时,时长 = ( 时长 + 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

Logo

前往低代码交流专区

更多推荐