Search

Rss Posts

Rss Comments

Login

 

Creating a Mp3 Player In VB.net 2008 (Express \ Standard)

Aug 16

A Mp3 player is a hard task, but if you plan to use the Windows Media Player Controls it becomes so much easier, and so much quicker. In this tutorial I shall show you how to build your own basic Mp3 Player. Of course this is not going to oust great clients such as Winamp and Foobar2000, but it’s a good way to get to know using custom COM elements.

Ok let’s get started before I get to sleepy! Open up VB.net (Express Edition is fine) and now click File – New Project. In the new window that shows click Windows Form Application and type the project name to be Mp3Player

image

Click OK. Now in the toolbox (if the toolbox is not showing go to View – Toolbox) right click and select Choose Items from the right click menu. In the new ‘Choose Toolbox Items’ window that shows click the COM Components tab and then scroll down until you find the ‘Windows Media Player’ Control. Click the tickbox next to the name of the control

image

Click OK. Now in the general section of your Toolbox you should have a element called ‘Windows Media Player’ Drag this element onto your application. Depending on your Windows Media Player version you should get something that looks like the below image

image

Now to make things easier for creating your application move the element to the bottom of your application. Because the user won’t see the media player it’s not important about the size of location. It’s at this point you might find it nicer to slightly enlarge the application window (you can do this by clicking on one of the corners of the application and dragging it out). We now need to add a listbox. This will be our playlist editor, where the user will be able to add songs to play. You can add a listbox from the toolbox common controls area. Give this listbox full width of your application and leave above 15\20% at the top of your app. Make sure the listbox is selected then in the right hand (default position) properties section rename the listbox to ‘playlist’. In the properties section Anchor to Top, Left, Bottom and Right!

image

Your application should some what like the above image by this stage. We now need to allow the user to import files to the listbox. This is probably the most complex code of a mp3 player, and still it’s still rather basic code! Drag a button (from toolbox – Common controls) onto your application. Give this button a name of import and a text value in the properties to ‘Import’. You should also Anchor this button to Top, Right. Now once again from the toolbox drag OpenFileDialog from the Dialogs section. Don’t worry nothing will show up on your main application but you will see something like what shows below.

open

Click this button once to bring up the properties in the right menu for the dialog. Rename it importdiag and change the value multiselect to true. In the filter section of the properties for importdiag type the following:

Mp3 Music|*.mp3

The above will make sure the user can only select Mp3 files. Now double click on the ‘Import’ button you created earlier. This should take you into code view. Make sure your mouse cursor is within the Private Sub import_click and then type

importdiag.ShowDialog()

This will in turn show a open file window that Windows will generate for us. It will only show if the user has clicked the import button. Making sure you still have the importdiag button still clicked on the right properties menu click the small lightning bolt. You should now see something somewhat like the below image.

Untitled-4

Double click on the item ‘FileOk’ (The actual text that says FileOK). You should now be in code view. Making sure your mouse is within the Private Sub importdiag_FileOk section, type the following code

For Each track As String In importdiag.FileNames
playlist.Items.Add(track)
Next

This code does the following: It goes though each one of the files the user selected and then adds it to our listbox. Because we’re only trying to create a simple Mp3 player we have not checked if the user is importing a valid filetype, but instead relying on the File Import window working in filtering to show on Mp3 tracks. Ok you can now test out importing Mp3 files. Press F5 to start debugging. Try important a collection of Mp3 files.

Now lets start to play these tracks! Add a button on your application and name it ‘play’, give it a text value of “Play”. Double click on this play button to enter the code view. Making sure your cursor is contained within Private Sub play_Click section type the following

AxWindowsMediaPlayer1.URL = playlist.SelectedItem

This calls our Windows media player item we added at the very start of the tutorial, and plays the selected item in the playlist. Add a new button to the application, this time call it stopbutton and have a text value of ‘Stop’. Double click the stop button and type the following code:

AxWindowsMediaPlayer1.Ctlcontrols.stop()

Once again add another button and this time call it pause with a text value of Pause. Double click it and in the code view type:

AxWindowsMediaPlayer1.Ctlcontrols.pause()

You can now open Mp3 files, play them, stop them and pause them. That’s it for this tutorial but on request I will go into more details with things such as track lengths, video playback and design. Let me know in the comments if you’d like that! I’d love to hear all your feedback! I’m off to bed now, I’m getting very sleepy!

Mp3player

Download The Source Code: Here

37 Comments

