Microsoft Media Player control in Sitefinity

May 20, 2009 Digital Experience
Creating a simple wrapper for the Media Player is easy. Such control can be used to play *.wmv files directly from Sitefinity Custom/Video library or from outside source - such as physical file or a video streaming web source. The first case is demonstrated in VideoWrapper.

 

How to:

1. Open MS Visual Studio and create a User Control file (*.ascx). Here, I will call it VideoWrapper

2. You will need to register the Microsoft Silverlight assembly. To do so just copy/paste following snippet:

 

<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %> 

3. Add the asp:MediaPlayer control

The file should look like this:

<%@ Control Language="C#"AutoEventWireup="true" CodeFile="VideoWrapper.ascx.cs" Inherits="VideoWrapper" %> 
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %> 
 
<asp:MediaPlayer ID="mediaPlayer" runat="server" /> 
 

4. Now, for the code. Open the control's code-behind file.

4.1 We are going to use the Sitefinity LibraryManager to retrieve the needed items from the database, so add the following references:

using System; 
using Telerik.Cms.Engine.Data; 
using Telerik.Libraries.Data; 
using Telerik.Libraries; 
 

 

4.2 In order to make the new control more flexible you can create few custom properties like this:

public int PlayerHeight{ getset;} 
public int PlayerWidth { getset; }    
public string LibraryProvider { getset; } 
public string LibraryName { getset; } 
public string ItemGuidID { getset; } 

Later, when the control is added to Sitefinity, these properties will be exposed directly in the CMS so you will be able to set them through the UI.

4.3 Now to the Page_Load event. The following code will stretch the Media Player:

this.mediaPlayer.Height = PlayerHeight; 
this.mediaPlayer.Width = PlayerWidth; 

the next lines are dedicated for retrieving the Video file from the database:

var manager = new LibraryManager(LibraryProvider); 
var libraries = manager.GetAllLibraries(); 
at this point the variable "libraries" contains references to all libraries in the previously specified provider.

 

Now we need to get the item and set it as the MediaSource for the MediaPlayer:

 

foreach (var library in libraries) 
        { 
            var currentLibrary = (Library)library; 
 
            if (currentLibrary.Name == LibraryName) 
            { 
                var lItems = currentLibrary.GetItems(); 
                 
                foreach(var it in lItems) 
                { 
                    var item = (CmsContentBase)it; 
                    if(item.ID.ToString()==ItemGuidID) 
                        mediaPlayer.MediaSource = item.UrlWithExtension; 
                } 
            } 
 
        } 

The code above is simply searching for the specified library and the specified item in this library, then the item Url is set as the mediaPlayer.MediaSource.

Remember that the Url should also contain the Item's Extensions!

5. Save and Upload the new Control to Sitefinity.

That is all it takes.

If you want to use external data sources just expose on string property and set the Media Player's Media Sources to it.

 

 

The Progress Team