|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Playing MID FilesCan you play MID format sound files from Visual Basic? You sure can, using the multimedia control.
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 controls 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 controls 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 hasnt 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
Thats all we need. Now run the program as shown in Figure 22.5 (weve 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 weve designed it.
The code for this example is located in the midplayer folder on this books accompanying CD-ROM. Playing AVI FilesThe Testing Department is calling again. The companys glorious founder has made an inspirational speech, which theyve 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, howeverhow can you display images? You can connect the multimedia control to a picture box control by setting the multimedia controls hwdDisplay property to the picture boxs hWnd property (the hWnd property is a handle to the window that actually makes up the picture boxs display). Lets see how this works in an example. Here, well play the AVI file C:\windows\help\scroll.avi, which comes with Windows as one of the Windows tutorial animationsthis 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 well display the status of the multimedia control. When the form first loads, well 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 controls 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
Thats itnow 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.
The code for this example is located in the aviplayer folder on this books accompanying CD-ROM.
|
|
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. |