Project DescriptionYouTube API for ASP.NET, AJAX-extended
API enables selecting the item from the playlist, setting the autoplay mode and object size (W/H):
see DEMO* at:
http://www.webinfocentral.com/RESOURCES/YouTubeMusicVideo.aspx?item=2&auto=1NOTE: ALL CONTENT/SAMPLES ARE SHOWN FOR INFORMATION PURPOSE ONLY.
Sample screenshot (Anastasia Volochkova, Russian prima ballerina dancing "Adiemus"
(Initial settings: 640x480, autoplay enabled, selected index=2)
Project contains:
1. Default Web page "Default.aspx" with corresponding code behind: both to be placed in Application root directory (ASP.NET 2.0+)2. Code Module "YouTubeScriptGenerator.cs" to be placed in AppCode directory_
3. Ajax Library file (AjaxControlToolkit.dll) to be placed in Bin directory
Sample Web Page contains a Literal1 control serving as a script container and a DropDownList control serving as a simple playlist:
(NOTE: ALL CONTENT IS SHOWN FOR DEMO PURPOSE ONLY)
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register assembly="AjaxControlToolkit"
namespace="AjaxControlToolkit"
tagprefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DEMO | YouTube API for ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updatemode="Conditional" >
<ContentTemplate>
<div>
<!-- ALL CONTENT IS SHOWN FOR DEMO PURPOSE ONLY-->
<asp:DropDownList ID="cmbPlaylist" runat="server" AutoPostBack="True">
<asp:ListItem Value="raRaxt_KM9Q">Sound Of Silence (Masters of Chant)</asp:ListItem>
<asp:ListItem Value="8qSeYLhe09s">For Whom The Bell Tolls (Bee Gees)</asp:ListItem>
<asp:ListItem Value="XP9tzWtLFus">Anastasia Volochkova(Adiemus)</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<h3>Sample Demo: Anastasia Volochkova, Russian prima ballerina dancing "Adiemus"</h3>
<h4>Initial settings: 640x480, autoplay enabled, selected index=2</h4>
<hr />
<h4>
More Demo available at:
<a href="http://www.webinfocentral.com/RESOURCES/YouTubeMusicVideo.aspx"
target="_blank">www.webinfocentral.com</a>
</h4>
<hr />
</form>
</body>
</html>
Code behind page contains script generator:
//****************************************************************************
// Module : Default.aspx.cs
// Type : ASP.NET web page code behind
// Developer : Alexander Bell (Infosoft International Inc)
// DateCreated : 06/29/2009
// LastModified : 07/01/2009
// Version : 1.2
// Description : YouTube API for ASP.NET
//****************************************************************************
// DISCLAIMER: This Application is provide on AS IS basis without any warranty
//****************************************************************************
//****************************************************************************
// TERMS OF USE : ALL YouTube CONTENT IS SHOWN AS DEMO SAMPLE ONLY
// : You can use it at your sole risk
//****************************************************************************
using System;
public partial class _Default : System.Web.UI.Page
{
// player width
private int _W = 640;
// player height
private int _H = 480;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// list item to play (specified by index)
int idx = 2;
// autoplay option
int auto = 1;
// get value from the list for selected index
cmbPlaylist.SelectedIndex = idx;
// generate script on page load
Literal1.Text = YouTubeScript.Get(cmbPlaylist.SelectedValue, auto, _W, _H);
}
else
{
// generate script on page postback
Literal1.Text = YouTubeScript.Get(cmbPlaylist.SelectedValue, 0, _W, _H);
}
}
}
Code Module
//******************************************************************************
// Module : YouTubeScriptGenerator.cs
// Author : Alexander Bell
// Copyright : 2009 Alexander Bell
// Date Created : 06/29/2009
// Last Modified : 07/01/2009
// Version : 1.02
// Description : YouTube player Javascript generator
//******************************************************************************
// DISCLAIMER: This Application is provide on AS IS basis without any warranty
//******************************************************************************
//******************************************************************************
// TERMS OF USE : This module is copyrighted.
// : You can use it at your sole risk provided that you keep
// : the original copyright note.
//******************************************************************************
using System;
using System.Text;
///*****************************************************************************
/// <summary>YouTube player Javascript generator</summary>
public static class YouTubeScript
{
/// <summary>
/// YouTube player script generator, default:
/// default width = 320
/// default height = 240
/// no autoplay
/// </summary>
/// <param name="id">id</param>
/// <param name="auto">int</param>
/// <returns>string</returns>
public static string Get(string id)
{
// default size: 320x240, no autoplay
return Get(id, 0, 320, 240);
}
#region YouTube script, autoplay option
/// <summary>
/// YouTube player script generator, overload:
/// default width = 320
/// default height = 240
/// </summary>
/// <param name="id">id</param>
/// <param name="auto">int</param>
/// <returns>string</returns>
public static string Get(string id, int auto)
{
// default size: 320x240
return Get(id, auto, 320, 240);
}
#endregion
#region YouTube script: autoplay option, adjustable W/H
/// <summary>
/// YouTube player script generator (w/autoplay, W/H options)
/// </summary>
/// <param name="id">id</param>
/// <param name="auto">int</param>
/// <param name="W">int</param>
/// <param name="H">int</param>
/// <returns>string</returns>
public static string Get(string id, int auto, int W, int H)
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<embed src='http://www.youtube.com/v/");
// select the item to play
sb.Append(id);
sb.Append("&autoplay=");
// set autoplay options (indicates number of plays)
sb.Append(auto.ToString());
sb.Append("' ");
sb.Append("type='application/x-shockwave-flash' ");
sb.Append("allowscriptaccess='never' enableJavascript ='false' ");
sb.Append("allowfullscreen='true' ");
// set width
sb.Append("width='" + W.ToString() + "' ");
// set height
sb.Append("height='" + H.ToString() + "' ");
sb.Append(@"></embed>");
string scr = sb.ToString();
sb = null;
return scr;
}
#endregion
}
///*****************************************************************************
Recent AJAX-enhanced RIA online projects by Dr. Alexander Bell
| YouTube | API | ASP.NET | Browser | Embedded Video | Embedded Music | C# | RIA | JavaScript | AJAX | WPF |