How to use a javascript to autoplay a html5 audio tag

吃可爱长大的小学妹 提交于 2019-12-11 16:47:33

问题


I saw this deface from anonymous, so I want to add a hidden chiptune song when playing the game, but got kinda lost.

<script type="text/javascript" src="http://www.ussc.gov/include/js/konami.js"></script>
<script type="text/javascript">
  var success = function() {
                document.body.style.backgroundImage = "url(http://3.bp.blogspot.com/-vKs53Qq5mBI/T9vCsCb-irI/AAAAAAAAB8U/CZsS0iY5RjY/s1600/14685_1_other_wallpapers_anonymous.jpg)";
                document.body.style.backgroundSize = "100%";
                if(window.KICKASSGAME){window.KICKASSGAME.menuManager.menu.messageTypeChangeShip('242,0');
                                       window.KICKASSGAME.menuManager.destroy()}else{
    alert("AntiSec CAEK-mode activated... Destroy the system! Controls: up, down, left, right, space to fire.");
                var KICKASSVERSION='2.0';
                var s = document.createElement('script');
                s.type='text/javascript';document.body.appendChild(s);
                s.src='//hi.kickassapp.com/kickass.js';
                void(0);}
  }

  var konami = new Konami(success);

</script> 

回答1:


play is the method you are looking for

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'YourSong.ogg');
audioElement.load()
audioElement.addEventListener("load", function() { 
  audioElement.play(); 
}, true);



回答2:


Check this out.. It may help you..!

function play_sound(url){
    if(play_html5_audio){
        var snd = new Audio(url);
        //code
        snd.play();
    }else{          
        var sound = $("<embed id='sound' type='audio/mpeg' />");
        //code
        $('body').append(sound);
    }
}

play_sound('beep.mp3');

Source Code found @ Automatically playing audio with HTML5 and Javascript



来源:https://stackoverflow.com/questions/15331929/how-to-use-a-javascript-to-autoplay-a-html5-audio-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!