dnana
dnana

Reputation: 163

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>

Upvotes: 4

Views: 13314

Answers (4)

Akashxolotl
Akashxolotl

Reputation: 566

<audio id="audio_killbill" muted controls onload="audiofor_10sec()">
    <source src="kill_bill.mp3.mp3"></source>  <!-- Webkit Browser -->
    <source src="kill_bill.mp3.ogg"></source>  <!-- Mozilla/Firefox -->
    Your browser isn't invited for super fun <code>audio</code> time.
</audio>

Vanilla JS

function audiofor_10sec(){
     let audio = getElementById('audio_killbill');
     audio.muted = false; // New browser rule doesn't lets audio play automatically
     audio.play();         
     setTimeout(() => {
            audio.pause();
            audio.currentTime = 0; // Works as audio stop
     }, 10000);
 }

Upvotes: 0

user1555320
user1555320

Reputation: 495

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.

Upvotes: 0

Anthony JEAMME
Anthony JEAMME

Reputation: 384

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

Upvotes: 4

Ketan
Ketan

Reputation: 13

Put timer/counter on load of JS as it come to 10 seconds call the below function.

Timer setInterval(Call Below Function, 10000)

Stop Audio $.each($('audio'), function () { this.stop(); alert('Audio Stop Successfully'); });

TRY THIS ::::

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>TIMER</title>  
</head>
<body>


<audio id="aud" controls autoplay>
  <source src="test.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>


<script type="text/javascript">
 var myaud = document.getElementById("aud");
 var k = setInterval("pauseAud()", 10000);

 function playAud() {
     myaud.play();
 }

 function pauseAud() {
     myaud.pause();
     alert('Audio Stop Successfully');
     clearInterval(k); 
 } 
</script> 
</body>
</html>

Upvotes: 1

Related Questions