How to preload a sound in Javascript?

前端 未结 6 1667
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 13:15

I can preload images easily thanks to the onload function. But it doesn\'t work with audio. Browsers like Chrome, Safari, Firefox, etc. don\'t support the

6条回答
  •  执笔经年
    2020-11-27 13:32

    //Tested on Chrome, FF, IE6
    
    function LoadSound(filename) {
        var xmlhttp;
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("load-sound").innerHTML = '';
            }
        }
        xmlhttp.open("GET", filename, true);
        xmlhttp.send();
    }
    

    Reference

    • http://sharkysoft.com/tutorials/jsa/content/048.html
    • jQuery: How do you preload sound?
    • http://jsarchive.8m.com/Main/audio.html

提交回复
热议问题