Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / Windows Forms

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 179K   18.2K   41   20
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)


Written By
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 6:11
ismaizzy7014-Nov-19 6:11 
BugGot Bug Pin
Member 1265933126-Sep-19 21:21
Member 1265933126-Sep-19 21:21 
Questionit's not clear the button u used..i understand only 3 button u used...play button,load button and play next button....if u can..please upload foto to make it more clear Pin
Member 145232979-Jul-19 9:19
Member 145232979-Jul-19 9:19 
AnswerRe: it's not clear the button u used..i understand only 3 button u used...play button,load button and play next button....if u can..please upload foto to make it more clear Pin
syed shanu9-Jul-19 20:06
mvasyed shanu9-Jul-19 20:06 
Questionno longer supported by YouTube Pin
habe826-Dec-18 4:06
habe826-Dec-18 4:06 
QuestionGood work Pin
san2debug23-Aug-17 20:31
professionalsan2debug23-Aug-17 20:31 
QuestionAwesome Player Pin
Member 118119574-Jul-15 6:57
Member 118119574-Jul-15 6:57 
QuestionShockwave Flash Object" is not showing Pin
Farhan Bajrai30-Apr-15 6:22
professionalFarhan Bajrai30-Apr-15 6:22 
Questionhow to add vlc player in winform of sharpdevelop IDE. Pin
Member 1106263725-Feb-15 19:38
Member 1106263725-Feb-15 19:38 
QuestionThank You its Perfect Pin
Mavzer5-Feb-15 23:37
Mavzer5-Feb-15 23:37 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun27-Jan-15 21:32
Humayun Kabir Mamun27-Jan-15 21:32 
Questionabsolutely necessary to use ShockWave plug-in ? Pin
BillWoodruff1-Dec-14 21:54
professionalBillWoodruff1-Dec-14 21:54 
Questionrtsp Pin
dommy1A24-Nov-14 4:38
dommy1A24-Nov-14 4:38 
AnswerRe: rtsp Pin
syed shanu24-Nov-14 13:53
mvasyed shanu24-Nov-14 13:53 
if Windows media player support to play rtsp it will works.
can you provide me your rtsp link i can check here and if it not works will find for some solution. Smile | :)
GeneralRe: rtsp Pin
dommy1A25-Nov-14 0:22
dommy1A25-Nov-14 0:22 
GeneralRe: rtsp Pin
syed shanu25-Nov-14 13:20
mvasyed shanu25-Nov-14 13:20 
GeneralMy vote of 5 Pin
Sheepings20-Nov-14 5:13
professionalSheepings20-Nov-14 5:13 
GeneralRe: My vote of 5 Pin
syed shanu20-Nov-14 15:14
mvasyed shanu20-Nov-14 15:14 
GeneralMy Vote of Five Pin
aarif moh shaikh20-Nov-14 1:58
professionalaarif moh shaikh20-Nov-14 1:58 
GeneralRe: My Vote of Five Pin
syed shanu20-Nov-14 15:15
mvasyed shanu20-Nov-14 15: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.