MediaPlayer Class

Provides a player that runs media (video or audio) using Silverlight in an ASP.NET Web page.

Namespace:  System.Web.UI.SilverlightControls
Assembly:  System.Web.Silverlight (in System.Web.Silverlight.dll)

Syntax

'Declaration
Public Class MediaPlayer _
    Inherits Silverlight _
    Implements IScriptControl
'Usage
Dim instance As MediaPlayer
public class MediaPlayer : Silverlight, 
    IScriptControl

Remarks

When you need to play audio or video files in your Web site, you can use the MediaPlayer control.

The MediaPlayer server control on an .aspx page is rendered as a Sys.UI.Silverlight.MediaPlayer client control in the HTML sent to the browser. You can interact with the MediaPlayer client control at runtime using ECMAScript (JavaScript).

The property settings for the MediaPlayer server control are sent to the MediaPlayer client control as initialization settings when that control is instantiated.

The MediaSource property indicates the media file for the MediaPlayer control.

The player appearance can be represented through skins. The skin displayed is determined by the MediaSkinSource property.

There are several MediaPlayer control properties that allow you to set the names of the client script functions to call at runtime when certain client events occur. For example, the OnClientChapterStarted property indicates the client script function that is called at runtime when a new chapter starts during playback of a media file.

The MediaPlayer server control inherits from the Silverlight control. This provides Web pages with the MediaPlayer control some underlying Silverlight functionality automatically, such as detecting if the browser has the Silverlight plug-in installed and displaying a graphic that allows the user to download the required version of the Silverlight plug-in if it is not installed.

The MediaPlayer server control inherits several properties from the Silverlight control that allow you to specify the name of a JavaScript function to call when certain client events occur on the Silverlight plug-in. For example, when the plug-in is resized, the JavaScript function whose name is indicated by the value of the OnPluginResized property runs.

Examples

The following example demonstrates how to use the MediaPlayer control. This example also shows how to set specific properties to manipulate the appearance of the Silverlight client plug-in.

<%@ Page Language="VB" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
    TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Controls for Silverlight</title>
    <link href="~/SilverlightStyles.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false" />
        <div>
            <h2>ASP.NET Controls for Silverlight: Using The MediaPlayer Server Control</h2>
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer1" ScaleMode="Stretch" AutoPlay="true" 
                MediaSource="../media/expressionstudio.wmv" Height="240" Width="320"
                PluginBackgroundColor="Black"  MediaSkinSource="../skins/Expression.xaml"/>
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer2" ScaleMode="None" AutoPlay="true" 
                MediaSource="../media/expressionstudio.wmv" Height="240" Width="320"
                PluginBackgroundColor="Black" MediaSkinSource="../skins/Expression.xaml" />
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer3" ScaleMode="Zoom" AutoPlay="true" 
                 MediaSource="../media/expressionstudio.wmv"
                PluginBackgroundColor="Black" MediaSkinSource="../skins/Expression.xaml" />
        </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
    TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Controls for Silverlight</title>
    <link href="~/SilverlightStyles.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false" />
        <div>
            <h2>ASP.NET Controls for Silverlight: Using The MediaPlayer Server Control</h2>
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer1" ScaleMode="Stretch" AutoPlay="true" 
                MediaSource="../media/expressionstudio.wmv" Height="240" Width="320"
                PluginBackgroundColor="Black"  MediaSkinSource="../skins/Expression.xaml"/>
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer2" ScaleMode="None" AutoPlay="true" 
                MediaSource="../media/expressionstudio.wmv" Height="240" Width="320"
                PluginBackgroundColor="Black" MediaSkinSource="../skins/Expression.xaml" />
            <p></p>
            <asp:MediaPlayer runat="server" ID="MediaPlayer3" ScaleMode="Zoom" AutoPlay="true" 
                 MediaSource="../media/expressionstudio.wmv"
                PluginBackgroundColor="Black" MediaSkinSource="../skins/Expression.xaml" />
        </div>
    </form>
</body>
</html>

The following example demonstrates how to set properties, such as the MediaSource property and MediaSkinSource property, of the MediaPlayer class.

<%@ Page Language="VB" %>
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        MediaPlayer1.MediaSource = DropDownList1.SelectedValue.ToString()
    End Sub
    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        MediaPlayer1.MediaSkinSource = DropDownList2.SelectedValue.ToString()
    End Sub
</script>
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>MediaPlayer</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:MediaPlayer 
            ID="MediaPlayer1" 
            runat="server" 
            MediaSource="../media/video1.wmv"
            MediaSkinSource="../skins/Professional.xaml"
            PlaceholderSource="../media/image0.jpg"
            ScaleMode="Stretch"
            AutoLoad="false"
            AutoPlay="false"
            PluginBackground="Black"
            Height="240px" 
            Width="320px">
            <PluginNotInstalledTemplate>
                Silverlight is not installed.    
            </PluginNotInstalledTemplate>    
        </asp:MediaPlayer>
        <p></p>
        <asp:Label ID="Label1" runat="server" Text="Video:"></asp:Label>
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="../media/video1.wmv">video1</asp:ListItem>
            <asp:ListItem Value="../media/video2.wmv">video2</asp:ListItem>
            <asp:ListItem Value="../media/video3.wmv">video3</asp:ListItem>
        </asp:DropDownList>
        <p></p>
        <asp:Label ID="Label2" runat="server" Text="Media Skin:"></asp:Label>
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem Value="../skins/Professional.xaml">Professional</asp:ListItem>
            <asp:ListItem Value="../skins/Classic.xaml">Classic</asp:ListItem>
            <asp:ListItem Value="../skins/Expression.xaml">Expression</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void  DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MediaPlayer1.MediaSource = DropDownList1.SelectedValue.ToString();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        MediaPlayer1.MediaSkinSource = DropDownList2.SelectedValue.ToString();
    }
