Results 1 to 6 of 6

Thread: [Resolved] MP3 tags

  1. #1

    Thread Starter
    Member DjDollarBill's Avatar
    Join Date
    May 2005
    Location
    Erie PA USA
    Posts
    41

    Resolved [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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         FolderBrowserDialog1.ShowDialog()
    4.         Dim srch As String = FolderBrowserDialog1.SelectedPath
    5.         Dim str As New ArrayList
    6.         getMP3Files(str, srch.ToString, "*.mp3")
    7.         For Each s As String In str
    8.         MessageBox.Show(s)
    9.                 End Sub
    10.  
    11.     Private Sub getMP3Files(ByRef str As ArrayList, ByVal startingDir As String, ByVal ext As String)
    12.  
    13.         str.AddRange(System.IO.Directory.GetFiles(startingDir, ext))
    14.         Dim dirs As String() = System.IO.Directory.GetDirectories(startingDir)
    15.         If Not dirs.Length = 0 Then
    16.             For Each d As String In dirs
    17.                 getMP3Files(str, d, ext)
    18.             Next
    19.         End If
    20.     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:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Text
    4.  
    5. Namespace Ambientware.multimedia
    6.     Public Class ID3Util
    7.         Private songTitle As String
    8.         Private artist As String
    9.         Private albumTitle As String
    10.         Private year As String
    11.         Private comment As String
    12.         Private cdTrack As Integer
    13.         Private genre As String
    14.         Private mp3 As FileStream
    15.         Private id3StartV1 As Long
    16.         Private id3StartV2 As Long
    17.         Private id3Version As Integer
    18.         Private id3Tag(128) As Byte
    19.         Private completeTag As String
    20.  
    21.  
    22.         Sub New(ByVal mp3FilePath As String)
    23.             mp3 = File.OpenRead(mp3FilePath)
    24.             id3StartV1 = mp3.Length() - 128
    25.             id3StartV2 = 0
    26.             setVersion()
    27.             populateFields()
    28.             mp3.Close()
    29.         End Sub
    30.  
    31.         'Retrieves the song title from the id3 tag
    32.         'return A string representing the song title
    33.         Public Function getSongTitle() As String
    34.             Return songTitle
    35.         End Function
    36.  
    37.         'Gets the name of the artist
    38.         'return A string representing the artist name
    39.         Public Function getArtist() As String
    40.             Return (artist)
    41.         End Function
    42.  
    43.  
    44.         'Get the name of the album
    45.         'return A string representing the name of the album the mp3 is from.
    46.         Public Function getAlbumTitle() As String
    47.             Return (albumTitle)
    48.         End Function
    49.  
    50.  
    51.         'Get the year the song was recorded.
    52.         'return A string representing the year the song was recorded.
    53.         Public Function getYear() As String
    54.             Return (year)
    55.         End Function
    56.  
    57.  
    58.         'Get the comment, if any, from the id3 tag
    59.         'return A string representing the comment portion of the id3 tag
    60.         Public Function getComment() As String
    61.             Return (comment)
    62.         End Function
    63.  
    64.  
    65.         'Get the track number from the cd
    66.         'return The track number from the album.
    67.         Public Function getCdTrack() As String
    68.             If (cdTrack = -1) Then
    69.                 Return ("Unknown")
    70.             Else
    71.                 Return (cdTrack.ToString())
    72.             End If
    73.         End Function
    74.  
    75.  
    76.         'Get the genre of the song
    77.         'return A string value representing the content genre, if available, of the mp3.
    78.         Public Function getGenre() As String
    79.             Return (genre)
    80.         End Function
    81.  
    82.  
    83.  
    84.         Private Function populateFields()
    85.             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"}
    86.  
    87.             songTitle = byteArrayToString(id3Tag, 3, 30)
    88.             If (Not verify(songTitle)) Then
    89.                 songTitle = "Unknown"
    90.             End If
    91.  
    92.             artist = byteArrayToString(id3Tag, 33, 30)
    93.             If (Not verify(artist)) Then
    94.                 artist = "Unknown"
    95.             End If
    96.  
    97.             albumTitle = byteArrayToString(id3Tag, 63, 30)
    98.             If (Not verify(albumTitle)) Then
    99.                 albumTitle = "Unknown"
    100.             End If
    101.  
    102.             year = byteArrayToString(id3Tag, 93, 4)
    103.             If (Not verify(year)) Then
    104.                 year = "Unknown"
    105.             End If
    106.  
    107.             comment = byteArrayToString(id3Tag, 97, 30)
    108.             If (Not verify(comment)) Then
    109.                 comment = "Unknown"
    110.             End If
    111.  
    112.             cdTrack = id3Tag(126)
    113.             If (cdTrack <= 0) Then
    114.                 cdTrack = -1
    115.             End If
    116.  
    117.             If ((id3Tag(127) >= 0) And (id3Tag(127) <= 125)) Then
    118.                 genre = genres(id3Tag(127))
    119.             Else
    120.                 genre = "Unknown"
    121.             End If
    122.  
    123.             completeTag = "SongTitle: " + songTitle + "\nArtist: " + artist + "\nAlbumTitle: " + albumTitle + "\nYear: " + year + "\nComment: " + comment + "\nTrack# " + getCdTrack() + "\nGenre: " + genre
    124.         End Function
    125.  
    126.  
    127.         'working function to return the string from the varios byte arrays
    128.         'that make up the ID3 tags in the MP3 files
    129.         Private Function byteArrayToString(ByVal arrByte() As Byte, ByVal intBeginPOS As Integer, ByVal intLength As Integer) As String
    130.             Dim enc As New System.Text.ASCIIEncoding
    131.  
    132.             Return enc.GetString(arrByte, intBeginPOS, intLength)
    133.         End Function
    134.  
    135.  
    136.         Private Shared Function verify(ByVal field As String) As Boolean
    137.             Dim validChars As String = "  abcdefghijklmnopqrstuvwxyz1234567890-!`~!@#$%&*()_=+[{}]|\\<>,./?  "
    138.             Dim found As Boolean = True
    139.             Dim i As Integer
    140.  
    141.             field = field.ToLower()
    142.  
    143.             For i = 0 To field.Length = 1
    144.                 If (validChars.IndexOf(field.Chars(i)) = -1) Then
    145.                     If (field.IndexOf(i) <> 0) Then
    146.                         found = False
    147.                     End If
    148.                 End If
    149.             Next
    150.             Return found
    151.         End Function
    152.  
    153.  
    154.         'Determine which version of the ID3 tags we are dealing with
    155.         Private Function setVersion()
    156.             Dim type As String = ""
    157.             Dim strTmp As String = ""
    158.             Dim X As Integer
    159.  
    160.             mp3.Position = id3StartV1
    161.             mp3.Read(id3Tag, 0, 128)
    162.  
    163.             strTmp = Me.byteArrayToString(id3Tag, 0, 3)
    164.  
    165.             If (strTmp.ToUpper.Equals("TAG")) Then 'Then we've got a version one
    166.                 id3Version = 1
    167.             Else 'We've got version two
    168.                 mp3.Position = id3StartV2
    169.                 mp3.Read(id3Tag, 0, 128)
    170.  
    171.                 strTmp = Me.byteArrayToString(id3Tag, 0, 3)
    172.  
    173.                 If (strTmp.ToUpper.Equals("ID3")) Then
    174.                     id3Version = 2
    175.                 Else
    176.                     id3Version = -1
    177.                 End If
    178.  
    179.             End If
    180.  
    181.             If (id3Version = 1) Then
    182.                 mp3.Position = id3StartV1
    183.                 mp3.Read(id3Tag, 0, 128)
    184.             Else
    185.                 mp3.Position = id3StartV2
    186.                 mp3.Read(id3Tag, 0, 128)
    187.             End If
    188.         End Function
    189.  
    190.  
    191.         'Get the id3tag version.
    192.         'return The id3 verison of the tag (1 or 2)
    193.         Public Function getID3Version() As Integer
    194.             Return (id3Version)
    195.         End Function
    196.  
    197.  
    198.         'Get the complete id3tag
    199.         'return A string representing the complete id3 tag as a concantenation of all the values.
    200.         Public Function getCompleteTag() As String
    201.             If (id3Version = 1) Then
    202.                 Return (completeTag)
    203.             Else
    204.                 Return ("Not a valid tag")
    205.             End If
    206.         End Function
    207.  
    208.     End Class
    209. 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]

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MP3 tags ARGHH

    What is the issue so we know what we're looking for?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member DjDollarBill's Avatar
    Join Date
    May 2005
    Location
    Erie PA USA
    Posts
    41

    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]

  4. #4

    Thread Starter
    Member DjDollarBill's Avatar
    Join Date
    May 2005
    Location
    Erie PA USA
    Posts
    41

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim mp3try As Ambientware.multimedia.ID3Util
    3.  
    4.         FolderBrowserDialog1.ShowDialog()
    5.         Dim srch As String = FolderBrowserDialog1.SelectedPath
    6.         Dim str As New ArrayList
    7.  
    8.  
    9.         getMP3Files(str, srch.ToString, "*.mp3")
    10.         For Each s As String In str
    11.             'MessageBox.Show(s) testing that files are found
    12.             Dim m As New DjMediaData
    13.             m.Title = mp3try.getSongTitle()
    14.  
    15.  
    16.         Next
    17.  
    18.     End Sub
    [VBCODE]
    Dim myPost as new UserPost
    if myPost.isHelp = true then
    messageBox.show("Please rate")
    else
    txtBox.text = "nevermind"
    [/VBCODE]

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member DjDollarBill's Avatar
    Join Date
    May 2005
    Location
    Erie PA USA
    Posts
    41

    Resolved 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
  •  



Click Here to Expand Forum to Full Width