![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
The device argument is the alias established for the device when it was opened. The setting argument is one of the following, which should be self-explanatory:
The Capability CommandUse the Capability command to determine if an opened device has a specified capability. Initiating this command sends a capability name as an argument: Capability CapabilityName In this case, the mciSendString function returns True in the ReturnString argument if the device has the capability, False if not. Note that these return values are the strings True and False and not the usual logical values. The most essential capabilities are listed here:
Dealing With ErrorsLike all other aspects of programming, the MCI has the potential for errors. A CD not inserted in the drive, a corrupt AVI file, or a malfunctioning sound card are examples of the many situations that can conspire to interfere with smooth multimedia presentations. Errors of this nature are not trappable using Visual Basics standard error-handling mechanisms (covered in Chapter 25). Even so, a well-designed program must detect multimedia errors and handle them gracefully, reporting needed corrective action to the user whenever possible. Fortunately, the MCI provides the necessary capabilities. As you may recall, the return value of the mciSendString function is zero on success and a non-zero error code if an error occurred. Central to MCI error trapping, then, is checking the return value of mciSendCommand every time a command is issued. If the return value is zero, everything is fine. If not...well, to be honest, a numerical error code is not much help. Sure, you could whip out your reference books and look it up, but is there a better way? Definitely. The better way is the mciGetErrorString function. Passing the numerical error code to this API function returns a string that describes the exact nature of the error. Because this is not a Visual Basic function, but rather a part of the Windows API, you must declare it as follows: Declare Function mciGetErrorString Lib winmm Alias mciGetErrorStringA _ (ByVal dwError As Long, ByVal lpstrBuffer As String, _ ByVal uLength As Long) As Long The dwError argument is the error code returned by mciSendString. The lpstrBuffer argument is a string variable where the descriptive error message will be placed. This should be a fixed-length string at least 255 characters long. The final argument, uLength, specifies the length of lpstrBuffer, in characters. Assuming that the variable Cmd contains an MCI command, the code to handle an MCI error would look like this: Dim ErrorMessage As String * 255, ReturnString As String * 255 x = mciSendString(Cmd, ReturnString, 255, 0) if x <> 0 then r = mciGetErrorString(x, ErrorMessage, 255) MsgBox(ErrorMessage) End If Demonstrating The MCI CommandsThe project SOUND1 presents a relatively simple demonstration of the multimedia techniques discussed so far. The program displays a blank form with a single menu command, File Open. You can open a video file (*.AVI), a WAV file (*.WAV), or a MIDI sequencer file (*.MID). The program will play it through to the end and close the file. You can then open another file and play it. Despite the programs name, it will play video as well as sound files. Whats unusual about this program is the method used to determine when playback is complete. As mentioned earlier, a well-designed multimedia program will not freeze while a command is executing, so we need some way to detect when a command has been completed. One approach is using mciSendCommand to send the command status alias mode to the device. The return value argument to the mciSendString function will contain a string giving the devices current mode. While the device is playing, the return string will be playing. By checking the device mode repeatedly, you know its safe to close the device as soon as the returned value is not playing. How can you do this? The answer lies in Visual Basics Timer control. Set the timer interval, start the timer, and it will repeatedly count down to zero, reset itself to the specified interval, then start counting down again. Each time the Timer counts to zero, its Timer event procedure is triggered. Code in this procedure can obtain the MCI device mode and close it once playback is completed. To specify the Timer controls countdown interval, set its Interval property. The unit used is milliseconds, or thousandths of a second. Next, set its Enabled property to True to begin timing, and set Enabled to False to terminate timing. The Timer control is never visible on screen (except during program design); it just works in the background. You can have more than one Timer control on the same form. Now lets get to work on the demonstration project. Start with a blank form and add one Timer control and one CommonDialog control, leaving the properties of both at their default settings. Create a File menu with two commands, Open and Exit. The forms objects and properties are given in Listing 16.1. Remember that this type of listing is not code you type in, but rather a shorthand way of listing the types and properties of the controls to be placed on the form. Listing 16.1 Objects and properties in SOUND1.FRM. Begin VB.Form Form1 Caption = MCI Demonstration Begin VB.Timer Timer1 End Begin MSComDlg.CommonDialog CommonDialog1 End Begin VB.Menu mnuFile Caption = &File Begin VB.Menu mnuFileOpen Caption = &Open Shortcut = ^O End Begin VB.Menu mnuSeparator Caption = - End Begin VB.Menu mnuFileExit Caption = E&xit End End End The program code is presented in Listing 16.2. From what you have learned in the text and the comments in the code, you should be able to figure out how the code works. Remember that you must place the two API function declarations in the General Declarations section of the forms code. You can eliminate typing by copying the declarations from the API text viewer application provided with Visual Basic.
|
![]() |
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. Read EarthWeb's privacy statement. |