Click here to Skip to main content
15,170,331 members
Articles / Desktop Programming / Windows Forms
Posted 27 Jan 2015

Tagged as


41 bookmarked

Audio and Video Player C# Winform

Rate me:
Please Sign up or sign in to vote.
4.98/5 (22 votes)
27 Jan 2015CPOL3 min read
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


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 "".

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 "" .

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.
    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++)
                   Startindex = 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".
    public void playfile(int playlistindex)
               if (listBox1.Items.Count <= 0)
               { return; }
               if (playlistindex < 0)
               WindowsMediaPlayer.settings.autoStart = true;
               WindowsMediaPlayer.URL = FilePath[playlistindex];
  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.
    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.

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.

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


  • 20th November, 2014: Initial release


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


About the Author

syed shanu
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

Questionstart playing video at a specific moment Pin
ismaizzy7014-Nov-19 7:11
Memberismaizzy7014-Nov-19 7:11 

Thanks for your article.

I need to index video by topic and when I click on topic, video have to start playing in these part of the video.
An example, I know that the topic sports start at 23rd minute of the video, when I click de topic the player has to start at 23rd minute and not at the begining

I am sorry for my english, I don't know if you understand me

Can you help me? please
Thanks in advance

Best regards
BugGot Bug Pin
Member 1265933126-Sep-19 22:21
MemberMember 1265933126-Sep-19 22:21 
Questionit's not clear the button u used..i understand only 3 button u button,load button and play next button....if u can..please upload foto to make it more clear Pin
Member 145232979-Jul-19 10:19
MemberMember 145232979-Jul-19 10:19 
AnswerRe: it's not clear the button u used..i understand only 3 button u button,load button and play next button....if u can..please upload foto to make it more clear Pin
syed shanu9-Jul-19 21:06
mvasyed shanu9-Jul-19 21:06 
Questionno longer supported by YouTube Pin
habe826-Dec-18 5:06
Memberhabe826-Dec-18 5:06 
QuestionGood work Pin
san2debug23-Aug-17 21:31
professionalsan2debug23-Aug-17 21:31 
QuestionAwesome Player Pin
Member 118119574-Jul-15 7:57
MemberMember 118119574-Jul-15 7:57 
QuestionShockwave Flash Object" is not showing Pin
Farhan Bajrai30-Apr-15 7:22
professionalFarhan Bajrai30-Apr-15 7:22 
Questionhow to add vlc player in winform of sharpdevelop IDE. Pin
Member 1106263725-Feb-15 20:38
MemberMember 1106263725-Feb-15 20:38 
QuestionThank You its Perfect Pin
Mavzer6-Feb-15 0:37
MemberMavzer6-Feb-15 0:37 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun27-Jan-15 22:32
MemberHumayun Kabir Mamun27-Jan-15 22:32 
Questionabsolutely necessary to use ShockWave plug-in ? Pin
BillWoodruff1-Dec-14 22:54
mveBillWoodruff1-Dec-14 22:54 
Questionrtsp Pin
dommy1A24-Nov-14 5:38
Memberdommy1A24-Nov-14 5:38 
AnswerRe: rtsp Pin
syed shanu24-Nov-14 14:53
mvasyed shanu24-Nov-14 14:53 
GeneralRe: rtsp Pin
dommy1A25-Nov-14 1:22
Memberdommy1A25-Nov-14 1:22 
GeneralRe: rtsp Pin
syed shanu25-Nov-14 14:20
mvasyed shanu25-Nov-14 14:20 
GeneralMy vote of 5 Pin
Sheepings20-Nov-14 6:13
professionalSheepings20-Nov-14 6:13 
GeneralRe: My vote of 5 Pin
syed shanu20-Nov-14 16:14
mvasyed shanu20-Nov-14 16:14 
GeneralMy Vote of Five Pin
aarif moh shaikh20-Nov-14 2:58
professionalaarif moh shaikh20-Nov-14 2:58 
GeneralRe: My Vote of Five Pin
syed shanu20-Nov-14 16:15
mvasyed shanu20-Nov-14 16:15 

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.