Multimedia In WPF

Introduction

 
We all are surrounded by media, and use it on a daily basis. Whether it be listening to Spotify or watching youtube or reading on Amazon kindle. This all are media types carrying some information. The media needs some kind of UI to control them. WPF has media control which used to Play, Pause, Stop, Volume, and Speed up or down media. let's play video of my cat. His name is Tango, if case you were wondering.
 
So, we will manual controls in our video, 3 buttons, one is for play, another for pause, and the last one is a toggle button for mute & unmute. The buttons must be rounded to have the feel of media control buttons, so let's create a ControlTemplate for them:
  1. <ControlTemplate  x:Key="MediaButtons" TargetType="Button">    
  2.             <Grid>    
  3.                 <Ellipse Stroke="Transparent" StrokeThickness="0">    
  4.                     <Ellipse.Fill>    
  5.                         <RadialGradientBrush>    
  6.                             <GradientStop Offset="0"  Color="LightGray" />    
  7.                             <GradientStop Offset="1"  Color="DarkGray" />    
  8.                             <GradientStop Offset="1"  Color="Gray" />    
  9.                             <RadialGradientBrush.Transform>    
  10.                                 <TransformGroup>    
  11.                                     <ScaleTransform ScaleY="0.65" />    
  12.                                 </TransformGroup>    
  13.                             </RadialGradientBrush.Transform>    
  14.                         </RadialGradientBrush>    
  15.                     </Ellipse.Fill>    
  16.                 </Ellipse>    
  17.                 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>    
  18.             </Grid>    
  19.         </ControlTemplate>     
Now our MediaElement and our 3 buttons are lined up, one after another.
  1. <Grid>    
  2.         <Grid.RowDefinitions>    
  3.             <RowDefinition Height="Auto"/>    
  4.             <RowDefinition Height="Auto"/>    
  5.         </Grid.RowDefinitions>    
  6.         <MediaElement x:Name="TangosVideo"    
  7.                       Height="200"    
  8.                       Width="200"    
  9.                       Source="C:\Users\Rikam\Videos\Tango.mp4"    
  10.                       LoadedBehavior="Manual"    
  11.                       />    
  12.         <StackPanel     
  13.             Orientation = "Horizontal"     
  14.             HorizontalAlignment="Center"    
  15.             Margin = "0,10,0,0"    
  16.             Grid.Row="1">    
  17.             <Button x:Name="PlayButton"    
  18.                 Background="Transparent"    
  19.                 Margin = "0,0,10,0"     
  20.                 Padding = "5"    
  21.                 Click = "PlayVideo"    
  22.                 Content="Play"    
  23.                 Height="40"     
  24.                 Width="40"    
  25.                 Template="{StaticResource MediaButtons}">    
  26.             </Button>    
  27.             <Button x:Name="PauseButton"    
  28.                 Content = "Pause"     
  29.                 Background="Transparent"    
  30.                 Margin = "0,0,10,0"     
  31.                 Padding = "5"     
  32.                 Click = "PauseVideo"     
  33.                 Height="40"     
  34.                 Width="40"    
  35.                 Template="{StaticResource MediaButtons}"/>    
  36.             <Button x:Name = "MuteButton"    
  37.                 Background="Transparent"    
  38.                 Content = "Mute"    
  39.                 Padding = "5"    
  40.                 Click = "MuteVideo"     
  41.                 Height="40"     
  42.                 Width="40"    
  43.                 Template="{StaticResource MediaButtons}"/>    
  44.         </StackPanel>    
  45. </Grid>     
At compile time you can see than Clickbait has is loaded already.
 
Multimedia In WPF
 
Now time to add some brain to our UI, which is the behavior of these controls.
 
Jump to the code behind and update it as follows:
  1. using System;  
  2. using System.Threading;  
  3. using System.Windows;  
  4.   
  5. namespace A  
  6. {  
  7.     /// <summary>  
  8.     /// Interaction logic for MainWindow.xaml  
  9.     /// </summary>  
  10.     public partial class MainWindow : Window  
  11.     {  
  12.         public MainWindow()  
  13.         {  
  14.             InitializeComponent();  
  15.             //setting up 80% volume  
  16.             TangosVideo.Volume = 100;  
  17.   
  18.             //starts playing as soo as page loads  
  19.             TangosVideo.Play();  
  20.         }  
  21.         void PlayVideo(Object sender, EventArgs e)  
  22.         {  
  23.             TangosVideo.Play();  
  24.         }  
  25.   
  26.         void PauseVideo(Object sender, EventArgs e)  
  27.         {  
  28.             TangosVideo.Pause();  
  29.         }  
  30.         void MuteVideo(Object sender, EventArgs e)  
  31.         {  
  32.   
  33.             if (TangosVideo.Volume == 100)  
  34.             {  
  35.                 TangosVideo.Volume = 0;  
  36.                 MuteButton.Content = "UnMute";  
  37.             }  
  38.             else  
  39.             {  
  40.                 TangosVideo.Volume = 100;  
  41.                 MuteButton.Content = "Mute";  
  42.             }  
  43.         }  
  44.     }  
  45. }  
Pretty simple to understand, so no need to explain.

Now run this app and see that the video running.

I am uploading the GIF so I can't really play music but believe me sound mutes and unmutes.
 
Multimedia In WPF
 
It works with audio as well, just replace the source.
  1. <MediaElement x:Name="TangosVideo"    
  2.                       Height="200"    
  3.                       Width="200"    
  4.                       Source="C:\Users\Rikam\Music\Sing For The Moment.mp3"    
  5.                       LoadedBehavior="Manual">    
  6. </MediaElement>    
And that's how you roll. You can do a lot of cool stuff with MediaElements.
 
I hope this article has helped you to gain some useful knowledge.
 
If you wish to connect, feel free to meet me @
And as always, Happy Coding!