Lyrical Binder in Visual Basic

Introduction: This tutorial is going to be on how to create a lyrical binder for music files. This program will very simply allow the user to select a music file already contained within their computers filesystem, enter lyrics for the song and save the bind. Then whenever they run the song from the program, the lyrics file will also open. Design: We need the program to have the following components... COMPONENT TYPE - COMPONENT NAME - DEFAULT TEXT Textbox lyricBox '' Button lyricBind 'Bind Lyrics' Listbox lyricSongList {song names found in given directory} Getting a Song List: First we want the listbox to be populated with the users music. So when the form loads, we run through each file within the default music library for the given Windows user and add each ones name (with .mp3 extension) to the listbox...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.     Dim path As String = "C:\Users\" + Environ("USERNAME") + "\Music\"
  3.     For Each f As String In My.Computer.FileSystem.GetFiles(path)
  4.         lyricSongList.Items.Add(f.Split("\")(f.Split("\").Count() - 1))
  5.     Next
  6. End Sub
Space Problems: When we try to call a file later on, spaces in the file name(s) may cause problems so lets remove the spaces by creating a function and parsing the data before we use it each time...
  1. Private Function removeSpaces(ByVal s As String)
  2.     Dim ret As String
  3.     For Each c As string In s
  4.         If Not (c Is Nothing And c = " ") Then
  5.             ret += c
  6.         End If
  7.         Next
  8.     Return ret
  9. End Function
Binding Lyrics: Next we want to bind lyrics to the selected song once the user has clicked on the 'lyricsBind' button...
  1. Private Sub lyricBind_Click(sender As Object, e As EventArgs) Handles lyricBind.Click
  2.     If (lyricBox.Text > "" And lyricSongList.SelectedIndex > -1) Then
  3.         If (My.Computer.FileSystem.FileExists(path + "\lyricsBinderLyrics\" + removeSpaces(lyricSongList.SelectedItem) + ".txt")) Then
  4.             Dim result As MsgBoxResult = MsgBox("A lyrics file has already been binded with this song, are you sure you wish to overwrite the lyrics?", MsgBoxStyle.YesNo, "Binding Lyrics...")
  5.                         If (result = MsgBoxResult.Yes) Then
  6.                         Dim filePath As String = path + "\lyricsBinderLyrics\" + removeSpaces(lyricSongList.SelectedItem) + ".txt"
  7.                    Using sw As New System.IO.StreamWriter(filePath)
  8.                         sw.Write(lyricBox.Text)
  9.                 End Using
  10.                 MsgBox("Overwritten.")
  11.                 ElseIf (result = MsgBoxResult.No) Then
  12.                 MsgBox("Cancelling..")
  13.                 End If
  14.         Else
  15.             If Not (My.Computer.FileSystem.DirectoryExists(path + "\lyricsBinderLyrics\")) Then
  16.                 My.Computer.FileSystem.CreateDirectory(path + "\lyricsBinderLyrics\")
  17.             End If
  18.             Dim filePath As String = path + "\lyricsBinderLyrics\" + removeSpaces(lyricSongList.SelectedItem) + ".txt"
  19.             Using sw As New System.IO.StreamWriter(filePath)
  20.                 sw.Write(lyricBox.Text)
  21.             End Using
  22.             MsgBox("Written.")
  23.         End If
  24.     End If
  25. End Sub
Playing Songs: Finally we want to create a way for the user to play the music when the lyrics open as well. Just add another button named 'lyricPlay' and add the click code as... Before we can use the Windows Media Player addon to play the music, we need to add a reference to it (and essentially import the component). So, go to Project > Add Reference... Click COM and find 'Windows Media Player' then tick the box next to it. Once you have checked it, click OK and it should import the component. Then right click within the toolbox, click on 'Choose Items...', click on the 'COM' tab, and select 'Windows Media Player' again. Click OK. You should now see a 'Windows Media Player' component within your toolbox, finally! Add one to the form , you may want to resize it so that it only shows the controls and not the video section.
  1. Private Sub lyricPlay_Click(sender As Object, e As EventArgs) Handles lyricPlay.Click
  2.     If (My.Computer.FileSystem.FileExists(path + "lyricsBinderLyrics\" + removeSpaces(lyricSongList.SelectedItem) + ".txt")) Then
  3.         System.Diagnostics.Process.Start(path + "lyricsBinderLyrics\" + removeSpaces(lyricSongList.SelectedItem) + ".txt")
  4.     End If
  5.     AxWindowsMediaPlayer1.URL = path + lyricSongList.SelectedItem
  6. End Sub
If the file for lyrics exists, it opens it. It also sets the URL of the Windows Media Player control/component to the selected item (the audio file) which automatically begins playing as well.

Add new comment