声音播放/停止/暂停 [英] Sound Play / Stop / Pause
本文介绍了声音播放/停止/暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个完善的JavaScript库.我可以使用以下代码播放声音.
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();
或
soundPlayer.stop();
但是我得到这个错误:
Uncaught TypeError: soundPlayer.stop is not a function
我该怎么办?
推荐答案
如果您进行了更改:
soundPlayer = new Audio(soundName).play();
为此:
soundPlayer = new Audio(soundName);
soundPlayer.play();
您的暂停将会起作用.问题是您为soundPlayer分配了播放"功能. SoundPlayer现在不是音频对象.
Your pause will be working. The problem is that you assigned "play" function to soundPlayer. SoundPlayer isnt an Audio object now.
代替stop()使用:
Instead of stop() use:
soundPlayer.pause();
soundPlayer.currentTime = 0;
它的工作原理与我猜想的一样.
It works the same I guess.
这篇关于声音播放/停止/暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文