小编Pol*_*nce的帖子

Playing audio file returns "Uncaught (in promise)" but works in console

I'm trying to play audio files (I've tried many). All of them are mp3s. I've tested the following on both MAMP localhost and also by just running it in the browser.

I use the following javascript:

var testSound = new Audio();
testSound.src = "a.mp3"
setTimeout(testSound.play.bind(testSound),100)
Run Code Online (Sandbox Code Playgroud)

This returns the error:

Uncaught (in promise)
Run Code Online (Sandbox Code Playgroud)

Trying to catch it:

var testSound = new Audio();
testSound.src = "a.mp3"
setTimeout(playSound,100)
function playSound () {
    testSound.play().then(response => {

    }).catch(e => {
        console.log(e);
    })
}
Run Code Online (Sandbox Code Playgroud)

returns nothing …

javascript audio html5 asynchronous html5-audio

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

asynchronous ×1

audio ×1

html5 ×1

html5-audio ×1

javascript ×1