Solved

onLoad Play Sound

Posted on 2004-09-14
15
1,376 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
Industry Leaders: 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!

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Title # Comments Views Activity
Can't get cursor to change 3 20
Link Anchor Not working 9 64
How do I Import CSV File In my PHP Application 29 66
Div not showing up 6 31
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
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 position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

740 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