<template>
  <div class="FlowAudioDiv box">
    <div class="FlowAudioIcon" @click="audioClick">
      <div v-show="!audioType" class="iconfont icon-audio-play"></div>
      <div v-show="audioType" class="iconfont icon-audio-pause"></div>
    </div>
    <div class="FlowAudioRange box_f1">

      <div class="FlowAudioRangeP box box_i_center">
        <div ref="touchLine" :class="`FlowAudioRangeL ${animationName}`">
          <div class="FlowAudioRangeLB" :style="`width:${touchIbfb}%`"></div>
        </div>
        <div class="FlowAudioRangeI" :style="`left:${touchILeft}px`" @touchend="touchEvent" @touchmove="touchEvent" @touchstart="touchEvent"></div>
      </div>
      <div class="FlowAudioRangeT box box_between"><span>{{sTime}}</span><span>{{eTime}}</span></div>
    </div>
  </div>
</template>

<script>
    export default {
        name: "d-flowAudio",
        mounted() {
          let self = this;
          //初始化音频
          this.audioDom = new Audio();
          this.audioLoad(this.src)

          this.audioDom.addEventListener('loadedmetadata',function(event){
            self.lTime = event.target.duration;
            self.eTime = self.timeStr(self.lTime);
          });
          this.audioDom.addEventListener('error',function(event){
          });
          this.audioDom.addEventListener('timeupdate',function(event){
            self.tTime = event.target.currentTime;
            self.audioToPx()
          });
          this.audioDom.addEventListener('waiting',function(event){
            self.animationName = 'FlowAudioRangeL_A'
          });
          this.audioDom.addEventListener('canplay',function(event){
            self.animationName = ''
          });
          this.audioDom.addEventListener('ended',function(event){
            self.audioLoad(this.src)
          });
        },
        data() {
            return {
              audioDom: null,
              sTime: "00:00",
              eTime: "00:00",
              lTime: 0,
              tTime: 0,
              proportion: 0,
              audioType: false,
              animationName: "",

              clientWidth: 0,
              pageX: 0,
              touchILeft: 0,
              _touchILeft: 0,
              touchIW: 0,
              touchIbfb: 0
            }
        },
        props: {
          src: String
        },
        components: {},
        methods: {
          audioClick() {
            if(!this.audioType)this.audioPlay()
            else this.audioPause()
          },
          audioLoad(src) {
            //初始化音频
            this.sTime = "00:00"
            this.eTime = "00:00"
            this.lTime = 0
            this.tTime = 0
            this.proportion = 0
            this.audioType = false
            //
            this.pageX = 0
            this.touchILeft = 0
            this._touchILeft = 0
            this.touchIW = 0
            this.touchIbfb = 0
            //
            this.audioDom.src = src
            this.audioDom.load()
          },
          audioPlay() {
            //播放
            this.touchIW = this.$refs.touchLine.clientWidth;
            this.audioType = true
            this.audioDom.play()
          },
          audioPause() {
            //暂停
            this.audioType = false
            this.audioDom.pause();
          },
          audioToPx() {
            this.sTime = this.timeStr(this.tTime)
            this.proportion = this.tTime / this.lTime
            this.touchILeft = parseInt(this.touchIW * this.proportion)
            this.touchIbfb = parseInt(this.proportion * 100)
          },
          pxToAudio() {
            this.tTime = this.proportion * this.lTime
            this.sTime = this.timeStr(this.tTime)
            this.audioDom.currentTime = this.tTime
          },
          timeStr(_s) {
            _s = parseInt(_s)
            let s = parseInt(_s%60)
            if(s<10) s="0"+s;
            let f = parseInt(_s/60)
            if(f<10) f="0"+f;
            return f+':'+s
          },
          touchEvent(event) {
            let touch = event.targetTouches[0];
            let self = this;
            switch(event.type) {
              case 'touchstart':
                self.audioPause()
                self.touchIW = self.$refs.touchLine.clientWidth;
                self.pageX = touch.pageX
                self._touchILeft = self.touchILeft
                break;
              case 'touchmove' :
                let c = self._touchILeft + touch.pageX - self.pageX;
                if(c<0)c = 0;
                if(c>self.touchIW-2)c = self.touchIW-2;
                self.proportion = c/self.touchIW
                self.touchIbfb = parseInt(self.proportion*100)
                self.touchILeft = c
                self.pxToAudio()
                break;
              case 'touchend' :
                self.audioPlay()
                self._touchILeft = self.touchILeft
                break;
            }
          }
        }
    }
</script>

<style scoped>
  .FlowAudioDiv {
    width: 100%;
    height: 60px;
    border-radius: 5px;
    padding: 10px 15px;
    box-sizing: border-box;
    border: 1px solid #dddddd;
  }
  .FlowAudioIcon {
    width: 38px;
    height: 38px;
    text-align: center;
    line-height: 38px;
    margin-right: 10px;
    color: #04be02;
    border: 1px solid #04be02;
    border-radius: 20px;
    position: relative;
  }
  .FlowAudioIcon::after {
    content: '';
    width: 100%;
    height: 100%;
    position: absolute;
    border-radius: 20px;
    left: 0;
    top: 0;
    display: none;
    z-index: 0;
    pointer-events: none;
    background: rgba(0, 0, 0, .1);
  }
  .FlowAudioIcon:active::after {
    display: block;
    pointer-events: none;
  }
  .FlowAudioIcon .icon-audio-play {
    padding-left: 3px;
    font-size: 20px;
  }
  .FlowAudioIcon .icon-audio-pause {
    font-size: 20px;
  }
  .FlowAudioRange {
    font-size: 20px;
    position: relative;
    height: 100%;
  }
  .FlowAudioRangeP {
    position: relative;
    width: 100%;
    height: 70%;
    padding: 0 10px;
    box-sizing: border-box;
  }
  .FlowAudioRangeT {
    height: 30%;
    line-height: 12px;
    font-size: 12px;
    color: #999;
    padding: 0 10px;
    box-sizing: border-box;
  }
  .FlowAudioRangeI {
    position: absolute;
    z-index: 2;
    width: 10px;
    height: 10px;
    top: 3px;
    border-radius: 10px;
    border: 5px solid #d5f2d8;
    background-color: #04be02;
  }
  .FlowAudioRangeL {
    height: 2px;
    width: 100%;
    background-color: #ddd;
  }
  .FlowAudioRangeL_A {
    background-image: url("../images/x.png");
    background-repeat: repeat-x;
    background-position-x: 5px;
    animation: FlowAudioRangeL_a 1s linear infinite;
  }
  @keyframes FlowAudioRangeL_a {
    from {background-position-x: 0px;}
    to {background-position-x: 40px;}
  }
  .FlowAudioRangeLB {
    height: 2px;
    background-color: #04be02;
  }
</style>
