Javascript - Play Sound (IE & FF)


/ Published in: JavaScript
Save to your folder(s)

Full example play sound and off&On sound play (Cookie engine).
Only copy one wav file in root, and the file of rename "_sonido.wav".
Create icons images/ico_on.gif, images/ico_off.gif.


Copy this code and paste it in your HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Reproducir sonido - By Nicolás Pardo 2007</title>
  6.  
  7. <script>
  8. function MM_findObj(n, d) { //v4.01
  9. var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  10. d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  11. if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  12. for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  13. if(!x && d.getElementById) x=d.getElementById(n); return x;
  14. }
  15.  
  16. /*Namespaces*/
  17. var Media = document.Media || {};
  18. // funciones para cookies
  19.  
  20. Media.PlaySound = {
  21. MSIE: navigator.userAgent.indexOf("MSIE"),
  22. NETS: navigator.userAgent.indexOf("Netscape"),
  23. OPER: navigator.userAgent.indexOf("Opera"),
  24. cookieName: "cookie_sound_active",
  25. imgOn: "images/ico_on.gif",
  26. imgOff: "images/ico_off.gif",
  27. Sound: "_sonido.wav",
  28. WriteCookie: function( name, value ){
  29. var expdate=new Date();
  30. expdate.setTime(expdate.getTime()+10*365*24*60*60*1000);
  31. document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString();
  32. },
  33. ReadCookie: function( name ){
  34. var namearg = name + "=";
  35. var nlen = namearg.length;
  36. var clen = document.cookie.length;
  37. var i = 0;
  38. while (i < clen) {
  39. var j = i + nlen;
  40. if (document.cookie.substring(i, j) == namearg) {
  41. var endpos = document.cookie.indexOf (";", j);
  42. if (endpos == -1) endpos = document.cookie.length;
  43. return unescape(document.cookie.substring(j, endpos));
  44. }
  45. i = document.cookie.indexOf(" ", i) + 1;
  46. if (i == 0) break;
  47. }
  48. return null;
  49. },
  50. OnOffSound: function( img ){
  51. newValue = Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == null ? 0 : 1;
  52. img.src = newValue == 1 ? Media.PlaySound.imgOn : Media.PlaySound.imgOff;
  53. Media.PlaySound.WriteCookie( Media.PlaySound.cookieName, newValue );
  54. },
  55. SetMediaIE: function(){
  56. if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
  57. document.write('<bgsound loop="0" name="MediaMyMediaObj" id="MediaMyMediaObj" >');
  58. }
  59. },
  60. PlayNow: function(){
  61. if( Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == null ){
  62. obj = MM_findObj("MediaMyMedia");
  63. if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
  64. obj = MM_findObj("MediaMyMediaObj");
  65. obj.src = Media.PlaySound.Sound;
  66. } else {
  67. obj = MM_findObj("MediaMyMediaDiv");
  68. obj.innerHTML = '<embed src="'+Media.PlaySound.Sound+'" hidden="true" volume="200" loop="0" type="audio/midi" >';
  69. }
  70. }
  71. }
  72. }
  73.  
  74. Media.PlaySound.SetMediaIE();
  75. </script>
  76. </head>
  77. <body>
  78. <a href="#"><img src="images/ico_on.gif" onclick="Media.PlaySound.OnOffSound(this)" border="0" /></a>
  79. <br />
  80.  
  81. <div name="MediaMyMediaDiv" id="MediaMyMediaDiv" style="margin:0; width:0; height:0;"></div>
  82. <input type="button" value="dame" onclick="Media.PlaySound.PlayNow()" />
  83. </body>
  84. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.