Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / WPF

Using the MediaPlayer in WPF

Rate me:
Please Sign up or sign in to vote.
4.79/5 (11 votes)
13 Apr 2013CPOL3 min read 89.8K   6.1K   21   17
How to use the MediaPlayer class in a WPF application.

Introduction

In this article I am explaining how to use the MediaPlayer class in a WPF application. This is a relatively short article because I am describing only how to draw a MediaPlayer on a visual surface. To demonstrate this, I have created a demo application using Visual C# 2010 Express Edition.

There are two commonly used approaches to play media in a WPF application. One is using the MediaElement control and the other is using the MediaPlayer class.

Using the MediaElement is a simple and straightforward approach. The MediaElement control has a Graphical User Interface and can be added using either XAML code or code-behind code. It provides playback and control capabilities using Play, Pause and Stop methods. The media file to be played is specified using the Source property of the MediaElement control.

Background

The MediaPlayer class is located in the System.Windows.Media namespace. It has no Graphical User Interface and has to be created in the code-behind file.

To play video using the MediaPlayer class you have to draw the MediaPlayer onto a visual surface and add the surface to the visual tree. The media file to be played is passed as a parameter in the form of a Uri to the Open method of the MediaPlayer class. You can control the play back of the video using the same methods as in MediaElement.

Using the code

The following XAML code defines a window on which the MediaPlayer will be displayed:

XML
<Window x:Class="MyMediaPlayer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MediaPlayer in WPF" Height="350" Width="525">
</Window>

The following is the code-behind code for displaying a video on the window background:

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MediaPlayer player = new MediaPlayer();
        player.Open(new Uri("airplane.mpg", UriKind.Relative));
        VideoDrawing drawing = new VideoDrawing();
        drawing.Rect = new Rect(0, 0, 300, 200);
        drawing.Player = player;
        player.Play();
        DrawingBrush brush = new DrawingBrush(drawing);
        this.Background = brush;
    }
}

It creates a MediaPlayer object and opens the airplane.mpg file. It then creates a VideoDrawing object and sets its drawing area by setting the Rect property. After that it initializes the Player property using the Player object and calls the Play() method to play the video. Finally it creates a DrawingBrush object, passing the VideoDrawing object as a parameter to its constructor, and initializes the Background property of the Window object using the DrawingBrush object.

However this code plays the video only once.

The following code can be used to repeat the playback once the media file reaches the end:

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MediaTimeline timeline = 
          new MediaTimeline(new Uri("airplane.mpg", UriKind.Relative));
        timeline.RepeatBehavior = RepeatBehavior.Forever;
        MediaClock clock = timeline.CreateClock();
        MediaPlayer player = new MediaPlayer();
        player.Clock = clock;
        VideoDrawing drawing = new VideoDrawing();
        drawing.Rect = new Rect(0, 0, 300, 200);
        drawing.Player = player;
        DrawingBrush brush = new DrawingBrush(drawing);
        this.Background = brush;
    }
}

This code first creates a MediaTimeline object and initializes it with the Uri of the media file to be played. After that, the RepeatBehavior property of the MediaTimeline is set to Forever to repeat the playback. Next, it creates a MediaClock object using the MediaTimeline. After that it creates a MediaPlayer object and initializes its Clock property with the MediaClock object. Then it creates a VideoDrawing object and sets its drawing area using the Rect property and sets the Player property. Finally it creates a DrawingBrush object using the VideoDrawing object as a parameter and sets the brush as the window background.

Note: The Copy to Output Directory property of the embedded video file must be set to Copy always or Copy if newer.

Points of Interest

It was initially difficult for me to understand how to use the WPF MediaPlayer. Doing some searching on the web and experimenting with the code helped me to come up with this article. I hope readers, particularly those new to WPF, would find my article useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
GeneralMy vote of 1 Pin
smton13-Aug-13 12:34
smton13-Aug-13 12:34 
GeneralRe: My vote of 1 Pin
Azim Zahir13-Aug-13 15:54
Azim Zahir13-Aug-13 15:54 
GeneralMy vote of 3 Pin
smton13-Aug-13 6:43
smton13-Aug-13 6:43 
QuestionControlling the MediaPlayer after the video has started playing Pin
Simon Pollock6-Jun-13 3:31
Simon Pollock6-Jun-13 3:31 
AnswerRe: Controlling the MediaPlayer after the video has started playing Pin
Azim Zahir9-Jun-13 17:45
Azim Zahir9-Jun-13 17:45 
GeneralMy vote of 4 Pin
jfriedman13-Apr-13 18:14
jfriedman13-Apr-13 18:14 
GeneralRe: My vote of 4 Pin
Azim Zahir30-Apr-13 3:25
Azim Zahir30-Apr-13 3:25 
QuestionThanks Pin
kmf10-Apr-13 22:28
kmf10-Apr-13 22:28 
AnswerRe: Thanks Pin
Azim Zahir12-Apr-13 22:38
Azim Zahir12-Apr-13 22:38 
GeneralExcellent Pin
Ravi Shankar K27-Mar-13 8:44
Ravi Shankar K27-Mar-13 8:44 
GeneralRe: Excellent Pin
Azim Zahir27-Mar-13 19:16
Azim Zahir27-Mar-13 19:16 
GeneralMy vote of 5 Pin
Ravi Shankar K27-Mar-13 8:42
Ravi Shankar K27-Mar-13 8:42 
QuestionPerfect it ! Pin
H_Zihuatanejo9-Aug-12 21:11
H_Zihuatanejo9-Aug-12 21:11 
Generaljust thank you Pin
Oven0825-Jul-12 22:04
Oven0825-Jul-12 22:04 
GeneralRe: just thank you Pin
Azim Zahir26-Jul-12 3:56
Azim Zahir26-Jul-12 3:56 
QuestionAspect ratio Pin
springy7627-Jun-12 22:05
springy7627-Jun-12 22:05 
AnswerRe: Aspect ratio Pin
Sandeep Mewara25-Jul-12 17:31
mveSandeep Mewara25-Jul-12 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.