Wmanager.js : gérer les fichiers wma avec windows media player cross-browser

Description

Et voici ma dernière source, wmanager.js (vous pouvez prononcer womanager ^^) version Bêta 1.0

Une API complètement dédié à la gestion de fichier WMA, avec l'aide des plugins Windows Media Player (WMP) pour IE, Firefox, Safari... Tout y est : Arrêt/pause/muet/avance rapide/informations sur la chansons/gestionnaire d'événements. Le but de ce projet était de rendre WMP complètement compatible avec tous les principaux browsers. Je n'y suis pas arrivé (bugs dans Webkit) mais c'est réussit à 95%.

Pourquoi Windows Media Player ? Car c'est la seule façon (pour l'instant) de lire les fichiers WMA sur Internet, un format qui, malgré son incompatibilité, est toujours populaire. Tout m'est venu de ce petit script (http://www.editeurjavascript.com/scripts/scripts_autres_3_364.php) qui disait entre autre que c'était réservé à IE. J'ai tenté d'y voir la vérité et inspiré par le concept (mais non le code) de SoundManager2, je me suis lancé dans cette aventure difficile tant les différences entre chaque navigateurs concernant l'interprétation du plugin était différente. Le 4/5 de mon temps a été consacrée à régler les bugs. De plus, personne auparavant n'avait créé de code pour régulariser WMP.

Je vous invite à télécharger la source =) Wmanager.js peut être utiliser seul (+ le plugin). Vous trouverez dans le ZIP une documentation complète ainsi que des exemples.

/*** EXEMPLE DISPONIBLE MAINTENANT SUR MON NOUVEAU SITE : http://www.jdmcreator.byethost5.com/sources/wmanager/index.html ***/

Source / Exemple :


