Click here to Skip to main content
Click here to Skip to main content

Using the MediaPlayer in WPF

, 13 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

<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:

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:

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)

Share

About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.
 
Of late I have developed keen interest in WPF and Windows Mobile programming.
 
Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
GeneralMy vote of 1 Pinmembersmton13-Aug-13 12:34 
GeneralRe: My vote of 1 PinmemberAzim Zahir13-Aug-13 15:54 
GeneralMy vote of 3 Pinmembersmton13-Aug-13 6:43 
QuestionControlling the MediaPlayer after the video has started playing PinmemberSimon Pollock6-Jun-13 3:31 
AnswerRe: Controlling the MediaPlayer after the video has started playing PinmemberAzim Zahir9-Jun-13 17:45 
GeneralMy vote of 4 Pinmemberjfriedman13-Apr-13 18:14 
GeneralRe: My vote of 4 PinmemberAzim Zahir30-Apr-13 3:25 
QuestionThanks Pinmemberkmf10-Apr-13 22:28 
AnswerRe: Thanks PinmemberAzim Zahir12-Apr-13 22:38 
GeneralExcellent PinmemberMember 473334127-Mar-13 8:44 
GeneralRe: Excellent PinmemberAzim Zahir27-Mar-13 19:16 
GeneralMy vote of 5 PinmemberMember 473334127-Mar-13 8:42 
QuestionPerfect it ! PinmemberH_Zihuatanejo9-Aug-12 21:11 
Generaljust thank you PinmemberOven0825-Jul-12 22:04 
GeneralRe: just thank you PinmemberAzim Zahir26-Jul-12 3:56 
QuestionAspect ratio Pinmemberspringy7627-Jun-12 22:05 
AnswerRe: Aspect ratio PinmvpSandeep Mewara25-Jul-12 17:31 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 13 Apr 2013
Article Copyright 2012 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid