MP3 reader Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

MP3 reader


Objetive:

ID3 specifications apply to any file or audiovisual container, but they are primarily used with audio containers. There are three compatible versions of the specification. For example, a file may contain both version 1.1 and version 2.0 tags simultaneously, and in this case, the media player must determine which tags are relevant.

ID3 version 1 is a very simple specification. It involves appending a fixed block size of 128 bytes to the end of the file in question. This block contains the following tags:

A header that identifies the presence of the ID3 block and its version. Specifically, this header comprises the characters "TAG".
Title: 30 characters.
Artist: 30 characters.
Album: 30 characters.
Year: 4 characters.
Comment: 30 characters.
Genre (music): 1 character.
All tags use ASCII characters, except for genre, which is an integer stored within a single byte. The musical genre associated with each byte is predefined in the standard definitions and includes 80 genres numbered from 0 to 79. Some tagging programs have expanded the predefined genres beyond 79.


Code:

Imports System
Imports System.IO
Public Class MP3Reader
    Public Shared Sub Main()
        Const SIZE As Integer = 128
        Dim data As Byte()
        Console.Write("Enter name: ")
        Dim name As String = Console.ReadLine()

        If Not File.Exists(name) Then
            Console.WriteLine("Not exists")
            Return
        End If

        Try
            Dim file As FileStream = File.OpenRead(name)
            data = New Byte(127) {}
            file.Seek(-128, SeekOrigin.[End])
            file.Read(data, 0, SIZE)
            file.Close()
            Dim b1 As Byte = data(0)
            Dim b2 As Byte = data(1)
            Dim b3 As Byte = data(2)

            If Convert.ToChar(b1) <> "T"c OrElse Convert.ToChar(b2) <> "A"c OrElse Convert.ToChar(b3) <> "G"c Then
                Console.WriteLine("not mp3 valid")
                Return
            End If

            Dim i As Integer = 3
            Dim title As String = ""

            While i < 33
                If data(i) <> 0 Then title += Convert.ToChar(data(i))
                i += 1
            End While

            Dim author As String = ""

            For i = 33 To 63 - 1
                If data(i) <> 0 Then author += Convert.ToChar(data(i))
            Next

            Dim album As String = ""

            For i = 63 To 93 - 1
                If data(i) <> 0 Then album += Convert.ToChar(data(i))
            Next

            Dim year As String = ""

            For i = 93 To 97 - 1
                If data(i) <> 0 Then year += Convert.ToChar(data(i))
            Next

            Dim comments As String = ""

            For i = 97 To 127 - 1
                If data(i) <> 0 Then comments += Convert.ToChar(data(i))
            Next

            Console.WriteLine("Data of MP3:")
            Console.WriteLine("----------------------------")
            Console.WriteLine()
            Console.WriteLine("Title: " & title)
            Console.WriteLine("Author: " & author)
            Console.WriteLine("Album: " & album)
            Console.WriteLine("Year: " & year)
            Console.WriteLine("Comments: " & comments)
            Console.WriteLine("Genre: " & data(127))
        Catch __unusedException1__ As Exception
            Console.WriteLine("Error")
        End Try
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.