(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Using Silverlight to Create a Video Player

(7 votes)
david
>
david
Joined Jul 08, 2008
Articles:   2
Comments:   1
More Articles
18 comments   /   posted on Jul 08, 2008
Categories:   Media

Note: This article is submitted by David for Silverlight Contest: Write and Win.Thanks a lot, David! Hello All, Please drop a comment if you like it.

This article is compatible with the latest version of Silverlight.

Silverlight sure has come a long way since the days of “WPF/E.” It has truly proven itself to be one of the best toolsets for Rich Internet Applications, and has effectively achieved its original goal of being an Internet version of WPF. The power and possibilities behind Silverlight are endless.

Source Code: VideoPlayer.zip

Creating the UI

Let’s start out by designing the interface of our video player. To do this, first create a project in Visual Studio called “VideoPlayer” with the Silverlight Application template.

 
Creating a Silverlight Application in Visual Studio 2008

As the project is being created, you will see a prompt asking about a web application to host the Silverlight application. If you don’t have an existing web application to host your Silverlight application in, go ahead and leave the default settings and hit OK, like this:


Web Application Host prompt while creating a Silverlight Application

Once the project is created, the first file you will see is a split designer/code view of Page.xaml. Looking in the code view, note that the root XAML element is <UserControl>, whereas in some earlier Silverlight releases the root element was almost always <Canvas>. You can drag and drop controls into the code view to create our user interface – designer drag-and-drop isn’t supported yet – or you can just copy and paste the following code. We need a MediaElement for hosting our video, and three Buttons (for Play, Pause, and Stop).

<UserControl x:Class="VideoPlayer.Page"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Width="640"
             Height="550">
    <Grid x:Name="LayoutRoot"
          Background="White">
        <Canvas>
            <MediaElement x:Name="mPlayer"
                          Width="640"
                          Height="480"
                          Source="a_video.wmv" />
 
            <Button x:Name="bPlay"
                    Background="Green"
                    Width="100"
                    Height="45"
                    Canvas.Left="8"
                    Canvas.Top="497"
                    Content="Play"
                    Click="bPlay_Click" />
            <Button x:Name="bPause"
                    Background="Yellow"
                    Width="100"
                    Height="45"
                    Canvas.Left="112"
                    Canvas.Top="497"
                    Content="Pause"
                    Click="bPause_Click" />
            <Button x:Name="bStop"
                    Background="Red"
                    Width="100"
                    Height="45"
                    Canvas.Left="216"
                    Canvas.Top="497"
                    Content="Stop"
                    Click="bStop_Click" />
        </Canvas>
    </Grid>
</UserControl>

You will notice a few things here. Firstly, there is the declaration of the root <UserControl> element. I still chose to throw in a <Canvas> element following this, for easily positioning the Buttons. You can see the size of the UserControl is quickly set within two attributes, along with the XAML namespaces references. The first child element after the <Canvas> is the MediaElement. The Source property should be replaced with whatever video you wish to display. Following the MediaElement, there are the three Buttonss which, respectively, are green (Play), yellow (Pause) and red (Stop). This is the general setup for the user interface. If you’d like to make changes or additions, go ahead! There is plenty of room for flexibility here – what about adding a video selection list, or a download feature?

Adding Control

We will continue with the core video player functionality. The video player will have no control with three mere Buttons alone! For this feat, we will need to wire the MouseLeftButtonDown events of these elements to functions in the page’s supporting code file (the defaults is Page.xaml.vb in VB.NET or Page.xaml.cs in C#). In the XAML file, we set the x:Name attributes of all the elements. This will give us access to the elements through code. Open the code file, which should look similar to the following:

'VB.NET
Partial Public Class Page
    Inherits UserControl
 
    Public Sub New()
        InitializeComponent()
    End Sub
End Class
 
//C#
public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
    }
}

Between the End Sub of the Page_Loaded event handler and the End Class line (or between the last two brackets), add the following event handlers. Each function handles the appropriate MouseLeftButtonDown event for a Button element, and then calls the corresponding method in the MediaElement.

'VB.NET
 
Private Sub bPlay_Click(ByVal sender As System.Object , ByVal e As System.Windows.RoutedEventArgs)
    mPlayer.Stop()
    mPlayer.Play()
End Sub
 
Private Sub bPause_Click(ByVal sender As System.Object , ByVal e As System.Windows.RoutedEventArgs)
    If mPLayer.CurrentState = MediaElementState.Paused Then
        mPlayer.Play()
    Else
        mPlayer.Pause()
    End If
End Sub
 
Private Sub bStop_Click(ByVal sender As System.Object , ByVal e As System.Windows.RoutedEventArgs)
    mPlayer.Stop()
End Sub
 
//C#
 
public void bPlay_Click(object sender, RoutedEventArgs e)
{
    mPlayer.Stop()
    mPlayer.Play()
}
 
public void bPause_Click(object sender, RoutedEventArgs e)
{
    if (mPLayer.CurrentState == MediaElementState.Paused)
    {
        mPlayer.Play();
    }
    else
    {
        mPlayer.Pause();
    }
}
 
public void bStop_Click(object sender, RoutedEventArgs e)
{
    mPlayer.Stop()
}

The video player should now work properly. The task of creating a fully functional video player is easily accomplished with Silverlight. Even in the time from the first few Alpha releases of it, we have seen Silverlight burst into popularity and power. Although the video player presented here seems to be a basic example, it does in fact cover all of the basic Silverlight techniques: creating a Silverlight application, using VS for designing in Silverlight, and the .NET Silverlight event handling system. Silverlight is a really useful new technology from Microsoft – one that is revolutionizing the way people use and develop the web as you read this. Now it’s up to you: what will you be inspired to create with this astounding new technology? Get out there, start coding, and have fun!


Subscribe

Comments

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Fallon Massey on Jul 08, 2008 18:34

    Actually, the only thing you need to do is use the "Click" event of your buttons.

    Your mousedown events don't work, even in VB, you have to use the "Click Event"

    This might have worked in Beta 1, but if you tried to run this before posting, you might have noticed that the buttons do not work.

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by cecaceo on Jul 24, 2008 13:16

    ERROR Sys.InvalidOperationException: MediaError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Roy on Sep 25, 2008 11:40

    It's not that difficult to get the buttons to work, just wire up the events from Page.xaml into Page.xaml.cs

    public

    Page()new RoutedEventHandler(Play_Click);new RoutedEventHandler(Pause_Click);new RoutedEventHandler(Stop_Click);public void Play_Click(object sender, EventArgs e) public void Pause_Click(object sender, EventArgs e) public void Stop_Click(object sender, EventArgs e)

    {

    InitializeComponent();

    bPlay.Click +=

    bPause.Click +=

    bStop.Click +=

    }

     

    {

    mPlayer.Play();

    }

     

    {

    mPlayer.Pause();

    }

     

    {

    mPlayer.Stop();

    }

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Bharat on Nov 05, 2008 05:01

    Yes it worked great.................

    Thanks alot..........

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Krishna on Dec 03, 2008 10:01

    MediaError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR

    Please help......

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by DKS on Jan 23, 2009 07:22

    MediaError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR

    Please help to solve this error...

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by CobaltSoft on Feb 23, 2009 00:28

    I see that I am not the only one that has run across this recently

    Any suggestions?

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by CobaltSoft on Feb 23, 2009 00:32

    Found It!!!

    as posted on http://dotnetdreamer.com/

    "Place the media resource in the ClientBin directory did the trick

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by kallol on Jun 18, 2009 23:41
    Is it possible to make a video player by c# by Visual studio 2005?
  • -_-

    Using Silverlight and vs 2010 beta to Create a Player......


    posted by Pranto on Feb 06, 2010 11:35

    Sorry i do not understand about code and controlling, how to it wil become easy. please help me.

    i want to create a player with VS 2010 beta.

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Dolphen on Feb 18, 2010 19:18
    will you plz explain me how to creat attractive video player in Silverlight
  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by anil on Mar 09, 2010 11:21
    The wmv file is not running in application...
  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by Anil Pandey on Mar 09, 2010 11:52
    Do i need to convert the WMV file in any other format as there is no errors in the code but nothing is happening adter clicking the PLAY button...

    What do i do now????
  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by DaveCS on Sep 01, 2010 13:25

    @Anil Pandey

    If it plays in windows media player (one that ships with Windows) it should work. A year and a half ago I created a large project and found it better if I converted the videos in accordance with the Microsoft wmv audio and video codecs. It was long ago that I did this and forgot the specific settings but I recall finding them on the Microsoft site.  I used an ASP.NET video conversion library to convert the the files server side.   To the best of my recollection the conversion library is called FlameLib or something to that effect.

    Good luck.   

  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by kjgyi on Dec 02, 2010 17:00
    iohkubhi
  • lnikolov

    RE: Using Silverlight to Create a Video Player


    posted by lnikolov on Jan 11, 2011 14:41
    The article has been updated to the latest version of Silverlight and Visual Studio.
  • -_-

    RE: Using Silverlight to Create a Video Player


    posted by keith on Apr 07, 2011 05:29
    can this be added to an ASP.NET page?
  • shahidKhan

    Re: Using Silverlight to Create a Video Player


    posted by shahidKhan on Jan 12, 2012 14:36

    how to make a video player on from my pc please send me coding


Add Comment

Login to comment:
  *      *       
Login with Facebook