使用howler做一个音频播放器到底有多简单

强大的howler.js

howler官网 https://howlerjs.com/
howler git地址 https://github.com/goldfire/howler.js

实现一个音乐播放器

下载howler

npm i howler

template代码

<template>
  <div class="video_test">
    <div class="player_audio" >
      <div>
        <button @click="play(true)" v-show="!playing">播放</button>
        <button @click="pause(true)" v-show="playing">暂停</button>
        <button @click="stop" v-show="playing">停止</button>
      </div>
      <div v-show="playing">
        <div>
          音量:
          <input type="range" min="0" max="1" step="0.1" v-model="volume"  @input="volumeChange" />
          <button @click="mute(true)" v-show="!muted">静音</button>
          <button @click="mute(false)" v-show="muted">解除静音</button>
        </div>
        <div>
          播放速度:
          <select @change="rateChange" v-model="rate">
            <option :value ="0.5">0.5</option>
            <option :value ="1.0">1.0</option>
            <option :value ="1.25">1.25</option>
            <option :value ="1.5">1.5</option>
            <option :value ="2.0">2.0</option>
          </select>
        </div>
        <div>
          播放进度条:
          <input type="range" v-model="value" min="0" max="100"  @drag-start="pause" @drag-end="play"  @input="change" />
        </div>
        <button @click="pre">上一首</button>
        <button @click="next">下一首</button>
      </div>
    </div>
  </div>
</template>
<script>
  import {Howl} from 'howler'
  export default {
    name:'video-test',
    data() {
      return {
        rate: 1,
        volume: 1,
        audioSrc:[
          'https://yuan-1252477692.cos.ap-guangzhou.myqcloud.com/zzzzz/mp3/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E4%B8%9C%E4%BA%AC%E4%B8%8D%E5%A4%AA%E7%83%AD.mp3',
          'https://yuan-1252477692.cos.ap-guangzhou.myqcloud.com/zzzzz/mp3/French%20Kiss%20-%20Jealousy.mp3',
          'https://yuan-1252477692.cos.ap-guangzhou.myqcloud.com/zzzzz/mp3/VIVI%20-%20%E5%BC%80%E5%BF%83%E5%BE%80%E5%89%8D%E9%A3%9E.mp3',
          'https://yuan-1252477692.cos.ap-guangzhou.myqcloud.com/zzzzz/mp3/%E9%9D%92%E6%98%A5%E5%86%8D%E8%A7%81.mp3'
        ],
        value:0,
        timer:'',
        playing:false,
        currentIndex:0,
        muted:false
      }
    },
    components:{
      // slider
    },
    created() {
    },
    mounted(){
      this.audioPlayer()
    },
    methods: {
      audioPlayer(){
        let that = this
        this.sound = new Howl({
          src: this.audioSrc[this.currentIndex % this.audioSrc.length],
          autoplay: true,
          loop: true,
          volume: this.volume,
          html5: true,
          preload:true,
          rate:this.rate,
          mute:that.muted,
          onload() {
            console.log('onload!',this);
          },
          onplay() {
            that.playing = true
            //that.duration = that.formatTime(Math.round(this._duration));
            that.step()
            console.log('onload!',this);
          },
          onmute() {
            that.muted = this._muted
          },
          onpause() {
            that.playing = false
            that.cancelAF()
          },
          onstop() {
            that.playing = false
            that.setTimerAndValue()
            that.cancelAF()
          },
          onend() {
            console.log('end')
            that.cancelAF()
            that.next()
          },
        });

      },
      formatTime(secs) {
        let minutes = Math.floor(secs / 60) || 0;
        let seconds = (secs - minutes * 60) || 0;
        return minutes + ':' + (seconds < 10 ? '0': '') + seconds;
      },
      play(){
        this.sound.play();
      },
      pause(){
        this.sound.pause()
      },
      stop(){
        this.sound.stop()
      },
      volumeChange(e){
        this.sound.volume(e.target.value)
      },
      mute(boolean){
        this.sound.mute(boolean)
      },
      rateChange(e){
        this.sound.rate(e.target.value)
      },
      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
        this.audioPlayer(this.currentIndex)
        this.play()
      },
      setTimerAndValue(){

        let seek = this.sound.seek();
        if (typeof seek === 'number'){
          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);
      },

      cancelAF(){
        cancelAnimationFrame(this.s)
      },
      change(e){
        this.sound.seek(this.sound._duration * e.target.value / 100);
        this.setTimerAndValue()
      },

    },
    beforeDestroy(){
      this.cancelAF()
    }
  }
</script>

css

<style scoped>
  .video_test{
    text-align: center;
  }
  .player_audio{
    display: inline-block;
    padding-bottom: 0.3rem;
    text-align: left;
  }
  .player_audio div{
    padding: 0.1rem 0.15rem
  }
</style>

demo演示地址

https://yuan-1252477692.cos.ap-guangzhou.myqcloud.com/test/music/index.html#/howler

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现两个音频文件同步播放,可以使用Howler.js的时间同步功能。以下是一个基本的示例代码: ```js // 创建两个音频实例 var sound1 = new Howl({ src: ['audio1.mp3'] }); var sound2 = new Howl({ src: ['audio2.mp3'] }); // 监听第一个音频实例的"play"事件 sound1.on('play', function() { // 获取第一个音频实例的当前播放时间 var startTime = sound1.seek() || 0; // 同步第二个音频实例的播放时间 sound2.seek(startTime); }); // 同时播放两个音频文件 sound1.play(); sound2.play(); ``` 在上面的代码中,我们首先创建了两个音频实例`sound1`和`sound2`,并为它们分别指定了音频文件的路径。然后,我们监听了`sound1`的`play`事件,获取了当前播放时间,并将其同步到`sound2`中,从而实现了两个音频文件的同步播放。 如果你想在同步播放时添加其他效果,比如交叉淡入淡出效果,你可以在`play`事件中使用Howler.js的`fade`方法来实现,例如: ```js // 监听第一个音频实例的"play"事件 sound1.on('play', function() { // 获取第一个音频实例的当前播放时间 var startTime = sound1.seek() || 0; // 同步第二个音频实例的播放时间,并添加交叉淡入淡出效果 sound2.seek(startTime); sound2.fade(0, 1, 1000); // 从0淡入到1,持续时间为1秒 }); // 同时播放两个音频文件,并添加交叉淡入淡出效果 sound1.play(); sound1.fade(1, 0, 1000); // 从1淡出到0,持续时间为1秒 sound2.play(); ``` 上面的代码中,我们在`play`事件中使用了`fade`方法来实现了交叉淡入淡出效果,使得两个音频文件在切换时更加平滑。同时,我们在两个音频实例的`play`方法中也添加了交叉淡入淡出效果,使得播放开始时也更加平滑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值