Load M3U playlist

This function needs a string file location of the M3U playlist. An empty string array (i.e. Dim ArrayName() as String) for the file locations (including the names) and a second empty string array for the file names.

This function also checks the file location to see if it is correct, it displays an error message when the file is not found or if it cannot figure out how the file location is relative to the M3U files location. This happens because the M3U can sometimes contain only a part of the files location and the program that loads the file needs to be able to work out if the path is a whole path, if it goes on the end of the M3U's path or if it adds onto the M3U's drive location only.

Hope you find this useful!


Public Function LoadM3U(ByVal strFileName As String, ByRef strFilePaths() As String, ByRef strNames() As String) As Boolean
   
   'Error handler
   On Error GoTo ErrHap
   
       'Declare variables
       Dim lngFileNo As Long
       Dim strTemp As String
       Dim i As Long
       Dim strLines() As String
       Dim lngLines As Long
       Dim strM3ULoc As String
       
       'Check if file exists
       If Dir(strFileName) <> "" Then
           
           'Get M3U location
           strM3ULoc = Left$(strFileName, InStrRev(strFileName, "\"))
           
           'Get new file number
           lngFileNo = FreeFile
           
           'Open the file
           Open strFileName For Input As lngFileNo
               
               'Get the file
               strTemp = Input(LOF(lngFileNo), #lngFileNo)
               
           Close lngFileNo
           
           'Split the file into it's lines
           strLines = Split(strTemp, vbCrLf)
           
           'Check that this file has enough lines
           If UBound(strLines) > 2 Then
           
               'Check that it's an M3U file
               If strLines(0) = "#EXTM3U" Then

                   'Get number of lines
                   lngLines = UBound(strLines)
                   
                   'Attention! If you have any errors over the next 2 lines then you need to make sure
                   'that you have declared the array variables without specifying their size,
                   'because here we're changing their sizes to match.  - Thanks
                   ReDim strFilePaths(0 To (lngLines / 2)) As String
                   ReDim strNames(0 To (lngLines / 2)) As String

                   'Loop through each line
                   For i = 1 To lngLines
                   
                       'Check what kind of data we've got
                       If Left$(strLines(i), 7) = "#EXTINF" Then
                           
                           'File name & length (but we don't return that). Get file name
                           strNames((i - 1) / 2) = Right$(strLines(i), Len(strLines(i)) - InStr(1, strLines(i), ","))
                           
                       Else
                           'File path. Verify the path
                           If Dir(strLines(i)) <> "" Then
                               'Pure path, including drive letter
                               strFilePaths((i / 2) - 1) = strLines(i)
                           ElseIf Dir(strM3ULoc & strLines(i)) <> "" Then
                               'Adding onto the M3U's path (most common)
                               strFilePaths((i / 2) - 1) = strM3ULoc & strLines(i)
                           ElseIf Dir(Left$(strM3ULoc, 3) & strLines(i)) <> "" Then
                               'Adding onto the M3U's drive only
                               strFilePaths((i / 2) - 1) = Left$(strM3ULoc, 3) & strLines(i)
                           Else
                               'Display error message
                               Call MsgBox("Cannot find file!" & vbCrLf & strLines(i), vbExclamation, "Error while loading a file!")
                           End If
                       End If
                       
                   Next i
                   
                   'Set return value to true
                   LoadM3U = True
               
               End If
               
           Else
               'Return error
               LoadM3U = False
           End If
           
       Else
           'Return error
           LoadM3U = False
       End If
       
Exit Function
ErrHap:
   'Check error
   If Err.Number = 10 Then
       'Display message and exit function
       Call MsgBox("Cannot accept fixed length arrays!", vbExclamation, "Error - Function 'LoadM3U'")
       Exit Function
   End If

   'Display error
   Call MsgBox(Err.Description & " Number: " & Err.Number, vbExclamation)
   
   'Set return value
   LoadM3U = False
   
End Function

You might also like...

Comments

Marc Pritchard Apart from my school work I put most of my effort into running my joke site, I'd like to think that it makes someone smile each day. For about 1/2 a year I've been programming more and more in serv...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr