ASP .Net server control for youtube embeded videos

Here is ASP .Net server control for youtube embeded videos.
The param definitions can be found here
Only Src param is required, other parameters are generated only if have another value than the default one.
Usage

<cc1:YouTubeEmbed ID="YouTubeEmbed1" runat="server" Src="http://www.youtube.com/v/P38oxTrWYp0&f=videos&c=ytapi-AdrianIftode-youtubelyricsmas-ia9d2g0e-0"
AutoPlay="false"
AllowFullScreen="false"
Border="true"
PrimaryBorderColor="0x6b8ab6"
SecondayBorderColor="0x2b405b"
GenieMenu="true"
HighDefinitionPlayback="false"
LoadRelatedVideos="false"
Loop="false"
ShowAnnotations="true"
ShowClosedAnnotations="true"
ShowInfo="false"
ShowSearch="false"
Start="68"
Width="600"
Height="400"/>

It creates the html for the following video.


Source code

using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System;
using System.Text;

namespace YouTubeControls
{
[DefaultProperty("Src"),
ToolboxData("<{0}:YouTubeEmbed runat=server></{0}:YouTubeEmbed>")]

public class YouTubeEmbed : WebControl
{
private string src;

[Bindable(true),
Category("Data"),
Description("Video source.")
]

public string Src {
get {
return src;
}
set {
src = value;
}
}

[PersistenceMode(PersistenceMode.Attribute),
Bindable(true),
Category("Appearence"),
DefaultValue(425)]
public int Width
{
get
{
var obj = ViewState["Width"];
return (obj == null) ? 425 : ((int)obj > 0) ? (int)obj : 425 ;
}
set
{
ViewState["Width"] = value;
}
}
[PersistenceMode(PersistenceMode.Attribute),
Bindable(true),
Category("Appearence"),
DefaultValue(349)]
public int Height
{
get
{
var obj = ViewState["Height"];
return (obj == null) ? 349 : ((int)obj > 0) ? (int)obj : 349 ;
}
set
{
ViewState["Height"] = value;
}
}

[Bindable(true),
Category("Behavior"),
DefaultValue(true),
Description("Sets whether the player should load related videos."),
PersistenceMode(PersistenceMode.Attribute)
]
public bool LoadRelatedVideos
{
get
{
var obj = ViewState["Rel"];
return (obj == null) ? true: (bool)obj;
}
set
{
ViewState["Rel"] = value;
}
}

[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Sets whether or not the initial video will autoplay when the player loads."),
PersistenceMode(PersistenceMode.Attribute)]
public bool AutoPlay
{
get
{
var obj = ViewState["AutoPlay"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["AutoPlay"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Cause the player to play the initial video again and again."),
PersistenceMode(PersistenceMode.Attribute)]
public bool Loop
{
get
{
var obj = ViewState["Loop"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["Loop"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Sets whether or not enable Javascript API."),
PersistenceMode(PersistenceMode.Attribute)]
public bool EnabledJSApi
{
get
{
var obj = ViewState["EnabledJSApi"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["EnabledJSApi"] = value;
}
}

[Bindable(false),
Category("Behavior"),
Description("Value can be any alphanumeric string. This setting is used in conjunction with the JavaScript API."),
PersistenceMode(PersistenceMode.Attribute)]
public string PlayerApiID
{
get
{
var obj = ViewState["PlayerApiID"];
return (obj == null) ? null : (string)obj;
}
set
{
ViewState["PlayerApiID"] = value;
}
}

[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Disable the player keyboard controls."),
PersistenceMode(PersistenceMode.Attribute)]
public bool DisableKeyboard
{
get
{
var obj = ViewState["DisableKeyboard"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["DisableKeyboard"] = value;
}
}
[Bindable(true),
Category("Appearence"),
DefaultValue(false),
Description("Genie menu (if present) appears when the user's mouse enters the video display area, as opposed to only appearing when the menu button is pressed."),
PersistenceMode(PersistenceMode.Attribute)]
public bool GenieMenu
{
get
{
var obj = ViewState["GenieMenu"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["GenieMenu"] = value;
}
}
[Bindable(false),
Category("Behaviour"),
DefaultValue(false),
Description("Enables a border around the entire video player."),
PersistenceMode(PersistenceMode.Attribute)]
public bool Border
{
get
{
var obj = ViewState["Border"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["Border"] = value;
}
}
[Bindable(false),
Category("Appearence"),
Description("Primary border color. Any RGB value in hexadecimal format"),
PersistenceMode(PersistenceMode.Attribute)]
public string PrimaryBorderColor
{
get
{
var obj = ViewState["PrimaryBorderColor"];
return (obj == null) ? null : (string)obj;
}
set
{
ViewState["PrimaryBorderColor"] = value;
}
}
[Bindable(false),
Category("Appearence"),
Description("Secondary border color. Any RGB value in hexadecimal format"),
PersistenceMode(PersistenceMode.Attribute)]
public string SecondayBorderColor
{
get
{
var obj = ViewState["SecondayBorderColor"];
return (obj == null) ? null : (string)obj;
}
set
{
ViewState["SecondayBorderColor"] = value;
}
}
[Bindable(false),
Category("Behaviour"),
DefaultValue(0),
Description("This parameter causes the player to begin playing the video at the given number of seconds from the start of the video."),
PersistenceMode(PersistenceMode.Attribute)]
public int Start
{
get
{
var obj = ViewState["Start"];
return (obj == null) ? 0 : ((int)obj > 0) ? (int)obj : 0;
}
set
{
ViewState["Start"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Enables the fullscreen button."),
PersistenceMode(PersistenceMode.Attribute)]
public bool AllowFullScreen
{
get
{
var obj = ViewState["AllowFullScreen"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["AllowFullScreen"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Enables HD playback."),
PersistenceMode(PersistenceMode.Attribute)]
public bool HighDefinitionPlayback
{
get
{
var obj = ViewState["HighDefinitionPlayback"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["HighDefinitionPlayback"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(true),
Description("Enables HD playback."),
PersistenceMode(PersistenceMode.Attribute)]
public bool ShowSearch
{
get
{
var obj = ViewState["ShowSearch"];
return (obj == null) ? true : (bool)obj;
}
set
{
ViewState["ShowSearch"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(true),
Description("Display information like the video title and rating before the video starts playing."),
PersistenceMode(PersistenceMode.Attribute)]
public bool ShowInfo
{
get
{
var obj = ViewState["ShowInfo"];
return (obj == null) ? true : (bool)obj;
}
set
{
ViewState["ShowInfo"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Display annotations."),
PersistenceMode(PersistenceMode.Attribute)]
public bool ShowAnnotations
{
get
{
var obj = ViewState["ShowAnnotations"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["ShowAnnotations"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue(false),
Description("Display closed annotations."),
PersistenceMode(PersistenceMode.Attribute)]
public bool ShowClosedAnnotations
{
get
{
var obj = ViewState["ShowClosedAnnotations"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["ShowClosedAnnotations"] = value;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.RegisterRequiresControlState(this);
}
protected override object SaveControlState()
{

object obj = base.SaveControlState();

if (!String.IsNullOrEmpty(src))
{
if (obj != null)
{
return new Pair(obj, src);
}
else
{
return src;
}
}
else
{
return obj;
}
}
protected override void LoadControlState(object state)
{
if (state != null)
{
Pair p = state as Pair;
if (p != null)
{
base.LoadControlState(p.First);
src = (string)p.Second;
}
else
{
if (state is string)
{
src = (string)state;
}
else
{
base.LoadControlState(state);
}
}
}
}


protected override void RenderContents(HtmlTextWriter writer)
{
var sb = new StringBuilder();
sb.AppendFormat("<object width=\"{0}\" height=\"{1}\">", Width, Height);
sb.AppendLine();
var sbMovieParam = new StringBuilder();
sbMovieParam.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}",
Src,
AutoPlay ? "&autoplay=1" : "",
Loop ? "&loop=1" : "",
EnabledJSApi ? "&enablejsapi=1" : "",
!String.IsNullOrEmpty(PlayerApiID) ? "&playerapiid="+ PlayerApiID : "",
DisableKeyboard ? "&disablekb=1" : "",
GenieMenu ? "&egm=1" : "",
(Border && (!String.IsNullOrEmpty(PrimaryBorderColor) || !String.IsNullOrEmpty(SecondayBorderColor))) ? "&border=1" : "",
(!String.IsNullOrEmpty(PrimaryBorderColor) && Border) ? "&color1="+ PrimaryBorderColor : "",
(!String.IsNullOrEmpty(SecondayBorderColor) && Border) ? "&color2="+ SecondayBorderColor : "",
Start > 0 ? "&start="+Start : "",
AllowFullScreen ? "&fs=1" : "",
HighDefinitionPlayback ? "&hd=1" : "",
ShowSearch == false ? "&showsearch=0" : "",
ShowInfo == false ? "&showinfo=0" : "",
ShowAnnotations == false ? "&iv_load_policy=3" : "",
ShowClosedAnnotations ? "&cc_load_policy=1" : ""
);
var movieParam = sbMovieParam.ToString();
sb.AppendFormat("<param name=\"movie\" value=\"{0}\"></param>", movieParam);
if ( AllowFullScreen ){
sb.AppendLine("<param name=\"allowFullScreen\" value=\"true\"></param>");
}
else{
sb.AppendLine("<param name=\"allowFullScreen\" value=\"false\"></param>");
}
sb.AppendLine();
sb.AppendFormat("<embed src=\"{0}\" type=\"application/x-shockwave-flash\" width=\"{1}\" height=\"{2}\"{3}></embed>"
,movieParam, Width, Height,
AllowFullScreen ? " allowfullscreen=\"true\"" : " allowfullscreen=\"false\""
);
sb.AppendLine("</object>");
var embed = sb.ToString();
writer.Write(embed);
}
}
}


Download library
8 Jun 2008 Update: link fixed

Comments

Popular posts from this blog

IIS 7.5, HTTPS Bindings and ERR_CONNECTION_RESET

Table Per Hierarchy Inheritance with Column Discriminator and Associations used in Derived Entity Types

About the "The Controls collection cannot be modified because the control contains code blocks" symptom