How To Get the Total Playing Time of an Audio CD

Last reviewed: June 21, 1995
Article ID: Q112766
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic 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 reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.