0

我想编写一个通用函数来设置音乐文件的标签字段,我正在使用“ TagLib Sharp ”库,我正在尝试编写一些可以命名为“ TagLib Sharp Helper Class ”的东西

要调用该过程,我想使用类似这样的语法来设置字段“专辑”:

 TagLibSharp.Set_Tag_And_Save( _
             "C:\Test.mp3", _
             Function() TagLib.File.Create("").Tag.Album, _
             "Test album name" _
 )

问题是我不知道如何处理过程中的 Lambda 表达式......

这是所有代码(请注意解释问题所在的注释行):

#Region " TagLib Sharp "

Public Class TagLibSharp

''' <summary>
''' Stores the Taglib object.
''' </summary>
Private Shared TagFile As TagLib.File = Nothing

''' <summary>
''' Sets a Tag field and saves the file changes.
''' </summary>
Public Shared Sub Set_Tag_And_Save(ByVal File As String, _
                                   ByVal Field As Linq.Expressions.Expression(Of Func(Of Object)), _
                                   ByVal Value As String)

    TagFile = TagLib.File.Create(File)

    Dim member As Linq.Expressions.MemberExpression = _
        If(TypeOf Field.Body Is Linq.Expressions.UnaryExpression, _
           DirectCast(DirectCast(Field.Body, Linq.Expressions.UnaryExpression).Operand, Linq.Expressions.MemberExpression), _
           DirectCast(Field.Body, Linq.Expressions.MemberExpression))

    MsgBox(member.Member.Name) ' Result: "Album"

    ' member.Member = Value ' Here is where I don't know what to do
    '
    ' This would be the ewuivalent:
    TagFile.Tag.Album = Value

    TagFile.Save()

End Sub

End Class

#End Region

更新

尝试按照 Ben Allred 解决方案的步骤解决问题,但出现异常。

TagLibSharp.Set_Tag_And_Save("c:\1.mp3", Function() TagLib.File.Create("").Tag.Album = "Test album name")

修改后的子:

Public Shared Sub Set_Tag_And_Save(ByVal File As String, _
                                   ByVal FieldSetter As Action(Of TagLib.File))

    TagFile = TagLib.File.Create(File)
    FieldSetter(TagFile) ' Here throws an exception with this message: "taglib/"
    ' FieldSetter(TagLib.File.Create(File)) ' The same exception with this one.
    TagFile.Save()

End Sub

更新 2:

现在我没有任何异常,但未设置标签字段。

TagLibSharp.Set_Tag_And_Save("c:\Test.mp3", Function(tagLibFile) tagLibFile.Tag.Title = "Test title name")

修改后的子:

Public Shared Sub Set_Tag_And_Save(ByVal File As String, _
                                   ByVal FieldSetter As Action(Of TagLib.File))

    TagFile = TagLib.File.Create(File)

    MsgBox(TagFile.Tag.Title) ' Result: Unbreakeable
    FieldSetter(TagFile)
    MsgBox(TagFile.Tag.Title) ' Result: Unbreakeable

    ' TagFile.Tag.Title = "Test title name"
    ' MsgBox(TagFile.Tag.Title) ' Result: "Test title name"

    TagFile.Save()

End Sub
4

2 回答 2

1

(我的语法是 C#。我不知道 VB 中的确切语法。)

用你所拥有的东西做你想做的最简单的方法是进行以下更改:

1 - 将 lambda 表达式更改为采用一个类型的参数TagLib.File

public void Set_Tag_And_Save(..., Action<TagLib.File> FieldSetter, ...)

2 - 将操作传递给调用Set_Tag_And_Save并设置操作中的值。

Set_Tag_And_Save("C:\Test.mp3", tagLibFile => tagLibFile.Tag.Album = "Test album name");

3 - 调用里面的动作Set_Tag_And_Save

FieldSetter(TagFile);
TagFile.Save();
于 2013-10-05T17:50:06.547 回答
1

我做了一些修改,将其称为:

TagLibSharp.Set_Tag_Fields("C:\Test.mp3", _
                           {Sub(x) x.Tag.Title = "Title Test", _
                            Sub(x) x.Tag.Performers = {"Artist Test"}, _
                            Sub(x) x.Tag.Album = "Album Test", _
                            Sub(x) x.Tag.Year = "2000"})

这是完整的助手类:

#Region " TagLib Sharp Helper "


' [ TagLib Sharp Helper ]
'
' // By Elektro H@cker
'
'
' Instructions:
' 1. Add a reference to "taglib-sharp.dll" into the project.
'
'
' Examples:
'
' MsgBox(TagLibSharp.FileIsCorrupt("C:\File.mp3")) ' Result: True or False
' MsgBox(TagLibSharp.FileIsWriteable("C:\File.mp3")) ' Result: True or False
' MsgBox(TagLibSharp.Get_Title("C:\File.mp3"))
' MsgBox(TagLibSharp.Get_Artist("C:\File.mp3"))
' MsgBox(TagLibSharp.Get_Album("C:\File.mp3"))
' MsgBox(TagLibSharp.Get_Genre("C:\File.mp3"))
' MsgBox(TagLibSharp.Get_Year("C:\File.mp3"))
' MsgBox(TagLibSharp.Get_Basic_TagInfo("C:\File.mp3"))
' TagLibSharp.RemoveTag("C:\File.mp3", TagLib.TagTypes.Id3v1 Or TagLib.TagTypes.Id3v2) ' Removes ID3v1 + ID3v2 Tags
' TagLibSharp.Set_Tag_Fields("C:\Test.mp3", Sub(x) x.Tag.Title = "Title Test"})
' TagLibSharp.Set_Tag_Fields("C:\Test.mp3", {Sub(x) x.Tag.Title = "Title Test", Sub(x) x.Tag.Performers = {"Artist Test"}})


Public Class TagLibSharp

''' <summary>
''' Stores the Taglib object.
''' </summary>
Private Shared TagFile As TagLib.File = Nothing

''' <summary>
''' Checks if file is possibly corrupted.
''' </summary>
Public Shared Function FileIsCorrupt(ByVal File As String) As Boolean

    Try
        Return TagLib.File.Create(File).PossiblyCorrupt

    Catch ex As Exception
        Throw New Exception(ex.Message)
        Return True

    Finally
        If TagFile IsNot Nothing Then TagFile.Dispose()

    End Try

End Function

''' <summary>
''' Checks if file can be written.
''' </summary>
Public Shared Function FileIsWriteable(ByVal File As String) As Boolean

Try
    Return TagLib.File.Create(File).Writeable

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return True

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Get TagTypes of file.
''' </summary>
Public Shared Function Get_Tags(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).TagTypes.ToString

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Remove a entire Tag from file.
''' </summary>
Public Shared Sub RemoveTag(ByVal File As String, ByVal TagTypes As TagLib.TagTypes)

Try
    TagFile = TagLib.File.Create(File)
Catch ex As Exception
    Throw New Exception(ex.Message)
    Exit Sub
End Try

Try

    If Not TagFile.PossiblyCorrupt _
    AndAlso TagFile.Writeable Then

        TagFile.RemoveTags(TagTypes)
        TagFile.Save()

    End If

Catch ex As Exception
    Throw New Exception(ex.Message)

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Sub

''' <summary>
''' Gets the Title tag field of file.
''' </summary>
Public Shared Function Get_Title(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).Tag.Title

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Gets the Artist tag field of file.
''' </summary>
Public Shared Function Get_Artist(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).Tag.Performers(0)

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Gets the Album tag field of file.
''' </summary>
Public Shared Function Get_Album(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).Tag.Album

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Gets the Genre tag field of file.
''' </summary>
Public Shared Function Get_Genre(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).Tag.Genres(0)

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Gets the Year tag field of file.
''' </summary>
Public Shared Function Get_Year(ByVal File As String) As String

Try
    Return TagLib.File.Create(File).Tag.Year

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Gets the basic tag fields of file.
''' </summary>
Public Shared Function Get_Basic_TagInfo(ByVal File As String) As String

Try
    TagFile = TagLib.File.Create(File)

    Return String.Format("Title: {1}{0}Artist: {2}{0}Album: {3}{0}Genre: {4}{0}Year: {5}", Environment.NewLine, _
                         TagFile.Tag.Title, _
                         TagFile.Tag.Performers(0), _
                         TagFile.Tag.Album, _
                         TagFile.Tag.Genres(0), _
                         TagFile.Tag.Year)

Catch ex As Exception
    Throw New Exception(ex.Message)
    Return String.Empty

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Function

''' <summary>
''' Sets a Tag field.
''' </summary>
Public Shared Sub Set_Tag_Fields(ByVal File As String, _
                                   ByVal FieldSetter As Action(Of TagLib.File))

Try
    TagFile = TagLib.File.Create(File)
Catch ex As Exception
    Throw New Exception(ex.Message)
    Exit Sub
End Try

Try

    If Not TagFile.PossiblyCorrupt _
    AndAlso TagFile.Writeable Then

        FieldSetter(TagFile)
        TagFile.Save()

    End If

Catch ex As Exception
    Throw New Exception(ex.Message)

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Sub

''' <summary>
''' Sets multiple Tag fields.
''' </summary>
Public Shared Sub Set_Tag_Fields(ByVal File As String, _
                                   ByVal FieldSetter() As Action(Of TagLib.File))

Try
    TagFile = TagLib.File.Create(File)
Catch ex As Exception
    Throw New Exception(ex.Message)
    Exit Sub
End Try

Try

    If Not TagFile.PossiblyCorrupt _
    AndAlso TagFile.Writeable Then

        For Each Field In FieldSetter
            Field(TagFile)
        Next

        TagFile.Save()

    End If

Catch ex As Exception
    Throw New Exception(ex.Message)

Finally
    If TagFile IsNot Nothing Then TagFile.Dispose()

End Try

End Sub

End Class

#End Region
于 2013-10-05T19:53:34.317 回答