PDA

View Full Version : Help with auPreload in the Click sound events


Alturis
09-19-2002, 04:12 PM
We are using the following script to play sounds when the mouse moves over buttons....

<script LANGUAGE="JavaScript"><!--
// Preload and play audio files with event handler (MouseOver sound)
// designed by JavaScript Archive, (c)1999
// Get more free javascripts at http://jsarchive.8m.com

var aySound = new Array();
// Below: source for sound files to be preloaded
aySound[0] = "laser.wav";

// DO NOT edit below this line
document.write('<BGSOUND ID="auIEContainer">')
IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0;
NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0;
ver4 = IE||NS? 1:0;
onload=auPreload;

function auPreload() {
if (!ver4) return;
if (NS) auEmb = new Layer(0,window);
else {
Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>";
document.body.insertAdjacentHTML("BeforeEnd",Str);
}
var Str = '';
for (i=0;i<aySound.length;i++)
Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>"
if (IE) auEmb.innerHTML = Str;
else {
auEmb.document.open();
auEmb.document.write(Str);
auEmb.document.close();
}
auCon = IE? document.all.auIEContainer:auEmb;
auCon.control = auCtrl;
}
function auCtrl(whSound,play) {
if (IE) this.src = play? aySound[whSound]:'';
else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"))
}
function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }
//-->
</script>


The problem is that the auPreload() is never executing just plugging this in the header as the instructions from this site say. We have to manaully add a call to it for the OnLoad event of our body. Can anyone shed some light on why this is? We are getting errors that pop up and are concerned that they are caused by manually calling auPreload at the wrong time.

Thanks for any help!

Mr J
09-19-2002, 05:46 PM
HI


I went to http://jsarchive.8m.com to see the script and comparing it with the one you posted there seems to be some
differences.

I cannot see function auPreload() in the original script

Anyway.....I tested the original script below and it does work ok.
Change the two sound files to your own





<SCRIPT><!--
// MouseOver/MouseClick audio.
// by JS Archive - http://jsarchive.8m.com

var ayAudio = new Array();
// Source for sound files to be preloaded
ayAudio[0] = "enter.wav";
ayAudio[1] = "enter2.wav";

function AudioCheck() {
var n = navigator.appName
if (n=="Netscape") this.ns = true;
else if (n=="Microsoft Internet Explorer" && document.all) this.ie = true;
else this.noAudio = true;
}

onload = function() {
is = new AudioCheck()
if (is.noAudio) return;

var s = '';
for (i=0; i<ayAudio.length; i++)
s += "<EMBED SRC='"+ayAudio[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>"

if (is.ns) {
auEmb = new Layer(0);
with (auEmb.document) { open();write(s);close() };
}
else document.body.insertAdjacentHTML("BeforeEnd",s);

auCon = is.ns? auEmb.document.embeds:auIEEmb;
auCon.ctrl = function(au,play) {
if (is.ie) this.src = play? ayAudio[au]:'';
else eval( "this[au]." + (play? "play()":"stop()") );
}
is.auDone = true;
}
function playAudio(whAu) { if (window.auCon) auCon.ctrl(whAu,1) }
function stopAudio(whAu) { if (window.auCon) auCon.ctrl(whAu,0) }
//-->
</SCRIPT>

<BGSOUND ID="auIEEmb">

<P><A HREF="javascript:void(0);" onMouseOver="playAudio(0)" onMouseOut="stopAudio(0)">Sound one</A>
<P><A HREF="javascript:void(0);" onMouseOver="playAudio(1)" onMouseOut="stopAudio(1)">Sound two</A>

Alturis
09-19-2002, 07:34 PM
Hmm... well trying that new code version, its still not working for us.

The crux of the issue is that the

onload =

is never executing... Has anyone run into this before? Im unfamiliar with the onload = syntax as ive never used it before. What things should we be aware of in how it works or what it expects? what reasons might ours be being skipped?

Mr J
09-19-2002, 10:35 PM
onload=

Means it will run that function when the script has loaded.

The script I posted was copied from the web address you gave.

And I tested it in my browser, IE5.5


What browser are you running it in?

Alturis
09-19-2002, 10:38 PM
IE 6

Any ideas?

Mr J
09-19-2002, 10:38 PM
PS

I have just copied and run the script you posted and inserted an alert in "function auPreload() " and it does run onload.

Post your link information

Alturis
09-19-2002, 10:43 PM
link information?

you mean our website?

We got around the problem by manually calling auPreload in the <Body OnLoad="auPreload()">, but this is causing debug errors to occur for people with debugging enabled. So we are assuming that putting the call within the Body is not the proper place. The document is NULL at this point which the function refers to.

Mr J
09-20-2002, 03:16 PM
No not your website link, the links that you are using to run the onmouseover sound effect. :)

OnLoad="auPreload()" does run I tried it as explained in my previous post the errors are elsewhere in the script.



Try this one out

<script language=javaScript>
<!--
function playsound(mysound){
var thissound= eval("document."+mysound)
thissound.play()
}
// -->
</script>

<embed src="soundfile1" autostart=false hidden=true name="sound1" mastersound>
<a href="#null" onmouseover="playsound('sound1')">OnClick</A>

adios
09-20-2002, 03:48 PM
onload=
Means it will run that function when the script has loaded.

http://www.devguru.com/Technologies/ecmascript/quickref/evhan_onload.html

Alturis
09-20-2002, 04:21 PM
adios: yes.. ive read such information since my post asking about it. But the problem is that all the documention about onload demonstrate it being used in the <body>... but in the code that is given here for sound events, there is an onload= right in the <Script> section... so should the onload= event within the script be called as soon as the script is "loaded"?

Thats what im not clear on... becuase its not being called for us.. we are debugging in Dreamweaver and the breakpoint is never hit.

adios
09-20-2002, 04:33 PM
Explanation:

When a new document (page) is fully loaded in the browser window, a load event is fired on the window object. Because of the way events are handled in browsers, this causes any JavaScript function assigned, like any other variable, to the onload property of the window object, to be run. There is no body.onload handler: the HTML <body> tag is used as a parking place for the HTML equivalent, for the obvious reason that there is no <window> tag, XML aside. It doesn't matter where you assign code to this event handler, <body> or window (sometimes the 'window' is omitted) - it's the exact same operation. If you do both, you'll simply over-write the first assignment with the second one.

The load event refers to the window - it has nothing to do with when a script is loaded. It's designed to allow programs to defer running until the objects they manipulate are available.

Hope that helps...