Solved

onLoad Play Sound

Posted on 2004-09-14
15
1,340 Views
Last Modified: 2009-07-29
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
0
Comment
Question by:princehyderabad
15 Comments
 
LVL 6

Expert Comment

by:sathishv
Comment Utility
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
0
 

Author Comment

by:princehyderabad
Comment Utility
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???
0
 
LVL 13

Expert Comment

by:davidlars99
Comment Utility
<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()">
0
 

Author Comment

by:princehyderabad
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:davidlars99
Comment Utility
<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
0
 

Author Comment

by:princehyderabad
Comment Utility
can you plz define stopped variable, bcoz I dont know all this JS... pleasee
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 13

Expert Comment

by:davidlars99
Comment Utility
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>
0
 

Expert Comment

by:stevegmag
Comment Utility
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>
0
 
LVL 13

Accepted Solution

by:
davidlars99 earned 125 total points
Comment Utility
this is more sofisticated code...

<html>
<head>

<script language=Javascript>

var current=1,tmr=null;

function Load_(){

   if(document.getElementById("snd"+current).ReadyState==4)  
        current++;  
   if(current==4){
        current=1;      
        document.getElementById("snd"+current).play();
        setTimeout('Play_()',10)
        return
   }
   setTimeout('Load_()',10);
}

function Play_(){

   if(current==4){
         current=1;
         Load_();
         return
   }  
   if(document.getElementById("snd"+current).PlayState==0){
         current++;  
         if(current==4){
               current=1;
               Load_();
               return
         }    
         document.getElementById("snd"+current).play();          
   }
   setTimeout('Play_()', 10)
}
</script>

</head>
<body topmargin="5" leftmargin="5" rightmargin="5" bottommargin="5" onload="Load_()">

<object ID="snd1" WIDTH=300 HEIGHT=26 CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" style="display:none">
   <param name="FileName"  value="file1.mp3">
   <param name="AutoStart"                 value="false">
   <param name="SendPlayStateChangeEvents" value="true">
   <param name="ShowAudioControls"         value="false">
   <param name="ShowTracker"               value="false">
   <param name="AutoSize"                  value="false">
   <param name="volume"                    value="0">
   <param name="loop"                      value="false">
   <param name="hidden"                    value="true">
</object>

<object ID="snd2" WIDTH=300 HEIGHT=26 CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" style="display:none">
   <param name="FileName"  value="file2.mp3">
   <param name="AutoStart"                 value="false">
   <param name="SendPlayStateChangeEvents" value="true">
   <param name="ShowAudioControls"         value="false">
   <param name="ShowTracker"               value="false">
   <param name="AutoSize"                  value="false">
   <param name="volume"                    value="0">
   <param name="loop"                      value="false">
   <param name="hidden"                    value="true">
</object>

<object ID="snd3" WIDTH=300 HEIGHT=26 CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" style="display:none">
   <param name="FileName"  value="file3.mp3">
   <param name="AutoStart"                 value="false">
   <param name="SendPlayStateChangeEvents" value="true">
   <param name="ShowAudioControls"         value="false">
   <param name="ShowTracker"               value="false">
   <param name="AutoSize"                  value="false">
   <param name="volume"                    value="0">
   <param name="loop"                      value="false">
   <param name="hidden"                    value="true">
</object>


</body>
</html>
0
 
LVL 1

Expert Comment

by:homewabbit
Comment Utility
Hi,

Not sure what code your using.. try this:

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

2scents
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
HomeWabbit: Where is stopSound defined?
DavidLars: var stopped=true; // you mean =false
0
 
LVL 13

Expert Comment

by:davidlars99
Comment Utility
>>  DavidLars: var stopped=true; // you mean =false

either way
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now