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

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>

Answers


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


first of all pay attention, automatic play won't work under iOS; as Apple prevents it to be possible; as for stopping audio, I always suggest to use mediaelement library, which supports non HTML5 browsers and has a complete interface to start/stop audio playback


Need Your Help

Multiple headers for a table wx Python

python wxpython wx

Howdi folks, I have recently started dabbling in WX widgets using Python and would like to seek some direction on a task I want to achieve. The challenge here is to have a table with multiple headers

Shannon's Entropy calculation

matlab statistics probability information-theory

I have a probability distribution that defines the probability of occurrence of n possible states.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.