Windows Media Player Control SDK Banner Art
*Contents *Index  *Topic Contents
*Previous Topic: Creating an HTML Application Faceplate
*Next Topic: Faceplate Layout in DHTML

Controlling the Windows Media Player from Scripts

Microsoft® Windows Media™ Player faceplates are possible because all the functionality of the Windows Media Player is exposed to web scripting languages. Anything you can do with the stand-alone Windows Media Player application, such as pushing the play button, a corresponding script command can replicate. Scripts can also retrieve information from the Windows Media Player control. Combined, these features enable scripts to make changes in the dynamic HTML (DHTML) faceplate graphics that correspond to changes in the operational state of the Windows Media Player.

This article contains the following sections.

Performing Basic Playback Operations

When you open the Windows Media Player as a stand-alone application, you are presented with a video display window and standard control buttons that mimic the functions of most VCRs and compact disc players. These buttons control basic playback operations for all media; listed from left to right (see illustration), these are:

Media Player control buttons
  • Play
  • Pause
  • Stop
  • Previous Track
  • Fast Reverse
  • Fast Forward
  • Next Track

Scripts can perform these operations by invoking methods on the Windows Media Player control. The correspondence between the operations and methods is shown in the table below.

Operation Method
Play MediaPlayer.Play();
Pause MediaPlayer.Pause();
Stop MediaPlayer.Stop();
Previous Track MediaPlayer.Previous();
Fast Reverse MediaPlayer.FastReverse();
Fast Forward MediaPlayer.FastForward();
Next Track MediaPlayer.Next();

Another element of the Windows Media Player interface is the position trackbar, a slider control that displays the elapsed time of the current track. For scripting, this position is stored in a read/write property called CurrentPosition. While a track is playing, the value of this property is the current elapsed time, in seconds. Comparing this value with the value of the Duration property yields the percentage of the total track time that has elapsed. To jump to an arbitrary time in a track, assign a new value to CurrentPosition.

Volume is also controlled by a slider, and is handled in script in a similar way. The read/write Volume property stores a value for the current volume setting. To change the volume, assign a new value to this property. Valid values range from -10,000 for the lowest setting to zero for the loudest setting. There is also a mute button on the control panel, which turns off all sound output from the Windows Media Player. You can toggle muting in script by assigning a Boolean value of either true or false to the Mute property.

Getting Information from the Windows Media Player

The Windows Media Player control stores information about its current activity and the files it can play. Most information is stored as the value of a property, which scripts can retrieve, although some information is accessed as a return value of a script function.

Any time you send script commands to it, the Windows Media Player control may already be playing a file, scanning through a file, skipping to another file, or waiting to play a file. The PlayState property indicates what action the Windows Media Player control is performing at the time the property is read. By reading this property, you can decide the best action to perform for a command based on its current play state. For example, if your script calls a function whenever a user hits the play button, the function should check to see if the Windows Media Player control is already playing a file. A Microsoft® JScript® function that performs this check might look like the following.

function onPlay()
{
    if (MediaPlayer.PlayState != 2) // If not already playing a file...
    {
        MediaPlayer.Play();
        playBtn.SelectActive();
        pauseBtn.SelectNormal();
        stopBtn.SelectNormal();
    }
}

If you were to call the Play method while the Windows Media Player was already playing a file, the call would have no effect on the Windows Media Player operation. To avoid this case, the onPlay function checks first to see if the PlayState property is not equal to 2 (which is the value of that property when a file is currently playing) before performing any operations. If the Windows Media Player is not currently playing a file, this function would start playback and change the appearance of the buttons on the faceplate to reflect the new state. In this example, the faceplate graphics are changed by the script commands SelectActive and SelectNormal, defined elsewhere on the page for each graphical button.

If the Windows Media Player uses an Advanced Stream Redirector (ASX) file to define the media files for playback, your script can retrieve information contained in the ASX file. While a media file is currently playing, the GetMediaInfoString method can return the author, title, and copyright information, as specified by the ASX entry for that media file. In addition, the GetMediaParameter method enables you to retrieve custom parameters, defined in the ASX file with PARAM tags. (For more information, see ASX Files for Faceplates and Using ASX Metafiles.)

Handling Events

At various times during its operation, the Windows Media Player generates events which can be handled by scripts. This can be useful for resetting your control button graphics when a media file finishes playing, or displaying new clip information on the faceplate when a new media file begins playing.

This is done in Microsoft® JScript® by specifying a script that will execute in response to a particular event, and placing it inside a SCRIPT tag. The FOR parameter of the SCRIPT tag gives the name of the Windows Media Player object (as defined in the OBJECT tag), and the EVENT parameter specifies the name of the event plus any parameter names.

For example, when a clip finishes playback, the Windows Media Player generates an EndOfStream event. The following example script would be called any time the Windows Media Player reached the end of a media file and the EndOfStream event occurred:

<SCRIPT FOR="MediaPlayer" EVENT="EndOfStream(bResult)" LANGUAGE="JScript">
    if (bResult == 0)
    {
        MediaPlayer.Stop();
        playBtn.SelectNormal();
        stopBtn.SelectActive();
        pauseBtn.SelectNormal();
    }
</SCRIPT>

The EndOfStream event passes one parameter to the script. If the clip has played all the way through, the value of this parameter is zero. In that instance, the script calls the Stop method to make sure playback is stopped and resets the control button graphics to reflect the new state.

For a full list of supported Windows Media Player events, consult Events in the Scripting Reference.


Top of Page Top of Page
© 1999 Microsoft and/or its suppliers. All rights reserved. Terms of Use.