【问题标题】:Howler.js: sound doesn't loadHowler.js:声音不加载
【发布时间】:2021-04-28 10:33:08
【问题描述】:

跟随basic example in the docs,但没有播放声音。我在 Network 选项卡中看到了 mp3 文件,但它是 90KB 而不是 5MB,所以我想它不能正确加载。

我尝试了不同的路径:src: ['@/assets/audios/test.mp3'], `src: ['../assets/audios/test.mp3']。没有任何效果。没有控制台错误。为什么它不起作用?

<template>
  <div class="container">
    <button @click="play">
      PLAY
    </button>
  </div>
</template>

<script>
import { Howl, Howler } from 'howler'

export default {
  data () {
    return {
      sound: ''
    }
  },
  mounted () {
    this.sound = new Howl({
      src: ['test.mp3']
    })
  },
  methods: {
    play () {
      this.sound.play()
    }
  }
}
</script>

【问题讨论】:

  • 你是从网络服务器上运行的吗?

标签: vue.js nuxt.js howler.js


【解决方案1】:

听起来您正在尝试为 src 加载资产 URL。

  1. 资产 URL 需要为 required,以便 Webpack 将实际 URL 解析为文件。

  2. 错误会被忽略,但您可以设置onloaderror 来处理它们。

export default {
  mounted () {
    this.sound = new Howl({
      // 1
      src: [
        require('@/assets/audios/test.mp3')
      ],

      // 2
      onloaderror(id, err) {
        console.warn('failed to load sound file:', { id, err })
      }
    })
  }
}

【讨论】:

  • 谢谢,但如果不使用资产 URL,那么文档中使用的包是什么?我不明白。同样使用您的代码,我现在收到此错误:Module parse failed: Unexpected character '' (1:3) 。这是否意味着 mp3 文件有问题?
  • “资产 URL”是指 Vue CLI 脚手架项目中 src/assets/ 中资源的 URL。 包不关心 URL 的来源,但它必须是现有资源的有效 URL。与&lt;img&gt;.src in Vue CLI scaffolds 不同,提供给Howl 的URL 不会自动转换,因此您需要为Howl 手动require
  • 关于解析错误:我无法在本地设置中重现该错误。我不知道那个错误的真正含义。
  • 如果由于某种原因您不想使用src/assets/require(URL),您可以将mp3 移至public/,其中URL 不需要转换。
  • 我将 mp3 移到那里,现在可以使用了,谢谢。但是你怎么知道它会起作用,文档对那部分只字未提。另外,不知道如何处理您的答案,因为从技术上讲我无法解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
相关资源
最近更新 更多