This article shall describe the construction of a custom control used to play video on an ASP.NET web page. The control is based upon the Windows Media Player active X control; with it you can add canned or live video to a web page by setting a property or two at design time, or from the ASP.NET page itself.

The solution contains two projects; the first is the web custom control and the seconds is a sample web site with two pages, each displaying the controls in use to display video. The control project is called "Web Video"; it contains a single web custom control called, "WVC". The demonstration website contained in the second project is called, "TestWVC". The website contains to web pages called "Default" and "Webcams".

The Code: Web Video - WVC Custom Control
The WVC custom control project is a web custom control project; it contains only a single control (WVC). This control wraps up the media player control and provides design time support for the control through a collection of properties used to set some of the various properties used by the media player control.
The only reference added to the project, aside from the defaults, was System. Design. System. Design is necessary to support some of the design time functionality exposed by the control such as using the URL editor with the file path property of the control.

The imports, namespace, and class declarations are as follows:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebVideo
- {
- [DefaultProperty("FilePath")]
- [ToolboxData("<{0}:WVC runat=server></{0}:WVC>")]
- public class WVC : WebControl
- {
After the class declaration, the next block of code is used to declare a set of local member variables, each of which shall be controlled by a related property:
- #region Declarations
- private string mFilePath;
- private bool mShowStatusBar;
- private bool mShowControls;
- private bool mShowPositionControls;
- private bool mShowTracker;
- #endregion
- #region Properties
- [Category("File URL")]
- [Browsable(true)]
- [Description("Set path to source file.")]
- [Editor(typeof(System.Web.UI.Design.UrlEditor),
- typeof(System.Drawing.Design.UITypeEditor))]
- public string FilePath
- {
- get
- {
- return mFilePath;
- }
- set
- {
- if (value == string.Empty)
- {
- mFilePath = string.Empty;
- }
- else
- {
- int tilde = -1;
- tilde = value.IndexOf('~');
- if (tilde != -1)
- {
- mFilePath = value.Substring((tilde + 2)).Trim();
- }
- else
- {
- mFilePath = value;
- }
- }
- }
- } // end FilePath property
- [Category("Media Player")]
- [Browsable(true)]
- [Description("Show or hide the tracker.")]
- public bool ShowTracker
- {
- get
- {
- return mShowTracker;
- }
- set
- {
- mShowTracker = value;
- }
- }
- [Category("Media Player")]
- [Browsable(true)]
- [Description("Show or hide the position controls.")]
- public bool ShowPositionControls
- {
- get
- {
- return mShowPositionControls;
- }
- set
- {
- mShowPositionControls = value;
- }
- }
- [Category("Media Player")]
- [Browsable(true)]
- [Description("Show or hide the controls.")]
- public bool ShowControls
- {
- get
- {
- return mShowControls;
- }
- set
- {
- mShowControls = value;
- }
- }
- [Category("Media Player")]
- [Browsable(true)]
- [Description("Show or hide the status bar.")]
- public bool ShowStatusBar
- {
- get
- {
- return mShowStatusBar;
- }
- set
- {
- mShowStatusBar = value;
- }
- }
- #endregion
- #region "Rendering"
- protected override void RenderContents(HtmlTextWriter writer)
- {
- try
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("<object classid=clsid:22D6F312-B0F6-11D0-94AB-
- 0080C74C7E95 ");
- sb.Append("codebase=http://activex.microsoft.com/activex/
- controls/mplayer/en/nsmp2inf.cab#Version=
- 5,1,52,701 Width = " + Width.Value.ToString() + " Height = "
- + Height.Value.ToString() + "type=application/x-oleobject
- align=absmiddle");
- sb.Append("standby='Loading Microsoft+reg; Windows+reg; Media
- Player components...' id=mp1 /> ");
- sb.Append("<param name=FileName value=" + FilePath.ToString()
- + "> ");
- sb.Append("<param name=ShowStatusBar value=" +
- ShowStatusBar.ToString() + "> ");
- sb.Append("<param name=ShowPositionControls value=" +
- ShowPositionControls.ToString() + "> ");
- sb.Append("<param name=ShowTracker value=" +
- ShowTracker.ToString() + "> ");
- sb.Append("<param name=ShowControls value=" +
- ShowControls.ToString() + "> ");
- sb.Append("<embed src=" + FilePath.ToString() + " ");
- sb.Append("pluginspage=http://www.microsoft.com/
- Windows/MediaPlayer type=application/x-mplayer2 ");
- sb.Append("Width = " + Width.Value.ToString() + " ");
- sb.Append("Height = " + Height.Value.ToString());
- sb.Append(" /></embed></object>");
- writer.RenderBeginTag(HtmlTextWriterTag.Div);
- writer.Write(sb.ToString());
- writer.RenderEndTag();
- }
- catch
- {
- // with no properties set, this will render "Display PDF
- // Control" in a
- // a box on the page
- writer.RenderBeginTag(HtmlTextWriterTag.Div);
- writer.Write("Display WVC Control");
- writer.RenderEndTag();
- } // end try-catch
- } // end RenderContents
- #endregion
In the catch portion of the try-catch block, if the control errors the words, "Display WVC Control" will appear on the page. This will happen if the file path property is not set (which it will not be when the control is dragged onto a form). In order to prevent the lack of a file name causing an error, the control is rendered differently (and without the media player control) as soon as it is added to the form.
Since the control is wrapped in a div; any of the properties also available to a div are also added to the control's property grid (for example, one may set the border style, background color, etc. of the control because those properties are available to the div).
The Code: Test Web WVC
The demonstration web project is comprised of two web pages; one page, the default, demonstrates a single control in use. It also uses a drop down list control set to auto postback; the list contains a set of items with the value property pointing to a valid source of media. When the drop down list is used, the media player's file path is set to point to the new source of media. The second page contains four controls each set to a different source and with the control panel options enabled. The code is trivial and is not reported in this document.


Summary
By wrapping up the media player control in a custom web control, it becomes very easy to embed or even dynamically embed the controls into web pages. By moving the code required to display a media player into the custom control, it is no longer to code out the content into each page using the controls.

SAKTHIVEL LOGANATHANPosted Dec 30, 2017, 4:53 AM
Thank you for sharing this. This is working for youtube source. is it possible to show the video file from the server folder ?. Also need to show the live stream from the CCTV camera. Please advice.
HemaPosted Mar 2, 2015, 3:22 AM
this suports only the .avi and .mpeg format
HemaPosted Mar 2, 2015, 3:20 AM
i have 1 requirement. my web need to play.mp4,.mov etc. can you send me the rest of the code?
naga nagaPosted Jul 18, 2013, 8:21 AM
i did not found webcam.aspx please privide that one.
Nanjunda SwamyPosted Mar 18, 2013, 11:01 AM
is this code supported in IPad and IPhone?
Amit JhaPosted Nov 30, 2012, 8:11 AM
How to play video in asp.net
ciro zamudioPosted Oct 30, 2012, 5:34 PM
No funciona cuando lo coloco dentro de una tabla se reproduce al fondo´pudieras ayudarem.
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
Jan AlfredPosted Oct 8, 2012, 5:13 AM
dear Scott, i have 1 requirement. my web need to play .avi, .wmv, .mp4, etc. can you send me the rest of the code? here's my email : [email protected] i already spend 1 whole month searching for playing video with that many video exts. Thanks b4.. =D
milind cPosted Jul 2, 2012, 8:21 AM
i m not able to run code
raja dhayalanPosted Dec 13, 2011, 4:40 AM
please help me
Dushyant ChaudharyPosted Sep 6, 2011, 12:17 PM
Hi Scott, Thanks for such a useful code. I googled much but didn't find useful solution. Thanks for the post. But I have 1 question and 1 requirement. Request you to pls mail me the complete code to [email protected] as file provided here by you doesn't contains webcams.aspx file. Secondly I want to ask that can I use the video from the device called DVR. I have attached a DVR and attach video cams to it on IP provided by my ISP. I registered at www.dyndns.com to get a host name which directs to the ip of system where DVR is used. I access the video in my browser through the host name provided by dyndns (say-http:\\dush.dyndns.com). the video appears on my browser. Pls tell me that can i use that url to show video in this control.
gharshadgPosted Jul 15, 2011, 1:19 AM
I like to have audio/video functionality on my web page with maximum format. Can any one help me out, i stuck in between coding from 1 week. gr8 if send code or give solution my id [email protected]
mehul rajputPosted Apr 22, 2011, 6:43 AM
This is not work in Firefox and Crome. its only works in IE. What'll sollution for that?? Thanks in Advance.
gina zhangPosted Nov 8, 2010, 1:20 PM
here is my email:[email protected] Many thanks!
Koteswararao MallisettiPosted Oct 13, 2010, 7:21 AM
can i play any type of videos will play like .swf and avi and wmv and .rm files
suranga MudhaligePosted Oct 5, 2010, 2:12 PM
Thank you for your great program !!!!!111 helped us a lot thnks
Mahesh ChandPosted Aug 25, 2010, 9:19 AM
Very useful.
RajeshPosted Aug 23, 2010, 8:30 PM
Dear all, Can i get webcams.aspx page from any of you.. Thanks in advance.. [email protected]
anamika singheditedPosted Jul 23, 2010, 2:17 AMEdited Jul 23, 2010, 4:44 AM
hi,i tried to build webvideo project but its not running properly.its not displaying anything.i hav not understand many things like methods u r creating in WVC.cs , where i call these methods.In aspx page we are not doing anything then how can it will display the mediaplay control.could u please define me what RenderContents method is doing?whether i hav to call this in my aspx page ,if yes then how pls. send me the code. pls help me how can i run this code.thanks in advance.
Julu GarleyPosted Jun 3, 2010, 10:31 AM
Thanks Scott... Does anyone has the VB version for this? Also all the contents are not included in the zip file.
Sohail BasheerPosted May 3, 2010, 8:58 AM
Hi, I want to diplsy video of my web cam place at my home, and i want to display live view of my web came on my web application using asp.net c#. kindly guid me. If you ive me source code i will really thankful to you. Sohail
rajendra peralaPosted Apr 22, 2010, 2:10 AM
Here how could i enable full sreen button for the end user. and even the player is not playing the flv content. may be the codecs need to be downloaded. any help
awais awaisallPosted Jan 16, 2010, 2:05 PM
gooooooooood work
suresh rajuPosted Nov 20, 2009, 6:20 AM
Hi its good article , but i am unable to find WebCams.aspx page can any one give information where to download
twssPosted Oct 13, 2009, 7:53 AM
It very nice Article and very useful code. Great Man!!!
David TaylorPosted Aug 24, 2009, 11:32 AM
I thought I could convert this to VB2008 but I can't. Does anyone have this code in VB? Thanks in advance. David
Kamal kannanPosted Aug 20, 2009, 5:37 AM
asdf
soumya royPosted Aug 12, 2009, 11:24 AM
hey m impressed wid ur job bro i hav found dis info very useful but as the zip does not hav full content send me WVC.zip to my id [email protected] thanks in advance , soumya
MikePosted Jun 5, 2009, 2:39 PM
The video does play if Media Player isn't installed. How can I test to see if Media player is installed on the clients PC? And prompt them to install it.
surasak ngathongPosted Apr 24, 2009, 2:34 AM
Hi Scott, Thank for shared.
syed ahmed ullahPosted Apr 9, 2009, 7:19 AM
its not taking the filepath when i am giving it through c# code like WebVideo.wvc obj=new WebVideo.wvc(); obj.FilePath="path of the video"; its not taking it please respond me to my mail id [email protected]
syed ahmed ullahPosted Apr 8, 2009, 4:52 AM
its not taking the filepath when i am giving it through c# code like WebVideo.wvc obj=new WebVideo.wvc(); obj.FilePath="path of the video"; its not taking it please respond me to my mail id [email protected]
syed ahmed ullahPosted Mar 26, 2009, 2:03 AM
Hi Scott this the great stuff which i was looking for, for me the problem is video is coming but its not displaying its controls[play,progress bar---] even though i set its properties to true kindly forward me whats the problem to [email protected] and thanks a lot.
syed ahmed ullahPosted Mar 26, 2009, 2:00 AM
Hi Scott this is the great stuff which i am looking for for me the problem is controls[play,progress bar---] are not appearing when my video clip is played even though i set its propertied to true
deepak jaiswalPosted Mar 25, 2009, 4:18 PM
its really a good code.. But I got 4 errors in the code Error 1 'ASP.default_aspx.GetTypeHashCode()': no suitable method found to override c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\test3\e032b6b1\e59e0fac\App_Web_xspl14eq.0.cs 289 Error 2 'ASP.default_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\test3\e032b6b1\e59e0fac\App_Web_xspl14eq.0.cs 293 Error 3 'ASP.default_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable' c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\test3\e032b6b1\e59e0fac\App_Web_xspl14eq.0.cs 129 Error 4 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). C:\Documents and Settings\Ram\Desktop\test3\Default.aspx.cs 1 33 C:\...\test3\ pls help me to resolve these errors...
chen danPosted Mar 23, 2009, 11:47 PM
thanks!i learn from a lot of this !
BinabicPosted Mar 23, 2009, 4:33 PM
thx, rate 5
hassan mohamedPosted Jan 10, 2009, 4:51 PM
i tried to display http://www.youtube.com/watch?v=9lbegiKusJ0 it did not work i dont know why
Jerry RoxasPosted Jan 9, 2009, 2:09 PM
great code, thx! two questions: 1. is it possible to use the control but not have the video play until the user hits the play button? currently, the video automatically loads and plays when the page is loaded. this causes a problem, because i use three instances of the control on a page, and they all start playing at once. 2. can the contol pay other types of media, or is it limited to wmv files? thanks again!
june ngPosted Dec 16, 2008, 10:37 PM
I have downloaded WVC.zip, However, TestWVC isn't found. I really need help for my project Any advice or help? Thanks (:
Dale SPosted Dec 2, 2008, 2:16 PM
Simple and elegant. Good use of extension.
Ahmed MohamedPosted Oct 27, 2008, 5:33 AM
please tell me why the control worked well in IE but not worked in FireFox
sudarshan sPosted Feb 4, 2008, 5:42 AM
Hi one asp.net file named WebCams.aspx is missing. Please help where can i find the same... Thanks in Advance.
kalpesh kalpeshPosted Jan 25, 2008, 5:07 AM
you have given the download option to download but i cannot download it
Sudheer Reddy VuyyuruPosted Dec 12, 2007, 7:12 AM
After i studied this article, i thought it solves my problem becaz am also searching how to play a video files in Webbrowser using Asp.net & C#.net. But in this article i not found "Test Web WVC" Web part code. I need that code to implement in my application. Please provide that code to me, it's very help to me.
Kamran KhatriPosted Oct 31, 2007, 9:41 AM
absoulutly great work done
Blocked AccountPosted Oct 18, 2007, 8:55 AM
Hi Scott, Good article but i have one problem with this control, This control works fine in mojila but in IE version less than 6.0 ,it doesn't display the video during scrolling of the page. Please help.
Chun ZhangPosted Sep 14, 2007, 9:54 PM
Hi Scott: This is the best example for playing video. Thank you for the contribution. I'd like to feed real time video from my USB camera or IP camera. Please advise, Thank in advance. Chun Zhang
rushabh mandviaPosted Aug 30, 2007, 7:06 AM
Hi scott!i have found this info.very useful.
r rPosted Jul 3, 2007, 3:16 AM
Hi Scott, Have gone through the article which displays video using a C# Web Custom Control, I have been doing the R&D almost for 3 weeks always got stuck in playlist area. Mentioned below are the steps involved: 1) I have alist of videos/audios (of any format 3gp,rm,mov,.mpeg|.mp3|.m3u|.aac|.aif|.iff|.mid|.midi|.mpa|.ra|.ram|.wav|.wma .wmv|.mpg|.3gp|.asf|.asx|.avi|.mov|.mp4|.qt|.rm|.swf|) 2)User selects any of this files and clicks on play button 3)so accordingly i need to dipaly the video / audio file 4)Also he has the option to include files in playlist which will execute the files one after the other. Now my problem is with WMPlayer u can run some specific files/Quicktime can run some specific files/Real player can run some specific files. Need to know which player to use so that it can deal with maximum of formats. Alos it would be great if you could assist me achieving the desitred output since you already have knowledge. Any help will be greatly appreciated. Luking ahead.... Reagrds, Rupali
Jack JoneseditedPosted Jun 20, 2007, 10:23 PMEdited Jun 20, 2007, 10:37 PM
Sweet, Can I add this control to a repeater and access it in the ItemDataBound event? You should post this to spikesolutions.net