want to play audio and it should be stop after 10 seconds inHtml and javascript?

html, javascript

Solution

A setTimeout seems more appropriate :

var audio = document.createElement("audio");
audio.src = "sound.mp3";

audio.addEventListener("canplaythrough", function () {
        setTimeout(function(){
            audio.pause();
            alert("Audio Stop Successfully");
        },
        10000);
}, false); 

Example

Problem

I am new in Html5 and javascript I want to play audio clip when i open html page and it should be stop after 10 seconds and display message that Audio play successfully. for simple audio play in html5 as below ``` <!DOCTYPE html> <html> <body> <audio controls> <source src="kill_bill.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> <script> setInterval(function(), 10000); $.each($('audio'), function () { this.stop(); alert('Audio Stop Successfully'); }); </script> </body> </html> ```

Original source