在计时器上播放 (html5) 音频
我有一个页面需要根据数组的值播放声音(并播放整个数组)。例如,我有一个数组 {1, 0, 0, 0, 1, 1, 0, 1, 0},我需要它们按顺序播放或暂停,1 = 播放,0 =“暂停”。
到目前为止,我已经尝试了很多事情,但就是无法让它按照我的预期工作。
我得到的最接近的是:
$.each(myFinalList, function(i, playit){
if(playit == 1){
$("#results").append("Play..");
setTimeout("playSound()", 3250);
}
else{
$("#results").append("Nothing..");
setTimeout("doNothing()", 3500);
}
};
function playSound(){
var snd = new Audio('');
if(snd.canPlayType('audio/mp3')){
snd = new Audio('sound.mp3');
}
snd.play();
}
function doNothing(){
//dont do anything
}
这个问题是声音没有在正确的时间播放,几乎就像 doNothing() 没有被调用一样。
有什么想法吗?
I have a page that needs to play a sound based on the value of an array (and play the whole array out). So, for example, I have an array {1, 0, 0, 0, 1, 1, 0, 1, 0} and I need them to play or pause in order, 1 = play, 0 = "pause".
I've tried a number of things so far, but just can't get it to work as I expect.
The closest I've gotten was:
$.each(myFinalList, function(i, playit){
if(playit == 1){
$("#results").append("Play..");
setTimeout("playSound()", 3250);
}
else{
$("#results").append("Nothing..");
setTimeout("doNothing()", 3500);
}
};
function playSound(){
var snd = new Audio('');
if(snd.canPlayType('audio/mp3')){
snd = new Audio('sound.mp3');
}
snd.play();
}
function doNothing(){
//dont do anything
}
The issue with this is that the sounds aren't playing at the proper time, almost like the doNothing() is not getting called.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
http://jsfiddle.net/ZkE8B/12/
让我知道它是否有助于
编辑: http://jsfiddle.net/ZkE8B/15/ 清除每个循环完成
Try this:
http://jsfiddle.net/ZkE8B/12/
let me know if it helps
edit: http://jsfiddle.net/ZkE8B/15/ clears the transcript on every cycle complete
听起来您想按顺序迭代 myFinalList 的每个元素,但每次迭代之间都有超时。在这种情况下,您可能想要执行类似的操作
,每 3500 毫秒一次迭代一个 myFinalList 的每个元素。
It sounds like you want to iterate through each element of myFinalList in order, but with a timeout between each iteration. In this case, you probably want to do something like
which will iterate through each element of myFinalList, one at a time, every 3500 milliseconds.