突发奇想,想弄个显示MP3文件信息和专辑图片的小Demo,个人不是大牛,遂百度之,总算搞定,现分享如下。
效果图:
GIF效果图:
主要是依靠2个DLL文件:ID3.dll 和 Interop.Shell32.dll,步骤如下:
1.保存音乐文件到本地
#region 保存音乐文件到本地 string strMp3 = @"~/upload/musics/"; if (!Directory.Exists(Server.MapPath(strMp3))) { Directory.CreateDirectory(Server.MapPath(strMp3)); } strMp3+= fileMp3.FileName ; if (File.Exists(Server.MapPath(strMp3))) { File.Delete(Server.MapPath(strMp3)); } fileMp3.SaveAs(Server.MapPath(strMp3)); #endregion
2.获取音乐文件信息
#region 获取音乐文件信息 string mp3InfoInterHtml = ""; ShellClass sh = new ShellClass(); Folder dir = sh.NameSpace(Path.GetDirectoryName(Server.MapPath(strMp3))); FolderItem item = dir.ParseName(Path.GetFileName(Server.MapPath(strMp3))); mp3InfoInterHtml += "文件名:" + dir.GetDetailsOf(item, 0)+"<br>"; mp3InfoInterHtml += "文件大小:" + dir.GetDetailsOf(item, 1) + "<br>"; mp3InfoInterHtml += "歌曲名:" + dir.GetDetailsOf(item, 21) + "<br>"; mp3InfoInterHtml += "歌手:" + dir.GetDetailsOf(item, 13) + "<br>"; mp3InfoInterHtml += "专辑:" + dir.GetDetailsOf(item, 14) + "<br>"; mp3InfoInterHtml += "时长:" + dir.GetDetailsOf(item, 27) + "<br>"; #endregion
3.显示专辑图片
#region 显示专辑图片 string picturePath = @"~/image/play_null_img.png"; if (!Directory.Exists(Server.MapPath(@"~/upload/images/"))) { Directory.CreateDirectory(Server.MapPath(@"~/upload/images/")); } // 加载MP3 ID3Info info = new ID3Info(Server.MapPath(strMp3), true); System.Drawing.Image image = null; if (info.ID3v2Info.AttachedPictureFrames.Count > 0) { image = System.Drawing.Image.FromStream(info.ID3v2Info.AttachedPictureFrames.Items[0].Data); picturePath = @"~/upload/images/" +DateTime.Now.ToString("yyyyMMddHHmmss")+ ".png"; if (File.Exists(Server.MapPath(picturePath))) { File.Delete(Server.MapPath(picturePath)); } image.Save(Server.MapPath(picturePath)); } imgMP3.ImageUrl = picturePath; dMp3.InnerHtml = mp3InfoInterHtml; #endregion
4.修改文件上传限制
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
源代码:GetMp3Info.zip
参考文章:
http://www.cnblogs.com/08shiyan/p/3579822.html
http://www.codeproject.com/Articles/17890/Do-Anything-With-ID