Tag Archives: CD

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

How to Open and Close the CDRom/DVDRom Tray Door using VB and VB.NET

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

Opening or closing the optical disk drive(ODD) tray door in any Visual Basic and Visual Basic.NET language is easy to do thanks to the Windows API. This article will be using code based on the MCISendString Api which I made a complete tutorial for starting MCI. Click this link to check it out.

These codes are setup for VB.NET but all you have to do is change Integers to Longs which will then work pefectly fine with Visual Basic 6.0.


So the first thing you’ll want is this API call…

VB.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

VB 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

 

Now just need to send the commands.

The code below will open and close the default CD/DVD Door and you won’t need to set/specify the ODD drive path.

'Pop it open
mciSendString("set cdaudio door open", 0, 0, 0)

'Shut it
mciSendString("set cdaudio door closed", 0, 0, 0)

 

OR

Use the codes below will setup and use the variables and the specified alias you want to use.

        '
        'This will simply get the api call return value that can be used if you use the MCIGetErrorString API.
        'This example won’t be using that API though.
        Dim retVal As Integer

        'This will contain the drive you want to open. Ex: D:\ or E:\ or whatever.
        Dim cdDrive As String
        '
        'Now setup the path to your computers cd or dvd drive you want to open or close.

        'Remember to put Quotes around any path you pass to the mci device.
        'Also specify the correct drive letter for the CD/DVD Drive.
        'I’m using drive “D:” which is the location of the DVD/CDRom on my computer.
        'You can specify the drive path like ‘D:’ or ‘D:\’
        cdDrive = Chr(34) & "d:" & Chr(34)

        'Now its time to setup the MCI Device using the CDAudio driver.

        'First setup a new device using the CDAudio driver with alias named “cd”.
        retVal = mciSendString("open " & cdDrive & " type cdaudio alias cd wait shareable", 0, 0, 0)

        'Now that the MCI Device is programmed and ready to go, you can start sending it commands.
        'The codes below will Open the CD or DVD Tray Door and Close the CD/DVD Door…

        'This will Open the door.
        retVal = mciSendString("set cd door open", 0, 0, 0)

        'This will Close the door.
        retVal = mciSendString("set cd door closed", 0, 0, 0)


Thats all there is to it! If the code executed properly, your optical drive should have opened and/or closed. The code here will work with all VBs and VB.NET languages.

Just Change all Integer types to Long types if your using VB 6.0.

If your wanting to make a complete CD Player application, I have complete libraries for both VB 5/6 and Visual Basic.NET at my Visual Basic Code Source website. Anyways, have fun!

Jason