Return to Snippet

Revision: 2676
at March 24, 2007 13:50 by nicolaspar


Updated 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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Reproducir sonido - By Nicolás Pardo 2007</title>

<script>
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*Namespaces*/
var Media = document.Media || {};
// funciones para cookies

Media.PlaySound = {
	MSIE: navigator.userAgent.indexOf("MSIE"),
	NETS: navigator.userAgent.indexOf("Netscape"),
	OPER: navigator.userAgent.indexOf("Opera"),
	cookieName:  "cookie_sound_active",
	imgOn: "images/ico_on.gif",
	imgOff: "images/ico_off.gif",
	Sound: "_sonido.wav",
	WriteCookie: function( name, value ){
		var expdate=new Date();
		expdate.setTime(expdate.getTime()+10*365*24*60*60*1000);
		document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString();
	},
	ReadCookie: function( name ){
		var namearg = name + "=";
		var nlen = namearg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + nlen;
			if (document.cookie.substring(i, j) == namearg) {
				var endpos = document.cookie.indexOf (";", j);
				if (endpos == -1) endpos = document.cookie.length;
				return unescape(document.cookie.substring(j, endpos));
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
		return null;
	},
	OnOffSound: function( img ){
		newValue = Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == null  ? 0 : 1;
		img.src = newValue == 1 ? Media.PlaySound.imgOn : Media.PlaySound.imgOff;
		Media.PlaySound.WriteCookie( Media.PlaySound.cookieName, newValue );
	},
	SetMediaIE: function(){
		if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
			document.write('<bgsound loop="0" name="MediaMyMediaObj" id="MediaMyMediaObj" >');
		}
	},
	PlayNow: function(){
	if( Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieName ) == null ){
		obj = MM_findObj("MediaMyMedia");
			if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
				obj = MM_findObj("MediaMyMediaObj");
				obj.src = Media.PlaySound.Sound;
			} else {
				obj = MM_findObj("MediaMyMediaDiv");
				obj.innerHTML = '<embed  src="'+Media.PlaySound.Sound+'" hidden="true" volume="200" loop="0" type="audio/midi" >';
			}
		}
	}
}

Media.PlaySound.SetMediaIE();
</script>
</head>
<body>
<a href="#"><img src="images/ico_on.gif" onclick="Media.PlaySound.OnOffSound(this)" border="0" /></a>
<br />

<div name="MediaMyMediaDiv" id="MediaMyMediaDiv" style="margin:0; width:0; height:0;"></div>
<input type="button" value="dame" onclick="Media.PlaySound.PlayNow()" />
</body>
</html>

Revision: 2675
at March 24, 2007 12:36 by nicolaspar


Initial 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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Reproducir sonido - By Nicolás Pardo</title>

<script>
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*Namespaces*/
var Media = document.Media || {};

Media.PlaySound = {
	MSIE: navigator.userAgent.indexOf("MSIE"),
	NETS: navigator.userAgent.indexOf("Netscape"),
	OPER: navigator.userAgent.indexOf("Opera"),
	Sound: "_sonido.wav",
	SetMediaIE: function(){
		if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
			document.write('<bgsound loop="0" name="MediaMyMediaObj" id="MediaMyMediaObj" >');
		}
	},
	PlayNow: function(){
	obj = MM_findObj("MediaMyMedia");
		if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
			obj = MM_findObj("MediaMyMediaObj");
			obj.src = Media.PlaySound.Sound;
		} else {
			obj = MM_findObj("MediaMyMediaDiv");
			obj.innerHTML = '<embed  src="'+Media.PlaySound.Sound+'" hidden="true" volume="200" loop="0" type="audio/midi" >';
		}
	}
}
Media.PlaySound.SetMediaIE();
</script>
</head>
<body>
<div name="MediaMyMediaDiv" id="MediaMyMediaDiv" style="margin:0; width:0; height:0;"></div>
<input type="button" value="dame" onclick="Media.PlaySound.PlayNow()" />
</body>
</html>

Initial URL

                                

Initial Description
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.

Initial Title
Javascript - Play Sound (IE & FF)

Initial Tags

                                

Initial Language
JavaScript