How to Create a Music Player in Visual Basic

Introduction: Welcome to my tutorial on how to create a music player in Visual Basic. Important: Before doing this you must already have the Windows Media Player control in your visual basic application form. If it is not in your control box follow these steps; - Open Toolbox - Right Click, select Choose Items - Click on COM Components - Add Windows Media Player - Add the media player from your toolbox to your form. Steps of Creation: Step 1: First lets create a form with; music player - play the music, listbox1 - contain the music files in the loaded directory, button1 - load textbox1 directory, textbox1 - hold current/new directory, button2 - play selected music file from listbox1, button3 - open laoded directory. We also need to make a string to hold the loaded location.
  1. Dim loadedLocation As String = Nothing
Step 2: Next, we will make the button1 click event action which will load all the .mp3 files in the given directory to our listbox1.
  1. ListBox1.Items.Clear()
  2. For Each foundFile As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  3. If (foundFile.ToLower().EndsWith(".mp3")) Then ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  4. Next
  5. loadedLocation = TextBox1.Text
We also set the loadedLocation string to the new directory so we can open it later. Step 3: For the button2 action we simply set the url of the media player control to the selected item in listbox1. It will play automatically.
  1. If (Not ListBox1.SelectedItem = Nothing) Then AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
Step 4: Finally we want to make the open directory button. Simple.
  1. Diagnostics.Process.Start(loadedLocation)
Project Complete! Below is the full source code and download to the project files.
  1. Public Class Form1
  2. Dim loadedLocation As String = Nothing
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. ListBox1.Items.Clear()
  5. For Each foundFile As String In My.Computer.FileSystem.GetFiles(TextBox1.Text)
  6. If (foundFile.ToLower().EndsWith(".mp3")) Then ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  7. Next
  8. loadedLocation = TextBox1.Text
  9. End Sub
  10.  
  11. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  12. If (Not ListBox1.SelectedItem = Nothing) Then AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
  13. End Sub
  14.  
  15. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  16. Diagnostics.Process.Start(loadedLocation)
  17. End Sub
  18. End Class

Comments

Submitted byMax Schindler (not verified)on Wed, 02/03/2021 - 03:59

Doesn't work. Don't waste your time.

Add new comment