Classe mp3 (tags, image)

Contenu du snippet

Cette classe permet de:
- lire les tags mp3 (v1 uniquement)
- récupérer sur fnac.com l'image correspondant (en fonction des tags)

Source / Exemple :


Imports System.IO
Imports System.Net

<Serializable()> Public Class cls_mp3tag

    Public path As String
    Public directory As String
    Public filename As String
    Public artist As String
    Public title As String
    Public album As String
    Public genre As String = 255
    Public comments As String
    Public track As String
    Public tagged As Boolean
    Public num As Integer

    ' Genres
    Public Enum enum_genres As Byte
        Blues = 0
        ClassicRock = 1
        Country = 2
        Dance = 3
        Disco = 4
        Funk = 5
        Grunge = 6
        HipHop = 7
        Jazz = 8
        Metal = 9
        NewAge = 10
        Oldies = 11
        Other = 12
        Pop = 13
        RnB = 14
        Rap = 15
        Reggae = 16
        Rock = 17
        Techno = 18
        Industrial = 19
        Alternative = 20
        Ska = 21
        DeathMetal = 22
        Pranks = 23
        Soundtrack = 24
        EuroTechno = 25
        Ambient = 26
        TripHop = 27
        Vocal = 28
        JazzFunk = 29
        Fusion = 30
        Trance = 31
        Classical = 32
        Instrumental = 33
        Acid = 34
        House = 35
        Game = 36
        SoundClip = 37
        Gospel = 38
        Noise = 39
        AlternRock = 40
        Bass = 41
        Soul = 42
        Punk = 43
        Space = 44
        Meditative = 45
        InstrumentalPop = 46
        InstrumentalRock = 47
        Ethnic = 48
        Gothic = 49
        Darkwave = 50
        TechnoIndustrial = 51
        Electronic = 52
        PopFolk = 53
        Eurodance = 54
        Dream = 55
        SouthernRock = 56
        Comedy = 57
        Cult = 58
        Gangsta = 59
        Top40 = 60
        ChristianRap = 61
        PopFunk = 62
        Jungle = 63
        NativeAmerican = 64
        Cabaret = 65
        NewWave = 66
        Psychadelic = 67
        Rave = 68
        Showtunes = 69
        Trailer = 70
        LoFi = 71
        Tribal = 72
        AcidPunk = 73
        AcidJazz = 74
        Polka = 75
        Retro = 76
        Musical = 77
        RocknRoll = 78
        HardRock = 79
        None = 255
    End Enum

    '******************************************************************************
    'recup les infos a partir des tags
    Public Sub init(ByVal e_path As String)

        Me.path = e_path

        Try

            Me.filename = Me.path.Substring(Me.path.LastIndexOf("\") + 1)
            Me.title = Me.filename
            Me.directory = Me.path.Substring(0, Me.path.LastIndexOf("\") + 1)

            Dim Buffer(128) As Byte
            Dim StreamReadWrite As System.IO.FileStream
            Dim Encodeur As New System.Text.ASCIIEncoding
            Try
                StreamReadWrite = New System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)
            Catch ex As Exception
                Exit Sub
            End Try

            Dim TagID3 As String
            StreamReadWrite.Seek(-128, System.IO.SeekOrigin.End)
            StreamReadWrite.Read(Buffer, 0, 128)
            TagID3 = Encodeur.GetString(Buffer)
            'Recherche du Tag TAG
            If TagID3.Substring(0, 3).Equals("TAG") Then
                'Initialisation de Title
                Me.title = TagID3.Substring(3, 30).Trim.Replace(Chr(0), "").Trim(CChar(vbNullChar))
                'Initialisation de Author
                Me.artist = TagID3.Substring(33, 30).Trim.Trim.Replace(Chr(0), "").Trim(CChar(vbNullChar))
                'Initialisation de Album
                Me.album = TagID3.Substring(63, 30).Trim.Trim.Replace(Chr(0), "").Trim(CChar(vbNullChar))
                'Initialisation de Comments
                Me.comments = TagID3.Substring(97, 28).Trim.Trim.Replace(Chr(0), "").Trim(CChar(vbNullChar))
                'Analyse du caractère 125 de TagID3
                If TagID3.Chars(125).Equals(0) Then
                    'Initialisation de Track
                    Me.track = Buffer.GetValue(126)
                    'Initialisation de Tagged
                    Me.tagged = False
                Else
                    'Initialisation de Track
                    Me.track = 0
                    'Initialisation de Category
                    Me.genre = Buffer.GetValue(127)
                    If Me.genre = "" Or Me.genre = " " Then
                        Me.genre = "255"
                    End If
                    If Me.title = "" Then Me.title = Me.filename
                    'Initialisation de Tagged
                    Me.tagged = True

                    'ferme le fichier
                    StreamReadWrite.Close()

                End If
            End If
        Catch IOex As System.IO.IOException

        End Try

        'si ya pas de title, on met le nom du fichier
        If Me.title = "" Then Me.title = Me.filename

        'si ya pas d'artiste, on met le nom du dossier 
        If IsNothing(Me.artist) OrElse Me.artist.Length = 0 Then Me.artist = "Inconnu"

    End Sub

    '******************************************************************************
    'recup l'image sur le site de la fnac :p
    Public Function get_picture(ByRef e_http As cls_http) As Image
        Try
            'envoi du formulaire avec les infos
            Dim requete_http As String
            Dim response_http As String
            requete_http = "http://www3.fnac.com/search/quick.do?text=" & Me.artist & " " & Me.title & " " & Me.album & "+&category=audio"
            response_http = e_http.get_url(requete_http)

            'recup le lien pour l'image
            Dim pos_debut As Integer = response_http.IndexOf("http://multimedia.fnac.com/multimedia/images_produits/petites")
            Dim pos_Fin As Integer = response_http.IndexOf("""", pos_debut)
            Dim url_img As String = response_http.Substring(pos_debut, pos_Fin - pos_debut)
            url_img = url_img.Replace("petites", "grandes")
            Debug.WriteLine(url_img)

            'enregistre l'image dans un fichier
            Return e_http.get_picture(url_img)

        Catch ex As Exception
            Debug.WriteLine("Erreur lors de la récupération de l'image: " & ex.Message)
            Return Nothing
        End Try

    End Function

End Class

Conclusion :


C'est un version beta, ya surement plein de bugs mais bon... (titou ma déja permis d'en resoudre :)
Autrement je voudrais remercier HVb pour son code sur la recup du source html d'une page web (http://www.vbfrance.com/code.aspx?id=5938) et de téléchargement de fichiers (http://www.vbfrance.com/code.aspx?id=6109)

Pour l'utiliser:
dim mp3 as new cls_mp3tag()
mp3.init(le_chemin_de_votre_mp3)
mp3.num = 1 'il doivent avoir un no, moi c'est la clé de ma base access
mp3.get_picture(le_dossier_dans_lequel_vous_voulez_l_enregistrer)
'affiche dans un picturebox
picturebox1.image=image.fromfile(mp3.img_path)
'affiche les tags dans un textbox
textbox1.text = mp3.artist & " - " & mp3.title & " - " & mp3.album

Enjoy :)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.