C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






A Simple Video Player - Construction


Posted Date: 03 Aug 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms

Posted By: Gaurav Arora       Member Level: Gold
Rating:     Points: 10



Some days ago, I faced a little problem while trying to show streaming contents on my Web-Projects. I went through many R&d’s and then decided to write a custom control for the same. This code sample is that of the custom control I wrote.

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, let us 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 + "<object classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' id='player' width='" + this.width + "' height='" + this.height + "' standby= '" + this.standByMessage + "'>";
whPlayer = whPlayer + "<param name='url' value='" + this.sourceUrl + "' />";
whPlayer = whPlayer + "<param name='src' value='" + this.sourceUrl + "' />";
whPlayer = whPlayer + "<param name='AutoStart' value='" + this.autoStart + "' />";
whPlayer = whPlayer + "<param name='Balance' value='" + this.balance + "' />";
whPlayer = whPlayer + "<param name='CurrentPosition' value='" + this.currentPosition + "' />";
whPlayer = whPlayer + "<param name='showcontrols' value='" + this.showcontrols + "' />";
whPlayer = whPlayer + "<param name='enablecontextmenu' value='" + this.contextMenu + "' />";
whPlayer = whPlayer + "<param name='fullscreen' value='" + this.isFullSize + "' />";
whPlayer = whPlayer + "<param name='mute' value='" + this.mute + "' />";
whPlayer = whPlayer + "<param name='PlayCount' value='" + this.playCount + "' />";
whPlayer = whPlayer + "<param name='rate' value='" + this.rate + "' />";
whPlayer = whPlayer + "<param name='uimode' value='" + this.uiMode + "' />";
whPlayer = whPlayer + "<param name='showdisplay' value='" + this.showDisplay + "' />";
whPlayer = whPlayer + "<param name='volume' value='" + this.volume + "' />";
whPlayer = whPlayer + "</object>";

return whPlayer;
}
#endregion
}


In Next article I will tell you [i]how to use the VideoCustom Control?[/i]


Attachments

  • Video Player Control (20031-31114-videoPlayer.ascx)
  • Video Player (20031-31122-videoPlayer.ascx.cs)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    VideoPlayer  .  Video Player using ASP.NET  .  Video player custom control using ASP.NET  .  Video player custom control  .  Interview  .  Custom Video Player control using ASP.NET  .  Asp.net  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: InterSoft WebGrid : Dynamic Unbound Hierarchical Grid
    Previous Resource: A Simple Video Player Control - Uses
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design

    teleconferencing

    Contact Us    Privacy Policy    Terms Of Use