Click here to Skip to main content
12,897,242 members (55,787 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 29 Jun 2013

MediaPlayer in WPF

, 1 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip will give you a better idea about creating a custom media player in WPF.


In this tip, I am going to create a custom media player in WPF. First, we set the background color and a static resource for the button background image. Here I use a VisualBrush to fill UI elements. The following code uses a VisualBrush to fill a rectangle. I am using media element as the visual. A visual object usually hosts one container panel such as a Grid or StackPanel and on this container, add 5 buttons for play, forward, backward, stop and open file functionality. We have to set LoadedBehavior="Manual" in the media element.

Using the Code

Here is the XAML code:

    <Style TargetType="Button">
        <Setter Property="Margin" Value="2,4"/>
        <Setter Property="Background">
                <ImageBrush ImageSource=
                "C:\Users\Public\Pictures\Sample Pictures\background.jpg"/>
    <SolidColorBrush x:Key="WindowBrush" Color="Gray"/>
    <Style TargetType="{x:Type Window}">
        <Setter Property="Background" 

        Value="{StaticResource WindowBrush}"/>
<Grid >
    <Rectangle Name="rectangleMedia" Margin="12,25,12,59" 

    RadiusY="7.5" RadiusX="7.5"  Stroke="GhostWhite" 

    StrokeThickness="2" >
            <VisualBrush TileMode="None">
                    <MediaElement Name="videoelement" 

    <Button Content="Play"  HorizontalAlignment="Left" 

    Margin="12,0,0,17" Name="button1" 

    VerticalAlignment="Bottom" Width="75" 

    Click="button1_Click" />
    <Button Content="Stop"  HorizontalAlignment="Left" 

    Margin="93,0,0,17" Name="StopButton" 

    VerticalAlignment="Bottom" Width="75" Click="StopButton_Click"/>
    <Button Content="Forward"  HorizontalAlignment="Left" 

    Margin="174,0,0,17" Name="ForwardButton" 

    VerticalAlignment="Bottom" Width="75" Click="ForwardButton_Click"/>
    <Button Content="Backward"  HorizontalAlignment="Left" 

    Margin="255,0,0,17" Name="BackWard" 

    VerticalAlignment="Bottom" Width="75" Click="BackWard_Click"/>
    <Button Content="Open"  HorizontalAlignment="Left" 

    Margin="340,0,0,17" Name="button2" 

    VerticalAlignment="Bottom" Width="75" Click="button2_Click" />

Here is the C# code:

The below code will open the WMV format file. For getting Openfiledialogue, add a reference to System.Windows.Forms. The filter will help us to open the filter's matching files. Here URI is the uniform resource identifier used to identify and load the file.

private void button2_Click(object sender, RoutedEventArgs e)
    System.Windows.Forms.OpenFileDialog opnDialogue = 
	new System.Windows.Forms.OpenFileDialog();
    opnDialogue.Filter = "Video Files(*.wmv)|*.wmv";
    if (opnDialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        this.videoelement.Source = new Uri(opnDialogue.FileName);
Play and Pause Function

The Play method will play the media which is not active and resume it when paused.

private void button1_Click(object sender, RoutedEventArgs e)
    if (this.button1.Content.ToString() == "Play")
        this.button1.Content = "Pause";
        this.button1.Content = "Play";
Stop Function

Here it will stop the media and reset to play from the beginning:

private void StopButton_Click(object sender, RoutedEventArgs e)
    // The Stop method stops and resets the media to be played from 
    // the beginning.
    this.button1.Content = "Play";
Forward Function

Here, timespan will set the current position of progress through the media's playback time which means the amount of time since the beginning of the media.

private void ForwardButton_Click(object sender, RoutedEventArgs e)
    this.videoelement.Position = this.videoelement.Position + TimeSpan.FromSeconds(10);
Backward Function
private void BackWard_Click(object sender, RoutedEventArgs e)
    this.videoelement.Position = this.videoelement.Position - TimeSpan.FromSeconds(10);


  • First edition on 29th June, 2013

This tip is based on


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


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to design Windows media player in wpf source code in adavance level Pin
Keerthi Viswanathan26-Feb-17 20:21
memberKeerthi Viswanathan26-Feb-17 20:21 
GeneralMy vote of 4 Pin
KaruppasamyP1-Jul-13 18:07
memberKaruppasamyP1-Jul-13 18:07 
GeneralRe: My vote of 4 Pin
josh-jw1-Jul-13 19:37
memberjosh-jw1-Jul-13 19:37 
QuestionRe: My vote of 4 Pin
KaruppasamyP1-Jul-13 23:24
memberKaruppasamyP1-Jul-13 23:24 
AnswerRe: My vote of 4 Pin
josh-jw1-Jul-13 23:51
memberjosh-jw1-Jul-13 23:51 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 1 Jul 2013
Article Copyright 2013 by josh-jw
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid