A VideoPlayer – Custom Control

Some day ago, I have faced a little problem to show streaming contents on my Web-Projects, I have gone through many R & d’s and then decided to write a custom control for the same, then I have written a custom control.

Here, the same, I want to share with you:
File Name : videoPlayer.ascx

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”videoPlayer.ascx.cs” Inherits=”videoPlayer” %>
<asp:PlaceHolder ID=”phError” runat=”server” Visible=”false”>
<%asp:Label ID=”lblError” runat=”server” ForeColor=”Red” Text=”Error” />
</asp:PlaceHolder>
<asp:Table ID=”tblPlayer” runat=”server” BorderWidth=”1″>
<asp:TableRow>
<asp:TableCell>
<asp:Literal ID=”ltVideo” runat=”server” />
</asp:TableCell>
</asp:TableRow>
</asp:Table>

Now, lets start to write code-behind as follows:
File Name : videoPlayer.ascx.cs

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : g_arora@hotmail.com */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class videoPlayer : System.Web.UI.UserControl
{
#region Properties to customize the Video/Audio Control

///
/// true:FullScreen, false:CustomSize
///
public Boolean isFullSize
{
set
{
ViewState[“isFullSize”] = value;
}
get
{
if (ViewState[“isFullSize”] != null)
return Convert.ToBoolean(ViewState[“isFullSize”]);
else
return true;
}
}
///
/// Full url-path of Video/Audio
///
public String sourceUrl
{

set
{
ViewState[“sourceUrl”] = value;
}
get
{
if (ViewState[“sourceUrl”] != null)
return Convert.ToString(ViewState[“sourceUrl”]);
else
return “http://www.video.com/myVideo.mpeg”; //Default video

}

}
///
/// width of player
///
public String width
{
set
{
ViewState[“width”] = value;
}
get
{
if (ViewState[“width”] != null)
return Convert.ToString(ViewState[“width”]);
else
return “640”;
}
}
///
/// Height of player
///
public String height
{
set
{
ViewState[“height”] = value;
}
get
{
if (ViewState[“height”] != null)
return Convert.ToString(ViewState[“height”]);
else
return “480”;
}
}
///
/// Custom message when player initializes
///
public String standByMessage
{
set
{
ViewState[“standByMessage”] = value;
}
get
{
if (ViewState[“standByMessage”] != null)
return Convert.ToString(ViewState[“standByMessage”]);
else
return “Please wait while the player inializes…”;
}
}
///
/// States whether media automatic starts or not
///
public Boolean autoStart
{
set
{
ViewState[“autoStart”] = value;
}
get
{
if (ViewState[“autoStart”] != null)
return Convert.ToBoolean(ViewState[“autoStart”]);
else
return true;
}
}
///
/// -100 is fully left, 100 is fully right.
///
public String balance
{
set
{
ViewState[“balance”] = value;
}
get
{
try
{
if (ViewState[“balance”] != null)
return Convert.ToString(ViewState[“balance”]);
else
return “0”;
}
catch
{
return “0”;
}
}
}
///
/// Position in seconds when starting.
///
public Int32 currentPosition
{
set
{
ViewState[“currentPosition”] = value;
}
get
{
if (ViewState[“currentPosition”] != null)
return Convert.ToInt32(ViewState[“currentPosition”]);
else
return 0;
}
}

///
/// Show play/stop/pause controls
///
public Boolean showcontrols
{
set
{
ViewState[“showcontrols”] = value;
}
get
{
if (ViewState[“showcontrols”] != null)
return Convert.ToBoolean(ViewState[“showcontrols”]);
else
return true;
}
}
///
/// Allow right-click
///
public Boolean contextMenu
{
set
{
ViewState[“contextMenu”] = value;
}
get
{
if (ViewState[“contextMenu”] != null)
return Convert.ToBoolean(ViewState[“contextMenu”]);
else
return false;
}
}
///
/// Toggle sound on/off
///
public Boolean mute
{
set
{
ViewState[“mute”] = value;
}
get
{
if (ViewState[“mute”] != null)
return Convert.ToBoolean(ViewState[“mute”]);
else
return false;
}
}
///
/// Number of times the content will play
///
public Int32 playCount
{
set
{
ViewState[“playCount”] = value;
}
get
{
if (ViewState[“playCount”] != null)
return Convert.ToInt32(ViewState[“playCount”]);
else
return 1;
}

}
///
/// 0.5=Slow, 1.0=Normal, 2.0=Fast
///
public Double rate
{
set
{
ViewState[“rate”] = value;
}
get
{
if (ViewState[“rate”] != null)
return Convert.ToDouble(ViewState[“rate”]);
else
return 1.0;
}
}
///
/// full, mini, custom, none, invisible
///
public String uiMode
{
set
{
ViewState[“uiMode”] = value;
}
get
{
if (ViewState[“uiMode”] != null)
return Convert.ToString(ViewState[“uiMode”]);
else
return “Full”;
}
}
///
/// Show or hide the name of the file/url
///
public Boolean showDisplay
{
set
{
ViewState[“showDisplay”] = value;
}
get
{
if (ViewState[“showDisplay”] != null)
return Convert.ToBoolean(ViewState[“showDisplay”]);
else
return false;
}
}

///
/// 0=lowest, 50= normal, 100=highest
///
public Int32 volume
{
set
{
ViewState[“volume”] = value;
}
get
{
if (ViewState[“volume”] != null)
return Convert.ToInt32(ViewState[“volume”]);
else
return 50;
}
}

#endregion

protected void Page_Load(object sender, EventArgs e)
{

try
{
ltVideo.Text = this.VideoPlayer(this.sourceUrl, this.isFullSize);
}
catch (Exception ex)
{
lblError.Text = ex.ToString();
phError.Visible = true;
}
}

#region VideoPlayer
///
/// Return the whPlayer to Play Video/ Audio Content
///
/// Source of content
/// Size of Player
///
private string VideoPlayer(string strsourceUrl, bool boolFullSize)
{
string whPlayer = “”;
strsourceUrl = strsourceUrl + “”;
strsourceUrl = strsourceUrl.Trim();

if (strsourceUrl.Length < 0)
{
//play content
}
else
{
throw new System.ArgumentNullException(“strsourceUrl”);
}

if (boolFullSize)
{
//this.width = String.Empty;
//this.height = String.Empty;
this.width = “800”;
this.height = “600”;
}
else
{
//continue with supplied width/height
}

whPlayer = whPlayer + “”;

return whPlayer;
}
#endregion
}

In Next article, I will tell you how to use the VideoPlayer Custom Control?

Leave A Reply