简体   繁体   中英

How do I play a sound over the same sound?

So I'm trying to get a sound to play at the same time, at different intervals. It's the exact same file, though I'm unsure how to play it multiple times without more than one tag.

It is actually very simple. Here is the code:

(You can click multiple times on the "Play" button to test)

 $(function () { $("button").click(function () { var audio = new Audio("https://www.gnu.org/music/free-software-song-herzog.ogg"); audio.play(); }); }); 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Audio</title> </head> <body> <button>Play</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </body> </html> 

have you considered using a setTimeout() function? for example if you play the sound, then moments later the same sound file is played.

var sound = document.getElementById("audio");
function PlaySound() {
setTimeout(function(){ sound.play(); }, 500);
setTimeout(function(){ sound.play(); }, 1000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM