Ever wish you could play Audio CD's 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 CD's (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. Ideal 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
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
mciSendString "play cd", 0, 0, 0
mciSendString "stop cd wait", 0, 0, 0
mciSendString "seek cd to " & CurrentTrack, 0, 0, 0
Dim Tracks As String * 30
Dim NumTracks as IntegermciSendString "status cd number of tracks wait", Tracks, Len(Tracks), 0
NumTracks = CInt(Mid$(Tracks, 1, 2))
These are the only commands you need to play an audio CD and all the tracks contained within it. I have created a CD Player sample project to further clarify this tutorial. Happy downloading, and keep on rockin.