jsa*_*yer 5 javascript audio audio-player
我正在开发一个健全的JavaScript库.我可以用下面的代码播放声音.
var soundPlayer = null;
function playSound(){
soundPlayer = new Audio(soundName).play();
}
Run Code Online (Sandbox Code Playgroud)
如何停止和暂停此音频?当我尝试这样的时候:
soundPlayer.pause();
Run Code Online (Sandbox Code Playgroud)
要么
soundPlayer.stop();
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Uncaught TypeError: soundPlayer.stop is not a function
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
如果你改变了:
soundPlayer = new Audio(soundName).play();
Run Code Online (Sandbox Code Playgroud)
为此:
soundPlayer = new Audio(soundName);
soundPlayer.play();
Run Code Online (Sandbox Code Playgroud)
你的停顿会有效.问题是你为soundPlayer分配了"播放"功能.SoundPlayer现在不是Audio对象.
而不是停止()使用:
soundPlayer.pause();
soundPlayer.currentTime = 0;
Run Code Online (Sandbox Code Playgroud)
它的工作方式与我猜的相同.