Due to the need to use real-time read audio and video files (MP3, WMA, WMV ...) Playback time length of the function, the results of the search are:
(1) Hard code Analysis of audio-visual files, the need to analyze various media formats, the most expensive;
(2) The use of Wmlib SDK, the need to familiarize themselves with the various SDK interface, and different versions of the WM interface, the cost of the second;
(3) using the COM interface of the system SHELL32, directly accessing the media stylistic properties, taking its specific content, the cost is minimal.
It is clear that the 3rd option is the fastest and immediately fenced:
① reference Shell32 the underlying interface c:\windows\system32\ Shell32.dll,vs automatically converted to Interop.Shell32.dll (note: 64-bit system and 32-bit system generated Interop.Shell32.dll different)
② encoding Read playback time length:
/// <summary>
/// 获取媒体播放时间长度,格式00:00:00。
/// </summary>
/// <param name="path">媒体路径</param>
/// <returns>播放时间长度</returns>
public static string GetMediaDuration(this string path)
{
try
{
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
Shell32.FolderItem folderItem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
return folder.GetDetailsOf(folderItem, 21);
}
catch (Exception ex)
{
ex.Error();
return null;
}
}