Imports System.IO
Imports System.Text

Public Class Form1

Inherits System.Windows.Forms.Form

    Public Path As String
    Public Title As String
    Public Album As String
    Public Artist As String
    Public Genre As String
    Public msec As Long
    Public Track As Integer

    Public Structure Tagg
        Dim MTag As String
        Dim MFlag As Integer
        Dim MSize As Integer
        Dim MData As Byte()
        Dim StrData As String
    End Structure

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Player.Play()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Load.ShowDialog()
        Player.FileName = Load.FileName
        Path = Load.FileName


        Me.Path = Path

        Dim MTags As New Collection()

        Try
            MTags = TagsFromFile() ' This needs just the Path value
            Me.Title = MTags("TIT2").StrData
            Me.Album = MTags("TALB").StrData
            Me.Artist = MTags("TPE1").StrData
            Me.msec = CLng(MTags("TLEN").StrData)
            Me.Genre = MTags("TCON").StrData
            Me.Track = CInt(MTags("TRCK").StrData)
        Catch ex As Exception

        End Try

        
    End Sub


    Private Function TagsFromFile() As Collection

        Dim ba(15) As Byte
        Dim theTags As New Collection()
        Dim MPEGFile As FileStream

        Try
            ' Open MP3 File
            MPEGFile = New FileStream(Path, FileMode.Open)

            ' Read MP3 header info
            MPEGFile.Read(ba, 0, 10)

            If (ba(0) = &H49) And (ba(1) = &H44) And (ba(2) = &H33) Then ' It's a proper MP3

            End If

            Dim theTag As Tagg

            theTag = TagFromStream(MPEGFile)

            Do While Not IsNothing(theTag.MTag)
                theTag.MTag = theTag.MTag.Substring(0, 4)  ' For 1.0 - coerce 6-byte string to 4-byte.

                theTags.Add(theTag, theTag.MTag)
                theTag = New Tagg()
                theTag = TagFromStream(MPEGFile)
            Loop

        Catch ex As Exception
        End Try

        MPEGFile.Close()
        Return theTags
    End Function


    ' =============================================================
    ' TagFromStream
    '
    ' Reads the next tag from an MP3 file, represented by a stream
    '

    Private Shared Function TagFromStream(ByVal sMPEG As Stream) As Tagg
        Dim theTag As New Tagg()

        Dim ba() As Byte
        Dim ascii As New ASCIIEncoding()

        ReDim ba(5)
        Try
            sMPEG.Read(ba, 0, 4)
            theTag.MTag = ascii.GetString(ba)

            'FOR VS.NET 2002
            theTag.MTag = theTag.MTag.Trim

            If ba(0) = 0 Then Return Nothing

            sMPEG.Read(ba, 0, 4)
            theTag.MSize = (65536 * (ba(0) * 256 + ba(1))) + (ba(2) * 256 + ba(3))

            sMPEG.Read(ba, 0, 3)
            theTag.MFlag = ba(0) * 256 + ba(1)

            ReDim ba(theTag.MSize + 1)

            If theTag.MSize > 0 Then
                sMPEG.Read(ba, 0, theTag.MSize - 1)
                theTag.MData = ba

                If theTag.MTag.Substring(0, 1) = "T" Then
                    theTag.StrData = ascii.GetString(ba).Trim
                End If
            End If
        Catch ex As Exception
        End Try

        Return theTag
    End Function

    Public Sub New(ByVal Path As String)

        Me.Path = Path

        Dim MTags As New Collection()

        Try
            MTags = TagsFromFile() ' This needs just the Path value
            Me.Title = MTags("TIT2").StrData
            Me.Album = MTags("TALB").StrData
            Me.Artist = MTags("TPE1").StrData
            Me.msec = CLng(MTags("TLEN").StrData)
            Me.Genre = MTags("TCON").StrData
            Me.Track = CInt(MTags("TRCK").StrData)
        Catch ex As Exception

        End Try

    End Sub


End Class
