Be sure to enter at http://www.codeoftheweek.com/contest.html 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.
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 questions@codeoftheweek.com.
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.
The below sample will show you how to open the CD device and start playing it.
Dim CD As New CDController CD.InitializeDevice If CD.IsMediaPresent Then MsgBox "There are " & CD.TrackCount & " tracks on this CD." CD.Play End If CD.CloseAll
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: http://www.codeoftheweek.com/membersonly/bi/0070.html