Rainbow Fresh
GIF Artist
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Introduction:
This is the second 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 loading and playing of playlists.
Components:
You will want one button to begin the loading process/function, and a Windows Media Player component as well, named 'AxWindowsMediaPlayer1'. To add the component, you may need to add the reference by going to:
Project > Add Reference > COM Components > Windows Media Player
Imports:
First we need to import System.IO to read/write to/from files later on, we do this like so...
Loading Playlists:
All of this code can go on one simple button click.
First we want to allow the user to select a .txt playlist file so we create a new openfiledialog, set the filter and properties and then display.
Next we verify that the selected file exists, and read the lines to a new list(of string) named 'lines'.
Finally we then want to iterate through each string line in our 'lines' string list, convert each one to the correct object format with the full music path, and add it to a new playlist of the correct object type for the windows media player playlist. We then set the current playist to the newly created playlist, it will automatically begin to play.
Running the Function:
On the button click we simply call the function...
This is the second 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 loading and playing of playlists.
Components:
You will want one button to begin the loading process/function, and a Windows Media Player component as well, named 'AxWindowsMediaPlayer1'. To add the component, you may need to add the reference by going to:
Project > Add Reference > COM Components > Windows Media Player
Imports:
First we need to import System.IO to read/write to/from files later on, we do this like so...
- Imports System.IO, WMPLib
Loading Playlists:
All of this code can go on one simple button click.
First we want to allow the user to select a .txt playlist file so we create a new openfiledialog, set the filter and properties and then display.
Next we verify that the selected file exists, and read the lines to a new list(of string) named 'lines'.
Finally we then want to iterate through each string line in our 'lines' string list, convert each one to the correct object format with the full music path, and add it to a new playlist of the correct object type for the windows media player playlist. We then set the current playist to the newly created playlist, it will automatically begin to play.
- Private
Function
loadPlaylist()
- Dim
username As
String
= Environ("username"
)
- Dim
musicPath As
String
= "C:\users\"
+ username + "\music"
- Dim
Playlist As
IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("thePlaylist"
)
- Dim
path As
String
= Nothing
- Using fo As
New
OpenFileDialog
- fo.Filter = "Text Files | *.txt"
- fo.FilterIndex = 1
- fo.RestoreDirectory = True
- fo.Multiselect = False
- fo.ShowDialog()
- path = fo.FileName
- End
Using
- Dim
lines As
New
List(Of String
)
- Using sr As
New
StreamReader(path)
- While
(sr.Peek <> -1)
- lines.Add(sr.ReadLine())
- End
While
- End
Using
- For
Each
FilePath As
String
In
lines
- Dim
VideoFile As
WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(musicPath + FilePath)
- Playlist.appendItem(VideoFile)
- Next
- AxWindowsMediaPlayer1.currentPlaylist = Playlist
- End
Function
Running the Function:
On the button click we simply call the function...
- Private
Sub
Button4_Click(sender As
Object
, e As
EventArgs) Handles Button4.Click
- loadPlaylist()
- End
Sub