Fandom

File Formats Wiki

M3U/code

< M3U

261pages on
this wiki
Add New Page
Talk0 Share

Visual BasicEdit

Parser implementation:

Imports System.IO
 
''' <summary>
''' Reads and writes M3U playlists.
''' </summary>
Public NotInheritable Class M3uManager
    Public Const M3uHeader As String = "#EXTM3U"
 
    Public Shared Function Read(ByVal stream As StreamReader) As IEnumerable(Of M3uItem)
        Dim itemList As New List(Of M3uItem)
        Dim header As String = ""
        Dim itemInfo As New M3uItem
        Dim extinfEnc As Boolean
        Dim cur As String
        Dim sp() As String
 
        Do Until header = M3uHeader OrElse stream.EndOfStream
            header = stream.ReadLine
        Loop
 
        Do Until stream.EndOfStream
            cur = stream.ReadLine
            If cur.StartsWith("#") Then
                cur = cur.Remove(0, 1)
                If cur.StartsWith("EXTM3U") Then
                    itemList.Clear()
                    Continue Do
                ElseIf cur.StartsWith("EXTINF:") Then
                    extinfEnc = True
                    cur = cur.Remove(0, 7)
                    sp = cur.Split(","c)
                    itemInfo.Duration = TimeSpan.FromSeconds(Integer.Parse(sp(0)))
                    itemInfo.Title = sp(1)
                End If
            Else
                If extinfEnc Then
                    itemInfo.Path = New Uri(cur)
                    extinfEnc = False
                    itemList.Add(itemInfo.Clone)
                Else
                    itemList.Add(New M3uItem With {.Path = New Uri(cur)})
                End If
            End If
        Loop
 
        Return itemList
    End Function
 
    Public Shared Function Read(ByVal path As String) As IEnumerable(Of M3uItem)
        Using s As New StreamReader(path)
            Return Read(s)
        End Using
    End Function
 
    Public Shared Sub Write(ByVal itemList As IEnumerable(Of M3uItem), ByVal stream As StreamWriter)
        stream.WriteLine(M3uHeader)
        For Each item As M3uItem In itemList
            stream.WriteLine("#EXTINF:{0},{1}", item.Duration.Seconds, item.Title)
            If item.Path.IsAbsoluteUri Then
                stream.WriteLine(item.Path.AbsolutePath)
            Else
                stream.WriteLine(item.Path.ToString)
            End If
        Next
    End Sub
 
    Public Shared Sub Write(ByVal itemList As IEnumerable(Of M3uItem), ByVal filePath As String)
        Using s As New StreamWriter(filePath)
            Write(itemList, s)
        End Using
    End Sub
 
    Public Shared Function Write(ByVal itemList As IEnumerable(Of M3uItem)) As String
        Dim t As String = M3uHeader + Environment.NewLine
        For Each item As M3uItem In itemList
            t += "#EXTINF:" + item.Duration.Seconds + item.Title + Environment.NewLine
            If item.Path.IsAbsoluteUri Then
                t += item.Path.AbsolutePath + Environment.NewLine
            Else
                t += item.Path.ToString + Environment.NewLine
            End If
        Next
        Return t
    End Function
 
End Class
 
''' <summary>
''' Represents an item in an M3U playlist.
''' </summary>
Public Class M3uItem
 
    Private _path As Uri
    Private _title As String
    Private _duration As TimeSpan
 
    Public Sub New()
    End Sub
 
    Public Property Path() As Uri
        Get
            Return _path
        End Get
        Set(ByVal value As Uri)
            _path = value
        End Set
    End Property
 
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property
 
    Public Property Duration() As TimeSpan
        Get
            Return _duration
        End Get
        Set(ByVal value As TimeSpan)
            _duration = value
        End Set
    End Property
 
    Friend Function Clone() As M3uItem
        Return New M3uItem() With {.Duration = _duration, .Path = _path, .Title = _title}
    End Function
 
End Class

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.