vue audio音频播放

vue audio音频播放

基于howler.js开发

myAudio.js

<template>
  <div class="video" ref="myAudio">
    <div class="player_audio">
      <!-- <div @click="play(true)" v-show="!playing">播放</div>
      <div @click="pause(true)" v-show="playing">暂停</div>
      <div @click="stop">停止</div>
      <div @click="mute(true)" v-show="!muted">静音</div>
      <div @click="mute(false)" v-show="muted">解除静音</div>
      <div @click="rate(0.5)">播放速度-0.5</div>
      <div @click="rate(1)">播放速度-1</div>
      <div @click="rate(1.25)">播放速度-1.25</div>
      <div @click="rate(1.5)">播放速度-1.5</div>
      <div @click="rate(2)">播放速度-2</div>
      <div @click="pre">上一首</div>
      <div @click="next">下一首</div> -->

      <div class="audio-area">
        <div class="audio">
          <van-slider
            v-model="value"
            active-color="#ee0a24"
            @drag-start="pause"
            @drag-end="play"
            @input="change"
          ></van-slider>
          <img src="./img/paly.png"  @click="play(true)" v-show="!playing" />
          <img src="./img/stop.png" @click="pause(true)" v-show="playing" />
        </div>
        <div class='time-area'>
          <span>{{ timer ? timer : '00:00' }}</span>
          <span>{{duration ? duration : '00:00'}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import './static/howler.min.js'
export default {
  name: 'myAudio',
  props:{
    audioSrc: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // audioSrc: ['http://music.163.com/song/media/outer/url?id=1401924960.mp3'],
      value: 0,
      timer: '',
      playing: false,
      currentIndex: 0,
      muted: false,
      duration: '',
      sound: null
    }
  },
  // components:{
  //     slider
  // },

  created() {},
  mounted() {
    this.audioPlayer()
    console.log(7777,this.sound)

  },
  methods: {
    audioPlayer(index = 0) {
      let _this = this
      // let au = document.createElement('audio')
      // au.src = this.audioSrc[index]

      // au.addEventListener(
      //   'loadedmetadata',
      //   function () {
      //     _this.duration = _this.formatTime(au.duration)
      //     // let duration = au.duration;
      //     //  alert("这段音乐的时长" + _this.formatTime(duration) + " 秒");
      //   },
      //   false
      // )
      this.sound = new Howl({
        src: 'http://music.163.com/song/media/outer/url?id=1401924960.mp3',
        autoplay: true,
        loop: true,
        volume: 0.5,
        html5: true,
        preload: true,
        rate: 1,
        mute: _this.muted,
        onload() {
          console.log('onload!', _this.sound, _this.sound.duration())
           _this.duration = _this.formatTime(_this.sound.duration())
        },
        onplay() {
          _this.playing = true
          //_this.duration = _this.formatTime(Math.round(this._duration));
          _this.step()
          // console.log('onload!', this)
        },
        onmute() {
          _this.muted = this._muted
        },
        onpause() {
          _this.playing = false
          _this.cancelAF()
        },
        onstop() {
          _this.playing = false
          _this.setTimerAndValue()
          _this.cancelAF()
        },
        onend() {
          console.log('end')
          _this.cancelAF()
          _this.next()
        }
      })
      // 因为ios的安全机制,把audio的canplay事件禁止了,导致了ios设备上自动播放功能不能简单的实现,需要用户点击一次才能播放
      this.sound.play()
      this.sound.pause()
    },

    formatTime(secs) {
      let minutes = Math.floor(secs / 60) || 0
      let seconds = secs - minutes * 60 || 0
      return (
        minutes + ':' + (Number(seconds).toFixed(0) < 10 ? `0${Number(seconds).toFixed(0)}` : Number(seconds).toFixed(0))
      )
    },

    play() {
      console.log(123, this.sound)
      this.sound.play()
    },

    pause() {
      this.sound.pause()
    },

    stop() {
      this.sound.stop()
    },

    mute(boolean) {
      this.sound.mute(boolean)
    },

    rate(rate) {
      this.sound.rate(rate)
    },

    pre() {
      this.stop()
      this.currentIndex = this.currentIndex >= 1 ? this.currentIndex - 1 : 0
      this.audioPlayer(this.currentIndex)
      this.play()
    },

    next() {
      this.stop()
      this.currentIndex = this.currentIndex + 1
      // console.log('this.currentIndex',this.currentIndex)
      if (this.currentIndex <= this.audioSrc.length - 1) {
        this.audioPlayer(this.currentIndex)
        this.play()
      } else {
        console.log('no next song!')
      }
    },

    setTimerAndValue() {
      let seek = this.sound.seek() || 0
      this.timer = this.formatTime(Math.round(seek))
      this.value = Math.round((seek / this.sound._duration) * 100) || 0
    },

    step() {
      this.setTimerAndValue()
      this.s = requestAnimationFrame(this.step)
      console.log(1)
    },

    cancelAF() {
      cancelAnimationFrame(this.s)
    },

    change(value) {
      this.sound.seek((this.sound._duration * value) / 100)
      this.setTimerAndValue()
    }
  },
  beforeDestroy() {
    this.cancelAF()
    this.stop()
  }
}
</script>

<style lang="scss" scoped>
.player_audio {
  padding-bottom: 0.3rem;

  .audio-area {
    .audio {
      position: relative;
      height: 64px;

      /deep/.van-slider {
        position: absolute;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
        width: 536px;
        .van-slider__bar {
              background-color: #1989fa !important;
        }
        .van-slider__button-wrapper {
          .van-slider__button {
            width: 14px;
            height: 14px;
            background: #1989fa;
          }
        }
      }
      img {
        position: absolute;
        top: 0;
        right: 0;
        width: 64px;
        height: 64px;
      }
    }
    .time-area {
      display: flex;
      justify-content: space-between;
      width: 536px;

    }
  }
}
</style>

评论

暂无评论