Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Play Sound

  1. #1
    cmc
    cmc is offline
    New to the CF scene
    Join Date
    May 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Play Sound

    How do you play a sound from an onClick event?

    tia,
    cmc

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Here is how you do it with a link onclick, if you want to give the user some controls, then check out the Embedded Windows Media Player thread(under Multimedia) there are a few versions there.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript"><!--
    function play(what){
    document.getElementById('music1').innerHTML="<embed type='application/x-mplayer2' id='music2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='"+what+"' name='MediaPlayer1' width='1' height='1' controltype='2' showcontrols='1' showstatusbar='1' AutoStart='true' loop='false'></embed>";
    }
    function stopsong(){
    document.getElementById('music1').innerHTML="";
    }
    //-->
    </script>
    </head>
    <body>
    <a href="http://urltosonghere.mp3" onclick="play(this.href);return false">Play Clip</a>
    <br>
    <a href="#" onclick="stopsong();return false">Stop Clip</a>
    <br><br>
    <span id="music1"></span>
    </div>
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is how you do it with a link onclick, if you want to give the user some controls, then check out the Embedded Windows Media Player thread(under Multimedia) there are a few versions there.

    Code:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript"><!--
    function play(what){
    document.getElementById('music1').innerHTML="<embed type='application/x-mplayer2' id='music2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='"+what+"' name='MediaPlayer1' width='1' height='1' controltype='2' showcontrols='1' showstatusbar='1' AutoStart='true' loop='false'></embed>";
    }
    function stopsong(){
    document.getElementById('music1').innerHTML="";
    }
    //-->
    </script>
    </head>
    <body>
    <a href="http://urltosonghere.mp3" onclick="play(this.href);return false">Play Clip</a>
    <br>
    <a href="#" onclick="stopsong();return false">Stop Clip</a>
    <br><br>
    <span id="music1"></span>
    </div>
    </body>
    </html>
    Just one reason Aerospace has gotten many nominations for helpful member

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    heh, thanks scooter.

  • #5
    cmc
    cmc is offline
    New to the CF scene
    Join Date
    May 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Play Sound reply

    Thanks for the speedy replies, You guys are awesome. The code you provided opens up another page, My goal is to utilize a <input type='button' value='Play Sound' onClick='javascript()'> that will play a wav file without opening another page.

    tia,
    cmc

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    No, no it doesn't open another page. You may have used the script incorrectly. Any go I supply is tested before I give it to someone. Can you show us the code to your page in question along with the script I provided inside it?

  • #7
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    Code:
    <HTML>
    <Head>
    <Script Language=JavaScript>
    
    	function playIt(URL){
    
    		isSelection = URL;
    		embedStr = "<Embed Src="+isSelection+" Width=70 Height=40></Embed>"
    		document.getElementById('isEmbed').innerHTML = embedStr;
    	}
    
    </Script>
    </Head>
    <Body>
    <Div id='isEmbed'></Div>
    <input type=button value="Play Sound" onclick="playIt('http://www.mywebsite.com/media/angie.mp3')">
    </Body>
    </HTML>

  • #8
    cmc
    cmc is offline
    New to the CF scene
    Join Date
    May 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <script type="text/javascript"><!--
    function play(what){
    document.getElementById('music1').innerHTML="<embed type='application/x-mplayer2' id='music2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='"+what+"' name='MediaPlayer1' width='1' height='1' controltype='2' showcontrols='1' showstatusbar='1' AutoStart='true' loop='false'></embed>";
    }
    function stopsong(){
    document.getElementById('music1').innerHTML="";
    }
    //-->
    </script>

    This opens up another page.

    <a href="http://192.168.0.252/schedule/ding.wav" onclick="play(this.href);return false">Play Clip</a>

    What I need is this:

    <input type='button' onClick="javascriptlay('ding.wav')">

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <input type="button" value="Play" onclick="play('http://somesong.mp3')"><input type="button" value="Stop" onclick="stop()">
    or use cranford's code, both will work fine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •