Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

onLoad Play Sound

Posted on 2004-09-14
15
Medium Priority
?
1,399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

610 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