声音播放/停止/暂停

Sound Play / Stop / Pause

我正在开发声音 JavaScript 库。我可以用下面的代码播放声音。

var soundPlayer = null;

function playSound(){

soundPlayer = new Audio(soundName).play();

}

如何停止和暂停此音频?当我这样尝试时:

soundPlayer.pause();

或者

soundPlayer.stop();

但是我得到这个错误:

Uncaught TypeError: soundPlayer.stop is not a function

我该怎么做?

如果你改变它:

soundPlayer = new Audio(soundName).play();

对此:

soundPlayer = new Audio(soundName);
soundPlayer.play();

你的暂停会起作用的。问题是您将 "play" 函数分配给了 soundPlayer。 SoundPlayer 现在不是音频对象。

代替 stop() 使用:

soundPlayer.pause();
soundPlayer.currentTime = 0;

和我猜的一样。