如何用API函数获得MP3、WMA等歌曲的时间长度?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/5656/5656292.xml?temp=.5834467看这个帖
      

  2.   

    Option Explicit'Public Declare Function
    Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As LongFunction shortname(LongPath As String) As String
        Dim ShortPath As String
        Dim pos As String
        Dim Ret&
        Const MAX_PATH = 260
        
        ShortPath = Space$(MAX_PATH)
        Ret& = GetShortPathName(LongPath, ShortPath, MAX_PATH)
        If Ret& Then
            pos = InStr(1, ShortPath, " ")
            shortname = Left$(ShortPath, pos - 2)
        End If
    End Function
    '
    Public Sub play(FileName As String)
        Dim Cb As Long
        Dim PlayStatus As String * 20
        Dim ShortFileName As String
        Dim TotalLength As Long
        
        If Dir(FileName) = "" Then
            MsgBox "文件" & FileName & "不存在!", vbInformation, "提示"
            Exit Sub
        End If
        mciExecute "close all"
        ShortFileName = shortname(FileName)
        mciExecute "open " & ShortFileName & " alias mp3"
        '得到时间长度
        mciSendString "status mp3 length", PlayStatus, Len(PlayStatus), Cb
        TotalLength = Val(Left(PlayStatus, 9))
    End SubPrivate Sub Form_Load()
        play "C:\ABC.wav"
    End Sub
      

  3.   

    用mciSendString这个函数可以获取一些歌曲的信息:
    歌曲长度:
       TCHAR cmd[266],shortname[256],name[256]="c:\\song\\青花瓷.mp3";
       TCHAR buffer[256];  //定义缓冲区的大小
        long length,curposition; //length歌曲长度  curposition记录当前播放位置
       GetShortPathName(name,shortname,sizeof(shortname)/sizeof(TCHAR));  //转换成短路径   wsprintf(cmd,"status %s length",shortname);
       mciSendString(cmd,buffer,sizeof(buffer)/sizeof(TCHAR),NULL);
       length=atol(buffer);  //单位毫秒   wsprintf(cmd,"status %s positon",shortname);
       mciSendString(cmd,buffer,sizeof(buffer)/sizeof(TCHAR),NULL);
       curpositon=atol(buffer);  //单位毫秒
       
      还有很多 请到:http://www.ahscyz.net.cn/xkzy/ShowArticle.asp?ArticleID=240