www.webdeveloper.com
Results 1 to 2 of 2

Thread: Trying to play wav file when img or button clicked

  1. #1
    Join Date
    Feb 2011
    Location
    Kansas City, Mo
    Posts
    4

    Trying to play wav file when img or button clicked

    Hi everybody, I just registered here tonight.
    I'm having a problem that I thought would be simple, but it's driving me crazy!
    I simply want to play a short .wav sound when a button or image is clicked. I have one that plays once when the page opens, but I don't want that. I want the sound to wait for the OnClick.
    This script I have looks right, but I'm new and don't fully understand how to go about this. If anyone would help me out here, I would solve the Middle East trouble, eliminate the National debt, and find jobs for all the unemployed!
    Thanks for any guidance, JD ps. I use Chrome & IE
    ------------------------------------------------------------------------
    <style type="text/javascript">

    <script>
    function EvalSound(soundobj) {
    var thissound=document.getElementById(soundobj);
    thissound.Play();
    }
    </script>

    </style>
    </head>

    <body>

    <form>
    <input type="button" value="Play Sound" onClick="EvalSound('/pages/laugh.wav')">
    </form>

    <!-- sound file is laugh.wav ,it is in the same folder as this page ( /pages/pgtest9.html )--!>
    </body>
    </html>
    ************************************************************************************
    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SIMBAR={5DC47607-819F-11DF-897C-00904BDA636E}; GTB6.6; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; yie8; AskTbNG2V5/5.9.1.14019)
    Timestamp: Sat, 12 Feb 2011 08:27:55 UTC


    Message: Object expected
    Line: 21
    Char: 1
    Code: 0
    URI: http://mountaindream.freeoda.com/pages/pgtest9.html

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    for chrome and IE9:
    Code:
    function playme (url){
        var audio=document.createElement("audio");
        audio.src=url;
        audio.play();	
    }
    
    playme("http://www.nch.com.au/acm/11k16bitpcm.wav");


    EDIT:
    i went ahead and cooked up an xbrowser version that works in IE, provided they have WindowMedia installed, which should be most IE machines.
    If anyone out there can improve the IE code, please post amended code.


    (tested: FF4, IE7, CH9)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>testing ability to play wav files</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    
    <input value='play' type='button'  onclick="playme(audioURL)" />
    
    <script type='text/javascript'>
    
    
    audioURL="http://www.nch.com.au/acm/11k16bitpcm.wav";
    
    
    function playme (url){
    
     if("v"=="\v"){ //IE:
        var audio=document.createElement("div");
    	document.body.appendChild(audio);
        audio.innerHTML= '<object height="10" width="1" style="left:-9999px; height: 0px; width: 0px;" \
    		CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">\n\
    		<PARAM name="autoStart" value="TRUE"/>\n\
    		<PARAM name="UIMode" value="mini"/>\n\
    		<PARAM name="URL" value="***" />\n\
    	</object>'.replace("***", url);
     }else{
        var audio=document.createElement("audio");
        audio.src=url;
        audio.play();	
     } 
    }//end playme
    	
    </script>	
    </body>
    </html>
    Last edited by rnd me; 02-13-2011 at 04:40 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles