zoukankan      html  css  js  c++  java
  • 【ASP.NET 进阶】获取MP3文件信息并显示专辑图片

    突发奇想,想弄个显示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

  • 相关阅读:
    【XShell】xshell 中“快速命令集”的使用
    【Ubuntu】Vritual Box 复制方式克隆
    【Linux】快速清空当前文件
    iOS---友盟推送遇到的坑
    iOS---stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0
    iOS---searchBar 搜索框 光标初始位置后移
    iOS---设置输入框的光标位置
    iOS tableviewcell 分割线 偏移和颜色
    iOS---去除url中的反斜扛
    iOS---UISearchBar限制输入字数
  • 原文地址:https://www.cnblogs.com/yc-755909659/p/4964571.html
Copyright © 2011-2022 走看看