Add your comment

  1. Kofi
    Sep 16 at 12:42

    Thanks. You are good.

    How about other file types and videos.

  2. Kofi
    Sep 16 at 12:49

    Wonderful! I just wrote my first media player.

    Can you help with other file types and videos?
    I want to write a player that will play anything.

  3. Nitin
    Oct 02 at 14:10

    The mp3 is working very fine.
    i am trying to select next and previous track can you help me.

  4. Kyle Kinkade
    Oct 03 at 22:40

    Hey, Thanks a ton. I am very new at this, but what I did differently once I got to the end. I completely ignored the Play Pause and Stop buttons. What I did instead was go to the playlist event, and for MouseDoubleClick, placed your Playlist.URL = Playlist.SelectedItem

    The purpose of this though, was because I wanted to use the nicer look of the windows mediaplayer. Is there a reason that you preffered to hide it?

  5. Daryl
    Oct 14 at 12:30

    This tut was very helpfull thanks

  6. Michal
    Oct 15 at 22:01

    Great…Could you be so kind and tell me how to make video player .Thank you very much

  7. austin
    Oct 18 at 14:31

    me too i would like to see more of your stuff. this is pertty good my first media player

  8. Ardiano1
    Oct 19 at 16:44

    Nice…..THnx for thish Tuto…….

  9. scoolj
    Oct 23 at 20:57

    pls can u tell me the step to insert the button into a project.

  10. Dylan
    Oct 24 at 21:34

    dude i would suggest adding a save playlist button, if i find out how i’ll post how on here
    P.S. thank you

  11. Ash
    Oct 27 at 19:53

    Hey. Erm can you make the code work for importdiag.SafeFileNames as it wont play when this code is put instead of importdiag.FileNames ?
    Any Help >?<

  12. Slap
    Nov 09 at 01:34

    Nice tutorial, although I use a logitech USB headset to listen to music, how would I make this player able to locate different output sources and use them?

  13. rakesh
    Dec 28 at 03:10

    ya that was a really nice and easy to implement tutorial but I would also like to play other file types and videos please post those tutorials too

  14. snow
    Dec 28 at 10:19

    Well, thank you very much. Because I think the information about creating this audio player you wrote is in more details. Therefore, as new learners, we study it better.
    But now, I want to create the function of buttons, such as previous, next. At the same time, I need to design the trackbars of songs speed and the time.
    If you see my message and are not that busy, I want to turn to you for help. Thanks a lot. ^-^

  15. Dana Kayyali
    Jan 20 at 15:58

    Hi,
    please how can i add sound in form to vb.net express edition
    like clap

  16. Gingey
    Jan 22 at 00:03

    I have made this and followed your tutorial exept for some reason i cannot select multiple songs to add into the listbox so i have to do it one by one.

    Secondly - the pause button doesnt pause it, instead it stops the song so when the play button is pressed again it plays over.

    Please get back to me dan!

    Great tut btw, keep them coming!!

  17. Gingey
    Jan 22 at 00:05

    Great! Solved the pause problem!

    Now just to add a next, previous and sort that selection out!

    thanks again

  18. Gary Callaghan
    Jan 30 at 15:42

    Hey i was just wondering if i could post this tutorial on my forums ? Of course i will link the tutorial back to here for people to check out your site.

    Hope to hear from you soon

  19. wendel
    Feb 04 at 02:21

    hi, im just a beginner in programming, specially in vb codes.. and i want to xplore more, i’ve learned a lot specially this creating mp3 player.. thanxs a lot..

  20. alzain
    Feb 06 at 02:50

    Awsome man

  21. alzain
    Feb 08 at 01:47

    can you give the same codes for c##?

  22. alzain
    Feb 08 at 01:47

    sorry i mean c#

  23. jason isherwood
    Feb 19 at 08:01

    Nice little tut. Loved it. Problem I have, being new to coding, is how to turn it into a fully fledged executable file. So far I can only run it on VB 2008 through the debug function.

  24. Subodh Nehra
    Feb 20 at 22:41

    Cool. Have not tried it yet but what I see is pretty awesome.

  25. John
    Feb 21 at 22:51

    When Im at the Private Sub “import_click”, an error message appears when I debug.

    It says, ” ‘Items is not a member of ‘AxWMPLib.AxWindowsMediaPlayer’.

    I need an answer.

  26. Rob
    Feb 21 at 23:15

    under some of the subs containing the code “AxWindowsMediaPlayer1″ it should
    actually say: “AxWMPLib.AxWindowsMediaPlayer”
    because it doesn’t recognize the latter code. This was during my experience.

  27. jason
    Mar 04 at 02:36

    An error occurred creating the form. See Exception.InnerException for details. The error is: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure “MyMP3Player.Player.resources” was correctly embedded or linked into assembly “MyMP3Player” at compile time, or that all the satellite assemblies required are loadable and fully signed.

    this is what i get. can anyone help me with this. pls!

  28. Rohan
    Mar 10 at 17:00

    I liked it.How do we print the name of the file excluding its directory

  29. Ddub
    Mar 17 at 19:22

    Great tutorial thanks a ton !

  30. mark
    Apr 01 at 11:56

    Anyone know if you can choose a specific time in a track you want start playing at?

  31. charles
    Apr 09 at 04:00

    thanks, this tutorial helped me a great deal not just for making an mp3 player but also for understand vb more in general

  32. Kev
    Apr 10 at 15:37

    I have the same problem as Gingey when I press pause it just stops. How did you fix it?

    Great tutorial by the way (”,)

  33. Raj
    Apr 13 at 11:35

    Very good tutorial. However, I want to play to mp3 audio files (one of the audio file will be a metronome file for keeping the timing) at the same time and with individual controls. Can you help me with the code for doing that? I would greatly appreciate your help. Thanks!

    Raj.

  34. Paco
    Apr 19 at 17:55

    hey can you show me telling a progressbar that tells you how much the song is ok XD

  35. allan
    Apr 27 at 15:08

    Thank’s for the tutorial, worked great! Just wondering though if you can make a button that will remove the selected track from the playlist? If you or someone else knows how, feel free to share. Thanks.
    Allan.

  36. meedo
    Apr 30 at 11:29

    wonder ful ,but how we can proceed the same
    project without mediaplayer

  37. king
    May 05 at 06:14

    hi thnx it is nice tutorial

Post a comment