使用Shell32讀取影音檔案屬性

來源:互聯網
上載者:User

由於需要用到即時讀取影音檔案(mp3、wma、wmv …)播放時間長度的功能,搜尋到的結果有:

(1)寫入程式碼分析影音檔案,需要分析各種媒體格式,代價最大;

(2)使用WMLib SDK,需要熟悉SDK各個介面,且不同版本的WM介面有別,代價次之;

(3)使用系統Shell32的COM介面,直接存取媒體文體屬性,取其特定內容,代價最小。

顯然第3種方案見效最快,立即操刀:

①引用Shell32底層介面c:\windows\system32\shell32.dll,VS自動轉換成Interop.Shell32.dll(註:64位系統和32位系統產生的Interop.Shell32.dll不一樣)

②編碼讀取播放時間長度:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.