Microsoft KB Archive/112766

= How To Get the Total Playing Time of an Audio CD =

Article ID: 112766

Article Last Modified on 10/29/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q112766



SUMMARY
The MultiMedia Application Programming Interface (API) and the MCI Control have built-in methods that retrieve the total playing time of a CD. The Length property of the MCI Control will retrieve total playing time of the current media.



MORE INFORMATION
After setting the MCI Control's TimeFormat property to MCI_FORMAT_TMSF(10) and summing the values returned by the DateAdd function to add the total time values for minutes and seconds together, you can calculate a CD's total playing time.

DateAdd starts adding from 12:00:00 AM, so you need to check to see if the total time is less than one hour. If it is, strip off the first three characters (12:) and the AM. If the time is greater than one hour, strip off the AM so that the return value looks like this:

1:09:43

-or-

57:45

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Add a text box (Text1) and an MCI Control (MMControl1) to Form1.  Place the following code in the Form_Load event of Form1: Sub Form_Load ' Initialize the CD: mmcontrol1.TimeFormat = 10 mmcontrol1.DeviceType = "CDAudio" mmcontrol1.Command = "Open" ' Dimension variables: Dim Length As Variant Dim CDSeconds As Integer, CDMinutes As Integer ' Calculate minutes and seconds: CDSeconds = CDSeconds + (mmcontrol1.Length And &HFF00&) / &H100 CDMinutes = CDMinutes + (mmcontrol1.Length And &HFF) ' Sum minutes and seconds: Length = DateAdd("s", CDSeconds, Length) ' Add Seconds Length = DateAdd("n", CDMinutes, Length) ' Add Minutes ' Determine if total running time is less than one hour: If (Left$(Length, 2)) = "12" Then ' Less than 1 hour Text1.Text = Mid(Length, 4, (Len(Length) - 4)) Else ' Greater than 1 hour: Text1.Text = Left(Length, (Len(Length) - 3)) End If  End Sub  Run the program. You should see the total playing displayed in the Text1 box.

NOTE: Before starting this program, a CD must be inserted and ready to play. Therefore, you should add code to detect when a CD has been inserted and then run the above code.

