#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Preloading my audio file in html


    I'm currently using the below way to play sound effect on my website. I need the sound to play out everytime I received a protocol from my slave device:

    <script>
    Code:
    function RxProtocol()
    {
        playSound('audio123.wav');
    }
    
    function playSound(soundfile) 
    {
        document.getElementById("dummy").innerHTML=
        "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
    }
    </script>
    
    <body>
    <span id="dummy"></span>
    </body>
    But seems like this way the audio played is delayed everytime. After I received my protocol for 2 seconds, the audio only played.

    Is it because I did not preload my audio? Could you guys teach me how to modify my code and make it into preload enable?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    You could try something like this in your body:
    Code:
    <!-- embed your audio files here -->
    <audio id="audio123">
    	<source src="audio123.wav">
    </audio>
    <audio id="audio124">
    	<source src="audio124.wav">
    </audio>
    And change your javascript like so:
    Code:
    function RxProtocol()
    {
        playSound('audio123');
    }
    
    function playSound(soundfile) 
    {
    	document.getElementById(soundfile).play();
    }

IMN logo majestic logo threadwatch logo seochat tools logo