编程语言
首页 > 编程语言> > C#计算MP3音频时长

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;
            }
           
        }

标签:string,C#,音频,System,Substring,dir,MP3,Path,songPath
来源: https://www.cnblogs.com/wml-it/p/16427566.html