Solved

onLoad Play Sound

Posted on 2004-09-14
15
1,367 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
ID: 12062225
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
ID: 12067740
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
ID: 12071315
<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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:princehyderabad
ID: 12089401
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
ID: 12089497
<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
ID: 12089677
can you plz define stopped variable, bcoz I dont know all this JS... pleasee
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12089740
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
ID: 12093008
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
ID: 12093348
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
ID: 12398343
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
ID: 12804072
HomeWabbit: Where is stopSound defined?
DavidLars: var stopped=true; // you mean =false
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12809428
>>  DavidLars: var stopped=true; // you mean =false

either way
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

830 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