Hour 25


During this hour you will learn

Adding Multimedia to Your Programs

This lesson will be a lot of fun! You'll learn how to use the Multimedia control to place multimedia capabilities into your applications. Your users will be able to hear sound clips and watch videos if you want to add those features to your applications.

The Multimedia control is extremely simple to use. You'll need to write only a small amount of code to use multimedia in your applications. The differences between a compact disc player and a video clip player are almost nothing to a programmer, as you'll learn in this lesson. The Multimedia control takes care of the tedium and lets you worry about the other aspects of your application.

FAST TRACK
If you want to concentrate more on getting advanced user input at this time rather than learn how to output multimedia files, you may want to skip to Hour 28, "Using Keyboard and Mouse I/O".

Topic 1: The Multimedia Control

Microsoft's Multimedia control provides simple and advanced multimedia capabilities for your applications. The Multimedia control supports the following kinds of multimedia objects:

The last three categories list file-name extensions associated with those kinds of multimedia capabilities. The first five kinds of multimedia capabilities are called simple multimedia devices because no data file is necessarily associated with those items. For example, if you use a VB application to control a CD playing in your CD-ROM drive, the data comes from the CD, not from a file.

The last three kinds of multimedia capabilities are called compound multimedia devices because of the extra file needed for data, as well as the play or record capabilities. If you want to play a wave audio file, for example, you must have access to the .WAV file, and your VB application gets its data from the .WAV disk file.

Most computers sold today are multimedia ready and conform to the industry standard called MPC (Multimedia Personal Computer). Nevertheless, the computer that runs your multimedia application must have the appropriate hardware before the multimedia software inside your application works. Obviously, if the computer doesn't have the .WAV file your application needs to play or if the computer has no CD-ROM, your application won't be able to perform audio file or compact disc playing.

Overview

Visual Basic uses the Multimedia control to perform multimedia operations. This section explains some of the Multimedia control's capabilities and prepares your system for the control.

Loading the Multimedia Control

The Multimedia control doesn't exist in your Toolbox's intrinsic tools, so you must add it. You must have VB's Professional or Enterprise edition to have access to the Multimedia control.

Add the Multimedia control to your Toolbox as you do other new tools. From the Project menu choose Components to display the Components dialog box. Select the control named Microsoft Multimedia Control 5.0 and click OK. The Multimedia control is loaded when you see it appear in your Toolbox, as shown in Figure 25.1.

FIG. 25.1

The Multimedia control must reside in your Toolbox before you can use it.

Preparing for the Multimedia Control

The Multimedia control is formally called the Multimedia Control Interface (MCI). If you have the correct hardware, the MCI connects your software to that hardware to control multimedia devices. The Multimedia control looks a lot like the panel of buttons on the front of a tape recorder (see Figure 25.2).

FIG. 25.2

The placed Multimedia control looks like a tape recorder's control buttons.

As your application uses the Multimedia control to play a device, such as a compact disc, VB updates the buttons according to the available functions. For example, if you click the Eject button, VB disables the other buttons, such as Play, until you close the CD door again.

You can control the number of buttons that appear on the Multimedia control and which ones are enabled at any one time. Some multimedia devices don't require all the buttons. For example, you can't record onto a compact disc, so you would hide the Record button if you were writing a CD interface to play an audio CD.

When you first place the Multimedia control on a form, none of the buttons are enabled.

Example

Prepare to use the Multimedia control by creating a new project. Add the Multimedia control to your Toolbox by pressing Ctrl+T to open the Components dialog box. Click the control named Microsoft Multimedia Control 5.0, and then click OK to add the control to your Toolbox. Double-click the control to place a control in the center of your form. Name this Multimedia control mciCD.

Now make a few modifications: Change the form's caption to Compact Disc Player and the form's name to frmCD. Add a label named lblCD with a Caption property that reads My CD. Center the text and change the point size to 24.

You can resize the Multimedia control to increase or decrease the button width, although you should leave the Multimedia control at its default size for this application. You can expand the form, however, to make more room for the control and its title.

Your Form window should look something like the one in Figure 25.3.

FIG. 25.3

You've now placed the Multimedia control and a title.

Place a right-justified label named lblTrack on the form beneath the buttons with an 18-point Caption that reads Track:. Add another label named lblTrackValue to the right of the previous label, left-justify its Caption, adjust the width to 810, and change the point size to 18 also. Place a fixed-single border around the last label. Your CD player not only will be able to play compact discs, but also will tell users which track is now playing.

Although you can enable the individual Multimedia control buttons, the Multimedia control supports a property called DeviceType that, when initialized with the device you want to use (such as CDAudio), automatically enables the correct buttons. Therefore, your job is to tell the application that the Multimedia control is to be a CD Audio player.

Click the Custom property to display the Property Pages dialog box. The value property you need to enter now is DeviceType, so that the control will know which buttons to enable. Enter the Multimedia-control named literal CDAudio. Although DeviceType is the only value you need to adjust, click the Controls tab to see the button controls you can set from this dialog box (see Figure 25.4). Setting individual buttons (for example, if you ever want to hide or enable specific buttons) is much easier to do here than from the regular Properties window.

FIG. 25.4

The Property Pages dialog box makes enabling buttons simple.

Click OK to close the dialog box. Now that you've set up your CD player, you have to add a little bit of code in the next section.

Next Step

The Multimedia control contains its own little programming language and can accept commands in a special property named Command. As you need to learn more of the Multimedia control language, this lesson will teach you the commands you need. The only two commands you need to implement a compact disc player are Open and Close.

When your application begins, you need to tell the Multimedia control to open whatever device you specified in the DeviceType property. Perhaps the best place to open the Multimedia control device is in the Form_Load() event procedure. Double-click the form and enter the following:

Private Sub Form_Load()

' Tell the Multimedia control to open the CD

mciCD.Command = "Open"

End Sub

The most important Multimedia control event is StatusUpdate. Every time the status changes on your CD, such as a track change, the StatusUpdate event takes off. To update the track numbers, place the following code in the StatusUpdate event procedure:

Private Sub mciCD_StatusUpdate()

' Update the tracks

lblTrackValue.Caption = mciCD.Track

End Sub

The Multimedia control supports several commands your applications can use to inform users of the Multimedia control's status. The Track property monitors the current track number. If users click the Next button while a compact disc is playing, the StatusUpdate event occurs and updates the label for the track number.

Finally, when the application ends, the Multimedia control will close. Nevertheless, you should begin to get in the habit of unloading all your forms and performing any final cleanup of your application before the application abruptly ends. In some rare MDI cases, a form may not unload properly if you don't specifically unload it in the Form_Unload() event procedure (the final procedure that executes when an application ends). Code the following property:

Private Sub Form_Unload(Cancel As Integer)

' Clean up the CD and form

mciCD.Command = "Close"

Unload Me

End Sub

The Close command tells the Multimedia control to stop playing the CD. If your application contains only a single form, Unload Me unloads the form from memory so that the application can gracefully exit and release all of its resources properly. If your application contained several forms, you would unload each form by name (or step through the Forms collection).

You can now play an audio CD. Insert a compact disc in your CD-ROM caddy. Close the Windows CD Player accessory program if it begins. When you run your application, the appropriate buttons will appear, along with the first track number. Figure 25.5 shows the CD player you just embedded within your application. As you click on the Next and Previous buttons, notice that the track value changes to reflect the current track (due to the Multimedia control's StatusUpdate event). Stop and pause the music to see how the Multimedia control adjusts itself. Eject the CD when you're done and close the application.

FIG. 25.5

You are playing beautiful music!

Topic 2: Making Waves

You'll learn more about the Multimedia control by creating a wave music player. A wave file (with the file name extension .WAV) contains audio music that someone digitally recorded for play by a device such as your sound card powered by the Multimedia control software. You'll set up the wave music player roughly the same way as you set up the CD player.

Overview

In this topic section, you'll not only learn how to manage a compound multimedia device (the wave player), but you'll also learn about the Multimedia control's Mode property. Mode contains lots of helpful information about the status of the control, and your program can access the Mode property to learn that information. Fortunately, Microsoft defined named literals to represent the various Mode values.

The Mode Values

Table 25.1 lists the Mode values that you'll need for the Multimedia control. These Mode values don't just work for the wave music audio player but for all the Multimedia control functions. For example, you could have tested MCI_MODE_PLAY in the preceding topic section's application and displayed a message on the form when the CD was playing.

Table 25.1 The Multimedia Control Mode Values

Value

Description

mciModeNotOpen
Multimedia device isn't open

mciModeStop
Multimedia device is stopped

mciModePlay
Multimedia device is playing

mciModeRecord
Multimedia device is recording

mciModeSeek
Multimedia device is seeking past information

mciModePause
Multimedia device is paused

mciModeReady
Multimedia device is ready

The Wave Player

When creating an application that demonstrates playing a .WAV audio file, you must tell the Multimedia control that you want the wave audio play function and tell it which .WAV file to play. Unless your application plays background music or an opening music sequence during startup, you'll probably want to add an Open dialog box to let users select a .WAV file to play.

The following example creates a wave audio file multimedia player. You'll be able to use the Mode values in Table 25.1 to display status information about the file being played. The Visual Basic system comes with a .WAV file named Mcitest.wav in VB's \Samples\CompTools\mci directory, and that's the file the next example plays.

Example

Save your previous project if you still have it open, and then use that project as the basis for this one. To keep both projects separate, save the project again under a new name (perhaps Lesson 25 MCI Proj) and modify the project according to these steps:

  1. Change the top label's name to lblWav and change the Caption property to Wave Player.
  2. Change the form's name to frmWav and the form Caption to Wave Music Player.
  3. Change the Multimedia control's name to mciWAV and change its DeviceType property to WaveAudio. (You can change the property in the Properties window or from the Property Pages dialog box that appears when you click the Custom property's ellipses.) Change Filename to the Mcitest.wav file located in your \VB\Samples\CompTools\mci directory.
  4. Wave files don't have tracks like CDs do, but you can still use the labels beneath the multimedia control buttons. Change the left label's name from lblTrack to lblStatus and change its Caption to read Status.
  5. Change the right label's name from lblTrackValue to lblStatusValue and blank out the Caption. Change the Width property to 2565.
  6. Add another pair of labels beneath the two you just modified with the same width and font properties. (You could copy them to create a set of label arrays.) Name the first label lblFile (unless you created a control array) and change the Caption to Filename:. You'll have to extend the left edge to make room for the caption.
  7. Change the left label's Name property to lblFileValue (unless you created a control array). Leave the label blank. After you center the labels beneath the buttons, your application should look like Figure 25.6.