</script>
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>MediaPlayer</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:MediaPlayer 
            ID="MediaPlayer1" 
            runat="server" 
            MediaSource="../media/video1.wmv"
            MediaSkinSource="../skins/Professional.xaml"
            PlaceholderSource="../media/image0.jpg"
            ScaleMode="Stretch"
            AutoLoad="false"
            AutoPlay="false"
            PluginBackground="Black"
            Height="240px" 
            Width="320px">
            <PluginNotInstalledTemplate>
                Silverlight is not installed.    
            </PluginNotInstalledTemplate>    
        </asp:MediaPlayer>
        <p></p>
        <asp:Label ID="Label1" runat="server" Text="Video:"></asp:Label>
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="../media/video1.wmv">video1</asp:ListItem>
            <asp:ListItem Value="../media/video2.wmv">video2</asp:ListItem>
            <asp:ListItem Value="../media/video3.wmv">video3</asp:ListItem>
        </asp:DropDownList>
        <p></p>
        <asp:Label ID="Label2" runat="server" Text="Media Skin:"></asp:Label>
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem Value="../skins/Professional.xaml">Professional</asp:ListItem>
            <asp:ListItem Value="../skins/Classic.xaml">Classic</asp:ListItem>
            <asp:ListItem Value="../skins/Expression.xaml">Expression</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

The following example demonstrates how to use simple error handling with the MediaPlayer server control. The OnPluginError attribute and the OnClientMediaFailed attribute of the MediaPlayer server control can be used to process client error events using JavaScript functions.

<%@ Page Language="VB" %>
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>MediaPlayer</title>
    <script type="text/javascript">   
    function OnPluginErrorHandler(sender, errorArgs)
    {
        var errorMsg = "Silverlight Plugin Error: \n\n";
        GeneralErrorHandler(sender,errorArgs,errorMsg);
    }
    function OnMediaFailedHandler(sender, errorArgs)
    {
        var errorMsg = "Silverlight Media Error: \n\n";
        GeneralErrorHandler(sender,errorArgs,errorMsg);
    }
    function GeneralErrorHandler(sender, errorArgs, errorMsg)
    {
        // Error information common to all errors.
        errorMsg += "Error Type:    " + errorArgs.get_error().errorType + "\n";
        errorMsg += "Error Message: " + errorArgs.get_error().errorMessage + "\n";
        errorMsg += "Error Code:    " + errorArgs.get_error().errorCode + "\n";

        // Display the error message.
        alert(errorMsg);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:MediaPlayer 
            ID="MediaPlayer1" 
            runat="server" 
            MediaSource="~/Media/expressionstudio.wmv"
            MediaSkinSource="~/skins/Professional.xaml"
            PlaceholderSource="~/media/image0.jpg"
            ScaleMode="Stretch"
            AutoLoad="true"
            AutoPlay="true"
            PluginBackground="Black"
            Height="240px" 
            Width="320px"
            OnPluginError="OnPluginErrorHandler"
            OnClientMediaFailed="OnMediaFailedHandler">
            <PluginNotInstalledTemplate>
                Silverlight is not installed.    
            </PluginNotInstalledTemplate>    
        </asp:MediaPlayer>
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>MediaPlayer</title>
    <script type="text/javascript"> 
    function OnPluginErrorHandler(sender, errorArgs)
    {
        var errorMsg = "Silverlight Plugin Error: \n\n";
        GeneralErrorHandler(sender,errorArgs,errorMsg);
    }
    function OnMediaFailedHandler(sender, errorArgs)
    {
        var errorMsg = "Silverlight Media Error: \n\n";
        GeneralErrorHandler(sender,errorArgs,errorMsg);
    }
    function GeneralErrorHandler(sender, errorArgs, errorMsg)
    {
        // Error information common to all errors.
        errorMsg += "Error Type:    " + errorArgs.get_error().errorType + "\n";
        errorMsg += "Error Message: " + errorArgs.get_error().errorMessage + "\n";
        errorMsg += "Error Code:    " + errorArgs.get_error().errorCode + "\n";

        // Display the error message.
        alert(errorMsg);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:MediaPlayer 
            ID="MediaPlayer1" 
            runat="server" 
            MediaSource="~/Media/expressionstudio.wmv"
            MediaSkinSource="~/skins/Professional.xaml"
            PlaceholderSource="~/media/image0.jpg"
            ScaleMode="Stretch"
            AutoLoad="true"
            AutoPlay="true"
            PluginBackground="Black"
            Height="240px" 
            Width="320px"
            OnPluginError="OnPluginErrorHandler"
            OnClientMediaFailed="OnMediaFailedHandler">
            <PluginNotInstalledTemplate>
                Silverlight is not installed.    
            </PluginNotInstalledTemplate>    
        </asp:MediaPlayer>
    </div>
    </form>
</body>
</html>

Inheritance Hierarchy

System.Object
  Control
    WebControl
      System.Web.UI.SilverlightControls.Silverlight
        System.Web.UI.SilverlightControls.MediaPlayer

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

MediaPlayer Members

System.Web.UI.SilverlightControls Namespace

Other Resources

Sys.UI.Silverlight.MediaPlayer

ASP.NET MediaPlayer Server Control