.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
SP
Sambanthamoorthy
Swati Kadam
rosgeppe

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Play Media Files in WPF

Posted By:Diptimaya Patra       Posted Date: March 24, 2010    Points: 25    Category: WPF    URL: http://dpatra.blogspot.com  

Play Media File in WPF. In this article we will see how to use Media Element in WPF and we will explore some of the basic functionality such as Play, Pause, Stop, Back and Forward.
 

Introduction

In this article we will see how to use Media Element in WPF and we will explore some of the basic functionalities such as Play, Pause, Stop, Back and Forward. Below example uses .wmv files.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as MediaSampleWPF.

MediaElement1.gif

Now we will first design the application so that all the functionalities would be clear to us.

Add a Media Element, and several Buttons for the functions to be achieved.

The following figure is the simple design of the application.

MediaElement2.gif

XAML Reference


        
            
            
        
        
        
            


Now to start with in the beginning of the application all the Buttons except Open should not be enabled. So lets do that.

#region Constructor
public Window1()
{
    InitializeComponent();
    IsPlaying(false);
} 
#endregion

#region IsPlaying(bool)
private void IsPlaying(bool bValue)
{
    btnStop.IsEnabled = bValue;
    btnMoveBackward.IsEnabled = bValue;
    btnMoveForward.IsEnabled = bValue;
    btnPlay.IsEnabled = bValue;
} 
#endregion

Now we will achieve the functionalities one by one.

Open Media

Add reference to the System.Windows.Forms DLL to use the OpenFileDialog.

MediaElement5.gif

Add the below code in the respective Button Click event.

#region Open Media
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
    ofd.Filter = "Video Files (*.wmv)|*.wmv";
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        MediaEL.Source = new Uri(ofd.FileName);
        btnPlay.IsEnabled = true;
    }
}
#endregion

Play and Pause Function

Add the below code in Play Button Click event to achieve the Play and Pause functions.

#region Play and Pause
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
    IsPlaying(true);
    if (btnPlay.Content.ToString() == "Play")
    {
        MediaEL.Play();
        btnPlay.Content = "Pause";
    }
    else
    {
        MediaEL.Pause();
        btnPlay.Content = "Play";
    }
} 
#endregion

Stop Function

Add the below code in Stop Button Click event.

#region Stop
private void btnStop_Click(object sender, RoutedEventArgs e)
{
    MediaEL.Stop();
    btnPlay.Content = "Play";
    IsPlaying(false);
    btnPlay.IsEnabled = true;
} 
#endregion

Move Back and Forward Function

Add the below code in respective Back and Forward Button Click events to achieve Back and Forward.

#region Back and Forward
private void btnMoveForward_Click(object sender, RoutedEventArgs e)
{
    MediaEL.Position = MediaEL.Position + TimeSpan.FromSeconds(10);
}

private void btnMoveBackward_Click(object sender, RoutedEventArgs e)
{
    MediaEL.Position = MediaEL.Position - TimeSpan.FromSeconds(10);
} 
#endregion

That's it run the application. As we mentioned in the code for opening only WMV files, you can open only WMV file.

You can add other extensions like AVI, MPG and MPEG to the Open function.

MediaElement10.gif

MediaElement11.gif

Hope you guys like this article. It's for the beginner level.
 Subscribe to Articles

     

Further Readings:

Responses
Author: gurli         Company URL: http://www.dotnetspark.com
Posted Date: April 05, 2010

when I play a video (wmv) then the audio is on but there is no video displayed.

/Gurli
Author: gurli         Company URL: http://www.dotnetspark.com
Posted Date: April 05, 2010

I use VS2010

/Gurli

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend