|
-
Aug 4th, 2005, 07:43 PM
#1
Thread Starter
Member
[Resolved] MP3 tags
Ok, some may know I have been working on this FOREVER (it seems that way) I am trying to scan a directory for mp3 files and read the found tags.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Dim srch As String = FolderBrowserDialog1.SelectedPath
Dim str As New ArrayList
getMP3Files(str, srch.ToString, "*.mp3")
For Each s As String In str
MessageBox.Show(s)
End Sub
Private Sub getMP3Files(ByRef str As ArrayList, ByVal startingDir As String, ByVal ext As String)
str.AddRange(System.IO.Directory.GetFiles(startingDir, ext))
Dim dirs As String() = System.IO.Directory.GetDirectories(startingDir)
If Not dirs.Length = 0 Then
For Each d As String In dirs
getMP3Files(str, d, ext)
Next
End If
End Sub
That gets my the files ok. And I have found this class to read the tags. But have not yet been able to get it right. Please, can someone help???
VB Code:
Imports System
Imports System.IO
Imports System.Text
Namespace Ambientware.multimedia
Public Class ID3Util
Private songTitle As String
Private artist As String
Private albumTitle As String
Private year As String
Private comment As String
Private cdTrack As Integer
Private genre As String
Private mp3 As FileStream
Private id3StartV1 As Long
Private id3StartV2 As Long
Private id3Version As Integer
Private id3Tag(128) As Byte
Private completeTag As String
Sub New(ByVal mp3FilePath As String)
mp3 = File.OpenRead(mp3FilePath)
id3StartV1 = mp3.Length() - 128
id3StartV2 = 0
setVersion()
populateFields()
mp3.Close()
End Sub
'Retrieves the song title from the id3 tag
'return A string representing the song title
Public Function getSongTitle() As String
Return songTitle
End Function
'Gets the name of the artist
'return A string representing the artist name
Public Function getArtist() As String
Return (artist)
End Function
'Get the name of the album
'return A string representing the name of the album the mp3 is from.
Public Function getAlbumTitle() As String
Return (albumTitle)
End Function
'Get the year the song was recorded.
'return A string representing the year the song was recorded.
Public Function getYear() As String
Return (year)
End Function
'Get the comment, if any, from the id3 tag
'return A string representing the comment portion of the id3 tag
Public Function getComment() As String
Return (comment)
End Function
'Get the track number from the cd
'return The track number from the album.
Public Function getCdTrack() As String
If (cdTrack = -1) Then
Return ("Unknown")
Else
Return (cdTrack.ToString())
End If
End Function
'Get the genre of the song
'return A string value representing the content genre, if available, of the mp3.
Public Function getGenre() As String
Return (genre)
End Function
Private Function populateFields()
Dim genres As String() = {"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychedelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Brass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhytmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "Acapella", "Euro-House", "Dance Hall"}
songTitle = byteArrayToString(id3Tag, 3, 30)
If (Not verify(songTitle)) Then
songTitle = "Unknown"
End If
artist = byteArrayToString(id3Tag, 33, 30)
If (Not verify(artist)) Then
artist = "Unknown"
End If
albumTitle = byteArrayToString(id3Tag, 63, 30)
If (Not verify(albumTitle)) Then
albumTitle = "Unknown"
End If
year = byteArrayToString(id3Tag, 93, 4)
If (Not verify(year)) Then
year = "Unknown"
End If
comment = byteArrayToString(id3Tag, 97, 30)
If (Not verify(comment)) Then
comment = "Unknown"
End If
cdTrack = id3Tag(126)
If (cdTrack <= 0) Then
cdTrack = -1
End If
If ((id3Tag(127) >= 0) And (id3Tag(127) <= 125)) Then
genre = genres(id3Tag(127))
Else
genre = "Unknown"
End If
completeTag = "SongTitle: " + songTitle + "\nArtist: " + artist + "\nAlbumTitle: " + albumTitle + "\nYear: " + year + "\nComment: " + comment + "\nTrack# " + getCdTrack() + "\nGenre: " + genre
End Function
'working function to return the string from the varios byte arrays
'that make up the ID3 tags in the MP3 files
Private Function byteArrayToString(ByVal arrByte() As Byte, ByVal intBeginPOS As Integer, ByVal intLength As Integer) As String
Dim enc As New System.Text.ASCIIEncoding
Return enc.GetString(arrByte, intBeginPOS, intLength)
End Function
Private Shared Function verify(ByVal field As String) As Boolean
Dim validChars As String = " abcdefghijklmnopqrstuvwxyz1234567890-!`~!@#$%&*()_=+[{}]|\\<>,./? "
Dim found As Boolean = True
Dim i As Integer
field = field.ToLower()
For i = 0 To field.Length = 1
If (validChars.IndexOf(field.Chars(i)) = -1) Then
If (field.IndexOf(i) <> 0) Then
found = False
End If
End If
Next
Return found
End Function
'Determine which version of the ID3 tags we are dealing with
Private Function setVersion()
Dim type As String = ""
Dim strTmp As String = ""
Dim X As Integer
mp3.Position = id3StartV1
mp3.Read(id3Tag, 0, 128)
strTmp = Me.byteArrayToString(id3Tag, 0, 3)
If (strTmp.ToUpper.Equals("TAG")) Then 'Then we've got a version one
id3Version = 1
Else 'We've got version two
mp3.Position = id3StartV2
mp3.Read(id3Tag, 0, 128)
strTmp = Me.byteArrayToString(id3Tag, 0, 3)
If (strTmp.ToUpper.Equals("ID3")) Then
id3Version = 2
Else
id3Version = -1
End If
End If
If (id3Version = 1) Then
mp3.Position = id3StartV1
mp3.Read(id3Tag, 0, 128)
Else
mp3.Position = id3StartV2
mp3.Read(id3Tag, 0, 128)
End If
End Function
'Get the id3tag version.
'return The id3 verison of the tag (1 or 2)
Public Function getID3Version() As Integer
Return (id3Version)
End Function
'Get the complete id3tag
'return A string representing the complete id3 tag as a concantenation of all the values.
Public Function getCompleteTag() As String
If (id3Version = 1) Then
Return (completeTag)
Else
Return ("Not a valid tag")
End If
End Function
End Class
End Namespace
Last edited by DjDollarBill; Aug 10th, 2005 at 01:05 AM.
Reason: RESOLVED
[VBCODE]
Dim myPost as new UserPost
if myPost.isHelp = true then
messageBox.show("Please rate")
else
txtBox.text = "nevermind"
[/VBCODE]
-
Aug 4th, 2005, 08:54 PM
#2
Re: MP3 tags ARGHH
What is the issue so we know what we're looking for?
-
Aug 4th, 2005, 09:15 PM
#3
Thread Starter
Member
Re: MP3 tags ARGHH
I am trying to use the class, but have yet been able to sort out what I need to do. I have tried passing the filepath as a string and as an array index with various errors (most have to do with invalid casts and null ref's) None of my breakpoints are being reached and most errors are in the assembly which is no help to me. It seems like I'm just missing something in the class use. I did go with your suggestion (playing with music files) but it was REAL slow and have since been trying all sorts of stuff.
[VBCODE]
Dim myPost as new UserPost
if myPost.isHelp = true then
messageBox.show("Please rate")
else
txtBox.text = "nevermind"
[/VBCODE]
-
Aug 5th, 2005, 12:51 AM
#4
Thread Starter
Member
Re: MP3 tags ARGHH
Ok .. I got a few things sorted, but am still getting a null ref exception in this block. Does anyone see the prob?
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mp3try As Ambientware.multimedia.ID3Util
FolderBrowserDialog1.ShowDialog()
Dim srch As String = FolderBrowserDialog1.SelectedPath
Dim str As New ArrayList
getMP3Files(str, srch.ToString, "*.mp3")
For Each s As String In str
'MessageBox.Show(s) testing that files are found
Dim m As New DjMediaData
m.Title = mp3try.getSongTitle()
Next
End Sub
[VBCODE]
Dim myPost as new UserPost
if myPost.isHelp = true then
messageBox.show("Please rate")
else
txtBox.text = "nevermind"
[/VBCODE]
-
Aug 5th, 2005, 12:58 AM
#5
Re: MP3 tags ARGHH
Where do you assign to the mp3try variable to be able to use its getSongTitle method, or is that a Shared method? If it is Shared then you should strictly be using the class name to identify it as such. If it's not, then that's your problem.
-
Aug 10th, 2005, 01:00 AM
#6
Thread Starter
Member
Re: MP3 tags ARGHH
problem solved at long last !
[VBCODE]
Dim myPost as new UserPost
if myPost.isHelp = true then
messageBox.show("Please rate")
else
txtBox.text = "nevermind"
[/VBCODE]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|