如何使用javascript自动播放html5音频标签

时间:2013-03-11 05:43:29

标签: javascript html html5 hidden

我从匿名中看到了这种污点,所以我想在玩游戏时添加一首隐藏的chiptune歌曲,但有点迷失。

<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> 

2 个答案:

答案 0 :(得分:3)

play是您正在寻找的方法

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

答案 1 :(得分:-1)

检查出来..它可能对你有帮助..!

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');

源代码@ Automatically playing audio with HTML5 and Javascript