Load and Play a Video File at Runtime using Silverlight 2 Beta 2

Posted by: Suprotim Agarwal , on 7/21/2008, in Category Silverlight 2, 3, 4 and 5
Views: 39223
Abstract: In this short article, we will quickly see how to load and play a video at runtime using the OpenFileDialog control in Silverlight 2.
Load and Play a Video File at Runtime using Silverlight 2 Beta 2
 
Note: This article is written based on a pre-release version of Silverlight and could change in future.
In this short article, we will quickly see how to load and play a video at runtime using the OpenFileDialog control in Silverlight 2.
If you are not familiar with hosting Silverlight 2 in ASP.NET, I hope you have read one of the previous article which talks about the prerequisites of creating Silverlight applications. The article can be found over here Hosting Silverlight Content in ASP.NET 3.5.
Step 1: Open Visual Studio 2008 > File > New Project > Select the language (C# or VB) > Select ‘Silverlight’ in the Project Types > from the templates, select ‘Silverlight Application’. Type a name ‘RuntimeMedia’ (for VB.NET users - RuntimeMediaVB) and location for the project and click ok.
Note: If you are unable to view the templates, you do not have Microsoft Silverlight Tools Beta 2 for Visual Studio 2008.
Step 2: You will observe that a default page called ‘Page.xaml’ gets created. Set the Width and Height of the user control to 600 & 500 respectively. Remove the <Grid> and replace it with <Canvas> and set its background to Gray.  The code after these changes will look similar to the following:
<UserControl x:Class="RuntimeMedia.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="600" Height="500">
    <Canvas Background="Gray" >
 
    </Canvas>
</UserControl>
 
Step 3: Now add a <MediaElement> and a <Button> Control. After setting the height and width of the MediaElement and and a few additional properties of the Button control, the code will look similar to the following:
    <Canvas Background="Gray" >
        <MediaElement x:Name="mediaEle" Height="440" Width="600"></MediaElement>
        <Button x:Name="btnShow" Click="btnShow_Click" Height="50" Width="100" Canvas.Top="450" Content="Load Media"></Button>       
    </Canvas>
Notice the Event Handler btnShow_Click. We will use this event to code the functionality to select media files at runtime.
Step 4: We will be using the OpenFileDialog class to select media files at runtime. Once the user has selected the file, we will play the selected files.
Go to the code behind (Page.xaml.cs or Page.xaml.vb) and write the following code:
C#
        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Media WMV|*.wmv";
            if (fileDialog.ShowDialog() == true)
            {
                FileDialogFileInfo fdfi = fileDialog.SelectedFile;
                mediaEle.SetSource(fdfi.OpenRead());
                mediaEle.AutoPlay = true;
            }
        }
VB.NET
    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim fileDialog As OpenFileDialog = New OpenFileDialog()
        fileDialog.Filter = "Media WMV|*.wmv"
        If fileDialog.ShowDialog() = True Then
            Dim fdfi As FileDialogFileInfo = fileDialog.SelectedFile
            mediaEle.SetSource(fdfi.OpenRead())
            mediaEle.AutoPlay = True
        End If
    End Sub
In the code above, we create an instance of the OpenFileDialog and specify the file filter string by setting the Filter property to "Media WMV|*.wmv". We then call the ShowDialog method to show the open file dialog box to the user. When the method returns, we test to see if the user pressed the OK button (fileDialog.ShowDialog() == true). If the result is True, we use the SetSource() method of the MediaElement to set its source to the supplied stream. The ‘AutoPlay’ property plays the media as soon as it is loaded.
That’s it. Run the application and check out the functionality to add video files at runtime. I hope you liked the article and I thank you for viewing it.  
 
If you liked the article,  Subscribe to my RSS Feed.
 
 
What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Navin KUmar on Wednesday, February 11, 2009 2:16 AM
nice
Comment posted by STRAWAN on Tuesday, March 17, 2009 6:22 AM
FileDialogFileInfo fdfi = fileDialog.SelectedFile;

After adding this item i have a major error in that ie. i need the namespace for this class

send immediately

confidentially+
Comment posted by Suprotim Agarwal on Wednesday, March 18, 2009 6:50 AM
Strawan: FileDialogFileInfo no more exists. Find the updated article over here http://www.dotnetcurry.com/ShowArticle.aspx?ID=217
Comment posted by Himmel on Thursday, May 28, 2009 12:57 AM
How can i define the the keyword"FileDialogFileInfo" in Vb. I use Visual web developer 2008 express. When i wrote the line:"Dim fdfi As FileDialogFileInfo = fileDialog.SelectedFile
" , one mistake alert: FileDialogFileInfo . Although I import the class: System.Windows.Control.IO and System.Windows.Controls.FileDialog, it doesn't work.
Please help me.
Thanks.
Comment posted by Suprotim Agarwal on Friday, May 29, 2009 12:56 PM
Himmel: Read my previous comment. FileDialogFileInfo no more exists in the Final release of Silverlight. Find the updated article over here http://www.dotnetcurry.com/ShowArticle.aspx?ID=217
Comment posted by Bob Hanson on Wednesday, July 15, 2009 10:06 PM
Can you please suggest how this can work for streaming a video after it was encoded using the Expression Encoder?

Comment posted by siva on Monday, January 11, 2010 2:42 AM
very nice...

but file dialogue box got error...

pls help me...

any namespace add to this...

Comment posted by fdsfdsf on Wednesday, October 27, 2010 4:50 AM
your code does not support.
FileDialogFileInfo fdfi = fileDialog.SelectedFile;
Arrer come

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook