|
Load and Play Media Files at Runtime using Silverlight 2
|
|
Rating: 3 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 10/19/2008,
in category "Silverlight 2, 3 and 4"
Views: this article has been read 12350 times
Abstract: There have been quite a few breaking changes from Beta 2 to the final release of Silverlight. One of these changes affects Silverlight 2 application using the OpenFileDialog. In this short article, we will study these changes and explore how to load and play media files at runtime using the OpenFileDialog control using the final release of Silverlight 2.
Load and Play Media Files at Runtime using Silverlight 2
Sometime back, I had written a similar article Load and Play a Video File at Runtime using Silverlight 2 Beta 2. The article made use of the OpenFileDialog class in Silverlight Beta2 to load files at runtime. There have been quite a few breaking changes from Beta 2 to the final release of Silverlight. One of these changes affects Silverlight 2 application using the OpenFileDialog. In this short article, we will study these changes and explore how to load and play media files at runtime using the OpenFileDialog control using the final release of Silverlight 2.
Before moving ahead, there are a couple of changes related to the OpenFileDialog that you should note if you are upgrading your Silverlight applications from Beta 2 to Final:
- The System.Windows.FileDialogFileInfo type no more exists. Its functionality is now being exposed via its base System.IO.FileInfo type.
- In Beta 2, System.Windows.Controls.OpenFileDialog exposed FileDialogFileInfo through some properties. These properties were modified and in the final release of Silverlight, they now return the FileInfo type.
- The property names ‘SelectedFile’ and ‘SelectedFiles’ have been changed to ‘File’ and ‘Files’.
With this information in hand, let us re-build our sample
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 for Visual Studio 2008. Check out this link to see how to obtain it.
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. You also need to reference the namespace System.IO:
C#
private void btnShow_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Media WMV|*.wmv";
if (fileDialog.ShowDialog() == true)
{
FileInfo fdfi = fileDialog.File;
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 FileInfo = fileDialog.File
mediaEle.SetSource(fdfi.OpenRead())
mediaEle.AutoPlay = True
End If
End Sub
In the code above, we are creating an instance of the OpenFileDialog and specifying 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 fileDialog.File to return an object of the FileInfo type. The SetSource() method of the MediaElement is used to set its source to the supplied stream using the FileInfo object. 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 and play video files at runtime. I hope you liked the article and I thank you for viewing it.