Sound Play / Stop / Pause

时光怂恿深爱的人放手 提交于 2020-07-06 10:04:29

问题


I'm developing a sound JavaScript library. I can play sound with below code.

var soundPlayer = null;

function playSound(){

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

}

How can I stop and pause this audio? When I try like this:

soundPlayer.pause();

Or

soundPlayer.stop();

But I get this error:

Uncaught TypeError: soundPlayer.stop is not a function

How can I do that?


回答1:


If you change that:

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

To that:

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

Your pause will be working. The problem is that you assigned "play" function to soundPlayer. SoundPlayer isnt an Audio object now.

Instead of stop() use:

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

It works the same I guess.



来源:https://stackoverflow.com/questions/43167907/sound-play-stop-pause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!