我想编写一个通用函数来设置音乐文件的标记字段,我正在使用“ TagLib Sharp ”库,我正在尝试编码可以被称为“ TagLib Sharp Helper Class ”的东西
要调用该过程,我想使用这样的语法来设置字段“Album”:
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
答案 0 :(得分: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();
答案 1 :(得分: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