![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Listing 16.2 Code in SOUND1.FRM. Option Explicit Private Declare Function mciSendString Lib winmm Alias mciSendStringA _ (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Private Declare Function mciGetErrorString Lib winmm Alias _ mciGetErrorStringA (ByVal dwError As Long, ByVal lpstrBuffer _ As String, ByVal uLength As Long) As Long Constant for the MCI alias since well only have one device open at a time. Const ALIAS = MyDevice Private Sub Form_Load() Set up the Open dialogs filter. CommonDialog1.Filter = _ WAVE (*.WAV)|*.wav|MIDI (*.MID)|*.mid|Video (*.AVI)|*.avi End Sub Private Sub Form_Unload(Cancel As Integer) Dim cmd As String, ret As String * 255, x As Long When the form unloads be sure the device is closed. cmd = close & ALIAS x = mciSendString(cmd, ret, 255, 0) End Sub Private Sub mnuFileExit_Click() End End Sub Private Sub mnuFileOpen_Click() Dim cmd As String, devicetype As String, ret As String * 255 Dim rv As Long, rv1 As Long Display the Open dialog. CommonDialog1.ShowOpen Exit if user canceled. If CommonDialog1.filename = Then Exit Sub Get the selected file name extension. cmd = UCase$(Right$(CommonDialog1.filename, 3)) Set the type argument depending on the extension of the file selected. Select Case cmd Case AVI devicetype = avivideo Case MID devicetype = sequencer Case WAV devicetype = waveaudio Case Else MsgBox (Invalid file format) Exit Sub End Select Set up the MCI command and send it. cmd = open & Chr$(34) & CommonDialog1.filename & Chr$(34) & _ type & devicetype cmd = cmd & alias & ALIAS rv = mciSendString(cmd, ret, 255, 0) On error. If rv <> 0 Then rv1 = mciGetErrorString(rv, ret, 255) MsgBox (Error opening device: & ret) Exit Sub End If Send the play command. cmd = play & ALIAS rv = mciSendString(cmd, ret, 255, 0) If rv <> 0 Then rv1 = mciGetErrorString(rv, ret, 255) MsgBox (Error playing device: & ret) Exit Sub End If Play command successful. Enable the timer and set a 1000 msec interval. Disable the File menu. Timer1.Interval = 1000 Timer1.Enabled = True mnuFile.Enabled = False End Sub Private Sub Timer1_Timer() Dim cmd As String, ret As String * 255, rv As Long Each time the timer counts down, check the MCI device to see if it is still playing. Set up the MCI command and send it. cmd = status & ALIAS & mode rv = mciSendString(cmd, ret, 255, 0) If the mode is not playing then we are done and can close the device, disable the timer, and enable the File menu. If Left$(ret, 7) <> playing Then cmd = close & ALIAS rv = mciSendString(cmd, ret, 255, 0) Timer1.Enabled = False mnuFile.Enabled = True End If End Sub Using The Visual Basic Multimedia ControlThe second method for sending MCI commands is Visual Basics Multimedia control. When displayed on a form, this control provides an array of buttons for controlling a multimedia device. The control is shown in Figure 16.2. The buttons are defined, from left to right, as Prev(ious), Next, Play, Pause, Back, Step, Stop, Record, and Eject. The first step in using the Multimedia control is to set its FileName property or its DeviceType property. Set the FileName property when the data to be played is in a file that specifies the type of device to be openedfor example, WAV, MIDI, and AVIVideo files. Set the DeviceType property when the device does not use filessuch as CD Audio or with a compound device where the associated files extension does not specify the type of device. Next, open the device by setting the Multimedia controls Command property to Open. The control takes care of the details of sending the command to the MCI and obtaining the necessary information about the device. When it comes to actually manipulating the device, you have two choices:
Of course, you can use these two methods in tandem. If the control is visible, you can still send commands by placing them in the Command property. Likewise, if the control is hidden, it can be made visible if needed. Much of the appeal of the Multimedia control lies in simplifying tasks that would be more difficult to accomplish by directly accessing the MCI, as shown earlier in this chapter. The control has many properties that correspond to information about the open multimedia device, such as the information accessible with the Capabilities command. For example, simply read the Multimedia controls CanEject property to determine if the open device can eject its media, and read the Mode property to determine the current state of the device. Lets take a closer look at some of these properties (refer to Visual Basic Help for details on all Multimedia control properties).
Multimedia Control Properties The Mode property returns one of the following values (as a type Long ), indicating the current mode of the open device as shown in Table 16.4. The CanEject, CanPlay, CanStep, and CanRecord properties return True or False, depending on whether the open device has the indicated capability. The AutoEnable property determines if the individual buttons on the Multimedia control are automatically enabled and disabled to reflect the capabilities of the open device (AutoEnable = True), or whether the program must enable and disable individual buttons by setting the corresponding ButtonEnabled property (AutoEnable = False), where Button is either Back, Eject, Next, Pause, Play, Prev, Record, Step, or Stop. The Enabled property controls user access to the entire Multimedia control. If Enabled is set to False, the user cannot select any of the controls buttons. |