HOME
Login
Change Info
Logout


TUTORIALS

C, C++
Win32
Java
Visual Basic
MFC
DCOM
Networking
C#
Perl
HTML
XML
ASP
PHP
Javascript
Other

DOWNLOADS
ITCLib
SourceVizor meets the notification, reporting, and admin needs of teams using Microsoft Visual SourceSafe.
Free 30-day trial!


Playing Multimedia Files in Visual Basic
Using the MCI Control, ActiveMovie Control and the Animation Control

by Ramalinga S. Danturthi, Ph. D.

Questions, comments, suggestions - We want to hear from you!Comment on this article Get the Acrobat PDF File For This Article Get Adobe Acrobat Reader
Most downloads under 500KB


Introduction

A great many Visual Basic books and other resources (including VB's online help) make multimedia programming seem very simple and straightforward. Unfortunately, most examples in the on-line resources do not meet the demands of the growing applications of multimedia. To help solve this problem, I developed a small module in which I expose techniques for multimedia programming in Visual Basic (VB) using three controls: the MCI control, the ActiveMovie control and the Animation control. These are standard controls in the Windows95/98 environment and are available in Visual Basic 5.0 and 6.0 professional editions.

There are some basic principles one should adhere to when playing multimedia files in VB:

  • Define a DeviceType property such as "AVIVideo" for the MCI Control. These device types are listed in the on-line books and help files.

  • Define the filename property for the MCI Control.

  • Select/Deselect the silent (if any sound is associated with the AVI file) property.

  • Play the file with the MCI Control's play command.

  • Wait for the file to complete the play command (not required if the wait property is used).

  • Issue a "stop" command to the MCI Control after completely playing the multimedia file.

  • Close the file associated with the MCI Control.

When dealing with a video file, prior to step 4 you will have to give it a window handler. You can play an AVI file without a handler, but giving it a window handler has several advantages, which I'll discuss below. Whichever control one uses - MCI control, ActiveMovie control or the Animation control - the steps you take to play a video or audio multimedia file are the same. The main difference is the commands that each control uses.

This article discusses the playing of video and audio files without displaying any buttons or counters for their corresponding controls. Instead, playing AV files is controlled entirely in the code. You can display the buttons (play, pause, stop, etc.) by changing the properties of the respective controls (Visible = True), or by issuing proper commands at run time.

The figure below depicts a VB form that demonstrates the behavior of the controls. Notice that it has three frames. One frame serves for the MCI Control, another the ActiveMovie control, and the third selects the mode to play – video, audio or both. There are six command buttons each in MCI Control and ActiveMovie Control frames. The mode selection frame has three option controls that let you select from video only, audio only, or audio/visual modes. The form also has a button for playing an AVI file with an Animation Control. There is an exit button for ending the program and two multimedia controls for playing the AVI and WAV files separately. Each control is described here under its appropriate heading. It is important to note that Animation control can not play an AVI file that has sound. The Windows environment provides many files that can be played with the Animation control – for example, the animation file showing pieces of paper flying from left to right when the copy command is executed.

[ Download the Visual Basic Project files ]



I. Multimedia Control or the MCI Control

According to the online help file in VB5.0, the MCI control is a set of push buttons that issues MCI commands to devices such as audio boards, MIDI sequencers, CD-ROM drives, audio CD players, videodisc players, and videotape recorders and players. The MCI control also supports the playback of Video for Windows (AVI) files. Experience shows that a multimedia control can also play an MPEG file. One can use the MCI control's notify services to detect whether a command has successfully executed.


a. The Wait property

The MCI control's wait property forces users to wait until the video or audio file is finished before they can take further action. If wait is set to false, users regain control immediately after the video or audio starts playing. Basically, the wait property instructs the system to wait until the next command (issued immediately after setting the wait property to True) of the MCI Control is complete. The following segment of the program demonstrates this idea here. Video appears on a different form named vclip.


Load vclip  'This is a form on which our video appears
With vclip
    .show
    .setfocus
End with
MMControl1.hwndDisplay = vclip.hwnd  'Discussed below
MMControl1.wait=True
MMControl1.Command="play"
MMControl2.wait=True
MMControl2.Command="play"
Unload vclip
Set vclip=Nothing


b. The HwndDisplay property

There is an important property called hwndDisplay, which is the window handler for the Windows environment. This specifies the output window for MCI MMMovie or overlay devices that use a window to display output. This property is not available at design time. There are many controls such as picture box that have the hwnd property. If one wants the output of the MCI control to appear in a particular picture_box control, one can set the hwndDisplay property as:

MMControl1.hwndDisplay = picture_box.hwnd

In this example, the output of the MMcontrol1 is displayed in the picture box. One needs to set the picture_box size to an appropriate height and width to see the output. Width and Height properties and other position control properties can be set at either design time or run time. If hwndDisplay is not set to any control, playing multimedia will cause the system to generate a new window with standard Windows features. This is a very interesting situation. In the present demonstration program, six different buttons can play the AVI file and the associated audio file. Immediately after executing the program, if one presses the No Handler button, the system generates a new window to show the animating video. However, if Handler to Picture Box is pressed first, then the display goes to the picture box. Clicking on the No handle button now causes the display to still go the picture box. Similarly, clicking No Handler after clicking Form Handler first causes the display to go to the form.

What happens is, the display goes to the existing default handler. Putting it another way, not defining a handler causes the MCI control to use the handler defined by the previous command(s). If there is no previous command, then a new window is generated. Unfortunately, the programmer can not control any properties (height, width, top, left, etc.) of a newly generated window at run time. Using a handler, however, allows the programmer to control the display output. The program segment included below shows how the hwndDisplay property is put to use for different purposes.


MMControl1.DeviceType = "AVIVideo"
MMControl2.DeviceType = "WaveAudio"
'Give the file names
MMControl1.filename = "c:\vb_programs\s01ppo01.avi"
MMControl2.filename = "c:\vb_programs\v01ppo01.wav"
MMControl1.Command = "open"
pon = MMControl1.Length
MMControl2.Command = "open"
Select Case choice 'Decide which button is pressed.

Case 1
    'No handler is used. Video appears on a separate 
    ' window.
    'Last handler will be used,
    'if no handler is defined. If the control
    'to which last handler was defined
    'is made invisible then we can not see video
    'At start, uses a new window
    MMControl1.Command = "play"
    MMControl2.Command = "play"
    Call holdon(pon / 14)

Case 2
    'Use handler to form. Stop button is invisible,
    'because stop button is on vclip.
    'If one uses me.hwnd then the video appears
    'on form m_media, because it is default.
    MMControl1.hWndDisplay = Me.hWnd
    MMControl1.Command = "play"
    MMControl2.Command = "play"
    Call holdon(pon / 14)

Case 3
    'Handler to picture box in form m_media.
    MMControl1.hWndDisplay = Picture_box.hWnd
    Picture_box.Visible = True
    MMControl1.Command = "play"
    MMControl2.Command = "play"
    Call holdon(pon / 14)

Case 4 
    'Handler to vclip.  Vclip is a separate window with 
    ' a Stop button.
    MMControl1.hWndDisplay = vclip.hWnd
    Load vclip
    With vclip
        .exit_button.Visible = True
        .selection = 22
        .Show
        .SetFocus
    End With
    MMControl1.Command = "play"
    MMControl2.Command = "play"
    Call holdon(pon / 14)
    'If the following is removed and the first button 
    ' (No Handler)
    'is pressed, the video goes to vclip.
    Unload vclip
    Set vclip = Nothing

Case 5
    'No delay loop. Use wait property to wait till 
    ' completion.
    'Good if audio is recorded with video. Then to 
    ' suppress audio
    'use 'silent' feature of the mmcontrol.
    MMControl1.hWndDisplay = vclip.hWnd
    Load vclip
    With vclip
        'Handler to form vclip. Stop button visible 
        ' and enabled.
        .exit_button.Visible = True
        .selection = 22
        .Show
        .SetFocus
    End With
    MMControl1.Wait = "true"
    MMControl1.Command = "play"
    MMControl2.Wait = "true"
    MMControl2.Command = "play"
    'If the following is removed and the first button 
    ' (No Handler)
    'is pressed, the video goes to vclip.
    Unload vclip
    Set vclip = Nothing

Case 6
   'Give choice of selecting either audio 
   ' or video
   MMControl1.hWndDisplay = Picture_box.hWnd
   Select_it.Visible = False
   Select Case audio_choice
       Case 1 'Video only
           MMControl1.Command = "open"
           MMControl1.Command = "play"
           Call holdon(pon / 14)
       Case 2 'Audio only
           MMControl2.Command = "open"
           MMControl2.Wait = True
           MMControl2.Command = "play"
           Call holdon(1)
       Case 3 'Video and Audio
           MMControl1.Command = "open"
           MMControl2.Command = "open"
           MMControl1.Command = "play"
           MMControl2.Command = "play"
           Call holdon(pon / 14)
   End Select
End Select


c. Stopping the AVI File

Because some multimedia files have tracks, programmers can jump tracks and play specified tracks by setting the Track, TrackLength and TrackPosition properties. The wait property, when set to true, makes the system wait until the next MCI command is complete. But when the wait property is used, it ties the system to the multimedia file being played and the user gets the control back only after the file is completely played. Therefore, if the user wishes to stop a file from playing fully, the best way is to not use the wait property.

This can also be accomplished by displaying user controls like pause, play, etc. In this example, the video form (vclip.frm) has a stop button at the lower left-hand corner. The wait property is not used and instead a delay loop (holdon) is introduced into the program. The system waits for a specified length of time, playing the video. The amount of time the video file plays can be obtained from the MCI control's length property for the video/audio file. The length property gives the length, which is approximately 12 or 13 times the video/audio time.

Forcing the length as a string variable (in this example 'pon' declared as integer), length can be printed to a label or text box. This property enables the user to determine the time the video/audio file plays. This way, you can call a delay loop for the required time. While the delay loop runs, the DoEvents method returns control to the user. Selecting a variable, such as Selection, allows the user to break the control loop if required. The variable is used in such a way that when the variable is set to false the loop continues running, and, when it is true, the loop is broken. Clicking the stop button on the vclip form essentially does the same thing. Case 4 in the above code demonstrates this.


d. Playing Audio, Video, or Both

Some files of the type "AVIVideo" can record both sound and animation. However, when video files and audio files are recorded separately, the user needs two MCI controls to play them separately to achieve proper synchronization. Playing them one after another with the same control results in the files playing sequentially - that is, video is played first and then the audio. On the other hand, two MCI controls simulate the condition of simultaneous audio/video, although it may be necessary to introduce a small delay between them to get the desired synchronization. In the example described here, a frame control for selecting the multimedia options is provided with the caption - Make a Selection. Clicking on the "Play Sound or Video" button enables this 'Make-a-Selection' frame and waits until the user chooses an option. Choosing the Video Only, Audio Only or Both options plays the appropriate file. The variable that controls this is the select case - end select loop with the variable audio_choice. Case 6 in the program segment shown above illustrates this idea.

Notice that the choice for Audio Only uses the wait property because, if a stop button is used to stop the audio, then the form vclip has to be visible. Since Audio Only is played, there is no point in displaying this additional form. In the cases of audio_choice, however, we use the delay loop to wait until the video and audio files are completely played. Another important consideration is the synchronization of the audio and video. So far we have been discussing how to play these files independently and assumed that the files are either only audio or only video. To achieve synchronization, it may be necessary to introduce additional delays. This requires experimentation.


e. Silent Property

What happens if the video file that you have recorded also contains audio? In that event, you can play the audio file with the MCI control and the synchronization problem does not arise at all. However, if you wish to play only the video or only the audio part of it, you will need to use the properties silent and visible, respectively, to do this. Setting the silent property to true suppresses the audio portion of the file, and setting the visible property to false suppresses the video by rendering the vclip form invisible.


f. Recording with MCI Control

If you are trying to record sound with your MCI control, you will notice that you can not save files with the commands Save and Close. This is a bug in Visual Basic. Microsoft has corrected the problem and has made it available in a service pack that is freely available at the Microsoft Web. You will need this service pack to successfully record with MCI control. Also search the Knowledge Base for all the listed bugs in the Visual Basic releases 5.0 and 6.0.



II. Animation Control

An Animation control can not play an AVI file that has sound. Furthermore, the Animation control can only display uncompressed AVI files, or AVI files that have been compressed using Run-Length Encoding (RLE). An example of this Animation control is the file copy progress bar in Windows 95/98 and Windows NT. There are some important properties, such as AutoPlay associated with Animation control, that run the AVI file. You can open video files, then play them, with the following commands to the animation control:


Animation1.open "[filepath\filename]"
Animation1.play

Once the file begins playing, control is returned to the user. It can then be used for another purpose, or it can wait a specified period of time until the video is completely played. The Stop and Close commands are similar to those of MCI controls. They are generally used after completely playing the video file. The MCI control is relatively simple to use but has the disadvantage of not being able to play any AVI file. AVI files it can play should not contain sound. These restrictions discourage users from using this control. However, the MCI control is useful for simple applications.



III. ActiveMovie Control

The most versatile tool in multimedia programming is the ActiveMovie control. This control can do the multimedia programming with and without sound and can play audio and video files separately, just as the MCI control can. An advantage of using ActiveMovie control is to show AVI video files in sizes different from the original (although it can show them at original size too, of course). Display options available with the ActiveMovie control are (as percentage of screen size): 6.25% (or 1/16th), 25%, 50%, 100%, and 200%. However, as size increases, the quality of the picture deteriorates. This also depends on the recording mode of the AVI file.

To demonstrate the ActiveMovie control, I created an ActiveMovie control frame with six command buttons - five controlling display size and the sixth activating the sound/video play option box. You can set the property MovieWindowSize to any allowed size using these buttons. Unlike MCI control, there are no handlers required and the movie is displayed in the ActiveMovie control itself.

There is an interesting point to note about the ActiveMovie control. Assume that the AVI file fits in the box of 5175 x 3540 twips (original size). If the form's ActiveMovie control has the dimensions 500 x 350 twips (nearly ten times less), the ActiveMovie is played in this small video frame when played for the first time with the Original Size button (i.e., 100%). Thereafter, playing the file with any other button generates the specified size. However, if you then replay the file with the Original Size button, the video plays in its actual original size - not in the smaller size as seen at the start of the program. The ActiveMovie placed on the form can be visible or invisible and can have all the controls like stop, pause, play, forward, backward and other related task bars such as showing time or frame numbers. If the Fullscreen property is set to true, then the entire screen shows the AVI file. Playing an audio or video can be done with. Like MCI control, the ActiveMovie control can play files of two different formats simultaneously, but you need more than one ActiveMovie control to do this.

The command button labeled Play Sound or Video invokes an option box for selecting video, audio or both. Since ActiveMovie control has the hwnd property available during run time, it is possible to use this as a handler to another control, such as MCI Control, and view the video played by MCI control in the ActiveMovie. If the video is played using the ActiveMovie and the display controls (mode - AmvTime or AmvFrames, display position controls, position controls) are visible, one can stop the video and also change properties by clicking the right mouse button. Display properties can be made visible at either design time or at run time.



IV. Application Notes

Like every ActiveX component, MCI Control, ActiveMovie control and Animation controls need certain system files. You will find a list of the required system files in the information files provided by VB. The ActiveMovie control requirements are listed in the file Act4Mov.inf in the C:\Windows\inf\ directory. The MCI control needs a file called Mci32.ocx. Animation control is provided by VB with Windows common controls version 5.0. These can be added to the project with the project-components tab before the application is built. While setting up an application, the Setup Wizard does not normally include every listed file in the Act4mov.inf, so the developer must do it manually. Any missing files may cause cause unpredictable errors, such as "unspecified error."

Conclusion

While multimedia applications are complicated, and while each application requires special treatment, the basic method for playing audio/visual files is essentially the same:

  • Define the file type and device,

  • Open the file,

  • Attach a window handler,

  • Play the file,

  • Occupy the play interval with the Wait command, a delay loop, or other process,

  • Stop playing the file, and

  • Close the file.

Compared to the Animation control, the MCI and ActiveMovie controls work better from the programmer's point of view, since they can use any AVI files without worrying about associated sound files, or whether it was created with a particular method of run length encoding (RLE). It appears that attaching a handler to the hwndDisplay property of the MCI control is a better method, since it offers greater flexibility.

Developed Under:

Windows 98
Visual Basic 5.0M

May have issues with:
Windows 2000
Visual Basic 6.0+

Have a fix suggestion?


Featured Article

An Introduction to C#
By Joey Mingrone

Register Today!


100% FREE

Members enjoy these benefits:
Access to ITI Downloads
Access to more articles and tutorials
Optional weekly newsletter
And more...

Click here to register
Or
Click here to log in



© 2008 Interface Technologies, Inc. All Rights Reserved
Questions or Comments? devcentral AT iticentral DOT com
PRIVACY POLICY