【问题标题】:Uncaught (in promise) DOMException: The element has no supported sources in audio play()Uncaught (in promise) DOMException: The element has no supported sources in audio play()
【发布时间】:2021-11-05 12:32:35
【问题描述】:

我打算在我的 js 代码中播放来自外部 url 的音频文件,但我在 google chrome 中不断收到 Uncaught (in promise) DOMException: The element has no supported sources. 和在 firefox 中出现 Uncaught (in promise) DOMException: The media resource indicated by the src attribute or assigned media provider object was not suitable. 的错误。

这是我的代码:

const sound = new Audio("https://soundcloud.com/saba-a-744718954/tick-1");

button.addEventListener("click", soundHandler);

function soundHandler() {
  sound.play()
}

感谢您的帮助。谢谢。

【问题讨论】:

    标签: javascript html web-audio-api


    【解决方案1】:

    传递给Audio 元素的 URL 必须解析为某些音频。

    您正在将 URL 传递给 HTML 文档!

    【讨论】:

      【解决方案2】:

      我用这个音频(audio.bfmtv.com)试试你的代码,效果很好:

      所以你的音频链接有问题!

      const appDiv = document.getElementById('app');
      appDiv.innerHTML = `<h1>Click here to start audio</h1>`;
      
      const sound = new Audio("https://audio.bfmtv.com/bfmbusiness_128.mp3?aw_0_1st.playerId=AudioPlayer_Web_Next");
      
      appDiv.addEventListener("click", soundHandler);
      
      function soundHandler() {
        sound.play()
      }
      

      你可以在这里试试:https://stackblitz.com/edit/js-wlfkh2?file=index.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-27
        • 2022-11-14
        • 2016-10-24
        • 2020-09-27
        • 2023-01-22
        • 1970-01-01
        • 2021-02-05
        相关资源
        最近更新 更多