我正在开发一个健全的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();
你的停顿会有效。问题是你为soundPlayer分配了“播放”功能。 SoundPlayer现在不是Audio对象。
而不是停止()使用:
soundPlayer.pause();
soundPlayer.currentTime = 0;
它的工作方式与我猜的相同。