By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,402 Members | 1,722 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How To Play Flash Video Files In ASP.NET using C#.net or VB.net

P: 85
There is a few different ways to show video on web page. Most popular are Windows Media Player, Quick Time, Adobe Flash or Silverlight. If you want to find out how to show video with Windows Media Player you can read How to build ASP.NET Media Player Control tutorial.

This tutorial will focus on using of Flash to show flash video files on ASP.NET web site.

Flash becomes widely used and today it is most used technology for showing movies on Internet. Flash is used by most popular video sharing web sites like YouTube or Google Video. It is compatible with almost every operating system and web browser and very prevalent on Internet users computers. To see Flash movies, users need to have Flash player installed. On my experiences, at least 95% visitors have Flash player installed which is more than other available technologies. Flash is client side technology, although it can be used with static HTML page, but it is usually manipulated by server side web application.
You can’t show flash video files directly. Flash is just a programming framework, which uses Action Script programming language. You need a program made in Flash (video player) to show .flv video on page. You can of course learn Action Script and develop your own player, but that is out of scope of this tutorial. I will leave that task to Flash developers. Fortunately, there are already some free and very good Flash video players available. Using of them will short your development cycle and provide you a quality solution. I selected JW FLV Player as the best solution.

Using of JW FLV Player on web page

There are a two basic steps when placing Flash application to web page:
  • First, declare and initiate a Flash object
  • Second, set properties of object according to your needs.
You can do this on two different ways, with static HTML tags <OBJECT > and , or by using a JavaScript.

With static HTML your code could look something like this:
Expand|Select|Wrap|Line Numbers
  1. <object width=”640″ height=”480″>
  2. <param name=”movie” value=”player.swf” />
  3. </object>
We need both <object > and tags to get browser compatibility. Internet Explorer uses <object> tag, but Firefox and Netscape can’t see it and recognize just . We set parameters by using <param > tags, and inside tag.

Code example above have added parameter name “movie” and value “player.swf”. You can add any other parameter on the same way.

Complete list of supported parameters for JW FLV Player you can see at their Flash vars page.
http://cdn5.tribalfusion.com/media/2...300x250_v8.swf

