• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

asp.net获取mp3播放时长

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1 Shell32

//添加引用:COM组件的Microsoft Shell Controls And Automation
        //然后引用 using Shell32;
        //如果出现“无法嵌入互操作类型 请改用适用的接口”报错————修改引用里面的shell32属性嵌入互操作类型改为false
        public static string GetMP3FileDurationAll(string fileName)
        {
            ShellClass sh = new ShellClass();
            Folder dir = sh.NameSpace(Path.GetDirectoryName(fileName));
            FolderItem item = dir.ParseName(Path.GetFileName(fileName));
            string str = dir.GetDetailsOf(item, 27); // 获取歌曲时长。
            FileOperate.Write_Txt(DateTime.Now.ToString("yyyyMMddHHmmss") + "_GetMP3", DateTime.Now + "  \n\r" + str + "\n");
            if (str != "")
            {
                var matchs = Regex.Match(str, @"(\d{2}):(\d{2}):(\d{2})");
                var hour = Convert.ToInt32(matchs.Groups[1].Value);
                var minute = Convert.ToInt32(matchs.Groups[2].Value);
                var second = Convert.ToInt32(matchs.Groups[3].Value);

                var len = hour * 3600 + minute * 60 + second + "|" + str;
                return len;
            }
            else {
                return "";
            }
           


        }

  2.winmm.dll

[DllImport("Kernel32", CharSet = CharSet.Auto)]
        static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
        [DllImport("winmm.dll")]
        public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
        public static string MusicTime(string musicFilePath)//musicFilePath是歌曲文件地址
        {
            if (File.Exists(musicFilePath))//是否存在这个文件
            {
                //利用MCI命令,返回值为文件时间
                StringBuilder shortpath = new StringBuilder(80);
                GetShortPathName(musicFilePath, shortpath, shortpath.Capacity);
                string musicname = shortpath.ToString();
                StringBuilder buf = new StringBuilder(80);
                mciSendString("close all", buf, buf.Capacity, 0);
                mciSendString("open " + musicname + " alias media", buf, buf.Capacity, 0);
                mciSendString("status media length", buf, buf.Capacity, 0);
                return buf.ToString().Trim();
                double ms = Convert.ToDouble(buf.ToString().Trim());
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)ms);
                return ts.ToString().Substring(3, 5);//这里你自己去决定返回的是什么格式
            }
            return "0";//如果文件不存在就返回"0"
        }

  3 ffmpeg

public static string Fromffmpeg(string fileName, string ffmpegVirtualPath)
        {
            string duration = "";
            using (System.Diagnostics.Process pro = new System.Diagnostics.Process())
            {
                pro.StartInfo.UseShellExecute = false;
                pro.StartInfo.ErrorDialog = false;
                pro.StartInfo.RedirectStandardError = true;

                pro.StartInfo.FileName = System.Web.HttpContext.Current.Server.MapPath(ffmpegVirtualPath);// AppDomain.CurrentDomain.BaseDirectory +"ffmpeg.exe";
                pro.StartInfo.Arguments = " -i " + fileName;

                pro.Start();
                System.IO.StreamReader errorreader = pro.StandardError;
                pro.WaitForExit(1000);

                string result = errorreader.ReadToEnd();
                if (!string.IsNullOrEmpty(result))
                {
                    result = result.Substring(result.IndexOf("Duration: ") +("Duration: ").Length, ("00:00:00").Length);
                    duration = result;
                    var matchs = Regex.Match(duration, @"(\d{2}):(\d{2}):(\d{2})");
                    var hour = Convert.ToInt32(matchs.Groups[1].Value);
                    var minute = Convert.ToInt32(matchs.Groups[2].Value);
                    var second = Convert.ToInt32(matchs.Groups[3].Value);

                    var len = hour * 3600 + minute * 60 + second + "|" + duration;
                    duration = len;
                }
                return duration;

            }
        }

  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何在ASP.NET中使用三层架构发布时间:2022-07-10
下一篇:
ASP.NETMVC程序播放H.264视频发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap