Click here to Skip to main content
14,331,896 members

Audio and Video Player C# Winform

Rate this:
5.00 (20 votes)
Please Sign up or sign in to vote.
5.00 (20 votes)
27 Jan 2015CPOL
In this Article i have explained about how to play Audio/Video and Youtube Vedio in our Windows Application using C#.Add your Audio and Video Files to play list 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 Play list and play the songs or the video file. I have used 2 Com Components to play Audio /Video and for playing the YouTube Video URL. In my project I have used the fallowing Com Components.

  1. Windows Media Player object (Com Component).
  2. Shockwave flash object (Com Component)

Audio/Video Player : 

To play any audio or video files which supported by windows media player can be played in my application. The first and important one 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 the 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 You tube video in our Shockwave Flash Object the You tube 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 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 play list. 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++)
               {
                   listBox1.Items.Add(FileName[i]);
               }


               Startindex = 0;
               playfile(0);
           }
       }

2) In 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)
           {
               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 trigger whenever the player play, pause, Stop and etc .Here I have used this method to check for the Song or video file when plays Finish or end .If the song end 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 return the windows Media Player status where the 8 is the Song or Vedio is 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;
           }

3)  Windows Media Player has the methods like Play, Pause and stop the player.

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.

 

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

History

  • Initial release on 2014/11/20.

License

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

Share

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

 
BugGot Bug Pin
Member 1265933126-Sep-19 21:21
memberMember 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
memberMember 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
memberhabe826-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
memberMember 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
memberMember 1106263725-Feb-15 19:38 
QuestionThank You its Perfect Pin
Mavzer5-Feb-15 23:37
memberMavzer5-Feb-15 23:37 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun27-Jan-15 21:32
memberHumayun Kabir Mamun27-Jan-15 21:32 
Questionabsolutely necessary to use ShockWave plug-in ? Pin
BillWoodruff1-Dec-14 21:54
mveBillWoodruff1-Dec-14 21:54 
Questionrtsp Pin
dommy1A24-Nov-14 4:38
memberdommy1A24-Nov-14 4:38 
AnswerRe: rtsp Pin
syed shanu24-Nov-14 13:53
mvasyed shanu24-Nov-14 13:53 
GeneralRe: rtsp Pin
dommy1A25-Nov-14 0:22
memberdommy1A25-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.

Article
Posted 27 Jan 2015

Stats

130.4K views
15.6K downloads
40 bookmarked