Insane666
Freelancing Master
LEVEL 1
200 XP
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...
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...
Binding Lyrics:
Next we want to bind lyrics to the selected song once the user has clicked on the 'lyricsBind' button...
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.
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.
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...
- Private
Sub
Form1_Load(sender As
Object
, e As
EventArgs) Handles MyBase.Load
- Dim
path As
String
= "C:\Users\"
+ Environ("USERNAME"
) + "\Music\"
- For
Each
f As
String
In
My.Computer.FileSystem.GetFiles(path)
- lyricSongList.Items.Add(f.Split("\"
)(f.Split("\"
).Count() - 1))
- Next
- 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...
- Private
Function
removeSpaces(ByVal
s As
String
)
- Dim
ret As
String
- For
Each
c As
string
In
s
- If
Not
(c Is
Nothing
And
c = " "
) Then
- ret += c
- End
If
- Next
- Return ret
- End
Function
Binding Lyrics:
Next we want to bind lyrics to the selected song once the user has clicked on the 'lyricsBind' button...
- Private
Sub
lyricBind_Click(sender As
Object
, e As
EventArgs) Handles lyricBind.Click
- If
(lyricBox.Text > ""
And
lyricSongList.SelectedIndex > -1) Then
- If
(My.Computer.FileSystem.FileExists(path + "\lyricsBinderLyrics\"
+ removeSpaces(lyricSongList.SelectedItem) + ".txt"
)) Then
- 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..."
)
- If
(result = MsgBoxResult.Yes) Then
- Dim
filePath As
String
= path + "\lyricsBinderLyrics\"
+ removeSpaces(lyricSongList.SelectedItem) + ".txt"
- Using sw As
New
System.IO.StreamWriter(filePath)
- sw.Write(lyricBox.Text)
- End
Using
- MsgBox("Overwritten."
)
- ElseIf
(result = MsgBoxResult.No) Then
- MsgBox("Cancelling.."
)
- End
If
- Else
- If
Not
(My.Computer.FileSystem.DirectoryExists(path + "\lyricsBinderLyrics\"
)) Then
- My.Computer.FileSystem.CreateDirectory(path + "\lyricsBinderLyrics\"
)
- End
If
- Dim
filePath As
String
= path + "\lyricsBinderLyrics\"
+ removeSpaces(lyricSongList.SelectedItem) + ".txt"
- Using sw As
New
System.IO.StreamWriter(filePath)
- sw.Write(lyricBox.Text)
- End
Using
- MsgBox("Written."
)
- End
If
- End
If
- 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.
- Private
Sub
lyricPlay_Click(sender As
Object
, e As
EventArgs) Handles lyricPlay.Click
- If
(My.Computer.FileSystem.FileExists(path + "lyricsBinderLyrics\"
+ removeSpaces(lyricSongList.SelectedItem) + ".txt"
)) Then
- System.Diagnostics.Process.Start(path + "lyricsBinderLyrics\"
+ removeSpaces(lyricSongList.SelectedItem) + ".txt"
)
- End
If
- AxWindowsMediaPlayer1.URL = path + lyricSongList.SelectedItem
- 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.