C#计算MP3音频时长

计算音频的时长

  /// <summary>
        /// 获取mp3文件的歌曲时间长度
        /// </summary>
        /// <param name="songPath"></param>
        /// <returns></returns>
        private string GetMp3Times(string songPath)
        {
            try
            {
                //完整路径
                songPath = System.Environment.CurrentDirectory + "\\" + songPath;
                string dirName = System.IO.Path.GetDirectoryName(songPath);
                string SongName = System.IO.Path.GetFileName(songPath);//获得歌曲名称  
                // FileInfo fInfo = new FileInfo(songPath);
                ShellClass sh = new ShellClass();
                Folder dir = sh.NameSpace(dirName);
                FolderItem item = dir.ParseName(SongName);
                string s = Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取歌曲时间

                //处理时间
                if (s.StartsWith("0:00"))
                {
                    s = s.Substring(s.Length - 2, 2);
                    if (s.StartsWith("0"))
                    {
                        s = s.Substring(1);
                    }
                }
              
                return s;
            }
            catch (Exception ex)
            {

                throw;
            }
           
        }
posted @   码农阿亮  阅读(702)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-06-30 C#中的小数和百分数计算
点击右上角即可分享
微信分享提示