VB和c#取得mp3长度

<< VB.Net >>

  1. Imports System.IO  
  2. Imports Microsoft.Win32 
  1. ' API 宣告  
  2. Private Declare Function mciSendStringA Lib "Winmm.dll" _  
  3.     (ByVal lpszCommand As StringByVal lpszReturnString As String, _  
  4.     ByVal cchReturn As IntegerByVal hwndCallback As IntPtr) As Integer 

 

  1. Private Sub Button1_Click(ByVal s As ObjectByVal e As EventArgs) Handles Button1.Click  
  2.  
  3.     MessageBox.Show(Cvt2HMS(GetMediaLen("C:\音樂欣賞\Candy Shop.mp3")))  
  4.     MessageBox.Show(Cvt2HMS(GetMediaLen("C:\音樂欣賞\國境之南.wmv")))  
  5.     MessageBox.Show(Cvt2HMS(GetMediaLen("C:\音樂欣賞\Time to say goodbye.wma")))  
  6.     MessageBox.Show(Cvt2HMS(GetMediaLen("C:\音樂欣賞\寶貝.flv")))  
  7.  
  8. End Sub 

 

  1. ' 取得多媒體檔案長度  
  2. Private Function GetMediaLen(ByVal File As StringAs Long 
  3.  
  4.     Dim key As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\MCI Extensions" 
  5.     Dim RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(key)  
  6.     Dim FileExt As String = (Path.GetExtension(File).Replace("."""))  
  7.     Dim tp As String = RegKey.GetValue(FileExt, "MPEGVideo")  
  8.     RegKey.Close()  
  9.  
  10.     Dim tm As New String(Chr(0), 128)  
  11.     Dim cmd As String 
  12.     cmd = "open """ & File & """ type " & tp & " alias Media" 
  13.  
  14.     If mciSendStringA(cmd, vbNullString, 0, 0) = 0 Then 
  15.         If mciSendStringA("status Media length", tm, tm.Length, 0) = 0 Then 
  16.             If tm <> "" Then GetMediaLen = Convert.ToInt32(tm) \ 1000  
  17.         End If 
  18.         mciSendStringA("close Media", vbNullString, 0, 0)  
  19.     End If 
  20.  
  21. End Function 

 

  1. ' 轉換成時分秒  
  2. Private Function Cvt2HMS(ByVal sec As LongAs String 
  3.  
  4.     Dim h, m, s As Integer 
  5.     Cvt2HMS = "" 
  6.     h = sec \ 3600  
  7.     If h > 0 Then Cvt2HMS &= h.ToString & "時" 
  8.     m = (sec Mod 3600) \ 60  
  9.     If m > 0 Then Cvt2HMS &= m.ToString & "分" 
  10.     s = (sec Mod 3600) Mod 60  
  11.     If s > 0 Then Cvt2HMS &= s.ToString & "秒" 
  12.  
  13. End Function 

 

<< C# >>

  1. using System.IO;  
  2. using Microsoft.Win32; 
  1. // API 宣告  
  2. [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]  
  3. public static extern int mciSendString(  
  4.     string lpstrCommand, string lpstrReturnString,  
  5.     int uReturnLength, int hwndCallback); 

 

  1. private void button1_Click(object s, EventArgs e)  
  2. {  
  3.     MessageBox.Show(GetMediaLen(@"D:\音樂欣賞\Dirty.mp3").ToString() + " Sec");  

 

  1. // 取得多媒體檔案長度  
  2. private long GetMediaLen(string File)  
  3. {  
  4.     long RetVal = 0;  
  5.     string key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions";  
  6.     RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(key);  
  7.     string FileExt = Path.GetExtension(File).Replace(".""");  
  8.     string tp = RegKey.GetValue(FileExt, "MPEGVideo").ToString();  
  9.     RegKey.Close();  
  10.  
  11.     string tm = new string((char)0, 128);  
  12.     if (mciSendString("open \"" + File + "\" type " + tp + " alias Media"null, 0, 0) == 0)  
  13.         {  
  14.         if (mciSendString("status Media length", tm, tm.Length, 0) == 0)  
  15.         {  
  16.             tm = tm.Trim((char)0);  
  17.             if (!string.IsNullOrEmpty(tm)) RetVal = Convert.ToInt64(tm) / 1000;  
  18.         }  
  19.         mciSendString("close Media"null, 0, 0);  
  20.     }  
  21.     return RetVal;  
posted @   94cool  阅读(380)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2009年8月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示