JavaScript Javascript - MP3播放器 - 声音
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Javascript - MP3播放器 - 声音相关的知识,希望对你有一定的参考价值。
<!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>HTML PLAYER</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"),
cookieNameActive: "cookie_sound_active",
cookieNameThisSound: "cookie_this_sound",
imgOn: "images/ico_on.gif",
imgOff: "images/ico_off.gif",
SoundDefault: 1,
Sounds: {
0:{"file":"mp3/uno.mp3","name":"UNO"},
1:{"file":"mp3/dos.mp3","name":"DOS"},
2:{"file":"mp3/tres.mp3","name":"TRES"},
3:{"file":"mp3/cuatro.mp3","name":"CUATRO"}
},
SoundStop: "mp3/silence.mp3",
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();
},
EliminarElemento: function(elemento) {
elemento.parentNode.removeChild(elemento);
},
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.cookieNameActive ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameActive ) == null ? 0 : 1;
img.src = newValue == 1 ? Media.PlaySound.imgOn : Media.PlaySound.imgOff;
objInfo = MM_findObj("MediaMyMediaDivInfo");
if( newValue == 0 ){
Media.PlaySound.Stop();
objInfo.innerHTML = 'Mute';
Media.PlaySound.WriteCookie( Media.PlaySound.cookieNameActive, newValue );
}else{
Media.PlaySound.WriteCookie( Media.PlaySound.cookieNameActive, newValue );
Media.PlaySound.Play();
}
},
Init: function(){
if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
document.write('<bgsound loop="0" name="MediaMyMediaObj" id="MediaMyMediaObj" >');
}
},
Play: function(){
if( Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameActive ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameActive ) == null ){
sound = Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameThisSound ) || Media.PlaySound.SoundDefault;
obj = MM_findObj("MediaMyMedia");
objInfo = MM_findObj("MediaMyMediaDivInfo");
objInfo.innerHTML = 'Estás escuchando ' + Media.PlaySound.Sounds[sound].name;
//alert(Media.PlaySound.Sounds[sound].file)
if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
obj = MM_findObj("MediaMyMediaObj");
obj.src = Media.PlaySound.Sounds[sound].file;
} else {
obj = MM_findObj("MediaMyMediaDiv");
obj.innerHTML = '<embed src="'+Media.PlaySound.Sounds[sound].file+'" hidden="true" volume="200" loop="0" type="audio/midi" >';
}
}
},
Next: function(){
sounds = Media.PlaySound.Sounds;
var firstSound = 0;
var lastSound = 0; for (var sonido in sounds){ lastSound++; } if( lastSound > 0 ) lastSound--;
thisSound = Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameThisSound ) || Media.PlaySound.SoundDefault;
nextSound = thisSound < lastSound ? parseInt(parseInt(thisSound,10)+1,10) : firstSound;
Media.PlaySound.WriteCookie( Media.PlaySound.cookieNameThisSound, nextSound );
Media.PlaySound.Play();
},
Previus: function(){
sounds = Media.PlaySound.Sounds;
var firstSound = 0;
var lastSound = 0; for (var sonido in sounds){ lastSound++; } if( lastSound > 0 ) lastSound--;
thisSound = Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameThisSound ) || Media.PlaySound.SoundDefault;
nextSound = thisSound > 0 ? parseInt(parseInt(thisSound,10)-1,10) : lastSound;
Media.PlaySound.WriteCookie( Media.PlaySound.cookieNameThisSound, nextSound );
Media.PlaySound.Play();
},
Stop: function(){
if( Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameActive ) == 1 || Media.PlaySound.ReadCookie( Media.PlaySound.cookieNameActive ) == null ){
obj = MM_findObj("MediaMyMedia");
objInfo = MM_findObj("MediaMyMediaDivInfo");
objInfo.innerHTML = 'Stop';
if((Media.PlaySound.MSIE>-1) || (Media.PlaySoundOPER>-1)) {
obj = MM_findObj("MediaMyMediaObj");
obj.src = Media.PlaySound.SoundStop;
} else {
obj = MM_findObj("MediaMyMediaDiv");
obj.innerHTML = '<embed src="'+Media.PlaySound.SoundStop+'" hidden="true" volume="200" loop="0" type="audio/midi" >';
}
}
}
}
Media.PlaySound.Init();
</script>
</head>
<body>
<a href="#"><img src="images/ico_on.gif" onclick="Media.PlaySound.OnOffSound(this)" border="0" align="absmiddle" /></a>
<span name="MediaMyMediaDiv" id="MediaMyMediaDiv" style="margin:0; width:0; height:0;"></span>
<input type="button" value="Previus" onclick="Media.PlaySound.Previus()" />
<input type="button" value="Play" onclick="Media.PlaySound.Play()" />
<input type="button" value="Stop" onclick="Media.PlaySound.Stop()" />
<input type="button" value="Next" onclick="Media.PlaySound.Next()" />
<span name="MediaMyMediaDivInfo" id="MediaMyMediaDivInfo"></span>
</body>
</html>
以上是关于JavaScript Javascript - MP3播放器 - 声音的主要内容,如果未能解决你的问题,请参考以下文章
javascript JavaScript isset()等效: - JavaScript
JavaScript 使用JavaScript更改CSS(JavaScript)