iTunes 播放列表整理工具

在数字音乐时代,iTunes 作为苹果公司推出的音乐管理软件,深受广大用户的喜爱。然而,随着时间的推移,用户的播放列表可能会变得杂乱无章,特别是当涉及到多CD专辑时,专辑名称的不一致性会导致曲目分散在不同的播放列表中。为了解决这个问题,本文将介绍一个使用.NET Framework2.0编写的iTunes播放列表整理工具。

工具概述

这个工具的主要功能是重新创建播放列表,使用艺术家和专辑名称作为依据。它特别适用于触摸屏系统,因为iTunes的拖放功能可能会导致播放列表的混乱。例如,当不小心将一个播放列表拖放到另一个播放列表上时,就会发生复制粘贴的情况。如果孩子们使用这个功能,播放列表的秩序将变得混乱不堪。

使用前提

要使用这个工具,需要确保电脑上已经安装了iTunes,并且版本至少是v7。此外,还需要安装.NET Framework2.0。这个工具使用了ITDetectorLib库,这是iTunes v7版本中新增的一个特性,它允许在不启动iTunes的情况下检测iTunes是否已安装,并获取版本信息。

编程注意事项

如果打算使用Visual Studio编写自己的程序,请小心IDE自动完成iTunes变量类型。例如,如果想声明一个IITSource,需要按下Escape键,否则它会变成IITSourceCollection。同样,对于其他一些IITxxx类型的变量也是如此。

代码解析

代码中包含了大量的注释,以帮助理解要实现的功能。代码相对直观,作者还根据自己的个人喜好添加了一些额外的功能。例如,多CD专辑的命名可能存在差异,如第一张CD可能在专辑名称中包含(Disc 1),而其他CD可能包含[Disc x]。为了简化名称并将曲目整合到一个播放列表中,作者移除了"(Disc"、"[Disc"、" Vol"等字符串。

Imports iTunesLib Imports ITDETECTORLib Imports System.Text Imports System.Threading Module TidyPlayLists Structurebums Dim Name As String Dim TrackTable() As iTunesLib.IITTrack End Structure Dim Albums(0) Asbums Dim WithEvents iTunesApp As iTunesLib.iTunesApp Dim mainLibrary As IITSource Dim LibPlayLists As IITPlaylistCollection Sub Main() Dim iDetect As ITDETECTORLib.iTunesDetector = New ITDETECTORLib.iTunesDetector If iDetect.IsiTunesAvailable Then Try iTunesApp = New iTunesLib.iTunesApp mainLibrary = iTunesApp.LibrarySource LibPlayLists = mainLibrary.Playlists iTunesApp.BrowserWindow.Minimized = True iTunesApp.ForceToForegroundOnDialog = True Dim Sources As IITSourceCollection = iTunesApp.Sources For i As Integer = 1 To Sources.Count If Sources.Item(i).Kind = ITSourceKind.ITSourceKindLibrary Then KillPlayLists(Sources.Item(i)) BuildPlayLists(Sources.Item(i)) End If Next Console.WriteLine("Done...") iTunesApp.BrowserWindow.Maximized = True Catch ex As Exception Console.WriteLine("iTunes may be busy...") End Try Else Console.WriteLine("iTunes not installed, closing") End If End Sub Private Sub KillPlayLists(ByVal LibSource As IITSource) Dim currPlaylist As IITPlaylist Dim uPlaylist As IITUserPlaylist Dim Count As Integer = 1 Console.WriteLine("Delete PlayLists in " + LibSource.Name) Do While Count <= LibSource.Playlists.Count currPlaylist = LibSource.Playlists.Item(Count) If currPlaylist.Kind = ITPlaylistKind.ITPlaylistKindUser Then uPlaylist = currPlaylist If uPlaylist.SpecialKind = ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindNone Or uPlaylist.SpecialKind = ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindFolder Then Try currPlaylist.Delete() Catch ex As Exception End Try Else Count += 1 End If Else Count += 1 End If Loop End Sub Private Sub BuildPlayLists(ByVal LibSource As IITSource) Dim i As Integer Dim x As Integer Dim ab As String Console.WriteLine("Build Album Names from " + LibSource.Name) For i = 1 To LibSource.Playlists.Count Dim pl As IITPlaylist = LibSource.Playlists.Item(i) If pl.Kind = ITPlaylistKind.ITPlaylistKindLibrary Then For Each Track As IITTrack In pl.Playlists.Tracks Next ElseIf pl.Kind = ITPlaylistKind.ITPlaylistKindUser Then Dim ul As IITUserPlaylist = pl If ul.SpecialKind = ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindMusic Then For Each Track As IITTrack In ul.Playlists.Tracks Next End If End If Next For Each Track As IITTrack In LibSource.Playlists.Item(1).Tracks If Mid(Track.KindAsString, 1, 13) = "Protected AAC" Then ab = ".Purchased" ElseIf Track.Album <> Nothing Then ab = Track.Album.ToLower If InStr(ab, "(disc") <> 0 Then ab = Mid(ab, 1, InStr(ab, "(disc") - 1) If InStr(ab, "[") <> 0 Then ab = Mid(ab, 1, InStr(ab, "[") - 1) If InStr(ab, "vol") <> 0 Then ab = Mid(ab, 1, InStr(ab, "vol") - 1) ab = StripQuote(ab).Trim Else ab = ".No Album" End If For x = 0 To Albums.Length If Albums(x).Name = Nothing Then Albums(x).Name = ab ReDim Preserve Albums(x).TrackTable(1) Albums(x).TrackTable(0) = Track ReDim Preserve Albums(x + 1) Exit For ElseIf Albums(x).Name = ab Then Albums(x).TrackTable(Albums(x).TrackTable.GetUpperBound(0)) = Track ReDim Preserve Albums(x).TrackTable(Albums(x).TrackTable.GetUpperBound(0) + 1) Exit For End If Next Next For i = 0 To Albums.Length - 2 ab = Albums(i).TrackTable(0).Artist For x = 0 To Albums(i).TrackTable.Length - 2 If ab <> Albums(i).TrackTable(x).Artist Then ab = "" Exit For End If Next If ab.Length > 30 Then If InStr(ab, ",") > 0 Then ab = Mid(ab, 1, InStr(ab, ",") - 1) Else ab = "" End If If ab <> "" Then ab += " - " Dim albumPlaylist As iTunesLib.IITPlaylist = iTunesApp.CreatePlaylist(ab + StrConv(Albums(i).Name, VbStrConv.ProperCase)) For x = 0 To Albums(i).TrackTable.Length - 2 albumPlaylist.AddTrack(Albums(i).TrackTable(x)) Next Next End Sub Public Function StripQuote(ByVal Source As String) As String Dim strX As New StringBuilder(Source) strX.Replace("!", "") strX.Replace("'", "") strX.Replace("`", "") strX.Replace("´", "") strX.Replace("- ", "") strX.Replace("...", "") Return strX.ToString End Function End Module

使用说明

要使用这个工具,只需要将上述代码复制到Visual Studio中,并确保电脑上已经安装了iTunes和.NET Framework2.0。然后,编译并运行程序,它将自动整理iTunes播放列表。

沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485