Playing Audio and Video Files In C#

This article shows to develop a windows application for playing audio and video files using c#.net.

Objective
 
To develop a Windows application for playing audio and video files using c#.net.
 
Design
 
1.gif
 
Design a form as above with an OpenFileDialog contol, one Button and the 'Windows Media Player' contol (COM component).
 
Note that OpenFileDialog control appears below the form (not on the form), which is used in our application for browsing audio/video files.
Steps for adding 'Windows Media Player' control (COM component) into Toolbox:
 
By default, 'Windows Media Player' control is not provided in the Toolbox, we have to add it into the toolbox if required.
Inorder to add 'Windows Media Player' control into toolbox
 
Right click on 'General' tab (or anyother tab) in toolbox ->select 'Choose Items...' ->select 'COM Components' tab ->select 'Windows Media Player' ->click on 'OK' button.
 
2.gif
3.gif
 
'Windows Media Player' control will appear in the toolbox.
 
4.gif
 
Now, drag 'Windows Media Player' control on to the form and place a button on it with text as 'Browse..' as shown in the design.
 
Code 
  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace mymediaplayer  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.         private void btnBrowse_Click(object sender, EventArgs e)  
  13.         {  
  14.             openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";  
  15.             if(openFileDialog1.ShowDialog()==DialogResult.OK)  
  16.                 axWindowsMediaPlayer1.URL = openFileDialog1.FileName;  
  17.         }  
  18.     }  
  19. }  
Output
 
5.gif