Reading mp3 tag information in vb.net

vkGunasekaran picture vkGunasekaran · Jun 12, 2011 · Viewed 10.1k times · Source

I'm doing a project in VB.NET 2005, in which I have to extract the tag information of mp3 files. For that purpose I have used code in this page. But the issue is when one of the tag is empty, it didn't return any values.

For example, using this i can retrieve album information like this,

    Dim album As String = ""
    album = objMP3V1.Frame(MP3ID3v1.FrameTypes.Album)

But I didn't know how to check the album variable is empty or not, I checked album variable

    If (album = "") Then
        MsgBox("true")
    ElseIf (album Is Nothing) Then
        MsgBox("true")
    ElseIf (album Is DBNull.Value) Then
        MsgBox("true")
    End If

but no success, can somebody help me.

Answer

phoxis picture phoxis · Jun 12, 2011

The ID3v1 tag is stored in the last 128 bytes of the file. The first three bytes is "TAG" telling that the file stores the tag. So first check if the file has a tag, Then read them.

I do not know VB but i think before reading the frame, you should first:

  1. Open the file Dim objMP3V1 As New MP3ID3v1("file_path")
  2. Test, if the file has an ID3v1 tag in it by testing if objMP3V1.TagExists flag is true
  3. Then read the fields/frames.

EDIT

The code in the link says

FileGet(intFile, strTag, lngLOF - 127, True)
        If (strTag.ToUpper <> "TAG") Then

            ' No ID3v1 tag found

            mblnTagExists = False
            mobjFrame(0) = ""
            mobjFrame(1) = ""
            mobjFrame(2) = ""
            mobjFrame(3) = ""
            mobjFrame(4) = ""
            mobjFrame(5) = ""
            mobjFrame(6) = ""

        Else

            ' ID3v1 tag found

            mblnTagExists = True

            ' Read all frames from the file

            FileGet(intFile, strTitle)
            FileGet(intFile, strArtist)
            FileGet(intFile, strAlbum)
            FileGet(intFile, strYear)
            FileGet(intFile, strComment)
            FileGet(intFile, bytDummy)
            FileGet(intFile, bytTrack)
            FileGet(intFile, bytGenre)

            ' Assign the frame content to the properties

            mobjFrame(0) = strTitle
            mobjFrame(1) = strArtist
            mobjFrame(2) = strAlbum
            mobjFrame(3) = strYear
            mobjFrame(4) = bytTrack
            mobjFrame(5) = strComment
            mobjFrame(6) = bytGenre

        End If
    End If

So, if the tag is not present then it should have "" assigned as the string.

ID3v1 fields are of fixed length, so if there is no string in the album field then it should contain a num string, that is, the first location of the field will contain a null character '\0', therefore it will return a null string "". I would tell you to check the this on a sample music file with ID3v1 tags. (You can even create a text file formatted with ID3v1 and test it).