Audio, Video and YouTube Video Player in C# Windows Forms


Introduction

The main purpose of this article is to explain how to create a simple audio/video and YouTube video player using C# Windows applications. The user can select an audio or video file and add to the Play List and play the songs or the video file. I have used 2 COM Components to play the audio/video and for playing the YouTube Video URL. In my project I have used the fallowing COM Components.
  • Windows Media Player object (COM Component).
  • Shockwave flash object (COM Component)
Audio/Video Player
 
Any audio or video files supported by the Windows Media Player can be played in my application. Therefore the first and most important one is to add the Windows Media Player COM Component to our project.
 
The following describes how to add the Windows Media Player COM Component to our Windows application:
  • Create your Windows application.
  • From the Tools Windows click Choose Items.
  • Select the COM Components Tab.
  • Search for "Windows Media Player" and click OK.

Now you can see the Windows Media Player will be added to your Tools windows. Just drag and drop the control to your Windows Forms.

Here you can see my Audio/Video Player screen.
 

My Audio/video Player has the following features:

  • Load audio/video File and add to playlist.
  • Play audio/video File
  • Pause audio/video File
  • Stop audio/video File
  • Play previous Song
  • Play Next Song
  • Play First Song of Play List
  • Play Last Song of Play List
YouTube Player

To play any YouTube URL video in our Windows application we can use the Shockwave Flash Object COM Component.
  • How to add Shockwave Flash Object COM Component to our Windows application
  • Create your Windows application
  • From the Tools Windows click Choose Items
  • Select the COM Components Tab
  • Search for "Shockwave Flash Object" and click OK
  
You will then see the Shockwave Flash Object added to your Tools window. Just drag and drop the control to your Windows Forms. Here you can see my YouTube screen.
 
   

Note: To play the YouTube video in our Shockwave Flash Object the YouTube URL should be edited.

For example we have the YouTube URL https://www.youtube.com/watch?v=Ce_Ne5P02q0

To play this video we need to delete "watch?" from the URL and also we need to replace the "=" next to "v" as "/".

So here for example the actual URL should be edited to be like "http://www.youtube.com/v/Ce_Ne5P02q0" .

If we do not edit the URL as in the preceding then it will not play in Shockwave.

Code part

Audio/Video Player Code

Load Audio and Video file to our Play List. Here using the Open File Dialog we can filter all our audio and video files. Add all the file names and paths to the String Array and bind them to the List Box.
  1. //Load Audio or Vedio files   
  2.         private void btnLoadFile_Click(object sender, EventArgs e)  
  3.         {  
  4.             Startindex = 0;  
  5.             playnext = false;  
  6.             OpenFileDialog opnFileDlg = new OpenFileDialog();  
  7.             opnFileDlg.Multiselect = true;  
  8.             opnFileDlg.Filter = "(mp3,wav,mp4,mov,wmv,mpg,avi,3gp,flv)|*.mp3;*.wav;*.mp4;*.3gp;*.avi;*.mov;*.flv;*.wmv;*.mpg|all files|*.*";    
  9.             if (opnFileDlg.ShowDialog() == DialogResult.OK)  
  10.             {  
  11.                 FileName = opnFileDlg.SafeFileNames;  
  12.                 FilePath = opnFileDlg.FileNames;  
  13.                 for (int i = 0; i <= FileName.Length - 1; i++)  
  14.                 {  
  15.                     listBox1.Items.Add(FileName[i]);  
  16.                 }  
  17.   
  18.   
  19.                 Startindex = 0;  
  20.                 playfile(0);  
  21.             }  
  22.         }  
  23.         #endregion  

