Visual Basic Code of the Week (COTW)
Issue #70
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Free Monthly Software Giveaway

Be sure to enter at now! If you entered last month, please be sure to enter again because your entry from last month does not count towards the prize this month. The prize is development tools worth over $300.

In this Issue

This issue we are introducing a class module to control the playback of an audio CD. This is the first in a two-part series about controlling the CD player using the mci library routines.

This source code is designed for VB 5.0 and up. Nearly all of it can be used in 4.0 32-Bit by removing the Enum and optional parameters. Questions? Email us at


The CDController class will show you how to control your audio CD player with a few easy-to-call methods.

Public Sub InitializeDevice()

InitializeDevice will setup the CD Player to play audio CDs.

Public Function IsMediaPresent() As Boolean

IsMediaPresent will return True if there is playable media in the CD drive otherwise it will return False.

Public Function TrackCount() As Long

TrackCount will return the number of tracks that are on the CD currently in the CD drive.

Public Sub CloseAll()

CloseAll will close any and all channels that the CDController class has opened. You should do this before terminating any program that uses the CDController class.

Public Sub Play(Optional lStartTrack As Long = 0, Optional lFinishTrack As Long = 0)
Public Sub Pause()
Public Sub StopNow()

Play will start playing the CD at its current track location unless you specify the starting track. You can specify the start and end track so if you only want to play tracks 4 through 6 call Play with the parameters 4,6. In a future issue we will show how to randomize the playing of a bunch of tracks and other fun stuff like that.

Pause and StopNow work exactly like you would expect. We would have liked to use Stop for this method name, but it is a reserved word so we used StopNow instead.

Sample Usage

The below sample will show you how to open the CD device and start playing it.

    Dim CD As New CDController

    If CD.IsMediaPresent Then
        MsgBox "There are " & CD.TrackCount & " tracks on this CD."
    End If

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945