[ASP. NET advanced] gets MP3 file information and displays album images,
In a whim, I want to get a Demo that shows MP3 file information and album images. I am not a big guy, So Baidu has done it. I will share the following.
:
GIF:
It mainly relies on two DLL files: ID3.dll and Interop. Shell32.dll. The steps are as follows:
1. Save the music file to your local device.
# Region Save the music file to a local 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. Get music file information
# Region obtain music file information 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 + = "file name:" + dir. getDetailsOf (item, 0) + "<br>"; mp3InfoInterHtml + = "file size:" + dir. getDetailsOf (item, 1) + "<br>"; mp3InfoInterHtml + = "song name:" + dir. getDetailsOf (item, 21) + "<br>"; mp3InfoInterHtml + = "singer:" + dir. getDetailsOf (item, 13) + "<br>"; mp3InfoInterHtml + = "album:" + dir. getDetailsOf (item, 14) + "<br>"; mp3InfoInterHtml + = "Duration:" + dir. getDetailsOf (item, 27) + "<br>"; # endregion
3. Show album Images
# Region show album image string picturePath = @"~ /Image/play_null_img.png "; if (! Directory. Exists (Server. MapPath (@"~ /Upload/images/") {Directory. CreateDirectory (Server. MapPath (@"~ /Upload/images/");} // load 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. Modify file upload restrictions
Source code: GetMp3Info.zip
References:
Http://www.cnblogs.com/08shiyan/p/3579822.html
Http://www.codeproject.com/Articles/17890/Do-Anything-With-ID