Build an MP3 Player

Tag Information

Deciphering the tag information

Now that we have our basic GUI in place, we can work on extracting information about the MP3. The most popular tag encryption appears to be ID3, so we'll base our code on that format. This standard stores the tag information in the last 128 bytes of the file. Table A shows the exact length and order of each piece of data.

Table A: ID3 tag information format

Data Length in bytes
Tag 3
Title 30
Artist 30
Album 30
Year 4
Comment 30
Genre 1

To read this information, you first open the MP3 file and grab the last 128 bytes. With ID3, the first three slots hold the string TAG if the file actually contains information. If the file does contain tag information, we'll store the last 128 bytes in a custom Type variable. After that, our code can cycle through the MP3 file, extracting information as it goes. Listing A shows the code that extracts this information as well as creates several important variables.

Listing A: Gathering MP3 tag information

Option Explicit
Dim GenresTypes
Dim Min As Integer
Dim Sec As Integer

Dim FileName As String
Dim FileOpen As Boolean
Dim CurrentTag As TagInfo

Private Type TagInfo
	Tag As String * 3
	Songname As String * 30
	artist As String * 30
	album As String * 30
	year As String * 4
	comment As String * 30
	genre As String * 1
End Type

Private Sub File1_Click()
Dim temp As String
On Error Resume Next
EraseTXTBoxes

If Right(Dir1.Path, 1) = "\" Then
	FileName = Dir1.Path & File1.FileName
Else
	FileName = Dir1.Path & "\" & File1.FileName
End If

Open FileName For Binary As #1
With CurrentTag
	Get #1, FileLen(FileName) - 127, .Tag
	If Not .Tag = "TAG" Then
		lblMsg.Caption = "No tag"
		Close #1
		Exit Sub
	End If
	Get #1, , .Songname
	Get #1, , .artist
	Get #1, , .album
	Get #1, , .year
	Get #1, , .comment
	Get #1, , .genre
	Close #1 

	txtTitle = RTrim(.Songname)
	txtArtist = RTrim(.artist)
	txtAlbum = RTrim(.album)
	txtYear = RTrim(.year)
	txtComment = RTrim(.comment)
	
	Temp = RTrim(.genre)
	txtGenreCode = Asc(Temp)
	Combo1.ListIndex = CInt(txtGenreCode) - 1
End With
End Sub

Private Sub Dir1_change() File1.FileName = Dir1.Path End Sub Sub Drive1_Change() ' change the folder path to the new drive Dir1.Path = Drive1.Drive ' change the file path File1.Path = Drive1.Drive End Sub

Notice that the code has to handle the genre character a little differently. That's because ID3 stores this data as a single ASCII character. To match up the actual number with its corresponding combobox description, the procedure converts the ASCII to a number, and then looks up that number in the combobox.

Writing the tag information

To write the tag information back to the file, you use a similar technique. Again, our code will take advantage of the Open command. This time, however, it will use the Put command. Listing B shows the procedure we attached to the command button cmdWriteTag's Click() event.

Listing B: Writing the tag information

Private Sub cmdWriteTag_Click()
If FileOpen Then
	MsgBox "You can't save to an open file", _
		vbCritical, "MP3 Tag Save Error"
	Exit Sub
End If
    
If Right(Dir1.Path, 1) = "\" Then
	FileName = Dir1.Path & File1.FileName
Else
	FileName = Dir1.Path & "\" & File1.FileName
End If

With CurrentTag
	.Tag = "TAG"
	.Songname = txtTitle
	.artist = txtArtist
	.album = txtAlbum
	.year = txtYear
	.comment = txtComment
	.genre = Chr(Combo1.ListIndex + 1)
    
	Open FileName For Binary Access Write As #1
		Seek #1, FileLen(FileName) - 127
		Put #1, , .Tag
		Put #1, , .Songname
		Put #1, , .artist
		Put #1, , .album
		Put #1, , .year
		Put #1, , .comment
		Put #1, , .genre
	Close #1
End With
End Sub

You might also like...

Comments

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.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”