Tried multiple solutions onsite, none worked: Play <audio> on Konami code

222 views Asked by At

This is my first question on Stack Overflow! :D There appear to be multiple similar questions, but I have tried each solution and none of them are working, and yet I'm determined to get to the bottom of this. The goal is to play audio upon successful konami code entry.

The simplest attempt is as follows (the scripts are found within the head, and the audio within the body. I have tried each solution with and without encapsulating them within a window.onload=function(){ } ):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://konami-js.googlecode.com/svn/trunk/konami.js"></script>

<script type="text/javascript">
  konami = new Konami(function() {
    document.getElementById('audio').play();
  });
</script>



<audio id="audio">
  <source src="js/ZipZipper.ogg" type="audio/ogg">
    <source src="js/ZipZipper.mp3" type="audio/mpeg">
</audio>

Another attempt looked like this:

var konami = new Konami(function() {
  var audioElement = document.createElement('audio');
  audioElement.setAttribute('src', 'js/ZipZipper.ogg');
  audioElement.load();
  audioElement.addEventListener("load", function() {
    audioElement.play();
  }, true);
});

And another, like so, again using the html audio tag

        konami = new Konami(playsong);
        playsong = function play() {
          var audio = document.getElementById("audio");
          audio.play();
        }

I am at a loss as to why nothing works. Everything seems to be in place!

1

There are 1 answers

0
mido On BEST ANSWER

According to the docs, you can do it like this:

konami = new Konami();
konami.code = function(){
    document.getElementById('audio').play();
};
konami.load();

Fiddle Demo

Note: not sure if this is how konami sequence must be, but the working sequence is up, up, down, down, left, right, left, right, b, a, enter