Windows Media Player Scripting Test

This discusses simple cross-browser scriting of Windows Media Player, in IE and Firefox. It depends on Firefox having the new Windows Media Player plugin installed, which unlike the previous WMP plugin, supports scripting. The plugin requires Windows XP or higher, so this will not work in earlier versions of Windows. The new plugin's installer specifically targets Firefox - it is not known if the plugin works in other browsers such as Opera yet - if it does, this method may work in those other browsers as well.

Play
Pause
Stop

Media Player Code:


    <!-- Internet Explorer * Windows Media Player ActiveX -->
    <object id="wmpAx" name="player1"
            classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6"
            type="application/x-oleobject" width="320" height="320"
            codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715"
            standby="Loading Microsoft Windows Media Player components...">
        <param name="url" value="ffwmv.wmv" />
        <param name="autostart" value="false" />
        <param name="ShowStatusBar" value="true" />
        <param name="volume" value="100" />

        <!-- For other browsers * Windows Media Player -->
        <!--[if !IE]> <-->
        <object id="wmp2" name="player1"
                data="ffwmv.wmv"
                width="320" height="320" type="application/x-ms-wmp" >
            <param name="autostart" value="false" />
            <param name="volume" value="0" />   
        </object>
        <!--> <![endif]-->

    </object>


WMP is only scriptable in IE if it is embedded specifially for ActiveX - this is the first Object tag. The presece of the classid attribite that specifies the ActiveX control will cause Firefox to ignore the object, as Firefox does not have ActiveX support. This necessitates the use of two objects - one for IE and one for other browsers. This will also necessitate that the javascript controls be able to detect which of the two objects it needs to control.

Elements inside an Object tag are not rendered unless the browser cannot render the Object tag. This works fine if the inner element is an Embed tag, but IE6 and under does not understand it when an Object tag contains another Object tag, and tries to render both. To work around this, the <!--[if !IE]> <--> and <!--> <![endif]--> comments prevent IE from rendering the HTML code between them. This is used to prevent IE from seeing the inner Object.

JavaScript Code:


    var player=false
    function setup()
    {
        if(!player)
        }
            var p1=document.getElementsByName("player1");
            for(var x=0;x<p1.length;x++)
            {
                if(p1[x].controls)
                {
                    player=p1[x];
                    break;
                }
            }
        }
    }

    function play()
    {
        setup();
        player.controls.play();
    }

    function pause()
    {
        setup();
        player.controls.pause();
    }

    function stop()
    {
        setup();
        player.controls.stop();
    }
   


There are two Objects. You cannot treat them both as a single player, therefore you have to determine which one is running and issue commands only to that one.

As you may have noticed, both of the players have the name attribute 'player1'. The global variable 'player' and the function 'setup()' determine which player with the name 'player1' to control.

'player' is initially set to false. When setup() is run it checks to see if player is false, and if it is it proceeds to determine which player object to control. It gets a list of all elements with the name 'player1' using document.getElementsByName(). A for loop cyclies through each Object in the list. Each Object is tested to see if Object.controls exists - if it does, then this is the correct Object - 'player' is set to be that Object element and the for loop is ended. 'player' now points to the Object that is playing.

Each control function in the script - play(), pause(), and stop() - will call setup() before executing its command, (in case it is the first instance of a command being clicked, then it runs player.play() (or pause() or stop() as appropriate).

Valid XHTML 1.0 Transitional