Playing audio after the page loads in html

后端 未结 4 722
日久生厌
日久生厌 2020-12-15 08:27

I inserted the audio in html. But the audio gets started before the entire page loads. I want the audio to be played after the entire page gets loaded.

<         


        
相关标签:
4条回答
  • 2020-12-15 08:41

    Just Copy and Paste this Code in Body section of your HTML Code.

    <audio autoplay>
      <source src="song.mp3" type="audio/mpeg">
    </audio>
    

    and make sure that your audio file should be in same folder.

    0 讨论(0)
  • 2020-12-15 08:44

    For a repeated tone place the below code

            <audio src="./audio/preloader.mp3" autoplay="autoplay" loop="loop"></audio>
    

    and for a single time, remove the loop. Then it should be like this

            <audio src="./audio/preloader.mp3" autoplay="autoplay"></audio>
    
    0 讨论(0)
  • 2020-12-15 09:02

    You're going to need JavaScript for that. Remove the autoplay attribute:

    <audio id="my_audio" src="bg.mp3" loop="loop"></audio>
    

    and add a script like this:

    window.onload = function() {
        document.getElementById("my_audio").play();
    }
    

    Or if you use jQuery:

    $(document).ready(function() {
        $("#my_audio").get(0).play();
    });
    
    0 讨论(0)
  • 2020-12-15 09:02

    I think you must use JS (jQuery) function to do get document ready

    $(document).ready(function()

    Play an audio file using jQuery when a button is clicked

    You have answer here (if I follow you right)

    0 讨论(0)
提交回复
热议问题