Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Audio and Video Player C# Winform

4.98/5 (22 votes)
27 Jan 2015CPOL3 min read 184.5K   18.4K  
How to play Audio/Video and Youtube Video in Windows App using C#
In this article, I explain how to play Audio/Video and Youtube Video in our Windows application using C#. Add your Audio and Video Files to playlist and play it from your WinForm. Paste your Youtube URL and play it from your WinForm.

Image 1

Introduction

The main purpose of this article is to explain how to create a simple Audio/Video and YouTube Video for Windows applications using C#. User can select Audio or Video file and add to the Playlist and play the songs or the video file. I have used two Com Components to play Audio /Video and for playing the YouTube Video URL. In my project, I have used the following Com Components:

  1. Windows Media Player object
  2. Shockwave flash object

Audio/Video Player

Any audio or video files which are supported by Windows Media player can be played in my application. The first and important thing is we need to add the Windows Media Player Com Component to our project.

How to add Windows Media Player Com Component to our Windows Application?

  1. Create your Windows Application.
  2. From Tools Windows, click Choose Items.
  3. Select Com Components Tab.
  4. Search for "Windows Media Player" and click OK.

Image 2

Now you can see that Windows Media player will be added in your Tools windows. Just drag and drop the control to your winform.

Here, you can see my Audio/Video Player screen:

Image 3

My audio/video player has features like:

  • 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 Shockwave Flash Object Com Component.
How to add Shockwave Flash Object Com Component to our Windows Application?

  1. Create your Windows Application.
  2. From Tools Windows, click Choose Items.
  3. Select Com Components Tab.
  4. Search for "Shockwave Flash Object" and click OK.

Image 4

Now you can see the Shockwave Flash Object will be added in your Tools windows. Just drag and drop the control to your winform.

Here, you can see my YouTube screen.

Image 5

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

For example, we have 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 about actual URL should be edited like this "http://www.youtube.com/v/Ce_Ne5P02q0" .

If we do not edit the url like above, it will not play in the Shockwave.

Using the Code

Audio/Video Player Code

  1. Load Audio and Video file to our playlist. Here, using the Open File Dialog, we can filter all our Audio and Video files. Add all the File name and path to String Array and bind to the List Box.
    C#
    private void btnLoadFile_Click(object sender, EventArgs e)
           {
               Startindex = 0;
               playnext = false;
               OpenFileDialog opnFileDlg = new OpenFileDialog();
               opnFileDlg.Multiselect = true;
               opnFileDlg.Filter = "(mp3,wav,mp4,mov,wmv,mpg,avi,3gp,flv)|*.mp3;
                          *.wav;*.mp4;*.3gp;*.avi;*.mov;*.flv;*.wmv;*.mpg|all files|*.*";
               if (opnFileDlg.ShowDialog() == DialogResult.OK)
               {
                   FileName = opnFileDlg.SafeFileNames;
                   FilePath = opnFileDlg.FileNames;
                   for (int i = 0; i <= FileName.Length - 1; i++)
                   {
                       listBox1.Items.Add(FileName[i]);
                   }
    
                   Startindex = 0;
                   playfile(0);
               }
           }
    
  2. This method will be called from First, Next, Previous, Last and from 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".
    C#
    public void playfile(int playlistindex)
           {
               if (listBox1.Items.Count <= 0)
               { return; }
               if (playlistindex < 0)
               {
                   return;
               }
               WindowsMediaPlayer.settings.autoStart = true;
               WindowsMediaPlayer.URL = FilePath[playlistindex];
               WindowsMediaPlayer.Ctlcontrols.next();
               WindowsMediaPlayer.Ctlcontrols.play();
           }
    
  3. Windows Media Player “PlayStateChange” event: This is Windows Media Player event which will be triggered whenever the player plays, pauses, stops, etc. Here, I have used this method to check for the Song or video file when plays Finish or end. If the song ends, then I set the "playnext = true". In my program, I have used the Timer control which will check for the "playnext = true" status and plays the next song.
    C#
    private void WindowsMediaPlayer_PlayStateChange
           (object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
           {
               int statuschk = e.newState;  // here, the Status returns the
                                            // Windows Media Player status where the 8 is
                                            // the Song or Video has completed the playing.
    
               // Now here, I check if the song is completed
               // then I Increment to play the next song
    
               if (statuschk == 8)
               {
                   statuschk = e.newState;
    
                   if (Startindex == listBox1.Items.Count - 1)
                   {
                       Startindex = 0;
                   }
                   else if (Startindex >= 0 && Startindex < listBox1.Items.Count - 1)
                   {
                       Startindex = Startindex + 1;
                   }
                   playnext = true;
               }
    
  4. Windows Media Player has methods like play, pause and stop the player.
    C#
    WindowsMediaPlayer.Ctlcontrols.play();
    WindowsMediaPlayer.Ctlcontrols.pause();
    WindowsMediaPlayer.Ctlcontrols.stop();
    

Youtube Video Player: This is simple and easy to use object. The Shockwave object has Movie property. Here, we can give our YouTube Video to play.
Here in button click, I give the input of textbox to Shockwave Flash object movie property.

C#
private void btnYoutube_Click(object sender, EventArgs e)
       {
           ShockwaveFlash.Movie = txtUtube.Text.Trim();
       }

History

  • 20th November, 2014: Initial release

License

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