Index Topic Contents | |
Previous Topic: What Background Do You Need? Next Topic: Placing the Windows Media Player Control in a Visual Basic Application |
Placing the Windows Media Player Control in a Web Page
As a Microsoft® ActiveX® control, the Microsoft® Windows Media™ Player control exposes methods, properties, and event support to other Microsoft® Windows® applications. Although ActiveX controls can be used as components in a variety of programming environments, they are extremely well-suited for use in Web pages. ActiveX controls can add exciting dynamic content to previously static Web pages, and they are easy to distribute over networks.
By using a scripting language, such as Microsoft® Visual Basic® Scripting Edition (VBScript) or Microsoft® JScript®, you can easily embed the Windows Media Player control in an HTML file. This article explains how to do this, using VBScript for the examples.
This article contains the following sections.
- Inserting the Windows Media Player Control
- Setting Control Properties with the PARAM Tag
- Adding a Simple User Interface for the Control
- Adding the Scripting Code
- Simple Windows Media Player Control Web Page
Inserting the Windows Media Player Control
The HTML OBJECT tag is used to embed ActiveX objects into an HTML file. The following code shows how to use the OBJECT tag to insert the Windows Media Player control.
<OBJECT ID="MediaPlayer1" WIDTH=320 HEIGHT=240 CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/ nsmp2inf.cab#Version=6,4,5,715" STANDBY="Loading Microsoft® Windows Media™ Player components..." TYPE="application/x-oleobject"> </OBJECT>The following OBJECT tag attributes are required.
- ID: The name used to reference this instance of the control in scripts.
- CLSID: The Class Identifier (CLSID) of the Windows Media Player control, which is used by the web browser to create the ActiveX object on the page.
Using the CODEBASE attribute is strongly recommended, but optional. It contains a Uniform Resource Locator (URL) pointing to a location where the Windows Media Player control can be downloaded from, if it is unavailable on a user's system. In addition to the address of the Windows Media Player object, the CODEBASE attribute can optionally specify a version number of the Windows Media Player control.
The Windows Media Player control is available for download on the Microsoft.com Web site, packaged as a .cab (cabinet) file.
Setting Control Properties with the PARAM Tag
One way to set the properties of a control in a Web page is to add PARAM tags between the OBJECT tags. This method is ideal for setting properties at design time. The following code shows you the OBJECT tag with some PARAM tags added.
<OBJECT ID="MediaPlayer1" WIDTH=320 HEIGHT=240 CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/ nsmp2inf.cab#Version=6,4,5,715" STANDBY="Loading Microsoft® Windows Media™ Player components..." TYPE="application/x-oleobject"> <PARAM NAME="FileName" VALUE="C:\ASFRoot\Welcome.asf"> <PARAM NAME="ShowControls" VALUE="False"> <PARAM NAME="AutoRewind" VALUE="True"> <PARAM NAME="AutoStart" VALUE="False"> </OBJECT>The PARAM tags used in the preceding example have two attributes: the first is the name of the property being set, and the second specifies the value of the property. In the previous example, the first PARAM tag sets the FileName property to C:\ASFRoot\Welcome.asf. The remaining PARAM tags initialize the values of the AutoRewind property to true, the ShowControls property to false, and the AutoStart property to false.
Adding a Simple User Interface for the Control
Adding a few buttons is the simplest way to demonstrate Windows Media Player control operation. Add the following three button controls to the body of your HTML page: one to start the Windows Media Player control, one to stop it, and one to display the current version of the Windows Media Player control.
<INPUT TYPE="BUTTON" NAME="BtnPlay" VALUE="Play"> <INPUT TYPE="BUTTON" NAME="BtnStop" VALUE="Stop"> <INPUT TYPE="BUTTON" NAME="BtnAbout" VALUE="About">Adding the Scripting Code
Scripting code adds interactivity to your page, enabling you to respond programmatically to events, to call methods, and to change run-time properties. First, you must use a SCRIPT tag to notify the browser which scripting language you intend to use. Include your script commands within HTML comment tags so browsers that do not support scripting do not render your code as text. Put the SCRIPT tag anywhere within the BODY of your HTML file and embed the comment-surrounded code within the opening and closing SCRIPT tags.
The following code calls the Windows Media Player control methods in response to events triggered by clicking the button controls.
<SCRIPT LANGUAGE="VBScript"> <!-- Sub BtnAbout_OnClick MediaPlayer1.AboutBox End Sub Sub BtnPlay_OnClick MediaPlayer1.Play End Sub Sub BtnStop_OnClick MediaPlayer1.Stop MediaPlayer1.CurrentPosition = 0 End Sub --> </SCRIPT>Each subroutine is attached to a specific button that was defined previously. When an event is triggered by a button click, the code makes a call (or series of calls) to the Windows Media Player control, specifying the action to be taken.
Simple Windows Media Player Control Web Page
By combining the elements previously discussed in this article, you can see how the pieces fit together in the following sample code.
Sample Code
<HTML> <HEAD><TITLE>Windows Media Player Test</TITLE></HEAD> <BODY> <OBJECT ID="MediaPlayer1" WIDTH=320 HEIGHT=240 CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/ nsmp2inf.cab#Version=6,4,5,715" STANDBY="Loading Microsoft® Windows Media™ Player components..." TYPE="application/x-oleobject"> <PARAM NAME="FileName" VALUE="C:\ASFRoot\Welcome.asf"> <PARAM NAME="ShowControls" VALUE="False"> <PARAM NAME="AutoRewind" VALUE="True"> <PARAM NAME="AutoStart" VALUE="False"> </OBJECT> <BR> <INPUT TYPE="BUTTON" NAME="BtnPlay" VALUE="Play"> <INPUT TYPE="BUTTON" NAME="BtnStop" VALUE="Stop"> <INPUT TYPE="BUTTON" NAME="BtnAbout" VALUE="About"> <SCRIPT LANGUAGE="VBScript"> <!-- Sub BtnAbout_OnClick MediaPlayer1.AboutBox End Sub Sub BtnPlay_OnClick MediaPlayer1.Play End Sub Sub BtnStop_OnClick MediaPlayer1.Stop MediaPlayer1.CurrentPosition = 0 End Sub --> </SCRIPT> </BODY> </HTML>Try this code with a multimedia file on your computer. Copy and paste the code into a text file, and change the value of the FileName attribute to specify the protocol, relative path, and name of the clip you wish to play.
Top of Page
© 1999 Microsoft and/or its suppliers. All rights reserved. Terms of Use.