/*******************************************************

                              • Wmanager.js version 1.0 ****************
                                    • JDMCreator 2010 *********************
                                                                                                              • /
function OnDSPlayStateChangeEvt(tn) { wmanager.eventManager(tn); } wmanager= new function(){ this.version="1.0" this.sounds=[]; this.soundStatus=[]; this.nb=0; this.playAll=function(){ for(i in this.sounds){ this.sounds[i].play(); } } this.stopAll=function(){ for(i in this.sounds){ this.sounds[i].stop(); } } this.pauseAll=function(){ for(i in this.sounds){ this.sounds[i].pause(); } } this.resumeAll=function(){ for(i in this.sounds){ this.sounds[i].resume(); } } this.prepareToBeReady=function(i){ tl=this.sounds[i].getObject().URL.substring(this.sounds[i].getObject().URL.lastIndexOf('/')+1,this.sounds[i].getObject().URL.length); tl=this.sounds[i].getObject().URL.substring(this.sounds[i].getObject().URL.lastIndexOf('\\')+1,this.sounds[i].getObject().URL.length); td=this.sounds[i].systemURL.substring(this.sounds[i].systemURL.lastIndexOf('/')+1,this.sounds[i].systemURL.length); td=this.sounds[i].systemURL.substring(this.sounds[i].systemURL.lastIndexOf('\\')+1,this.sounds[i].systemURL.length); if(this.sounds[i].getObject().currentMedia!=null && tl==td && this.sounds[i].getObject().currentMedia.attributeCount>1){ try{this.sounds[i].onsystemready(this.sounds[i]);}catch(e){} try{this.sounds[i].onready(this.sounds[i]);}catch(e){} } else{ this.nb=i; window.setTimeout(function(){wmanager.prepareToBeReady(wmanager.nb)},50); } } this.eventManager=function(tn){ for(i in this.sounds){ can=false; yn=this.sounds[i].getObject(); ty=this.sounds[i]; tu=this.soundStatus[i]; if(yn!=null){ if(yn.playState!=tu){ if(yn.playState==tn){ if(tu==0){ this.prepareToBeReady(i); try{ty.onsoundready(ty);}catch(e){} if(tn==3 && !this.sounds[i].settings.autoPlay){ break; } } can=true; } this.soundStatus[i]=tu; } if(can){ try{ty.onchangeplaystate(tn);}catch(e){} switch(tn){ case 1: ty.playing=false; ty.paused=false; ty.stopped=true; try{ty.onstop(ty);}catch(e){} break; case 2: ty.playing=false; ty.paused=true; ty.stopped=false; try{ty.onpause(ty);}catch(e){} break; case 3: ty.playing=true; ty.paused=false; ty.stopped=false; try{this.sounds[i].onsystemplay(this.sounds[i]);}catch(e){} try{this.sounds[i].onplay(this.sounds[i]);}catch(e){} break; case 8: try{ty.onfinish(ty);}catch(e){} break; default: break; } wmanager.soundStatus[i]=t1.playState; } } } } this.createSound=function(){return false;} this.isWMPInstalled=function(){ if(navigator.mimeTypes.length==0){ return undefined; } for(var i=0; i<navigator.mimeTypes.length; i++) { if(navigator.mimeTypes[i].type=='audio/x-ms-wma'){ return true; } } return false; } this.createPlayer=function(tn){ tl=new this.playerCreater(tn); this.sounds.push(tl); this.soundStatus.push(0); return tl.id; } this.getSoundById=function(tn){ for(i in this.sounds){ if(this.sounds[i].id==tn){ return this.sounds[i]; } } } this.playerCreater=function(tn){ this.systemURL=tn.src; this.onready=tn.onready; this.playing=false; this.isReady=false; this.settings=tn; this.settings.height=(tn.height==undefined) ? 320 : tn.height; this.settings.width=(tn.width==undefined) ? 300 : tn.width; this.settings.showControls=(tn.showControls==undefined) ? true : tn.showControls; this.id=tn.id; ty=document.createElement("div"); tnu=(tn.invisible) ? 'width=1 height=1 style="position:absolute;top:0px;left:0px;width:1px;height:1px;clip:rect(1px 1px 1px 1px);"' : 'width='+tn.width+' height='+tn.height; ty.innerHTML='<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="test2" data="'+tn.src+'" '+tnu+' volume=" 0"><param name="URL" ref value="'+tn.src+'"><param name="autostart" value="true" /><param name="volume" value="0" /><object id="test" data="'+tn.src+'" type="application/x-ms-wmp" '+tnu+'><param name="URL" ref value="'+tn.src+'"><param name="autostart" value="true" /><param name="volume" value="0" /><param name="autostart" value="true" /><object id="test" data="'+tn.src+'" type="audio/x-ms-wma" '+tnu+'><param name="URL" ref value="'+tn.src+'"><param name="autostart" value="true" /></object></object></object>'; this.divObject=ty; tn.element.appendChild(ty,tn.element); this.paused=false; this.stopped=true; this.onsystemready=function(t,i){ wmanager.soundStatus[i]=t; if(!this.settings.autoPlay){ this.stop(); } if(!this.settings.showControls){ this.deleteControls(); } if(this.settings.invisible){ this.changeSettings('invisible',true); } } this.setURL=function(ty){ tn=this.getObject(); if(tn!=null){ for(var i=0;i<wmanager.sounds.length;i++){ if(wmanager.sounds[i]=this){ tu=i; } } this.systemURL=ty; wmanager.soundStatus[tu]=0; try{this.onbeforechangeurl();}catch(e){} tn.URL=ty; } } this.getURL=function(tn){ tn=this.getObject(); if(tn!=null){ return tn.URL; } } this.stop=function(){ tn=this.getObject(); if(tn!=null){if(tn.controls.isAvailable('Stop')){ tn.controls.stop() } } } this.fastForward=function(){ tn=this.getObject(); if(tn!=null){if(tn.controls.isAvailable('FastForward')){ tn.controls.fastForward()} } } this.play=function(){ tn=this.getObject(); if(tn!=null){if(tn.controls.isAvailable('Play')){ tn.controls.play()} } } this.pause=function(){ tn=this.getObject(); if(tn!=null){ if(tn.controls.isAvailable('Pause')){ tn.controls.pause() }} } this.getVolume=function(){ tn=this.getObject(); if(tn!=null){ return tn.settings.volume; } } this.setVolume=function(ty){ tn=this.getObject(); if(tn!=null){ tn.settings.volume=ty; return tn.settings.volume==ty; } return false; } this.getDuration=function(){ tn=this.getObject(); if(tn!=null){ return tn.currentMedia.duration } } this.getCurrentPosition=function(){ tn=this.getObject(); if(tn!=null){ return tn.controls.currentPosition; } } this.getSongInformation=function(ty){ tn=this.getObject(); if(tn==null){ return false; } ty=ty.toLowerCase(); ty=(ty=="artist" || ty=="tpe1") ? "author" : ty; ty=(ty=="songname" || ty=="tit2") ? "title" : ty; ty=(ty=="artist" || ty=="tpe1") ? "WM/AlbumArtist" : ty; ty=(ty=="album" || ty=="talb") ? "WM/AlbumTitle" : ty; ty=(ty=="comm" || ty=="comment") ? "description" : ty; ty=(ty=="tcon" || ty=="genre") ? "WM/GenreID" : ty; ty=(ty=="genrename") ? "WM/Genre" : ty; ty=(ty=="trck" || ty=="track") ? "WM/TrackNumber" : ty; ty=(ty=="tyer" || ty=="year") ? "WM/Year" : ty; try{ ty=tn.currentMedia.getItemInfo(ty); if(ty!=""){ return ty; } } catch(e){} return false; } this.getVersion=function(){ return this.getObject().versionInfo; } this.getBalance=function(ty){ tn=this.getObject(); if(tn!=null){ return tn.settings.balance; } } this.muted=false; this.mute=function(ty){ tn=this.getObject(); if(tn!=null){ tn.settings.mute=true; this.muted=true; } } this.unmute=function(ty){ tn=this.getObject(); if(tn!=null){ tn.settings.mute=false; this.muted=false; } } this.setBalance=function(ty){ tn=this.getObject(); if(tn!=null){ tn.settings.balance=ty; } } this.setPosition=function(ty){ tn=this.getObject(); if(tn!=null){ tn.controls.currentPosition=ty; } } this.getCurrentPositionText=function(){ tn=this.getObject(); if(tn!=null){ return tn.controls.currentPositionString; } } this.changeSettings=function(tn,tv){ switch(tn){ case "id" : this.id=tv; this.settings.id=tv; break; case "src" : this.setURL(tv); this.settings.src=tv; break; case "width" : this.settings.width=tv; ty=this.getObject(); if(ty!=null){ ty.width=tv; ty.style.width=tv+"px"; } break; case "height" : this.settings.height=tv; ty=this.getObject(); if(ty!=null){ ty.height=tv; ty.style.height=tv+"px"; } break; case "showControls" : tv=(tv==true) ? "full" : "none"; this.settings.showControls=tv; this.deleteControls(tv); break; case "invisible" : ty=this.getObject(); td=this.getCurrentPosition()*1; if(!tv){ ty.style.position="static"; ty.width=this.settings.width; ty.height=this.settings.height; ty.style.width=this.settings.width+"px"; ty.style.height=this.settings.height+"px"; ty.style.clip=""; } else{ ty.style.position="absolute"; ty.style.top="0px"; ty.style.left="0px"; ty.width="1"; ty.height="1"; ty.style.width="1px"; ty.style.height="1px"; ty.style.clip="clip:rect(1px 1px 1px 1px)"; } this.setPosition(td); this.settings.invisible=tv; break; default: break; } return null; } this.deleteControls=function(t){ t=(t==undefined) ? "none" : t; tn=this.getObject(); if(tn!=null){ tn.uiMode=t; return true; } return false; } this.resume=function(tn){ this.play(tn); } this.getObject=function(tn){ t1=this.divObject.firstChild; if(t1.controls){return t1} if(t1.lastChild.controls){ return t1.lastChild;} return null; } if(!tn.showControls){ this.deleteControls(); } try{ this.getObject().attachEvent("playStateChange",OnDSPlayStateChangeEvt); } catch(e){ try{ this.getObject().addEventListener("playStateChange",OnDSPlayStateChangeEvt) } catch(e){} } } }

Conclusion :


Je mettrai les exemples sur Internet en temps et lieu.

Pour l'instant, profitez ^^ Vous pouvez l'utiliser sur Internet, un petit commentaire pour me le mentionner serait gentil, mais ce n'est pas obligé ^^ Si vous trouvez des bugs (j'en connais quelques-uns que je n'ai toujours pas réussit à régler), si vous avez des suggestions, des commentaires, des questions.... n'hésitez pas à laisser un commentaire et je vous répondrai ;)

Codes Sources

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.