Sound in a JavaScript Alert

3

I hope you can support me with the following question.

At the moment of sending a alert in a validation I would like a sound to be output along with the alert as I can do that.

I have tried several things but they do not work, I hope you can guide me.

Thank you.

<audio src="Imagenes/alarma_fuerte.mp3" id="sonido"></audio>
<input type="button" id="alarma" value="sonaaaar !!!">

<script type="text/javascript">


    document.getElementById('#sonido').play();
    alert("elertaa !! ");

</script>
    
asked by fokus 30.07.2018 в 18:59
source

2 answers

2

You can use the play function to play your audio :

HTML:

<audio id="xyz" src="tuAudio.mp3" preload="auto"></audio>

Javascript:

document.getElementById('xyz').play();
alert("Alerta");
    
answered by 30.07.2018 / 19:03
source
3

Without playing with HTML:

I recommend you create another function

 var myAudio= document.createElement('audio');
 var myMessageAlert = "";
 myAudio.src = 'audio.mp3';
 myAudio.addEventListener('ended', function(){
    alert(myMessageAlert);
 });
function Myalert(message) { 
    myAudio.play();
    myMessageAlert = message;
} 
Myalert("Mensaje");

But if you want to overwrite alert() , it is the same as the previous one but you change the name of the function.

function alert(message) { 
  myAudio.play();
  myMessageAlert = message;
} 
alert("Mensaje");

Updated; The ended event is added to the dynamic audio element.

  

The ended event occurs when the audio / video has reached the end.

    
answered by 30.07.2018 в 19:04