In this issue we are enhancing the class that we introduced in issue #70. This class controls the playback of an audio CD. This is the second 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. This version of the class has much better error-checking and many more features.
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.
Public Function TrackCount() As Long
Returns the number of tracks that are recorded on the CD currently loaded in the drive.
Public Property Get TrackNumber() As Long Public Property Let TrackNumber(lTrack As Long)
The get property returns the track the CD is currently cued or playing from. The let property will change the track number to the track passed in lTrack. If the CD was playing it will continue to play after the track change.
Public Function Mode() As String
Returns the current state of the CD device. Possible values that can be returned are "not ready", "paused", "playing", and "stopped" values. Some devices can return the additional "open", "parked", "seeking" values.
Public Property Get Position() As String Public Property Let Position(sPosition As String)
The get property returns the current cued or playing location of the CD device. The position is returned in the format specified with SetTimeFormat. The default for this class is tmsf which stands for tracks, minutes, seconds, and frames. This means that if the CD device is currently playing track two, the Position property might return 02:04:32:35 which would mean track 2 and 4 minutes 32 seconds and 35 frames into the track.
Public Property Get LengthOfDisc() As String
The length of the disc in the format specified by SetTimeFormat.
Public Function LengthOfTrack(lTrack As Long) As String
The length of the track specified by lTrack in the format specified by SetTimeFormat. This can be used to gather a list of tracks and the associated lengths.
Public Function IsPlaying() As Boolean
Returns True when a CD is playing and False otherwise.
Public Sub RewindTrack()
Rewinds a track to the start of the current track.
Public Sub SplitPosition(sPos As String, lTrack As Long, sTimeInto As String)
Splits the tmsf format (tt:mm:ss:ff) into the track number and the time component. A position such as what is returned by the Position method should be passed as the sPos parameter. The lTrack and sTimeInto variables will be set within this routine and returned to the caller.
See the above information for return values for the specific functions or properties. All routines might possibly return an error 5 (illegal function call) with the description set to the text that was returned from the multimedia library. The error source will be the method or function name where the error occurred. One example that might cause an error is if there is no CD device installed in the system.
The below sample will show you how to open the CD device and start playing it. For a complete CD player sample, http://www.codeoftheweek.com/membersonly/bi/issue71 for a downloadable project.
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/0072.html