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 MID Files

Can you play MID format sound files from Visual Basic? You sure can, using the multimedia control.


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.

For example, we can play the C:\windows\media\canyon.mid file that comes with Windows. To do that, add a multimedia control, MMControl1, to a form, as well as a label, Label1, in which we can display the multimedia control’s current operation (such as playing, stopped, and so on).

When the form first loads, we can open the canyon.mid file this way in the multimedia control:

Private Sub Form_Load()
    MMControl1.FileName = "c:\windows\media\canyon.mid"
    MMControl1.Command = "Open"
End Sub

Besides playing the MID 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.5 (we’ve added a label to the program to display a caption). When you click the Play button, the MID file will be played. Our program works as we’ve designed it.


Figure 22.5  Playing MID files from Visual Basic.

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

Playing AVI Files

The Testing Department is calling again. The company’s glorious founder has made an inspirational speech, which they’ve been lucky enough to capture in an AVI file. Oh good, you say. They ask, can your program play that speech on demand?

You can play AVI files with the multimedia control. That control just displays a bar of control buttons, however—how can you display images? You can connect the multimedia control to a picture box control by setting the multimedia control’s hwdDisplay property to the picture box’s hWnd property (the hWnd property is a handle to the window that actually makes up the picture box’s display).

Let’s see how this works in an example. Here, we’ll play the AVI file C:\windows\help\scroll.avi, which comes with Windows as one of the Windows tutorial animations—this one shows how to use scroll bars. Add a picture box, Picture1, to your form, as well as a multimedia control, MMControl1, and a label, Label1, in which we’ll display the status of the multimedia control.

When the form first loads, we’ll open scroll.avi and connect the multimedia control to the picture box Picture1 this way:

Private Sub Form_Load()
    MMControl1.FileName = "C:\windows\help\scroll.avi"
    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 AVI file as they like. The animation appears in the picture box Picture1.

Besides playing the AVI file, we can display what the multimedia control is doing (for example, playing, stopped, and so on) 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—now run the program as shown in Figure 22.6. As you can see in that figure, the program plays the AVI in the picture box. Our multimedia animation example is a success.


Figure 22.6  Playing AVI files with the multimedia control.

The code for this example is located in the aviplayer 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.