Project Description
YouTube API written in C# allows embedding video player into ASP.NET Web page, providing multiple customization options.

DEMO: CLICK ON THE SAMPLE IMAGE TO OPEN VIDEO PLAYER (Web TV app)

YouTubeSampleScreenshot.jpg

Sample screenshot (Anastasia Volochkova, Russian prima ballerina dancing "Adiemus"

NOTE: ALL CONTENT/SAMPLES ARE SHOWN FOR INFORMATION PURPOSE ONLY.

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="XP9tzWtLFus">Anastasia Volochkova(Adiemus)</asp:ListItem>
                    <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: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=0</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:

//****************************************************************************
// Module           :   Default.aspx.cs
// Type             :   ASP.NET web page code behind
// Developer        :   Alexander Bell (Infosoft International Inc)
// DateCreated      :   06/29/2009
// LastModified     :   10/15/2009
// 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)
        {
            #region Start mode customization via Web Query String
            int idx = 0;
            int auto = 0;
            string qry = "";

            // Autostart
            try {
                qry = "auto"; 
                qry = (Request.QueryString[qry] == null) ? "" : Request.QueryString[qry];
                if (qry != "") { auto = int.Parse(qry); }
            } catch { }

            // Item index
            try {
                qry = "item"; 
                qry = (Request.QueryString[qry] == null) ? "" : Request.QueryString[qry];
                if (qry != "") { idx = int.Parse(qry); }
            } catch { }
            #endregion

            // 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    :   10/15/2009
// 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
 }
///*****************************************************************************
ADDITIONAL RESOURCES
  1. WebTV pilot project
YouTube API C# YouTube API YouTube API C# ASP.NET Browser Embedded Video Embedded Music RIA JavaScript AJAX

Last edited Aug 20, 2015 at 2:55 PM by DrABELL, version 15