home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Playing CDs From Your CD-ROM Drive

The Testing Department is calling again. Where’s that program to play CDs from the user’s CD-ROM drive? On its way, you say.

It’s easy to create a program that will play music CDs in your computer’s CD-ROM drive. Just add a multimedia control, MMControl1, to a form, and a label, Label1, which we’ll use to display the player’s current operation (for example, playing, stopped, and so on).

When the form loads, we just set the multimedia control’s DeviceType property to CDAudio and open the device:

Private Sub Form_Load()
    MMControl1.DeviceType = "CDAudio"
    MMControl1.Command = "Open"
End Sub

That’s all it takes. Now the user can play the CD in the computer’s CD-ROM drive by using the buttons in the multimedia control.

Besides playing the CD, we can display what the multimedia control is doing in a label, Label1, by adding this code to the multimedia control’s StatusUpdate event handler:

Private Sub MMControl1_StatusUpdate()
    Dim strMode As String
    strMode = ""

    Select Case MMControl1.Mode

        Case mciModeReady
            strMode = "Ready."

        Case mciModeStop
            strMode = "Stopped."

        Case mciModeSeek
            strMode = "Seeking."

        Case mciModePlay
            strMode = "Playing."

        Case mciModeRecord
            strMode = "Recording."

        Case mciModePause
            strMode = "Paused."

    End Select

    Label1.Caption = strMode

End Sub

Finally, we stop the CD (if it hasn’t already been stopped) and close the multimedia control when the form is unloaded:

Private Sub Form_Unload(Cancel As Integer)
    MMControl1.Command = "Stop"
    MMControl1.Command = "Close"
End Sub


TIP:  You can even eject a CD with the multimedia control’s Eject command, if the CD drive supports that command.

The program is ready to run—run it now as shown in Figure 22.3 (we’ve added a few more labels to hold captions like “CD Player” and so on in the program there). If you have loaded a music CD into your CD-ROM drive, you should be able to play that CD using the CD player program.


Figure 22.3  Our Visual Basic CD player.

The code for this program is located in the cdplayer folder on this book’s accompanying CD-ROM.


TIP:  If you don’t have a sound card in your computer (and so no speakers) but still want to play CDs with our CD player program, don’t despair just yet—most modern CD-ROM drives come with an earphone jack in the front. Just plug your earphones right in.

Playing WAV Files

The Testing Department is calling again. How’s that program that plays WAV sound files coming? Coming right up, you say.

It’s easy to write a program to play WAV files using the multimedia control—just set the control’s FileName property to the name of the file to open, and open it with the Open command. The multimedia control’s buttons will become active at that point, and users can play the file as they like, or, if you’ve hidden the multimedia control, you can use its Command property to play the file with the Play command.


TIP:  If you really just want to play sounds under program control, you can avoid the heavy drain on system resources by interfacing directly to Windows to play sounds instead of using a multimedia control. See “Multimedia Without Multimedia Controls” near the end of this chapter.

Let’s see an example. Here, we set the file to work with to C:\windows\media\ding.wav (which comes with Windows) and then open that file, making the buttons of the multimedia control, MMControl1, active when the form loads:

Private Sub Form_Load()
   MMControl1.FileName = "C:\WINDOWS\MEDIA\DING.WAV"
   MMControl1.Command = "Open"
End Sub

Now the user can play the WAV file using the multimedia control’s buttons.

Besides playing the WAV file, we can display what the multimedia control is doing in a label, Label1, by adding this code to the multimedia control’s StatusUpdate event handler:

Private Sub MMControl1_StatusUpdate()
    Dim strMode As String
    strMode = ""

    Select Case MMControl1.Mode

        Case mciModeReady
            strMode = "Ready."

        Case mciModeStop
            strMode = "Stopped."

        Case mciModeSeek
            strMode = "Seeking."

        Case mciModePlay
            strMode = "Playing."

        Case mciModeRecord
            strMode = "Recording."

        Case mciModePause
            strMode = "Paused."

    End Select

    Label1.Caption = strMode

End Sub

Finally, we stop playback (if it hasn’t already been stopped), and close the multimedia control when the form is unloaded:

Private Sub Form_Unload(Cancel As Integer)
    MMControl1.Command = "Stop"
    MMControl1.Command = "Close"
End Sub

That’s all we need. Now run the program as shown in Figure 22.4 (we’ve added a label to the program to display a caption). When you click the Play button, the WAV file will be played. Our program is a success.


Figure 22.4  Playing WAV files from Visual Basic.

The code for this example is located in the wavplayer folder on this book’s accompanying CD-ROM.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.