Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 264,031 times

Contents

Related Categories

Build an MP3 Player - Tag Information

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

© 2001 Element K Journals, a division of Element K Press LLC ("Element K"). Element K and the Element K logo are trademarks of Element K LLC

Comments

  • Lyrics support

    Posted by AziMo on 07 Jan 2008

    hey there...does anyone know the way a media player with lyrics support??

     

    thanx!! 

  • Re: TW 2000

    Posted by possible181 on 03 Jun 2007

    Anyone one plz help me with mediaplayer's iserviceprovider and how to program remoting mediaplayer

  • WMP.DLL VB.net media player master

    Posted by Qwazimoto on 08 Nov 2006

    Hi people I have written a few media players and a few turned out good(most were crap thaugh).
    If any one wants any help, Id be glad to.

    My media player im working on now has the followin...

  • Re: [62] Build an MP3 Player

    Posted by KoA on 24 Oct 2006

    Can someone help me?

    First, im getting Object does not support this this methor or property on ".Filename = Filename"
    Next, im getting the same thing for for the 2 options in the timer?
    ...

  • Posted by mazdak_zp on 23 Sep 2005

    when you want to get then path of a FileListBox or DirListBox, if it returns a folder in drive it return for example "c:\windows" without "\" at the end if path. but if the path was a drive it returns...