1 Shell32
Add Reference: COM component of Microsoft Shell Controls and Automation//and then reference using Shell32; If you see "Unable to embed interop types using applicable interfaces" error ———— modify the reference inside the Shell32 property to the embedded interop type to false public static string Getmp3filedurationall (String fi Lename) {Shellclass sh = new Shellclass (); Folder dir = sh. NameSpace (Path.getdirectoryname (fileName)); FolderItem item = dir. ParseName (Path.getfilename (fileName)); String str = dir. Getdetailsof (item, 27); Gets the length of the song. 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 * + second + "|" + str; return Len; } else {return ""; } }
2.winmm.dll
[DllImport ("Kernel32", CharSet = CharSet.Auto)] static extern Int32 GetShortPathName (String path, StringBuilder Sho Rtpath, 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 is the song file address {if (File.exists (Musicfilepa TH))//Whether there is this file {//using the MCI command, the return value is file time StringBuilder ShortPath = new StringBuilder (8 0); 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);//Here you decide for yourself what format to return} return "0";//returns "0" if the file does not exist
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 * + second + "|" + duration; Duration = Len; } return duration; } }
ASP. NET get MP3 playback duration