How To Get the Total Playing Time of an Audio CD

ID: Q112766


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0


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

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add a text box (Text1) and an MCI Control (MMControl1) to Form1.


  3. 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 


  4. 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.


REFERENCES

For more information, see the Multimedia Programmer's Reference, CDAudio

Additional query words: 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: August 31, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.