I tell you, I want the file to play and stop as a specific key is pressed.
I have also thought that it stops with the press of another key, but now I am working only on a press of a key, specifically the M (77).
Another problem I have is that every time I press the music the file is played as many times as I press it, I'm still solving that.
I show you the code:
<script>
window.onload=function () {
document.onkeydown=teclado
}
//arranca la música en bucle
function startAudio(reproducir){
var musica =new Audio("musica.wav");
musica.addEventListener('ended',function()
{
this.currentTime=0.60;
this.play();
},false);
musica.play();
}
function teclado(objeto){
var tecla = objeto.which;
var num;
var reproducir;
//var situacionY = document.getElementById("mira").offsetLeft;
// var situacionX = document.getElementById("mira").offsetTop;
switch (tecla){
case 77://m
reproducir=true;
if(reproducir){
startAudio();
}
break;
case 78://n
reproducir=false;
stopAudio();
break;
default :alert("Q:esquivar. space:disparar cursor:moverse");
}
}
</script>
As you can see, I use a switch
, the code really has many more things, I have only eliminated what is left over to show you what I am not solving efficiently; other themes like dodging, shooting, etc. I have already solved them or I am improving it, but the concrete problem is this, only this point.
You had the right idea, I would do something like this.
If you absolutely don't need to start the loop in
0.60
, and can start from the beginning, you can use the property.loop
instead of using theended
event.