Re: how to get a wav file track length
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Sun, 25 Nov 2007 18:35:07 -0500
"caver_dave" <caverdave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E24A328F-E870-4A60-A79C-F145125022A5@xxxxxxxxxxxxxxxx
Now i know this may sound like a stupid question, no change there!
But how do I get the track / play time of a wav file?
I am currently using the windows media player to do this but it appears to
have a downside that it does not travel well and different boxes do not
automatically include the ref to the dll/ocx when opening the project in
the
IDE.
I have googled for a solution but only come up with how to play a wav
file,
plaese can anyone help?
Is there a simple solution?
Or even a fiendishly complicated one?
Using pure VB or API, I don't mind as long as I can erase the media player
reference!
One way that doesn't require a reference to anything is to use MCI. It's
pretty simple:
-----BEGIN CODE
Option Explicit
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 Long
Private Function GetShortFileName(ByVal sFileName As String) As String
Dim sBuffer As String
Dim lRet As Long
Const MAX_PATH As Long = 260
sBuffer = String$(MAX_PATH, vbNullChar)
lRet = GetShortPathName(sFileName, sBuffer, MAX_PATH)
GetShortFileName = Left$(sBuffer, lRet)
End Function
Private Sub Form_Click()
Dim sFileName As String
Dim lRet As Long
Dim sBuffer As String
Const ALIAS As String = "WAVFILE"
sFileName = GetShortFileName("k:\sounds\fishhead.wav")
lRet = mciSendString("open " & sFileName & " type waveaudio alias " &
ALIAS & " wait", vbNullString, 0&, 0&)
If lRet = 0 Then
sBuffer = String$(128, vbNullChar)
'The status command with the length parameter will return the length
in milliseconds
lRet = mciSendString("status " & ALIAS & " length wait", sBuffer,
Len(sBuffer), 0&)
sBuffer = Left$(sBuffer, Len(sBuffer) - 1)
End If
mciSendString "close " & ALIAS, vbNullString, 0&, 0&
If lRet <> 0 Then
MsgBox "Unable to open .wav file or obtain length"
Else
MsgBox CMinSec(CLng(sBuffer)) & " seconds"
End If
End Sub
Private Function CMinSec(ByVal lMilliSecs As Long) As String
'Converts milliseconds to minutes and seconds in a format of
'"mm:ss.ss"
Dim iHundredths As Integer
Dim iMins As Integer
Dim iSecs As Integer
'Convert to seconds
iSecs = lMilliSecs \ 1000
'Determine minutes
iMins = iSecs \ 60
'Determine remaining seconds
If iMins >= 1 Then
iSecs = iSecs Mod (iMins * 60)
End If
'Determine hundredths of second
If lMilliSecs Mod 60000 = 0 Then
'exactly a minute interval (1:00.00, 2:00.00, etc.)
iSecs = 0
iHundredths = 0
Else
iHundredths = (lMilliSecs Mod 1000) \ 10
End If
CMinSec = Format$(iMins, "00") & ":" & Format$(iSecs, "00") & "." &
Format$(iHundredths, "00")
End Function
-----END CODE
It's VERY important to get and only use short paths and filenames with MCI.
This is because spaces in a path or filename screw up MCI because spaces are
used to separate parameters in the MCI command string. You can sometimes get
away with enclosing a long path/filename in quotation marks, but this is not
ideal either because MCI is limited on the total length of a command string
and long paths and/or filenames can easily cause you to exceed this limit.
Alternatively, you can send command messages instead of command strings (use
the mciSendCommand API function for messages). But, IMO, command messages
are not nearly as easy-to-use as strings, despite the hit of string
concatenation and getting the short path/file name (which only needs done
once if you use an alias, and you should always use an alias anyway).
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: how to get a wav file track length
- From: caver_dave
- Re: how to get a wav file track length
- Prev by Date: Re: How to find the datatype of a variable?
- Next by Date: Re: Reading a text file from the internet
- Previous by thread: Re: how to get a wav file track length
- Next by thread: Re: how to get a wav file track length
- Index(es):
Relevant Pages
|