Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

onLoad Play Sound

hi,

Can anyone tell me whtz the easiest way to load a .wav file on page load and when click on the speaker image it should stop playing.

ie.. on page load default .wav will start playing, click <Speaker> will stop playing .wav, Click <Speaker> again will start playing .wav

Thx,
H
Avatar of sathishv
sathishv

You can use the embed tag. The autostart attribute tells the browser to start playing once the page loads.

<EMBED SRC="/sounds/mattdum.mid" autostart="true">

Cheers,
Sathish
Avatar of princehyderabad

ASKER

I got you, but how about stoping it !!!

Autostart on page load, fine but when user dont wanna listen and wanna read the text how can he stop???
<embed name="snd" SRC="/sounds/mattdum.mid" autostart="true">

<input type="button" value="Stop" onclick="document.snd.stop()">

<input type="button" value="Play" onclick="document.snd.play()">
Okay here is case:

I have page on load it autostart audio fine, anybody can do that, but I have only one Speaker Image, if I click that the autostarted audio should stop. And if I wanna hear again, I would click the same Speaker Image and it should start playing. Same Image, on 1 click stop, on other click play.

hope this is more clear.
<embed name="snd" SRC="/sounds/mattdum.mid" autostart="true">

<input type="button" value="Play/Stop" onclick="if(stopped){stopped=false;document.snd.play()}if(!stopped){stopped=true;document.snd.stop()}">

stopped variable must be defined first
can you plz define stopped variable, bcoz I dont know all this JS... pleasee
sure


<html>
<head>
<script language="Javascript">
var stopped=true;
function play_(){

   if(stopped){
       stopped=false;
       document.snd.play()
   }
   if(!stopped){
       stopped=true;
       document.snd.stop()
   }

}
</script>
</head>
<body>
<embed name="snd" SRC="/sounds/mattdum.mid">
<input type="button" value="Play/Stop" onclick="play_()">
</body>
</html>
I have this on one of my sites, got it from somewhere else...


<script language="JavaScript">
<!--
var soundFile = "../inc/snippets/sounds/Arachnoi-Cosmic_D-62.wav";
if (soundFile.indexOf(",") > 0) {
      var sounds = soundFile.split(",");
      soundFile = sounds[Math.floor(Math.random()*sounds.length)];
}
if (navigator.plugins.length>0) {
      if (navigator.mimeTypes["audio/wav"] && navigator.mimeTypes["audio/wav"].enabledPlugin) document.write('<embed src="'+soundFile+'" autostart="true" loop="true" hidden="true"></embed>');
}
else {
      document.write('<embed src="'+soundFile+'" autostart="true" loop="true" hidden="true"></embed>');
}
//-->
</script>
<noscript><embed src="/inc/snippets/sounds/Arachnoi-Cosmic_D-62.wav" autostart="true" loop="true"></embed></noscript>
<noembed><bgsound src="/inc/snippets/sounds/Arachnoi-Cosmic_D-62.wav" loop=true></noembed>
ASKER CERTIFIED SOLUTION
Avatar of davidlars99
davidlars99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Not sure what code your using.. try this:

<a href="javascript:stopSound()">Stop the sound</A>

2scents
Avatar of Michel Plungjan
HomeWabbit: Where is stopSound defined?
DavidLars: var stopped=true; // you mean =false
>>  DavidLars: var stopped=true; // you mean =false

either way