This method will be called from First, Next, Previous, Last and from the List Box Selected Index Change event with passing the “selectedindex” value. In this method from the array check for the selected file and play using the " WindowsMediaPlayer.URL" as in the following:

  1. // To Play the player  
  2.         public void playfile(int playlistindex)  
  3.         {  
  4.             if (listBox1.Items.Count <= 0)  
  5.             { return; }  
  6.             if (playlistindex < 0)  
  7.             {  
  8.                 return;  
  9.             }  
  10.             WindowsMediaPlayer.settings.autoStart = true;  
  11.             WindowsMediaPlayer.URL = FilePath[playlistindex];  
  12.             WindowsMediaPlayer.Ctlcontrols.next();  
  13.             WindowsMediaPlayer.Ctlcontrols.play();  
  14.         }  
  15.         #endregion  
This is a Windows Media Player event that will be triggered whenever the player plays, pauses, stops and so on. Here I have used this method to check for the song or video file when the playing finishes or ends. If the song ends then I set the  "playnext = true". In my program I used the Timer control that checks for the "playnext = true"status and plays the next song.
  1. #region This is Windows Media Player Event which we can used to fidn the status of the player and do our actions.  
  2.         private void WindowsMediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)  
  3.         {  
  4.             int statuschk = e.newState;  // here the Status return the windows Media Player status where the 8 is the Song or Vedio is completed the playing .  
  5.   
  6.             // Now here i check if the song is completed then i Increment to play the next song  
  7.   
  8.             if (statuschk == 8)  
  9.             {  
  10.                 statuschk = e.newState;  
  11.   
  12.                 if (Startindex == listBox1.Items.Count - 1)  
  13.                 {  
  14.                     Startindex = 0;  
  15.                 }  
  16.                 else if (Startindex >= 0 && Startindex < listBox1.Items.Count - 1)  
  17.                 {  
  18.                     Startindex = Startindex + 1;  
  19.   
  20.                 }  
  21.                 playnext = true;  
  22.             }  
  23.         }  
  24.          #endregion  
The Windows Media Player has the methods like Play, Pause and Stop for the player. 
  1. WindowsMediaPlayer.Ctlcontrols.next();  
  2. WindowsMediaPlayer.Ctlcontrols.play();  
  3. WindowsMediaPlayer.Ctlcontrols.Stop();  
YouTube Video Player: This is a simple and easy to use object. The Shockwave object has a Movie property. Here we can provide our YouTube video to play.
Here in the button click I provide the input of the TextBox to the Shockwave Flash objectmovie property.
  1. private void btnYoutube_Click(object sender, EventArgs e)  
  2.        {  
  3.            ShockwaveFlash.Movie = txtUtube.Text.Trim();  
  4.        }  
Using the Audio and Video Player in our Main Form

I have created the Audio and Video player as the User Control. In my project file you can find both “AudioVedioCntl.cs” and “YouTubeCntl.cs” inside the PlayerControls folder. Create the object for the user controls and add the controls to our form or Panel. In the form load I called a method “LoadPlayerControl”. To this method I passed the parameter as 0 and 1, 0 for adding and displaying the Audio Player to the panel and 1 for adding and displaying the YouTube player.
  1. private void frmMain_Load(object sender, EventArgs e)  
  2.         {  
  3.             LoadPlayerControl(0);  
  4.         }  
  5.   
  6.         private void LoadPlayerControl(int playerType)  
  7.         {  
  8.             pnlMain.Controls.Clear();  
  9.   
  10.             if (playerType == 0)  
  11.             {  
  12.                 SHANUAudioVedioPlayListPlayer.PlayerControls.AudioVedioCntl objAudioVideo = new PlayerControls.AudioVedioCntl();  
  13.                 pnlMain.Controls.Add(objAudioVideo);  
  14.                 objAudioVideo.Dock = DockStyle.Fill;  
  15.             }  
  16.             else  
  17.             {  
  18.                 PlayerControls.YouTubeCntl objYouTube = new PlayerControls.YouTubeCntl();  
  19.                 pnlMain.Controls.Add(objYouTube);  
  20.                 objYouTube.Dock = DockStyle.Fill;  
  21.             }  
  22.         }