CrackIt™
Release Engineer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Introduction:
This is the first of a two part tutorial on how to create a custom music playlist and player in Visual Basic.
Note:
Our Visual Basic player will simply use the Windows Media Player control and this series is going to be mainly focusing on the IO. If you're only interested in custom music players, it may save you time by skipping this series.
This Tutorial:
This part will be covering the form design, music files list, and creating playlists.
Imports:
First we need to import System.IO to read/write to/from files later on, we do this like so...
Design:
Our form design will consist of;
listbox1 - to hold all the music files found in the music directory.
listbox2 - to hold all the current playlists.
button1 - create playlist
button2 - add song to playlist.
button3 - delete playlist.
textbox1 - playlist name.
Form Load:
Once the form has loaded we first want to load all the music files from our main music directory. So first we need to get the username of the currently logged in user, we can get this through...
Now we can use this username to create our default windows music library path...
Next we want to iterate through each found file in both the main directory and each found directory within the main directory, and add the (if applicable, additional directory name(s)) and file name to the listbox1...
Create Playlist:
Next we want to create our 'create playerlist' button function. This will simply create a .txt file in a directory, our default directory will be the windows music path followed by '/player/'. We accept the name in textbox1 as the playlist name...
Delete Playlist:
For deleting a playlist, we first get the listbox2 selected item (playlist name), ensure that the file exists in our default playlist files directory ('music directory/player') and if it does, we delete it...
Adding Songs:
For adding songs, we first ensure that a valid song and playlist is selected, and that the playlist file exists, we then write the song name to the file using a new StreamWriter...
Updating Playlists:
Finally we need to update our listbox2 with all the playlists files names from our default playlist files directory each time we make changes, so first we create the function to iterate through each file and add the filename (not full directory) to the listbox2 items collection...
Then we call it using;
after we add or delete a playlist.
This is the first of a two part tutorial on how to create a custom music playlist and player in Visual Basic.
Note:
Our Visual Basic player will simply use the Windows Media Player control and this series is going to be mainly focusing on the IO. If you're only interested in custom music players, it may save you time by skipping this series.
This Tutorial:
This part will be covering the form design, music files list, and creating playlists.
Imports:
First we need to import System.IO to read/write to/from files later on, we do this like so...
- Imports System.IO
Design:
Our form design will consist of;
listbox1 - to hold all the music files found in the music directory.
listbox2 - to hold all the current playlists.
button1 - create playlist
button2 - add song to playlist.
button3 - delete playlist.
textbox1 - playlist name.
Form Load:
Once the form has loaded we first want to load all the music files from our main music directory. So first we need to get the username of the currently logged in user, we can get this through...
- Dim
username As
String
= Environ("username"
)
Now we can use this username to create our default windows music library path...
- Dim
musicPath As
String
= "C:\users\"
+ username + "\music"
Next we want to iterate through each found file in both the main directory and each found directory within the main directory, and add the (if applicable, additional directory name(s)) and file name to the listbox1...
- Dim
dirs As
New
List(Of String
)
- For
Each
d As
String
In
My.Computer.FileSystem.GetDirectories(musicPath)
- For
Each
f As
String
In
My.Computer.FileSystem.GetFiles(d)
- ListBox1.Items.Add(f.Substring(musicPath.Count, f.Count - musicPath.Count))
- Next
- Next
- For
Each
f As
String
In
My.Computer.FileSystem.GetFiles(musicPath)
- ListBox1.Items.Add(f.Substring(musicPath.Count, f.Count - musicPath.Count))
- Next
- updateLists()
Create Playlist:
Next we want to create our 'create playerlist' button function. This will simply create a .txt file in a directory, our default directory will be the windows music path followed by '/player/'. We accept the name in textbox1 as the playlist name...
- Private
Sub
Button1_Click(sender As
Object
, e As
EventArgs) Handles Button1.Click
- Dim
username As
String
= Environ("username"
)
- Dim
musicPath As
String
= "C:\users\"
+ username + "\music"
- If
Not
(My.Computer.FileSystem.DirectoryExists(musicPath + "/player"
)) Then
My.Computer.FileSystem.CreateDirectory(musicPath + "/player"
)
- File.Create(musicPath + "/player/"
+ TextBox1.Text + ".txt"
)
- updateLists()
- End
Sub
Delete Playlist:
For deleting a playlist, we first get the listbox2 selected item (playlist name), ensure that the file exists in our default playlist files directory ('music directory/player') and if it does, we delete it...
- Private
Sub
Button3_Click(sender As
Object
, e As
EventArgs) Handles Button3.Click
- Dim
username As
String
= Environ("username"
)
- Dim
musicPath As
String
= "C:\users\"
+ username + "\music\player\"
- If
(My.Computer.FileSystem.FileExists(musicPath + TextBox1.Text)) Then
My.Computer.FileSystem.DeleteFile(musicPath + TextBox1.Text)
- updateLists()
- End
Sub
Adding Songs:
For adding songs, we first ensure that a valid song and playlist is selected, and that the playlist file exists, we then write the song name to the file using a new StreamWriter...
- Private
Sub
Button2_Click(sender As
Object
, e As
EventArgs) Handles Button2.Click
- If
(ListBox1.SelectedIndex > -1 And
ListBox2.SelectedIndex > -1) Then
- If
(My.Computer.FileSystem.FileExists(ListBox2.SelectedItem)) Then
- MsgBox(ListBox2.SelectedItem.ToString())
- Using sw As
New
StreamWriter(ListBox2.SelectedItem.ToString())
- sw.WriteLine(ListBox1.SelectedItem.ToString())
- End
Using
- Else
: MsgBox("Playlist gone."
)
- End
If
- End
If
- End
Sub
Updating Playlists:
Finally we need to update our listbox2 with all the playlists files names from our default playlist files directory each time we make changes, so first we create the function to iterate through each file and add the filename (not full directory) to the listbox2 items collection...
- Private
Function
updateLists()
- Dim
username As
String
= Environ("username"
)
- Dim
musicPath As
String
= "C:\users\"
+ username + "\music\player\"
- ListBox2.Items.Clear()
- For
Each
s As
String
In
My.Computer.FileSystem.GetFiles(musicPath)
- ListBox2.Items.Add(s)
- Next
- End
Function
Then we call it using;
- updateLists()
after we add or delete a playlist.