playing sound - Netscape vs. IE
I have a question about playing sound using <embed> and onMouseOver. Actually, I was able to get it to work very nicely by doing basically the following:
<html>
<script>
function playSound(){
document.name1.play(false)
}
function stopSound(){
doument.name1.stop()
}
</script>
<body>
<embed name="name1" src="song.mid" hidden=true loop=false autostart=false mastersound>
<a href="javascript:void('')" onMouseOver="playSound()" onMouseOut="stopSound()"><img src="picture.gif"></a>
</body
</html>
This works fine using Netscape Navigator or Netscape Communicator. However, when I use Internet Explorer (I think it was either v4.0 or v5.0) I get script errors on the 'document.name1.play' statement saying method or property is unsupported.
Any ideas on how I can get this to work with Internet Explorer? Thanks a million!
Bob
Kool Dude posted this at 03:48 — 24th February 2000.
They have: 19 posts
Joined: Oct 1999
Inn IE mae bee u kan uze a bgsound andd thenn plae itt onmouseover insted? Mite worc.
Bob posted this at 15:07 — 24th February 2000.
They have: 117 posts
Joined: Feb 2000
Kool Dude,
Thanks for the reply. I'm not sure how to run a bgsound from an onmouseover. It's not really the embed that doesn't work, that works fine with both browsers. It's the .play statement in the function that IE doesn't like. In order to play the bgsound from an onmouseover, I'd still need some script with some statement to do that, correct?
Ian posted this at 22:55 — 24th February 2000.
They have: 67 posts
Joined: Aug 1999
Hi Bob,
here is a mouseover sound script you might like to try. it works in NS and IE browsers.
<!-- --><BGSOUND ID="auIEContainer">
<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] = "YOUR.wav";
aySound[1] = "YOUR1.wav";
// DO NOT edit below this line
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><!-- -->
Then in your link add
onMouseOver="playSound(0)" onMouseOut="stopSound(0)"
IE: <A HREF="YourPage.html" onMouseOver="playSound(0)"
onMouseOut="stopSound(0)"></A>
You can preload and use different sounds on seperate links by adding aysound[2]=... etc and changing your mouseover to playsound(1), stopsound(1) etc.
Hope this helps.
Ian
Webmaster A1 JavaScripts
A1 JavaScripts
Web Development - Big Resources Inc
BIG Resources.com
Bob posted this at 23:14 — 24th February 2000.
They have: 117 posts
Joined: Feb 2000
Ian,
Thanks for the detailed reply! I'm still very new to this, and I'm trying to teach myself Javascript, so I really don't fully understand everything you're doing here, but I think I'll figure it out. Thanks again!
Bob
Bob posted this at 23:38 — 24th February 2000.
They have: 117 posts
Joined: Feb 2000
Ian,
OK. I cpoied this into an HTML document and tried it. I'm getting a syntax error on the statement:
ver4 = IE| |NS? 1:0;
And I get the error "playSound is not defined".
Any ideas?
Thanks again,
Bob
Bob posted this at 02:53 — 25th February 2000.
They have: 117 posts
Joined: Feb 2000
Ian,
I figured out what was wrong. The statement should have been:
ver4 = IE|NS? 1:0; instead of:
ver4 = IE| |NS? 1:0;
This seems to work great with my test case in both Netscape and IE. Thanks a million for your help!
Bob
Bob posted this at 04:54 — 25th February 2000.
They have: 117 posts
Joined: Feb 2000
Ian,
Sorry to bother you. First of all, a million thanks for the mouseover sound script you
gave me. It works great for both Netscape and IE.
I'm hoping you can help with one more thing. I think it should be simple at this point,
but I've been up til midnight trying to get it to work with no luck. I have 6 different
images on the screen, and with your script I can get a different song to play with each
image. The last thing i was trying to do was to also change the image to a different
picture when the music starts. I can get this to work for a single picture by putting
document.image1.src="newpic.gif" in the playSound funtion, and
document.image1.src="oldpic.gif" in the stopSound function (where image1 is in the
<img> tag (<img src="oldpic.gif name="image1>. The problem is trying to figure out how to pass parameter correctly to the playSound and stopSound functions so it
changes the correct images. Any ideas on how to do this?
Thanks again, you've really been great and I appreciate it tremendously.
Bob
Bob posted this at 13:09 — 26th February 2000.
They have: 117 posts
Joined: Feb 2000
OK everyone, I promise this will be my last post on this subject.
Again, thanks to Ian for his script - it works great, and I even figured out how to get all my images to change along with playing the sound.
My problem is that I really do not fully understand how the script works. I have spent hours looking at it and referencing my Javascript manual, but most of it still does not make sense to me. It is driving me crazy to have code running my sight that I do not understand. (I guess I need to get myself into a more advanced Javascript course).
If anyone has the time and would be willing to post a short expaination of what each line of code is doing, it would be tremendously appreciated.
Thanks in advance,
Bob
Lloyd Hassell posted this at 16:17 — 26th February 2000.
They have: 231 posts
Joined: Feb 2000
For using sound in a webpage, I would go for a plugin like Flash. It is easy to learn and there is a terrific free sound on/off demo that comes with Flash.
------------------
Lloyd Hassell
[email protected]
http://go.to/hass
:: Lloyd Hassell :: http://www14.brinkster.com/lloydh ::
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.