• We just launched and are currently in beta. Join us as we build and grow the community.

Custom Playlist Handler in Visual Basic - Part 1 - Creating Playlists

CrackIt™

Release Engineer
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
31
Likes
135
Bits
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...

  1. 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...

  1. Dim

    username As

    String

    = Environ("username"

    )

Now we can use this username to create our default windows music library path...

  1. 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...

  1. Dim

    dirs As

    New

    List(Of String

    )
  2. For

    Each

    d As

    String

    In

    My.Computer.FileSystem.GetDirectories(musicPath)
  3. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetFiles(d)
  4. ListBox1.Items.Add(f.Substring(musicPath.Count, f.Count - musicPath.Count))
  5. Next
  6. Next
  7. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetFiles(musicPath)
  8. ListBox1.Items.Add(f.Substring(musicPath.Count, f.Count - musicPath.Count))
  9. Next
  10. 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...

  1. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  2. Dim

    username As

    String

    = Environ("username"

    )
  3. Dim

    musicPath As

    String

    = "C:\users\"

    + username + "\music"
  4. If

    Not

    (My.Computer.FileSystem.DirectoryExists(musicPath + "/player"

    )) Then

    My.Computer.FileSystem.CreateDirectory(musicPath + "/player"

    )
  5. File.Create(musicPath + "/player/"

    + TextBox1.Text + ".txt"

    )
  6. updateLists()
  7. 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...

  1. Private

    Sub

    Button3_Click(sender As

    Object

    , e As

    EventArgs) Handles Button3.Click
  2. Dim

    username As

    String

    = Environ("username"

    )
  3. Dim

    musicPath As

    String

    = "C:\users\"

    + username + "\music\player\"
  4. If

    (My.Computer.FileSystem.FileExists(musicPath + TextBox1.Text)) Then

    My.Computer.FileSystem.DeleteFile(musicPath + TextBox1.Text)
  5. updateLists()
  6. 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...

  1. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  2. If

    (ListBox1.SelectedIndex > -1 And

    ListBox2.SelectedIndex > -1) Then
  3. If

    (My.Computer.FileSystem.FileExists(ListBox2.SelectedItem)) Then
  4. MsgBox(ListBox2.SelectedItem.ToString())
  5. Using sw As

    New

    StreamWriter(ListBox2.SelectedItem.ToString())
  6. sw.WriteLine(ListBox1.SelectedItem.ToString())
  7. End

    Using
  8. Else

    : MsgBox("Playlist gone."

    )
  9. End

    If
  10. End

    If
  11. 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...

  1. Private

    Function

    updateLists()
  2. Dim

    username As

    String

    = Environ("username"

    )
  3. Dim

    musicPath As

    String

    = "C:\users\"

    + username + "\music\player\"
  4. ListBox2.Items.Clear()
  5. For

    Each

    s As

    String

    In

    My.Computer.FileSystem.GetFiles(musicPath)
  6. ListBox2.Items.Add(s)
  7. Next
  8. End

    Function

Then we call it using;

  1. updateLists()

after we add or delete a playlist.

 

452,292

324,736

324,744

Top