There is a problem with static HTML if visitors access with Internet Explorer. Flash application would not start until visitor clicks on it. If you just place a cursor over a movie you’ll see an ugly border. Common way to avoid need to click every time when page load is to initiate Flash player with JavaScript. JavaScript code for starting Flash movie could look like this:
Expand|Select|Wrap|Line Numbers
  1. <p id=’preview’>The player will show in this paragraph</p>
  2. <script type=’text/javascript’ src=’swfobject.js’></script>
  3. <script type=’text/javascript’>
  4. var s1 = new SWFObject(‘player.swf’,'player’,’400′,’300′,’9′);
  5. s1.addParam(‘allowfullscreen’,'true’);
  6. s1.addParam(‘allowscriptaccess’,'always’);
  7. s1.addParam(‘flashvars’,'file=video.flv’);
  8. s1.write(‘preview’);
  9. </script>
As you can see, nothing complicated here. Code creates SWFObject dynamically and then set its parameters. In this case visitor is not required to click to start a movie. Of course, JavaScript must be enabled in visitor’s web browser.

Manipulating Flash Player with ASP.NET

Basically, playing video files with JW FLV Player is simple, you just create an object and set needed parameters as in two code examples above. But, if you work with large number of video files or you need to enable to your users to add their movies you need to automate things. Writing static HTML code for every video certainly not sounds professional. Fortunately, with a little ASP.NET code we can make this task much easier .

Creating a custom ASP.NET Flash Player Control
Basic idea is this: Custom ASP.NET control will have properties related to JW FLV Player parameters. In run time, control will render appropriate content on client side so JW FLV player will show video correctly, and you can manipulate flash videos with ASP.NET web application. Now, we can create admin ASP.NET pages, load videos from database, enable database search etc., without hard coding.

I created ASP.NET JW FLV Flash Video Player Control that do this task. Control is free and you can use it to show Flash video files on your web site. Source code of control is also available in C# andVB.NET. Example web project is included in download to see how it works.
Flash Player Control Code Analysis
As you can see from C# and VB.NET code listings, control code consists from three parts:
1. Declaring of properties default values on the beginning. For server control, I used same default values like JW FLV Flash player already uses. I added two additional properties to describe location of JW FLV .swf and .js file, like in code bellow.
[ C# ]
Expand|Select|Wrap|Line Numbers
  1. #region Properties defaults
  2. const string DefaultSwfobject_jsLocation = “”; // in the same directory
  3. const string DefaultFlashPlayerLocation = “”; // in the same directory
  4. const bool DefaultAllowFullScreen = true;
  5. const string DefaultFile = “”;
  6. const bool DefaultAutoStart = false;
  7. const int DefaultBufferLength = 1;
  8. #endregion
[ VB.NET ]
Expand|Select|Wrap|Line Numbers
  1. #Region ”Properties defaults”
  2. Const DefaultSwfobject_jsLocation As String = “” ’ in the same directory
  3. Const DefaultFlashPlayerLocation As String = “” ’ in the same directory
  4. Const DefaultAllowFullScreen As Boolean = True
  5. Const DefaultFile As String = “”
  6. Const DefaultAutoStart As Boolean = False
  7. Const DefaultBufferLength As Integer = 1
  8. #End Region
2. Value of every property is saved to ViewState if value is different from default value. For example, code for AllowFullScreen property looks like this:
[ C# ]
Expand|Select|Wrap|Line Numbers
  1. [Bindable(true), Category("Settings")]
  2. bool AllowFullScreen
  3. {
  4.   get
  5.   {
  6.     if(ViewState["AllowFullScreen"] == null)
  7.       return DefaultAllowFullScreen;
  8.     else
  9.       return (bool)ViewState["AllowFullScreen"];
  10.   }
  11.   set
  12.   {
  13.      if(value != DefaultAllowFullScreen)
  14.         ViewState["AllowFullScreen"] = value;
  15.       else
  16.         ViewState["AllowFullScreen"] = null;
  17.   }
  18. }
[ VB.NET ]
Expand|Select|Wrap|Line Numbers
  1. <Bindable(True), Category(“Settings”)> _
  2. Property AllowFullScreen() As Boolean
  3.   Get
  4.     If ViewState(“AllowFullScreen”) Is Nothing Then
  5.       Return DefaultAllowFullScreen
  6.     Else
  7.       Return ViewState(“AllowFullScreen”)
  8.     End If
  9.   End Get
  10.   Set(ByVal value As Boolean)
  11.     If value <> DefaultAllowFullScreen Then
  12.       ViewState(“AllowFullScreen”) = value
  13.     Else
  14.         ViewState(“AllowFullScreen”) = Nothing
  15.     End If
  16.   End Set
  17. End Property
3. RenderContents function produces output to, depended of values of every property, correctly initialize Flash player on client side by using a JavaScript, like mentioned before.

Conclusion

With custom ASP.NET Flash Player control that manipulate client side flash player you can save a lot of time. I tested control in Firefox and Internet Explorer and worked fine. Please let me know if you find some bug or you have a suggestion for improvement.
Going professional with ASPNetFlashVideo
Control introduced in this tutorial is useful in some simpler scenarios, but keep in mind that JW FLV Player is free only for noncommercial projects. Fortunately, there is pure ASP.NET control namedASPNetFlashVideo that allows you to effortlessly incorporate beautiful Adobe Flash Video (FLV) media into ASP.NET website. For $49 you can use it on single site or just $99 for use on unlimited number of web sites. Except common interface commands for playing flash video files, ASPNetFlashVideosupports skins, custom user interface, play lists, integration with Visual Studio, search engine optimization and more. Things like no ongoing subscription costs, free future updates or 30 days money back guarantee justify decision to give it a try.
Aug 1 '12 #1
Share this Article
Share on Google+