PDA

View Full Version : A strange problem



nodeal
11-21-2007, 01:54 AM
Hi.

I have run into a strange problem with the "Random Background Music" script.

See, I have modified it a little bit because I need it to play .wav sound effects. -Which is not a problem at all - it does that perfectly...

The problem arise when I try to run it after a timed delay, using "setTimeout" or "setInterval": After the preset delay the function then runs a single time at which point the browser stops working:

The page don't autoreload (as it's supposed to if I use the "setTimeout" solution). - Rightclick don't work until I reload manually. - And when I reload (or does anything else on the toolbar) the page goes blank. It does play the soundfile when I refresh manually though, but without the delay.

Having tested a bit it seems there is no problem with running the function as such. No problem there. - It is only when I try to run it after a delay...


Can anybody please help me with this? - (It's propably my bad cos I dont have much of the first clue on java. I know, I know).

--
Here is the full code - Try it and you'll see what I mean:

<head>

<script language="JavaScript">

<!--
//By JavaScript Kit (http://www.javascriptkit.com) More JavaScripts here!

function play()
{
var sound1="audio/01.wav"
var sound2="audio/02.wav"
var sound3="audio/03.wav"
var sound4="audio/04.wav"
var sound5="audio/05.wav"
var x=Math.round(Math.random()*4)

if (x==0) x=sound1
else if (x==1) x=sound2
else if (x==2) x=sound3
else if (x==3) x=sound4
else x=sound5
if (navigator.appName=="Microsoft Internet Explorer")
document.write('<bgsound src='+'"'+x+'"'+' loop="no">')
else
document.write('<embed src='+'"'+x+'"'+'hidden="true" border="0" volume="2" width="20"

height="20" autostart="true" loop="false">')
}

-->

</script>

</head>

<body background="testimage.jpg">

<script>
setInterval("play()",1500)
</script>

</body>

-----

Ups.. Just realize that it wasn't a script here from DynamicDrive.. Oh well.. That doesn't mean you can't have an opinion on the problem anyways...

- Also just discovered another thing: After I reload the page and can use rightclick again: If I then choose "Show source" from the right-click menu all I get is a blank document containing nothing but one statement: <bgsound src="audio/01.wav" loop="no">

That would sort of explain the blank page and why there is no functionality etc... But WHAT THE... ???!!!

jscheuer1
11-21-2007, 03:45 AM
If you were to simply do:


setInterval("document.write(' ')", 1000);

Just that much would screw up the page. If you document.write after the page is parsed, you will overwrite the page.

These days though, and probably even back then, there are other ways to deal with this, at least for most browsers, IE no longer requires bgsound. Unfortunately the valid object tag isn't as fully supported as it should be, so I'll use embed:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var sound=[];
sound[0]='audio/01.wav';
sound[1]='audio/02.wav';
sound[2]='audio/03.wav';
sound[3]='audio/04.wav';
sound[4]='audio/05.wav';
sound.sort(function(){return 0.5 - Math.random();});
</script>
</head>
<body>
Hi<br>
<script type="text/javascript">
document.write('<embed style="position:absolute;top:-1000px;left:-1000px;" src="'+sound[0]+'" autostart="false" width="0" height="0" name="theSound" enablejavascript="true">');
setInterval('document.theSound.Play()', 1500);
</script>
There!
</body>
</html>

Works in Opera, FF, and IE, not in Safari.

Twey
11-21-2007, 04:42 AM
Ouch. That's some bad coding :-\ Using the <embed> element (although I'm sure there's probably a way to do it using <object> as well):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Sound Test</title>
<script type="text/javascript">
var RandomSound = {
getEmbed: function() {
var emb = document.createElement("embed");
emb.style.display = "none";
emb.setAttribute("autostart", false);
emb.setAttribute("enableJavascript", true);

return (RandomSound.getEmbed = function() {
return emb;
})();
},
play: function(src) {
var e = RandomSound.getEmbed();
e.src = src;
e.Play();
},
playRandom: function(playlist) {
playlist = playlist || RandomSound.playlist;
RandomSound.play(playlist[Math.floor(Math.random() * playlist.length)]);
}
};

RandomSound.playlist = [
"audio/01.wav",
"audio/02.wav",
"audio/03.wav",
"audio/04.wav",
"audio/05.wav"
];

onload = function() {
setTimeout(RandomSound.playRandom, 1500);
};
</script>
</head>
<body>
<p>Hi there!</p>
</body>
</html>

jscheuer1
11-21-2007, 07:13 AM
Something I forgot to mention, if you are interested in preventing script errors in those browsers that cannot handle the Play() or other methods used, instead of just:


setInterval('document.theSound.Play()', 1500);

It should be:


if(document.theSound&&typeof document.theSound.Play!='undefined')
setInterval('document.theSound.Play()', 1500);

Also, I did a little more investigating about browsers. Apparently Opera now supports the object tag for this, in addition to IE and FF. However, Opera's support for sound is fickle, sometimes it works, sometimes it doesn't, even on the same page. It also will sometimes reject certain sound files that other browsers find just fine. My report on Safari is for Safari Win and may not apply to Safari Mac, and even on Win may just be a configuration issue.

nodeal
11-21-2007, 06:38 PM
Thank you to both of you.

Twey's script dont work in my browser (IE6), so I'll relate to jscheuer1's script which works fine... Except that in stead of playing different random .wav files each 1500 milliseconds it plays the same - randomly selected - .wav file over and over each 1500 milliseconds. ;) lol.

I'd still like it to get to work this way without having to reload the browser content. But if you got better things to do then it's ok, cos if all else fails I just found a very simple and practical solution: I simply insert 3 seconds of silence into each .wav file itself, using audiacity. (Why didn't I think of that right away?! Doh!) I still have to reload the browser though. And of course it's not an ingenious java solution (so maybe I shouldn't post it here at all - though I thought you might be interested or amused to hear about it none the less). So although I'm no longer so desperate for it; any java (or other) programming suggestions are still very welcome...

And once again thank you very much to both of you.

jscheuer1
11-22-2007, 01:07 AM
If the sounds are all of short duration, as I'm imagining, and I understand what you are looking for, this should work out:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var sound=[];
sound[0]='audio/01.wav';
sound[1]='audio/02.wav';
sound[2]='audio/03.wav';
sound[3]='audio/04.wav';
sound[4]='audio/05.wav';
sound.sort(function(){return 0.5 - Math.random();});
</script>
</head>
<body>
Hi<br>
<script type="text/javascript">
for (var i = sound.length-1; i > -1; --i)
document.write('<embed style="position:absolute;top:-1000px;left:-1000px;" src="'+sound[i]+'" autostart="false" width="0" height="0" name="theSound'+i+'" enablejavascript="true">');
setInterval("document['theSound'+Math.floor(Math.random()*sound.length)].Play()", 1500);
</script>
There!
</body>
</html>

Demo:
http://home.comcast.net/~jscheuer1/side/rotate_snd.htm

Otherwise, please explain in more detail what exactly you want.

nodeal
11-22-2007, 03:27 AM
That works perfect. Thank you so much!

nodeal
11-22-2007, 03:51 AM
Ummm ... On closer inspection I guess I was a bit too quick there... -I was aiming at the sound effects coming at an interval of about a minute apart or so. So I set the interval to 60000. But they still come no more than about 10 seconds apart.. Strange..

(one hour later)

..Aaaahh... But rebooting my PC proved to work wonders. I guess it was just my browser was tired. :p

-Thanx again. It works perfect!

Twey
11-22-2007, 04:00 AM
Twey's script dont work in my browser (IE6), so I'll relate to jscheuer1's script which works fine...Please explain what happens, what errors you get, &c. Also check to see if you get errors in Firefox, because if you do they'll be much easier to understand than IE's excuse for error reporting. John's code in this case uses some very bad coding practices (among them the use of document.write() that's causing you to have to refresh your page every time you want to play a different sound) and I wouldn't really consider it a viable solution.

nodeal
11-22-2007, 04:40 AM
Please explain what happens, what errors you get, &c.

It don't execute. It's as simple as that. My browser just says "there was an error on the page." - For the sake of the experiment I just tried to implement it on the actual page too, just to see. But it don't affect the other javascript running on it too. That runs just fine.

There are only two things of notice: First is that the error message dont occur till a couple of seconds after pageload. So I guess the problem occur when it tries to play the first wav file. - The second thing is that a "broken image" produced by the other javascript on that page for some reason is located differently. (The other javascript switches between two images on mouseover - for some reason it shows the "broken image" on pageload, until first time its being activated by the mouseover)..

The other, switch-images scripts works just fine though, and places the images where theyre supposed to be placed. No idea why the "broken image" gets moved. Normally that gets horizontally aligned to the left side and vertically centered in its table cell. But in the version of the page with Tways soundfx script it looks like it gets centered both vertically and horizontally.


Also check to see if you get errors in Firefox, because if you do they'll be much easier to understand than IE's excuse for error reporting.

Sorry. I don't have firefox and I have no experience with it. (And Im not going to switch to firefox either.)



John's code in this case uses some very bad coding practices (among them the use of document.write() that's causing you to have to refresh your page every time you want to play a different sound) and I wouldn't really consider it a viable solution.

Well it worsk perfectly, and its simple. And Im lazy, so its just fine for me. ;)