Audio Player using Javascript with play and pause on mouse over and mouse out

Audio Player using Javascript with play and pause on mouse over and mouse out

The following javascript code will play , pause and stop the audio player on button click. To make it play, pause and resume on mouse over and mouse out you can call the appropriate function on the mouse over and mouse out of any html element like div, td, p etc. The code is as follows

<body onload=”init()”>
<script>
 function init()
 {
     document.getElementById(’wmpPlayer’).innerHTML = wmpCreate(’Alanis.mp3′);
 }
 function wmpCreate(url) {

    var str = “”;

    if (navigator.appName == “Netscape”) {
         // create it for FF.
         str = ‘<object id=”player” type=”application/x-ms-wmp” data=”‘+url+’” width=”1″ height=”1″>’;
         str += ‘<param name=”URL” value=”‘+url+’”>’;
         str += ‘<param name=”uiMode” value=”none”>’;
         str += ‘<param name=”autostart” value=”0″>’;
         str += ‘<param name=”hidden” value=”true”>’;
         str += ‘<param name=”pluginspage” value=”http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx”>’;           
         str += ‘</object>’;
    }
    else
    {
          str = ‘<embed NAME=”player” src=”‘+url+’” mce_src=”‘+url+’” MASTERSOUND LOOP=”false” AUTOSTART=”false” AUTOREWIND=”false” HIDDEN=”true” WIDTH=”0″ HEIGHT=”0″>’;
    }

    return str;

}
</script>

function player(toDo,toWhat)
{
     if (navigator.appName == ‘Netscape’)
       {
          var wmp = document.getElementById(toWhat);
          if(doWhat == ‘play’)
               wmp.controls.play();
          else if(doWhat == ‘pause’)
               wmp.controls.pause();
          else if(doWhat == ’stop’)
               wmp.controls.pause();    
       }
       else
       {
          var A = eval(’document.’+toWhat);
          if (A != null){
               if (document.M == null)
               {
                    document.M = false; var m;
                    for(m in A)
                    {
                         if (m == “ActiveMovie”)
                         {
                              document.M = true; break;
                         }
                    }
               }
               if (document.M)
               {
                    if (doWhat==’stop’) A.pause();
                    else
                    {
                         //A.SelectionStart = 0;
                         A.play();
                    }
               }
          }
       }  
}

</script>
<div id=’wmpPlayer’></div>
<input type=”button” name=”play” onclick=”player(’play’,'player’)” value=”Play”>
<input type=”button” name=”pause” onclick=”player(’pause’,'player’);” value=”Pause”>
<input type=”button” name=”stop” onclick=”player(’stop’,'player’)” value=”stop”>
</body>

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.