Click Here!
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 MPG Files

The Testing Department is calling again. Your new program, SuperDuperMultimedia4U, is terrific—but how about playing MPG format files? No problem, you say.

You can play MPG format files with the multimedia control. How do you display the images in the MPG file? You connect the multimedia control to a picture box using the picture box’s hWnd property, placing that handle in the multimedia control’s hWndDisplay property. Opening the file itself makes the buttons in the multimedia control active.

Let’s see an example; here, we’ll play an MPG file named, say, demo.mpg. Add a multimedia control, MMControl1, to a program now, as well as a picture box, Picture1, in which we’ll play demo.mpg. We also add a label, Label1, to display the multimedia control’s status.

When the form loads, we load the multimedia control’s FileName property with the name of the file we want to open:

Private Sub Form_Load()
    MMControl1.FileName = "c:\demo.mpg"
...
End Sub

Next we connect the picture box’s hWnd property to the multimedia control’s hWndDisplay property to connect the picture box to the multimedia control:

Private Sub Form_Load()
    MMControl1.FileName = "c:\demo.mpg"
    MMControl1.hWndDisplay = Picture1.hWnd
...
End Sub

Finally, we open the file, which enables the buttons in the multimedia control:

Private Sub Form_Load()
    MMControl1.FileName = "c:\demo.mpg"
    MMControl1.hWndDisplay = Picture1.hWnd
    MMControl1.Command = "Open"
End Sub

Now when users click the buttons in the multimedia control, they can play, stop, and restart the MPG file as they like. The animation appears in the picture box Picture1.

Besides playing the MPG file, we can display what the multimedia control is doing (for example, playing, stopped, and so forth) 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 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 it—multimedia controls can play files in this format just as they can play AVI files; our MPG player is a success. The code for this example is located in the mpgplayer folder on this book’s accompanying CD-ROM.

Keeping Track Of Multimedia Command Execution Using Notification

You can gain more control over the multimedia control using the Notify property and the Done event. When you set a multimedia control’s Notify property to True, you’ll get notification when the control finishes executing commands. How does it notify you? It generates a Done event.

In fact, when you set Notify to True, your program is only supposed to be notified when the multimedia control is finished with the next command, but in fact, Done events appear to be generated for every command as long as Notify is True.

Let’s see an example. Here, we add a multimedia control, MMControl1, to a program and set its Notify property to True when the form loads:

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

When the control finishes executing a command, it fires a Done event, which we catch in an event handler procedure:

Private Sub MMControl1_Done(NotifyCode As Integer)

End Sub

Here, the event handler procedure is passed a notification code, NotifyCode, which can take these values:

  mciSuccessful—1; command completed successfully
  mciSuperseded—2; command was superseded by another command
  mciAborted—4; command was aborted by the user
  mciFailure—8; command failed

In this example, we just display a message box, indicating to the user that the multimedia command is finished:

Private Sub MMControl1_Done(NotifyCode As Integer)
    MsgBox "Finished the multimedia command."
End Sub

Using multimedia notification, you can coordinate your multimedia actions—for example, if you have two multimedia controls, you might not want to start playing sounds with one until the other is finished playing its own sounds.


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.