VUE + howler.js 播放音频

VUE2 + howler.js 播放音频  自动播放 

<div
    id="audio_btn"
    :class="audioClass"
    @click="audioClick"
/>

安装(howler.js官网:GitHub - goldfire/howler.js: Javascript audio library for the modern web.

npm install howler
<script>
import { Howl } from 'howler'

export default {
  data() {
    url:'', //mp3地址
    audioPlay: true, //播放状态
    soundBgm: null, //audio js
    audioClass: '' //audio class
  },
  created() {
    this.newAudioDom()
  },
  methods: {
    /** audio定义 */
    newAudioDom() {
      this.soundBgm = new Howl({
        src: [this.url],
        loop: true, //自动播放
        preload: true,
      })
      // 播放(强制自动播放)
      this.soundBgm.play()
      this.audioClass = 'rotate'  
    },
    /** 视频播放 */
    audioClick() {
      if (this.audioPlay) {
        // 暂停
        this.soundBgm.pause()
        this.audioClass = 'rotate rotate-pause'
      } else {
        // 播放
        this.soundBgm.play()
        this.audioClass = 'rotate'
      }
      this.audioPlay = !this.audioPlay
    }
  },
  beforeDestroy() {
    // 销毁
    this.soundBgm.unload()
  },
}

</script>
<style lang="less" scoped>
#audio_btn {
  width: 35px;
  height: 35px;
  position: absolute;
  right: 14px;
  background-image: url(../../assets/video/music.png);
  background-size: contain;
}
.rotate {
  -webkit-animation: rotating 10s linear infinite;
  -moz-animation: rotating 10s linear infinite;
  -o-animation: rotating 10s linear infinite;
  animation: rotating 10s linear infinite;
}
@-webkit-keyframes rotating {
  from {
    -webkit-transform: rotate(0);
  }
  to {
    -webkit-transform: rotate(360deg);
  }
}
@keyframes rotating {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}
@-moz-keyframes rotating {
  from {
    -moz-transform: rotate(0);
  }
  to {
    -moz-transform: rotate(360deg);
  }
}
.rotate-pause {
  animation-play-state: paused !important;
  -webkit-animation-play-state: paused !important; /* Safari 和 Chrome */
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个流行的前端框架,而 Three.js 是一个用于创建 3D 图形的 JavaScript 库。将两者结合起来可以创建出令人惊叹的 3D 应用程序。下面是 vue+three.js 的每步解析: 1. 创建 Vue.js 应用程序 首先,需要创建一个 Vue.js 应用程序。可以使用 Vue CLI 来生成一个新的项目或手动创建一个新的 Vue.js 应用程序。 2. 安装 Three.js 可以使用 npm 包管理器安装 Three.js,也可以直接下载 Three.js 库文件并将其添加到项目中。 3. 在 Vue 组件中引入 Three.jsVue.js 中使用 Three.js 需要将其导入到组件中。可以使用 ES6 的 import 语句来引入 Three.js。 4. 创建场景、相机和渲染器 在 Three.js 中创建 3D 场景需要场景、相机和渲染器。可以在 Vue.js 组件的 mounted 钩子函数中创建它们。 5. 创建 3D 对象 可以使用 Three.js 来创建各种类型的 3D 对象,如立方体、球体、平面等。可以在 Vue.js 组件的 mounted 钩子函数中创建它们。 6. 将 3D 对象添加到场景中 创建 3D 对象后,需要将它们添加到场景中。可以使用场景对象的 add 方法来添加它们。 7. 渲染场景 最后,在 Vue.js 组件的 mounted 钩子函数中使用渲染器对象来渲染场景。 这是一个基本的 vue+three.js 应用程序的步骤。当然,还有很多其他的细节需要注意,如处理用户交互、加载 3D 模型等。但是,掌握了这些基本步骤,就可以开始创建自己的 3D 应用程序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值