Retrieve The Length Of WAV, AVI And MIDI Files

Know how much time run any WAV, AVI and MIDI file.

Module Code

Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _
    lpstrCommand As String, ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Form Code

Function GetMediaLength(FileName As String)

    Dim MediaLength As Long
    Dim RetString As String * 256
    Dim CommandString As String

    'open the media file
    CommandString = "Open " & FileName & " alias MediaFile"
    mciSendString CommandString, vbNullString, 0, 0&

    'get the media file length
    CommandString = "Set MediaFile time format milliseconds"
    mciSendString CommandString, vbNullString, 0, 0&
    CommandString = "Status MediaFile length"
    mciSendString CommandString, RetString, Len(RetString), 0&
    GetMediaLength = CLng(RetString)

    'close the media file
    CommandString = "Close MediaFile"
    mciSendString CommandString, vbNullString, 0, 0&

End Function


Private Sub Form_Load()

    Dim Seconds, Minutes As Integer
    Dim MilliSeconds As Long
    ' replace "c:\my_media_file.wav" with the path to your media file
    MilliSeconds = GetMediaLength("c:\my_media_file.wav")
    ' the function GetMediaLength return the media length in milliseconds,
    ' so we will calculate the total minutes and seconds
 
    Seconds = Int(MilliSeconds / 1000) Mod 60
    Minutes = Int(MilliSeconds / 60000)
    MilliSeconds = MilliSeconds Mod 1000

    TotalTime = Minutes & ":" & Seconds & ":" & MilliSeconds
    MsgBox (TotalTime)
   
End Sub

Go Back