VB:Tutorials:WINAPI:Playing an Audio CD

From GDWiki

Jump to: navigation, search

Ever wish you could play audio CDs from within your own Visual Basic programs? Well, you probably didn't wish that.. but I did, so I went and figured it out! The advantage of playing audio CDs (as opposed to playing large wave files off the hard drive) is that it takes very little CPU overhead and memory to do, and you can still play other sound effects over top of it. Handy for game purposes if you plan to distribute your game on a CD ROM. Simply store your songs as audio tracks on the CD along with your game data and play these audio tracks during your game, when appropriate.

Most of the cool things that you can do with Visual Basic require some sort of API call, and this is no exception. You have to utilize the MCI (Media Control Interface):

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

The lpstrCommand is where you tell the MCI what you'd like to do, I'll show examples later. lpstrReturnString is the string you pass to the function when you want a return value. uReturnLength is simply the length of the string you've passed as lpstrReturnString, and hwndCallback... just pass zero (0) and it works :)

So, how do you get this function to do anything useful? It's all in the lpstrCommand. First we'll use it to initialize the MCI cd audio session.

mciSendString "close all", 0, 0, 0
mciSendString "open cdaudio alias cd wait shareable", 0, 0, 0
mciSendString "set cd time format tmsf wait", 0, 0, 0

We pass "close all" to close any audio currently playing. "open cdaudio alias cd wait shareable" initializes the MCI CD audio, and "set cd time format tmsf wait" sets up the CD timing format. You need all of these things to play a CD, so be sure to use them all before attempting the next function:

mciSendString "play cd", 0, 0, 0

If you send the "play cd" string you will start the CD playing on the current track at the current time. If you haven't selected a track yet then this will cause the first track to start playing at the beginning.

mciSendString "stop cd wait", 0, 0, 0

This will stop the CD wherever it currently is. It will NOT start over again at the start of the track, this is more like a pause function than a stop function. If you send "play cd" again it will recommence playing at the exact location within the track that it left off.

mciSendString "seek cd to " & CurrentTrack, 0, 0, 0

This function will skip to the specified CurrentTrack where current track is an integer from 1 to the maximum number of tracks on the CD. Only call this function when you've previously called the "stop cd wait" function, or else things will get all messed up :)

Dim Tracks As String * 30
Dim NumTracks as Integer
 
    mciSendString "status cd number of tracks wait", Tracks, Len(Tracks), 0
    NumTracks = CInt(Mid$(Tracks, 1, 2))

In order to determine how many tracks exist on a CD, we must first set up a string of 30 characters and pass this as the lpstrReturnString. after sending "status cd number of tracks wait", the Tracks string will now be filled with the appropriate information. This information will be contained in the first two characters of the string. Even though we're only using the first two characters, we have to pass a string of 30 characters since this is required by the function.

Categories