且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB和c#取得mp3长度

更新时间:2022-09-10 14:43:42

<< VB.Net >>


  1. Imports System.IO
  2. Imports Microsoft.Win32

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


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


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


  1. ' 轉換成時分秒
  2. Private Function Cvt2HMS(ByVal sec As Long) As String
  3. Dim h, m, s As Integer
  4. Cvt2HMS = ""
  5. h = sec \ 3600
  6. If h > 0 Then Cvt2HMS &= h.ToString & "時"
  7. m = (sec Mod 3600) \ 60
  8. If m > 0 Then Cvt2HMS &= m.ToString & "分"
  9. s = (sec Mod 3600) Mod 60
  10. If s > 0 Then Cvt2HMS &= s.ToString & "秒"
  11. 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");
  4. }


  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. string tm = new string((char)0, 128);
  11. if (mciSendString("open \"" + File + "\" type " + tp + " alias Media", null, 0, 0) == 0)
  12. {
  13. if (mciSendString("status Media length", tm, tm.Length, 0) == 0)
  14. {
  15. tm = tm.Trim((char)0);
  16. if (!string.IsNullOrEmpty(tm)) RetVal = Convert.ToInt64(tm) / 1000;
  17. }
  18. mciSendString("close Media", null, 0, 0);
  19. }
  20. return RetVal;
  21. } 本文转自94cool博客园博客,原文链接http://www.cnblogs.com/94cool/archive/2009/08/31/1557020.html,如需转载请自行联系原作者