Monday, 30 December 2013

Play a Video File from the Web - ASP.NET

In this article we are going to see how to play a video file from the web or offline, for this we are creating a application which will take the input in from the text and Play the file.

We are creating a Literal control and assign the object value at run time while play.

HTML:
<form id="form1" runat="server">
    <div>
  <br />
  <br />
  <asp:literal id="Literal1" runat="server"></asp:literal>
  <br />
  <br />
  <asp:textbox id="InputText" runat="server" width="500px" height="50px" wrap="true"
            textmode="multiLine" readonly="false" >http://localhost:30687/Harivarasanam.mp4</asp:textbox>
  <br />
  <br />
  <asp:button id="Button1" runat="server" text="Play" onclick="Button1_Click" />
    </div>
    </form>



C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Player : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string mySourceUrl = this.InputText.Text;
                bool isFullSize = false;
                this.Literal1.Text = this.GetWma(mySourceUrl, isFullSize);
            }
            catch (Exception ex)
            {
                this.Response.Write(ex.ToString());
            }
        }


        private string GetWma(string sourceUrl, bool isFullSize)
        {
            string newtag = "";
            sourceUrl = sourceUrl + "";
            sourceUrl = sourceUrl.Trim();



            if (sourceUrl.Length > 0)
            {
            }
            else
            {
                throw new System.ArgumentNullException("sourceUrl");
            }

            string myWidthAndHeight = "";



            if (isFullSize)
            {
                myWidthAndHeight = "";
            }
            else
            {
                myWidthAndHeight = "width='640' height='480'";
            }



            newtag = newtag + "<object classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' id='player' " + myWidthAndHeight + " standby='Please wait while the object is loaded...'>";
            newtag = newtag + "<param name='url' value='" + sourceUrl + "' />";
            newtag = newtag + "<param name='src' value='" + sourceUrl + "' />";
            newtag = newtag + "<param name='AutoStart' value='true' />";

/* -100 is fully left, 100 is fully right. */
newtag = newtag + "<param name='Balance' value='0' />";                    

/* Position in seconds when starting. */
newtag = newtag + "<param name='CurrentPosition' value='0' />";

/* Show play/stop/pause controls. */
 newtag = newtag + "<param name='showcontrols' value='true' />";

/* Allow right-click. */
newtag = newtag + "<param name='enablecontextmenu' value='true' />";

/* Start in full screen or not. */
             newtag = newtag + "<param name='fullscreen' value='" +                                                  isFullSize.ToString() + "' />";             

             newtag = newtag + "<param name='mute' value='false' />";
           
/* Number of times the content will play.*/
 newtag = newtag + "<param name='PlayCount' value='1' />";                 

/* 0.5=Slow, 1.0=Normal, 2.0=Fast*/
newtag = newtag + "<param name='rate' value='1.0' />";         

/* full, mini, custom, none, invisible */
newtag = newtag + "<param name='uimode' value='full' />";                  

/* Show or hide the name of the file.*/
newtag = newtag + "<param name='showdisplay' value='true' />";             

/* 0=lowest, 100=highest */
newtag = newtag + "<param name='volume' value='50' />";        
newtag = newtag + "</object>";  

            return newtag;
        }
    }
}


Output:


After Play





















From this article we can see how to play a video file from web or from the offline.

No comments:

Post a Comment