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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Handling Multimedia Control Errors

As mentioned earlier, using the Multimedia control has the potential for errors. A well-designed program must detect multimedia errors and handle them gracefully, reporting needed corrective action to the user when possible. Fortunately, the Multimedia control provides you with the necessary capabilities:

  Most important are the Error and ErrorMessage properties. The Error property contains the error code returned by the last MCI command, and the ErrorMessage property provides a string description of the error. When everything works okay, the error code is zero, and the string in ErrorMessage is “The specified command was carried out.” You can check these properties in the StatusUpdate event procedure to keep a constant eye on the progress of the MCI command.
  The second tool for dealing with errors is the NotifyCode argument passed to the Done event procedure. If this argument is equal to mciFailure (value = 8), the command failed, and we can look in the Error and ErrorMessage properties for details.
  The third tool is the ErrorCode argument passed to the ButtonCompleted event procedures. If ErrorCode is not zero, an error occurred. Once again, check the Error and ErrorMessage properties for details on the error.

A Multimedia Control Demonstration

Now that we have explored the more important details of the Multimedia control, let’s see just how easy it is to use. The project SOUND2 uses the Multimedia control to implement a simple media player. You can open a media file (MIDI, WAV, or AVI) or the CD Audio device (assuming an audio CD is in the drive). Then you can use the Multimedia control’s buttons to play the device. There’s no error handling in the program—a feature you might want to add. In fact, you could add any number of enhancements to the program. The Multimedia control has enough power to serve as the heart of a full-featured media player. A good project to tackle on your own would be modifying this program so that the Multimedia control is hidden, but still used as the program’s interface to the MCI.

The program, shown operating in Figure 16.3, has a single form with two controls: Multimedia and CommonDialog. There’s also a File menu with three commands: Open, Play CD, and Exit. The form’s objects and properties are shown in Listing 16.3 and its code in Listing 16.4.


Figure 16.3  The SOUND2 media player.

Listing 16.3 Objects and properties in SOUND2.FRM.

Begin VB.Form Form1
   Caption         =   “Multimedia Player”
   Begin MSComDlg.CommonDialog CommonDialog1
   End
   Begin MCI.MMControl MMControl1
   End
   Begin VB.Menu mnuFile
      Caption         =   “&File”
      Begin VB.Menu mnuFileOpen
         Caption         =   “&Open”
      End
      Begin VB.Menu mnuFilePlayCD
         Caption         =   “&Play CD”
      End
      Begin VB.Menu mnuSeparator
         Caption         =   “-”
      End
      Begin VB.Menu mnuFileExit
         Caption         =   “E&xit”
      End
   End
End

Listing 16.4 Code in SOUND2.FRM.

Option Explicit

Const mciPause = 529
Dim DeviceOpen As Boolean

Private Sub Form_Load()

Dim x As Integer, y As Integer

‘ Initialize flag.
DeviceOpen = False

‘ Set form size and control position.
x = Form1.Width - Form1.ScaleWidth
y = Form1.Height - Form1.ScaleHeight

Form1.Width = MMControl1.Width + x
Form1.Height = MMControl1.Height + y

MMControl1.Left = 0
MMControl1.Top = 0

CommonDialog1.Filter = “Wave (*.WAV)|*.wav|MIDI (*.MID)|*.mid|Video (*.AVI)|*.avi”
End Sub

Private Sub Form_Unload(Cancel As Integer)

‘ Close the device.

MMControl1.Command = “Close”

End Sub

Private Sub MMControl1_Done(NotifyCode As Integer)

‘ Enable the File menu.
mnuFile.Enabled = True

End Sub

Private Sub MMControl1_PlayClick(Cancel As Integer)

‘ Set Notify so the Done event will be triggered.
MMControl1.Notify = True
‘ Disable the menu while play is in progress.
mnuFile.Enabled = False

End Sub

Private Sub MMControl1_StopClick(Cancel As Integer)

‘ Enable the menu.
mnuFile.Enabled = True

End Sub

Private Sub mnuFileExit_Click()

End

End Sub

Private Sub mnuFileOpen_Click()

‘ If the device is open, close it.
If DeviceOpen Then
    MMControl1.Command = “Close”
    DeviceOpen = False
End If

‘ Show the Open dialog.
CommonDialog1.ShowOpen

‘ If user cancels, exit sub.
If CommonDialog1.FileName = “” Then Exit Sub

‘ Set MM control properties.
MMControl1.FileName = CommonDialog1.FileName
MMControl1.DeviceType = “”
MMControl1.Command = “Open”
DeviceOpen = True

End Sub

Private Sub mnyFilePlayCD_Click()

‘ Open the CD Audio device. The CD should already be
‘ inserted in the drive.
MMControl1.DeviceType = “CDAudio”
MMControl1.Command = “Open”
DeviceOpen = True

End Sub


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