FIG. 25.6

The wave music player is almost complete.

Change the Form_Load () event procedure to the following:

Private Sub Form_Load ()

' Tell the Multimedia control to open the WAVE player

mciWAV.Command = "Open"

End Sub

You must also change the Form_Unload() event procedure as follows:

Private Sub Form_Unload(Cancel As Integer)

' Clean up the WAVE and form

mciWAV.Command = "Close"

Unload Me

End Sub

Run the application. When you click the Play button, you'll hear a cuckoo sound.

Next Step

The wave player isn't quite complete. The labels beneath the buttons don't display the status or file-name information. You can supply these labels with their values in the StatusUpdate() event procedure (see Listing 25.1). You'll need to use the Mode values to determine the proper play mode.

Listing 25.1 Status.bas: Adding Status Information to the Labels

Private Sub mciWAV_StatusUpdate()

  ' Display the status

  If mciWAV.Mode = mciModeNotOpen Then

    lblStatusValue(0).Caption = "Not Ready"

  ElseIf mciWAV.Mode = mciModeStop Then

    lblStatusValue(0).Caption = "Stopped"

  ElseIf mciWAV.Mode = mciModePlay Then

      lblStatusValue(0).Caption = "Play"

  ElseIf mciWAV.Mode = mciModeRecord Then

    lblStatusValue(0).Caption = "Record"

  ElseIf mciWAV.Mode = mciModePause Then

    lblStatusValue(0).Caption = "Paused"

  ElseIf mciWAV.Mode = mciModeReady Then

    lblStatusValue(0).Caption = "Ready"

  End If

  ' Display the filename being played

  lblStatusValue(1).Caption = "Mcitest.Wav"

End Sub

Change the label names to the actual names that you used in steps 6 and 7 of the previous topic section's example if you didn't create control arrays to hold the labels. Otherwise, your application won't run correctly because the code uses a control array for the labels, not individual label names.

When you run the player, your Form window should look like the one in Figure 25.7.

FIG. 25.7

The cuckoo is working.

As you play the wave file, consider the following:

Topic 3: Playing Video Clips

You've already mastered the Multimedia control! Believe it or not, playing a video clip isn't much different from playing a wave audio file, as you'll see in this topic section. You must supply the video file name to play (the Multimedia control as a video player is a compound device, so the file name is critical). Then set up the Multimedia control to handle video playing, and your application will be showing the latest in multimedia entertainment.

Overview

Your Multimedia control requires a bit of help when playing video clips. Rather than keep only a simple panel of buttons, you need a projection screen from which to show the video. The most common control to display videos with is the Picture Box control. Therefore, you'll add a Picture Box control to every application that needs to display a video file.

Device Type Values

So far, you used a Multimedia control DeviceType property value of CDAudio to play compact discs and of WaveAudio to play .WAV audio files. Table 25.2 lists all the DeviceType properties that the Multimedia control supports. Although you can enter these in the Custom property's Property Page dialog box, you may need to set these values at runtime with assignment statements if you need your Multimedia control to perform double duty and play different kinds of media files.

Table 25.2 The Multimedia Control DeviceType Values

Value

Description

AVIVideo
.AVI video file

CDAudio
CD audio

DAT
Digital tape

DigitalVideo
Digital video

MMMovie
Multimedia movie clip (.MM extension)

Other
Newly supported multimedia device files

Overlay
An audio clip that overlaps another

Scanner
Scanned image import and management

Sequencer
MIDI sequencer file (.MID extension)

VCR
Video tape recorder

