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
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
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
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!
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.
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:
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
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.
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
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:
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:
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!
Download The Source Code: Here