SIGN UP MEMBER LOGIN:    
ARTICLE

Playing and Controlling Video Playback in XAML Silverlight

Posted by Abhimanyu Kumar Vatsa Articles | Silverlight with C# May 02, 2011
In this article, you will learn how to play video and control it's playback in Silverlight.
Reader Level:

Introduction

Using videos in a Silverlight based application is a very exciting feature that come early Silverlight 2. Today we see the most extended Silverlight specially Microsoft added GPU Hardware Acceleration for video and bitmap in Silverlight 3 and now Silverlight 4 supports the output protection for video and audio.

To use or develop video based applications in Silverlight 4, we need to have media player 10 installed. [Download Media Player]

How to Play Video

To play the video, we can simply drag the compatible [List of Supported Media Formats] video file on the development stage. There are many more ways to add media files as I have discussed in my one of the post. [Different Ways to Add Media files in XAML Silverlight]

Now, if you add videos in the development stage and test it, then it will have the following three annoying work:

  1. Movie will automatically start

  2. Movie runs untill the end and when reaches at end can't run again

  3. Once movie starts there is no way to stop or pause this

But by using some coding in code-behind we can control the playback of movie. Let's see how!

1. Dealing with Automatic Start

By default videos automaticall get started when we run the project, it is distracting feature from the users point of view. To change auto start false, select the video on stage and in properties uncheck the 'AutoPlay' option.

image002.jpg
 

2. Dealing with Endless Playback

By default, when the video reaches the end then it stops and does not start again. To change such a setting follow the steps:

  1. Select the video on stage

  2. In Properties, switch the mode from 'Properties' to 'Events'.

  3. In the Event list, point to 'MediaEnded' label and type the event handler name (I will be using md_ended_eve) and then press tab to apply it and it automatically switches you to code-behind with a new event.

  4. Now type the following code inside event handler.

    (intro_wmv).Stop();
    (intro_wmv).Play();
     
    In above code 'intro_wmv' is my media fine name.
     

  5. Now test the application.

Note to use 'AutoPlay' to true when you are testing endless loop.

XAML Code

<Grid
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SilverlightApplication1.MainPage"
          Width="640" Height="480">
          <MediaElement x:Name="intro_wmv"
          Margin="54,64,104,60"
          Source="/intro.wmv"
          Stretch="Fill"
          MediaEnded="md_ended_eve" AutoPlay="False"/>
</Grid>
 

XAML.CS Code

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace
SilverlightApplication1
{
      
public partial class MainPage : Grid
       {
             
public MainPage()
              {
                    
// Required to initialize variables
                     InitializeComponent();
              }
             
private void md_ended_eve(object sender, System.Windows.RoutedEventArgs e)
              {
                    
// TODO: Add event handler implementation here.
                     (intro_wmv).Stop();
                     (intro_wmv).Play();
              }
             
       }
}

image004.jpg
 

 

3. Control Video Playback by Pause and Play

By default in Silverlight video plays and we can't control it by pausing and playing. But by writing some lines in code-behind we can control this playback. For this we have to create the event as we saw in above example. Let's follow the steps:

  • Open the event explorer by switching the property (look above image).

  • Type the event name in 'MouseLeftButtonDown', I will be using here 'pause_start_evn' and press tab to switch in event handler mode.
     

  • In the appeared event type the following code:

    private bool IsPaused=true;
    private void pause_start_evn(object sender, System.Windows.Input.MouseButtonEventArgs e)
                  {
                         // TODO: Add event handler implementation here.
                         if(IsPaused)
                         {
                               (intro_wmv as MediaElement).Play();
                               IsPaused=false;
                         }
                         else
                         {
                               (intro_wmv as MediaElement).Pause();
                               IsPaused=true;
                         }
       }
     
    In the above code, 'intro_wmv' is my media file name.
     

  • Now test the application and check by right mouse click on video surface.

XAML Code

<Grid
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SilverlightApplication1.MainPage"
          Width="640" Height="480">
          <MediaElement x:Name="intro_wmv"
          Margin="54,64,104,60"
          Source="/intro.wmv"
          Stretch="Fill"
          MediaEnded="md_ended_eve"
          MouseLeftButtonDown="pause_start_evn"/>
</Grid>

XAXM.CS Code

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication1
{
      
public partial class MainPage : Grid
       {
             
public MainPage()
              {
                    
// Required to initialize variables
                     InitializeComponent();
              }

              private void md_ended_eve(object sender, System.Windows.RoutedEventArgs e)
              {
                    
// TODO: Add event handler implementation here.
                     (intro_wmv).Stop();
                     (intro_wmv).Play();
              }
             
private bool IsPaused=true;
             
private void pause_start_evn(object sender, System.Windows.Input.MouseButtonEventArgs e)
              {
                    
// TODO: Add event handler implementation here.
                     if(IsPaused)
                     {
                           (intro_wmv
as MediaElement).Play();
                           IsPaused=
false;
                     }
                    
else
                     {
                           (intro_wmv
as MediaElement).Pause();
                           IsPaused=
true;
                     }
              }
       }
}

Now that's all about the controlling of video playback in a Silverlight based application. Enjoy the coding.

Be tuned for next post.

HAVE A HAPPY CODING!!

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, For creating a Silverlight chat application I can recommend the Ozeki VoIP SIP SDK. For sample source codes check this: www.voip-sip-sdk.com/p_257-silverlight-video-chat-example-voip.html The support team is also responsive so you can make your project even faster with the help of them. BR

Posted by Wilson Lastimoza Mar 19, 2012
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor