Play a CD Audio track

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"

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS