Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Creating a Playlist Control

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
27 Oct 2012CPOL 11K   2   2
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Manfred Rudolf Bihy31-Oct-12 5:09
professionalManfred Rudolf Bihy31-Oct-12 5:09 
GeneralRe: My vote of 1 Pin
rspercy653-Nov-12 16:11
rspercy653-Nov-12 16:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.