The syntax of this onmouseover sound array is wrong. Can someone show me the correct way to make a simple preload sound array?

<script type="text/javascript">
      var aySound = new Array();
      aySound[0] = "http://localhost/a.mp3";
      aySound[1] = "http://localhost/b.mp3";
      aySound[2] = "http://localhost/c.mp3";
      aySound[3] = "http://localhost/d.mp3";
      aySound[4] = "http://localhost/e.mp3";
      aySound[5] = "http://localhost/f.mp3";

     onload=auPreload;
     function auPreload() {
           auCon.control = auCtrl;
           } 
     var Str = '';
        for (i=0;i<aySound.length;i++) 
        Str += .innerHTML="<embed src='" +aySound[i]+ "' hidden='true' autostart='true' loop='false' />";
    function auCtrl(whSound,play) {
           this.src = play? aySound[whSound]:'';
           }
    function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
    function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }
</script> 

Thanks jb

3 answers

danwellman 5600
2
points
This was chosen as the best answer

The part where you define the array is fine:

var aySound = new Array();
      aySound[0] = "http://localhost/a.mp3";
      aySound[1] = "http://localhost/b.mp3";
      aySound[2] = "http://localhost/c.mp3";
      aySound[3] = "http://localhost/d.mp3";
      aySound[4] = "http://localhost/e.mp3";
      aySound[5] = "http://localhost/f.mp3";

nothing wrong with that at all, except, as o.k.w suggested, I would use square-bracket notation instead, e.g. var aSound = [];

The next part of the script is wrong: onload=auPreload;

Here you're setting onLoad to the auPreload function, but the preload function hasn't been defined yet so the sript won't know what it is causing an error. Additonally I think you want onLoad to be a function that is executed when the page loads, but you're not attaching the function to the onLoad event using addEventListener() (or the MS equivalent) so onLoad is just becoming a global variable.

The auPreload function is defined as a function correctly, however you're setting the control property of the auCon object without having defined the object first. Objects need to be defined like this:

var auCon = {}; or window.auCon = {};

then you can add a control (or any other) property. Also, you're setting the value of the control property to auCtrl, but you haven't defined this function at this point in the script, so again this will throw errors.

The bit where you create the Str variable seems wrong, it looks like something is missing here - you're defining a variable to hold string content, which is fine, but then in the for loop you're trying to set the innerHTML property of the variable. You don't need to do this. the string will still be a string even if it contains markup characters such as '<' etc. The innerHTML property is a property of HTMLDomElements, so you probably want to get an element (or series of elements) from the page and set the innerHTML property of that so that it accepts the string as mark-up.

Try rearranging the script to this:

<script type="text/javascript">
      var aySound = new Array();
      aySound[0] = "http://localhost/a.mp3";
      aySound[1] = "http://localhost/b.mp3";
      aySound[2] = "http://localhost/c.mp3";
      aySound[3] = "http://localhost/d.mp3";
      aySound[4] = "http://localhost/e.mp3";
      aySound[5] = "http://localhost/f.mp3";

      function auCtrl(whSound,play) {
           this.src = play? aySound[whSound]:'';
      }

      function auPreload() {
          window.auCon = {};
          window.auCon.control = auCtrl;
      } 

      window.addEventListener("load", auPreload, false);

      //I really have no idea about this part, you need to get an element from the page or something...
      var Str = '';
      for (i=0;i<aySound.length;i++) {
        Str += "<embed src='" +aySound[i]+ "' hidden='true' autostart='true' loop='false' />";
      }

    function playSound(whSound) { if (window.auCon) window.auCon.control(whSound,true); }
    function stopSound(whSound) { if (window.auCon) window.auCon.control(whSound,false); }
</script> 

That should get you started, but it will probably need further debugging. How are you testing the script? You should be using Firefox with the Firebug plugin as this will report errors in the console so you can see what is stopping the script from running.

to be honest, playing sound with JS is not a trivial task and if you don't know javascript it probably isn't the best thing to start on. Have a read through the w3Schools online JS tutorials to give you a foundation as this will explain the basics of JS

Answered almost 7 years ago by danwellman
  • +1 for the extensive answer :) o.k.w almost 7 years ago
  • After some studying, I suspect `window.auCon` could be an iframe which loads the sound files. o.k.w almost 7 years ago
  • ahh, good point...that would make sense, inwhich case it should probably be something like var auCon = document.getElementById("iframe_id_here"); danwellman almost 7 years ago
  • But then again, iframes don't have a control property...I suspect auCon needs to be an object. I can see how setting the src of the iframe with the path to the media file could work, but it needs more to interface this iframe (if it exists) with the auCtrl function... danwellman almost 7 years ago
o.k.w 2355
2
points

I am not able to test your codes without the html markups. If the error is array related, try either of the followings:

var aySound = new Array(6);
//or
var aySound = [];
Answered almost 7 years ago by o.k.w
  • I will ass the HTML markup Battle Tested almost 7 years ago
  • using square brackets is the best :D danwellman almost 7 years ago
  • Dan: Why is one better than the other? Doug almost 7 years ago
0
points

im just trying to learn java, i pieced together some code from other scripts to make a simple array. i know that my code doesnt make sense to a pro. most of it dont make sense to me, thats why i am trying to learn it. i really dont know if i even should have used auCon. i am lost. if someone could fix that code, maybe i can learn a lot from their fix. thanks jb

Answered almost 7 years ago by Battle Tested