Videodisc
Videodisc player

WaveAudio
Wave audio file (.WAV extension)

Connecting the Picture Box Control

Your application might contain several Picture Box controls, so the Multimedia control must know to which Picture Box control to send the video output. You must tell the Multimedia control the name of the Picture Box control in which to show the video in the Multimedia control's hWndDisplay property.

hWnd is a common Windows programming prefix that represents a handle or, more accurately, a device context. Output from Visual Basic doesn't represent true screen output but windowed output. (Actually, you can send output to any Windows device with the same basic set of commands, so Windows devices are more virtual than real to your program. Windows performs all the complicated conversions to get output printed on a printer or color screen.) The bottom line is that your program doesn't send video clip output to your a screen but only to a device context. That device context is almost always your screen, but the Multimedia control needs to know the proper device context so that it knows which window to play the file inside and can manipulate borders appropriately so the video stays within the window.

Example

Create a new application. Name the form frmVideo and specify Video Player for the form's Caption. Add a Multimedia control to the top center of the form. (You'll have to add the Microsoft Multimedia Control 5.0 to your Toolbox window first.) Name the Multimedia control mciVideo.

Add a Picture Box control to fall below the Multimedia control buttons and size the Picture Box control to the size you see in Figure 25.8. Name the Picture Box control picVideo.

FIG. 25.8

The Picture Box control will display the video.

Change the Multimedia control's DeviceType to AVIVideo. Select the .AVI file named Blur24.AVI that you'll find in the \VB\Graphics\AVIs directory.

You're almost finished! As you can see, setting up video isn't any different from setting up a normal audio player. Add the following Form_Load() event procedure:

Private Sub Form_Load()

  ' Open the video player

  mciVideo.Command = "Open"

  ' Connect the video player to the Picture Box

  mciVideo.hWndDisplay = picVideo.hWnd

End Sub

Run your program to see the numbers, one of which is shown in Figure 25.9, flash by in the video.

FIG. 25.9

The video plays flawlessly!

Next Step

As you work with the Multimedia control, you'll learn more shortcuts. For example, you don't have to specify the device type, such as AVIVideo, when programming compound devices because Visual Basic will look at the compound device's file extension to determine the device type needed.

Also, you probably noticed that the status labels don't always update right on time. In other words, when running Topic 2's wave file player, the status label didn't always display Play until the clip was almost finished. The StatusUpdate event occurs every few milliseconds. If you want to update such labels more frequently to gain more accuracy, change the UpdateInterval to a smaller value (1,000 is the default so that the status updates once each second).

Don't make the UpdateInterval value too small, or your Multimedia control application will consume too much time and slow down your system. Often, this slowdown results in shaky playback of Multimedia control files.

Summary

If you're like me, you're surprised at how simple the Multimedia control makes adding multimedia to applications. The Multimedia control, available in VB's Professional and Enterprise editions, makes quick work of multimedia and takes care of such tedious details as the buttons that should be enabled, and such device mechanics as ejecting the CD caddy properly.

Your biggest job is to open the Multimedia control, select the device type, and specify a file name for compound devices that need files. The control does the rest of the work. After you set up the control, users will then determine what happens next by clicking the Multimedia control buttons.

Although you learned the fundamentals of the Multimedia control, you might want to browse the online help for additional Multimedia control properties and methods. You can keep track of and display the time and tracks as the clip plays, as well as the position within the file that's now playing. Also, you can adjust the playback so that only part of the file plays from the start and stop positions you specify.

In the next hour, "Using Form Templates," you'll learn how to use the templates that provide common functionality between applications.

Hour 25 Quiz

  1. What's the difference between a simple multimedia device and a compound multimedia device?
  2. Why doesn't Visual Basic enable every button when you first place the Multimedia control on a form?
  3. What does the Mode property do?
  4. Why does a wave player need to supply a Rewind button?
  5. What extra programming effort is required to record audio?
  6. What does the Picture Box control do for video clips?
  7. What's another name for a handle?
  8. How does the Multimedia control know which Picture Box control to send the video to?
  9. True or false: You must set the DeviceType property when programming the Multimedia control for compound devices.
  10. How can you update the status of a Multimedia control device more frequently?

Hour 25 Homework Exercises

  1. Although the Multimedia control takes care of enabling the buttons that need to appear (depending on the events taking place), you may want to hide unneeded buttons. For example, you need no Record button when playing compact discs. Change the first topic section's project so that the project displays only the buttons needed to play and manage compact discs.
  2. Modify the first topic section's CD player application to show the total number of tracks on the CD (stored in the Tracks property), as well as the track length (stored in the TrackLength property).
  3. Change Topic 2's wave player to ask users for a file to play and play that file. Use the File Open button. You can add a menu option or command button to trigger users' file-name entries.

© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.