How to Playback a Audio Compact Disk (Play/Stop/Pause a CD) using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.10 out of 5)
 

Thankfully playing audio based .cda cds is an easy process and does not require having to rely on 3rd party controls. You can simply use the Microsoft Windows MCI Command Interface to do this. As you may be aware of you can do almost all of your Media based playback needs by using this interface including playing movie files. I do plan on making a new article on playing movie files in the future. I have a complete tutorial with some source code on how to use and program the mciSendString API so you can get a much deeper understanding on how it works and how to use it. Click on this link if you want to read it.

To get started you first need to setup the main API call that is required. Simply add the API below to your declarations section…

_____________________________

Visual Basic 5.0/6.0….

'
'Api call to send the commands to the mci device
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

Visual Basic .NET…

'
'Api call to send the commands to the mci device
Private Declare Function mciSendString Lib “winmm.dll” Alias “mciSendStringA” (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

_____________________________

Now that the main API call is setup and declared you need to start using it to program the device and send it commands. The first thing you need to do is create a new device and set the device to use the CDAudio driver. As you may have guessed this driver will do AudioCD playback. Below is how to create a new device using the CDAudio driver.

Note: In this article if your using VB 5/6 you only need to change Integer or Int32 types to Long types.

_____________________________

'
'This will hold the mci return value. The value in this variable will not be used in this article however.
Dim retVal As Integer
'
'This will be used to specify the drive letter of the CD or DVDRom drive to use.
Dim cdDrive As String
'
'This should be set to the CD or DVD drive your Music CD is in.
cdDrive = "D:\"
'
'Add quotes which will keep from having to get the short path since the mci device could fail if using the normal long file and path. Remember to set cdDrive to the CDRom or DVDRom optical drive. Specify like: D: or D:\ if D is your drive, or E: or E:\ and so on.
cdDrive = Chr(34) & cdDrive & Chr(34)
'
'Create a new CDAudio device with the alias CD. “CD” is how we will refer to the device  in this program.
retVal = mciSendString(“open “ & cdDrive & ” type cdaudio alias cd wait shareable”, vbNullString, 0, 0)
'
'Send the command to set the time format to the device default. Set this so when asking for time/track information we can get the value as a Tracks/Minutes/Seconds/Frames format.
retVal = mciSendString(“set cd time format tmsf”, vbNullString, 0, 0)

_____________________________

Now that a device has been created to use the CDAudio driver its time to start sending some commands.

_____________________________

The code below will return the total number of tracks on the audio cd in the cd drive…

'
'This variable is used to receive the value when requesting information from the mci device.
Dim buf As String
'
'Give it a 128 space buffer.
buf = Space(128)
'
'This will get the total number of tracks returned from the device.
Dim trackCount As Integer

retVal = mciSendString(“status cd number of tracks”, buf, 128, 0)
'
'Get the integer value from the buffer. It will return 0 (zero)  if no tracks are detected or if no audio cd is present.
trackCount = CInt(Val(buf))

_____________________________

If trackCount has the number 12 then there are 12 tracks starting at Track #1 through to Track #12. So when referring to a specific track you need to specify the exact number you want the information from.

Next you can now go ahead and start Playing, Stopping, and Pausing/Resuming a Audio CD’s tracks. First you want to tell the device which track you want to play. The next set of codes will do that.

_____________________________

'
'This will hold the track number to play. It is then set to track 2 below.
Dim trackToPlay As Integer
'
'Set to play Track number 2 on the compact disk.
trackToPlay = 2
'
'You need to set the time format to the cd devices default so it knows that 1, 2, 3, 4, and so on is the track number.
'Set the time format to the cd devices default Tracks, Minutes, Seconds, Frames when you are wanting to seek to a specific track.
retVal = mciSendString(“set cd time format tmsf”, vbNullString, 0, 0)
'
'This will tell the device to move to the track number specified. Simply put in the track number you want to play.
retVal = mciSendString(“seek cd to “ & trackToPlay, vbNullString, 0, 0)

_____________________________

You can now start Playing, Stopping, Pausing, and whatever it is you want to do.

_____________________________

'
'Start playing the cd at the track you already specified above.
retVal = mciSendString(“play cd”, vbNullString, 0, 0)
'
'Stop the cd from playing.
retVal = mciSendString(“stop cd”, vbNullString, 0, 0)
'
'Will pause the CD if its playing.
retVal = mciSendString(“pause cd”, vbNullString, 0, 0)
'
'Resumes the cd to playing if it was paused.
retVal = mciSendString(“resume cd”, vbNullString, 0, 0)

_____________________________

Thats ALL there is to it! IF the codes in this article was properly implemented then it should Start, Stop, Pause, and Resume an Audio Compact Disk (CD). There is MUCH more you can do with Audio CD’s as well like getting the length, position, fast forward, Opening/Closing the CD Door and so on. But some of those features might be implemented in a future article.

I suggest you really go check out my MCI Command tutorial this site, or an older revision but downloadable .doc version so you can try and learn how to actually implement your own custom functionality and possibly checking out the documentation at microsoft.msdn.com.

If you don’t care or have the time to create your own source code for audio CD based playback then you can check out my AudioCD based class libraries. My vbcodesource.com website has MANY libraries available that greatly simplifies the task of creating a CD player based application. I even recently released a brand new library named csAudioCD Library v2.5 Pro Enhanced for Visual Basic 2005 and VB 2008 that has Over 50 subs and properties. I have libraries for VB.NET 02/03 and VB 5.0/6.0 as well. I suggest you goto my main website under the controls and examples section if your interested. There are example applications available as well on how to use the audio cd libraries.

Anyways, I hope you got something useful from this article and it was easy to understand as well. Sometime in the future I may expand on this subject by showing how to get position, duration based information and such. Have Fun!

Jason

9 thoughts on “How to Playback a Audio Compact Disk (Play/Stop/Pause a CD) using VB and VB.NET

  1. Jason Post author

    The first thing you will need is either a hardware or a software DVD decoder available.

    Microsoft does not include one with Windows most likely do to licensing costs and other reasons.

    A hardware decoder would be like a newer ATI Radeon video card which has hardware assisted DVD decoding. But I don’t exactly have any other info on tapping directly in to those features.

    The other option is to have a software decoder which would be something like Cyberlink Power DVD or WinDVD software.

    Anyways, instead of using mci its probably better for DVD playback to use a legacy COM control made by Microsoft which is called: MSWebDVD class. You can add that control to the toolbox by right clicking on your toolbox area and click on ‘Choose Items’. Then click on the COM tab and scroll down till you see MSWebDVD Class. Go ahead and checkmark that control and click ok.

    You can then doubleclick the control from the toolbox and go from there.

    I do not have any examples or other real info to give you. You do need some sort of dvd decoder. I do know some free players like ClassicMediaPlayer has dvd decoding capabilities and maybe try checking out a free Codec Pack.

    Jason

    Reply
  2. John

    Hello Jason~
    Great Tutorial~
    I was just wondering if you knew how I can get the track number that it is currently playing?
    -Thanks
    -John

    Reply
  3. Jason Post author

    John, the code below should give you what you want.

    Dim buf As String = Space(128)

    retVal = mciSendString(“status cd current track”, buf, 128, 0)

    Return CInt(Val(buf))

    Jason

    Reply
  4. reconrey

    Hey great tut Jason!
    I saw your tutorial on How-to Playback Movies earlier and i was wondering if you knew how to playback a DVD?
    Thanks

    Reply
  5. Jason Post author

    @ Jeff – I’ve actually never tried to change the play rate of a cd. I have of mp3’s, wma’s, and all that but never a cd.

    I am going to say it does NOT support changing the playback speed, but I am not 100% sure. You can try to use code similar to the code below if you want to test it…

    mciSendString(“set cd speed ” & 500, vbNullString, 0, 0)

    The code above if it works for cdaudio would play the cd at half of the normal rate. 1000 is 100%, or the normal play rate, while 1500 would be 50% faster, ect….

    Feel free to comment on whether it did or didn’t work for you. I’ve never heard of anyone wanting to change the play rate of cd’s before you so it’s not anything i’ve ever tried or considered.

    I don’t have the time I would try it myself or I would. But see if the code above will do anything for you. 🙂

    Jason

    Reply
  6. Jason Post author

    I have no other info using mci. You can try using a addon module to the Bass.dll library. It has a CDAudio add-on that might do what you want. There should be other free CDAudio libraries out there as well.

    Jason

    Reply

Leave a Reply

Your email address will not be published.