[英]How can i make three html audio elements work sequentially repeated using javascript
How can i make three HTML audio elements work sequentially repeated each one three times or more. 如何使三个HTML音频元素按顺序重复执行三次或更多次。
means I will make the first audio work three times, then the second work three times, then the third three times 意味着我将第一次音频工作三次,然后第二次工作三次,然后第三次三次
I have this code, but i don't have an idea to make the audio elements repeated: 我有这个代码,但我不知道重复音频元素:
HTML & JavaScript: HTML和JavaScript:
function play(j){ var audio = document.getElementById('audio'+j); //declaring audio audio.play(); //playing audio audio.addEventListener('ended', function (){ //after audio ends, it will check weather it will play again or no j++; if(j<=3){ play(j); } }) }
<audio controls id="audio1"> <source src="http://learn.shayhowe.com.s3-website-us-east-1.amazonaws.com/assets/misc/courses/html-css/adding-media/jazz.ogg" type="audio/mpeg"> your browser doesn't support audio </audio><br/> <audio controls id="audio2"> <source src="http://learn.shayhowe.com.s3-website-us-east-1.amazonaws.com/assets/misc/courses/html-css/adding-media/jazz.ogg" type="audio/mpeg"> your browser doesn't support audio </audio><br/> <audio controls id="audio3"> <source src="http://learn.shayhowe.com.s3-website-us-east-1.amazonaws.com/assets/misc/courses/html-css/adding-media/jazz.ogg" type="audio/mpeg"> your browser doesn't support audio </audio> <button onclick="play(1)">run audio</button>
function play(j){
var audio = document.getElementById('audio'+Math.floor(j)); //declaring audio
audio.play(); //playing audio
audio.addEventListener('ended', function (){ //after audio ends, it will check weather it will play again or no
j+=1/3;
if(j<=3){
play(j);
}
})
}
Simply just increase by a third (and round the number if you use it), so that it plays three times... 只需增加三分之一(如果使用它就将数字四舍五入),这样就可以播放三次......
Or more beautiful (using an double-recursive approach ;)): 或者更漂亮(使用双递归方法;)):
function playthree(i){
(function play(c){
var audio = document.getElementById('audio'+i); //declaring audio
audio.play(); //playing audio
audio.addEventListener('ended', function (){ //after audio ends, it will check weather it will play again or no
if(c>=3 ){//if more than three per audio
if(i<3){//and not reached the last audio
playthree(i+1);//play next audio
}//else end all
}else{//we didnt reached the third play, lets keep going...
play(c+1);
}
});
})(1)//start with 1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.