您的位置:首页 > 编程语言 > C#

C# 获取音乐相关信息

2016-07-20 14:37 453 查看
目前对于支持大部分格式的方法找到了两个

像ID3这样只支持MP3和m4a格式的就不说了

1.使用Microsoft Shell Controls And Automation

即引用对应的com组件即可



在代码中添加

using Shell32;


使用方法如下

string[] Info = new string[7];
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(System.IO.Path.GetDirectoryName(path));
FolderItem item = dir.ParseName(System.IO.Path.GetFileName(path));
Info[0] = dir.GetDetailsOf(item, 21);
//Info[1] = dir.GetDetailsOf(item, 20);
Info[6] = dir.GetDetailsOf(item, 14);
Info[3] = dir.GetDetailsOf(item, 27);
Info[3]= Info[3].Substring(Info[3].IndexOf(":") + 1);
Info[4] = dir.GetDetailsOf(item, 1);


获取的信息全靠GetDetailsOf的第二个参数

全部可获取信息如下

ID  => DETAIL-NAME
0   => Name     //文件名
1   => Size     //文件大小
2   => Type
3   => Date modified
4   => Date created
5   => Date accessed
6   => Attributes
7   => Offline status
8   => Offline availability
9   => Perceived type
10  => Owner
11  => Kinds
12  => Date taken
13  => Artists   //艺术家
14  => Album     //专辑
15  => Year      //出版年份
16  => Genre
17  => Conductors
18  => Tags
19  => Rating
20  => Authors   //同艺术家
21  => Title     //标题
22  => Subject
23  => Categories
24  => Comments
25  => Copyright
26  => #         //编号
27  => Length    //时长
28  => Bit rate
29  => Protected
30  => Camera model
31  => Dimensions
32  => Camera maker
33  => Company
34  => File description
35  => Program name
36  => Duration
37  => Is online
38  => Is recurring
39  => Location
40  => Optional attendee addresses
41  => Optional attendees
42  => Organizer address
43  => Organizer name
44  => Reminder time
45  => Required attendee addresses
46  => Required attendees
47  => Resources
48  => Free/busy status
49  => Total size
50  => Account name
51  => Computer
52  => Anniversary
53  => Assistant's name
54  => Assistant's phone
55  => Birthday
56  => Business address
57  => Business city
58  => Business country/region
59  => Business P.O. box
60  => Business postal code
61  => Business state or province
62  => Business street
63  => Business fax
64  => Business home page
65  => Business phone
66  => Callback number
67  => Car phone
68  => Children
69  => Company main phone
70  => Department
71  => E-mail Address
72  => E-mail2
73  => E-mail3
74  => E-mail list
75  => E-mail display name
76  => File as
77  => First name
78  => Full name
79  => Gender
80  => Given name
81  => Hobbies
82  => Home address
83  => Home city
84  => Home country/region
85  => Home P.O. box
86  => Home postal code


但这种方法大部分情况下无法正常获取作者

像这种



这个方法只能读取那个 参与创作的艺术家 下面那个 唱片集艺术家 无法获取

但好多歌曲都是只有 唱片集艺术家 有内容

还有一个不足就是无法获取专辑封面,而大部分的歌曲内都包含了专辑封面

2.使用Taglib-sharp

下载

c#项目中直接引用 添加命名空间后即可使用

使用方法如下:

TagLib.File f = TagLib.File.Create(path);
if (f.Tag.AlbumArtists.Length != 0)
Info[1] = f.Tag.AlbumArtists[0];
else
Info[1] = "";
if (f.Tag.Artists.Length != 0)
Info[5] = f.Tag.Artists[0];
else
Info[5] = "";
Info[2] = f.Tag.Album;


获取专辑封面

public static MemoryStream GetCover(string path)
{
TagLib.File f = TagLib.File.Create(path);
if (f.Tag.Pictures != null && f.Tag.Pictures.Length != 0)
{
var bin = (byte[])(f.Tag.Pictures[0].Data.Data);
return new MemoryStream(bin);
}
else
return null;
}


var ss = GetMusicInfo.GetCover(path);
if (ss != null)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ss;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
image.Source = bitmap;
}


设置专辑封面

public static void SetCover(string path)
{
TagLib.File file = TagLib.File.Create(path);
TagLib.Picture pic = new TagLib.Picture();
pic.Type = TagLib.PictureType.FrontCover;
pic.Description = "Cover";
pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
pic.Data = new TagLib.ByteVector(System.IO.File.ReadAllBytes("D://aaa//aaa.jpg"));
file.Tag.Pictures = new TagLib.IPicture[] { pic };
file.Save();
}


Tag中有好多属性可以获取

不过Taglib-sharp对中文编码的支持不好,会出现乱码

建议两中方法结合使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: