element+vue 自定義播放器組件

只控制能不能重複播放和拖拽進度,2個參數theUrl和type

<template>
  <div class="di main-wrap" v-loading="audio.waiting">
    <!-- 這裏設置了ref屬性後,在vue組件中,就可以用this.$refs.audio來訪問該dom元素 -->
    <audio
      ref="audio"
      class="dn"
      :src="url"
      :preload="audio.preload"
      @play="onPlay"
      @error="onError"
      @waiting="onWaiting"
      @pause="onPause"
      @timeupdate="onTimeupdate"
      @loadedmetadata="onLoadedmetadata"
      @ended="onended"
    ></audio>
    <div class="audio-container">

      <img @click="startPlayOrPause" v-if="audio.playing && canPlay" class="btn" src="../../../assets/images/component/audio_pause.png" alt="">
      <img @click="startPlayOrPause" v-else-if="!audio.playing && canPlay" class="btn" src="../../../assets/images/component/audio_start.png" alt="">
      <img v-else-if="!canPlay" class="btn" src="../../../assets/images/component/audio_disabled.png" alt="">

      <div class="time1">
          {{ audio.currentTime | formatSecond}}
      </div>

      <el-slider
        v-model="sliderTime"
        :format-tooltip="formatProcessToolTip"
        @change="changeCurrentTime"
        class="slider"
        :disabled="!type"
      ></el-slider>

      <div class="time2">
          {{ audio.maxTime | formatSecond }}
      </div>
    </div>
  </div>
</template>

<script>
function realFormatSecond (second) {
  var secondType = typeof second
  if (secondType === 'number' || secondType === 'string') {
    second = parseInt(second)
    var hours = Math.floor(second / 3600)
    second = second - hours * 3600
    var mimute = Math.floor(second / 60)
    second = second - mimute * 60
    return (
      ('0' + mimute).slice(-2) + ':' + ('0' + second).slice(-2)
    )
  } else {
    return '00:00'
  }
}
export default {
  props: {
    theUrl: {
      type: String,
      required: true
    },
    type: {
      type: Boolean,
      required: true
    }
  },
  name: 'audioPlay',
  data () {
    return {
      url: this.theUrl,
      audio: {
        currentTime: 0,
        maxTime: 0,
        playing: false,
        muted: false,
        speed: 1,
        waiting: true,
        preload: 'auto'
      },
      sliderTime: 0,
      canPlay: true
    }
  },
  methods: {
    // 進度條toolTip
    formatProcessToolTip (index = 0) {
      index = parseInt((this.audio.maxTime / 100) * index)
      return '進度條: ' + realFormatSecond(index)
    },
    // 播放跳轉
    changeCurrentTime (index) {
      this.$refs.audio.currentTime = parseInt(
        (index / 100) * this.audio.maxTime
      )
    },
    startPlayOrPause () {
      return this.audio.playing ? this.pausePlay() : this.startPlay()
    },
    // 開始播放
    startPlay () {
      this.$refs.audio.play()
    },
    // 暫停
    pausePlay () {
      this.$refs.audio.pause()
    },
    // 當音頻暫停
    onPause () {
      this.audio.playing = false
    },
    // 當發生錯誤, 就出現loading狀態
    onError () {
      this.audio.waiting = true
    },
    // 當音頻開始等待
    onWaiting (res) {
      // console.log(res);
    },
    // 當音頻開始播放
    onPlay (res) {
      // console.log(res);
      this.audio.playing = true
      this.audio.loading = false
      let target = res.target
      let audios = document.getElementsByTagName('audio');
      [...audios].forEach(item => {
        if (item !== target) {
          item.pause()
        }
      })
    },
    // 當timeupdate事件大概每秒一次,用來更新音頻流的當前播放時間
    onTimeupdate (res) {
      //   console.log('timeupdate')
      //   console.log(this.audio.maxTime)
      //   console.log(res.target.currentTime)
      this.audio.currentTime = res.target.currentTime
      this.sliderTime = parseInt(
        (this.audio.currentTime / this.audio.maxTime) * 100
      )
    },
    // 當加載語音流元數據完成後,會觸發該事件的回調函數
    // 語音元數據主要是語音的長度之類的數據
    onLoadedmetadata (res) {
      //   console.log("loadedmetadata");
      //   console.log(res);
      //   console.log(res.target.duration)
      this.audio.waiting = false
      this.audio.maxTime = parseInt(res.target.duration)
    },
    // 當音頻播放結束的時候
    onended (res) {
      // console.log(res)
      if (!this.type) {
        this.canPlay = false
      }
    }
  },
  filters: {
    formatSecond (second = 0) {
      return realFormatSecond(second)
    }
  },
  created () {

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main-wrap {
  padding: 10px 15px;
}
.slider {
  display: inline-block;
  width: 440px;
  top: 14px;
  margin-left: 15px;
  margin-right: 20px;
}
.di {
  display: inline-block;
}
.download {
  color: #409eff;
  margin-left: 15px;
}
.dn {
  display: none;
}
.audio-container{
    width: 620px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
}

.btn{
    height: 36px;
    width: 36px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    margin-right: 20px;
    cursor: pointer;
}

.time1{
    width: 42px;
    height: 22px;
    color: #999898;
    font-size: 16px;
    margin-right: 20px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.time2{
    width: 42px;
    height: 22px;
    color: #999898;
    font-size: 16px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.audio-container >>> .el-slider__runway{
    margin-top: 14px;
}
</style>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章