If you want to play a track of an audio CD from VB you can use MCI functions. The main MCI function is mciSendString, that sends command strings to the system and execute them:
Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
The first argument is the command string, lpstrReturnString receives return information (if needed), uReturnLength indicates the size, in characters, of lpstrReturnString, and hwndCallback is used for system notification. Remember that every command string described in this tip must be sent to the system with:
CommandString = "Your MCI command here"RetVal = mciSendString(CommandString, vbNullString, 0, 0)
First you must open the CDAudio device with
CommandString = "Open cdaudio alias MyCDAudio"
“MyCDAudio” is an arbitrary name that you’ll use in subsequent command strings as an alias of the filename. After opening the device you have to set the time format to a special mode called tracks/minutes/seconds/frames (TMSF): in this mode you can select a track indicating only its number, instead of the precise time location in on the CD:
CommandString = "Set MyCDAudio time format TMSF"
Now you can play the CD track with the command
' Play the Nth trackCommandString = "Play MyCDAudio from " & N & " to " & (N + 1) & " wait"
For example, to play the track number 4, the command string is
CommandString = "Play MyCDAudio from 4 to 5 wait"
The execution is synchronous, but you can play the file asynchronously and receive notifications replacing “wait” with “notify” and setting hwndCallback argument to the hWnd property of your form. In this case the system sends the form a MM_MCINOTIFY message when the playback is finished. To intercept this message you have to create a custom window procedure and use subclassing techniquews. With asynchronous executions, you can pause / resume and stop playback with:
' pause playing the trackCommandString = "Pause MyCDAudio"' resume a paused trackCommandString = "Resume MyCDAudio"' terminate playing the trackCommandString = "Stop MyCDAudio"
When you’ve finished to use the CDAudio device, you must close it with
CommandString = "Close MyCDAudio"