Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Creating a Playlist Control

3.00/5 (2 votes)
27 Oct 2012CPOL 11.3K  
Resuable Playlist Control for Use with COM Media Player Objects Such as Window Media Player

Introduction 

A much more simple way to create and manage Playlists for COM Media Player Objects such as Window Media Player.

Background

When I first attempted doing some Tutorials some time ago, One of the ones I was most excited to attempt the Windows Media Player Tutorial. I was annoyed at the difficultly to make a Playlist or Media Library to use with it. About a month or so ago I came up with this solution and forgot about it, then about a week ago while cleaning out my computer I came across the Source Code for this. So here it is.

Using the code

Here's the code to use to create the Playlist control.

VB.NET
//
Imports Microsoft.VisualBasic.FileIO.FileSystem
Public Class MainControl
    Property MediaFolderPath As String = Nothing
    Property SupportedExtentions As List(Of String)
    Property MediaFiles As List(Of String)
    Property MediaFileNames As List(Of String)
    Property SelectedMediaFilePath As String = Nothing
    Property SelectedMediaFriendlyName As String = Nothing
    Enum ListMode As Integer
        FileMode = 0
        NameMode = 1
    End Enum
    Public Sub Init(Optional ByVal FetchNames As Boolean = False)
        Try
            MediaFiles = New List(Of String)
            If IsNothing(SupportedExtentions) = False Then
                If FileIO.FileSystem.DirectoryExists(MediaFolderPath) = True Then
                    For Each file As String In GetFiles(MediaFolderPath, FileIO.SearchOption.SearchAllSubDirectories)
                        For Each Extention As String In SupportedExtentions
                            If file.EndsWith(Extention) = True Then
                                MediaFiles.Add(file)
                                Exit For
                            End If
                        Next
                    Next
                End If
            End If
            If FetchNames = True Then
                FetchMediaNames()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub FetchMediaNames()
        If IsNothing(MediaFiles) = False Then
            MediaFileNames = New List(Of String)
            For Each MediaItem As String In MediaFiles
                Dim FriendlyName As String = GetFileInfo(MediaItem).Name.Replace(GetFileInfo(MediaItem).Extension, "")
                MediaFileNames.Add(FriendlyName)
            Next
        End If
    End Sub
    Public Sub PopulateList(ListForUse As ListMode)
        If ListForUse = ListMode.FileMode Then
            For Each item As String In MediaFiles
                PlayistView.Items.Add(item)
            Next
        ElseIf ListForUse = ListMode.NameMode Then
            For Each friendlyname As String In MediaFileNames
                PlayistView.Items.Add(friendlyname)
            Next
        End If
    End Sub
    Private Sub PlayistView_DoubleClick(sender As Object, e As EventArgs) Handles PlayistView.DoubleClick
        If IsNothing(PlayistView) = False Then
            SelectedMediaFilePath = MediaFiles(PlayistView.SelectedIndex)
            SelectedMediaFriendlyName = GetFileInfo(MediaFiles(PlayistView.SelectedIndex)).Name.Replace(GetFileInfo(MediaFiles(PlayistView.SelectedIndex)).Extension, "")
        End If
    End Sub
End Class

//  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)