Forum Moderators: open

Message Too Old, No Replies

function to play BGSound

         

Reaper9207

10:01 am on Apr 16, 2004 (gmt 0)

10+ Year Member



With this I click the button to activate PlayPrayerSound. Earlier it was giving me an error, so I added the "window." command before the "doocument.", but it isn't openning the sound fire.
The sound files are named the same as the html files.

Is there something I am missing?

Any help would be appreicated.

Here is my code:


function PlayPrayerSound()
{
if(window.document.BGSound!= null)document.BGSound.Stop();
if(window.document.PrayerSound!= null)document.PrayerSound.Play();
}

Sincerly,
Justin

Alternative Future

10:29 am on Apr 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Reaper9207,

See if this works for you?

if(!document.getElementById('BGSound').play()){
document.getElementById('BGSound').stop();
}
if(!document.getElementById('PrayerSound').play()){
document.getElementById('PrayerSound').play();
}

I have replaced the window.document with window.getElementById, but I think the real issue you had was with the uppercase Play and Stop JavaScript is case sensitive and that functions are lowercase.

HTH,

-George

Reaper9207

10:56 am on Apr 16, 2004 (gmt 0)

10+ Year Member



Thanks, but now it's giving me, the RunTime Error again, saying that Line 1321: "This Object doesn't support this property or method."

Any clues? As of now, I have it:

1319. function PlayPrayerSound()
1320. {
1321. if(!window.getElementById('BGSound').play()){
1322. window.getElementById('BGSound').stop();
1323. }
1324. if(! window.getElementById('PrayerSound').play()){
1325. window.getElementById('PrayerSound').play();
1326. }
1327. }

I tried replacing window with document and with window.document But both of those gave me an error, saying that, Document/window.document.getElementById fuction does not exist. or something like that.

Alternative Future

11:08 am on Apr 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Reaper9207,

Firstly the getElementById is part of the document object rather than the window. Therefore window.getElementById will not work it has to be document.getElementById.

Here is the full working example i set up local to my machine:

<html>
<head>
<script language=javascript>
function PlayPrayerSound()
{
if(!document.getElementById('BGSound').play()){
document.getElementById('BGSound').stop();
}
if(!document.getElementById('PrayerSound').play()){
document.getElementById('PrayerSound').play();
}
}
</script>
</head>
<body>
<embed src="C:\WINDOWS\Media\ringin.wav" autostart=false id="BGSound" hidden=true>
<embed src="C:\WINDOWS\Media\ringin.wav" autostart=false id="PrayerSound" hidden=true>

<a href="javascript:PlayPrayerSound()">Test sound</a>
</body>
</html>

This example uses windows sounds my path is above you will have to replace the paths above to an actual *.wav on your machine.

Let's know how you get on with that...

-George

Reaper9207

1:31 am on Apr 17, 2004 (gmt 0)

10+ Year Member



I got everything the way you have it, and I'm still getting the,

Line 1321: "This Object doesn't support this property or method."

It's an MP3 File, do you think this might be the cause? Should I convert it to Wav?

Bernard Marx

1:44 am on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Confusion reigns.

If you are using the BGSOUND element:

<bgsound src="blah.wav" .....>

then, getElementById will not work, because 'bgsound' isn't it's id, but it's tagName.

You could try getting a reference with: document.getElementsByTagName('bgsound')[0]

but you might as well use the DOM level 0 method:

document.bgsound

..back where we started. Try this..

[msdn.microsoft.com ]

Reaper9207

1:59 am on Apr 17, 2004 (gmt 0)

10+ Year Member



Well, I'm editing this for a friend, who had HIS friend design it awhile back ... now it looks all crapped up. lol. Almost tempted to have him just have the whole thing redone.

Ok, after further investigations, I have noticed this is the tag it has:

<EMBED NAME=BGSound HIDDEN=TRUE SRC="sound/anxiety4.mp3" VOLUME="-500" LOOP="FALSE" AUTOSTART="TRUE">
<EMBED NAME=PrayerSound HIDDEN=TRUE SRC="prayers/anxiety4.mp3" VOLUME="-500" LOOP="FALSE" AUTOSTART="FALSE">

With that, should I change the current Embed tags? or should I change the Javascript to fit those current embeded tags.

Thanks for all the help,
Justin

Bernard Marx

3:33 am on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it looks like I was wrong - a little.

I need some sleep so I can't answer fully

With that, should I change the current Embed tags? or should I change the Javascript to fit those current embeded tags.

Perhaps that's a matter of preference.

Very likely, you can't reference the tag because it has a name attribute, not id. My preference would be to put in id attributes (same as name). Then try again:

var sound1 = document.getElementById('BGSound ')
etc

Put the element into a global var like that so it's easier to get at.

Hopefully that works. If not, check up on the methods available to the element. Here you go:

[webreference.com ]

Goodnight!