![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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 declaration must be placed in one of two locations:
Now lets look at the functions arguments. The first one, lpstrCommand, is a string containing the command that you want to send to the MCI (these are the commands shown previously). The second argument, lpstrReturnString, is used by the MCI to return a message to the calling program. Not all commands result in a return message. The third argument, uReturnLength, specifies the length of the returned string. The final argument is used to specify a callback function that is executed when the command has completed executing. The function returns zero if it is successful in carrying out the requested action, and a non-zero error code otherwise. Callback functions are used by some Windows API functions when they need to interact with the calling program. When a program calls an API function that uses a callback, it passes the address of the callback function as an argument to the API function. Callback itself is a regular Visual Basic function that you write, limited only by the requirements of the API function. To get the address of a function, use the AddressOf operator. Thus, if your callback function for use with mciSendString is named MyCallBack, you would call the API function as follows: retval = mciSendString(cmd, reply, 0, AddressOf(MyCallBack) The multimedia tasks in this chapter are relatively simple, however, and do not require the use of a callback function. Later, Ill show you another way to detect when a command has completed. Now for a real example. Once you have declared the function, you play a MIDI file as follows: cmd = Open & Chr$(34) & C:\win95\media\canyon.mid & Chr$(34) & _ type sequencer alias canyon retval = mciSendString(cmd, reply, 0, 0) cmd = play canyon retval = mciSendString(cmd, reply, 0, 0) In a real program we would have to wait for play to finish before closing the device. cmd = close canyon retval = mciSendString(cmd, reply, 0, 0) Note how the file name itself is enclosed in double quotes by using Chr$(34). If you executed this code as iswith the Close command sent immediately after the Play commandyou would hear nothing. The device would be closed immediately. You need some way to pause until the playback is completewhether sound or video. Two methods are available for doing this. One method uses the Wait parameter to the Play command. Wait instructs the MCI not to accept any other commands, such as Close, until the current playback is complete: cmd = play canyon wait retval = mciSendString(cmd, reply, 0, 0) cmd = close canyon retval = mciSendString(cmd, reply, 0, 0) With this code, the full CANYON.MID file will play. Because the program is frozen during playback, however, the user cannot continue with other tasks. Thus, Wait is a good solution for short sounds where a frozen program is not a problem. For longer playback, it clearly will not suffice. For this, you can use the Status command, which has the following syntax: Status device parameter The device argument specifies which devices status we are requesting; this is the name we assigned with the Alias keyword when we opened the device. The parameter argument specifies the actual status we are requesting. Table 16.3 lists the Status parameters commonly needed for sound and video devices. Here is a method that uses the Status command to delay issuing the Close command until the music file is finished playing: cmd = Open & Chr$(34) & C:\win95\media\canyon.mid & Chr$(34) & _ type sequencer alias canyon retval = mciSendString(cmd, reply, 0, 0) cmd = play canyon retval = mciSendString(cmd, reply, 0, 0) Wait for play to finish before closing. cmd = status canyon mode do retval = mciSendString(cmd, reply, 0, 0) until reply <> playing cmd = close canyon retval = mciSendString(cmd, reply, 0, 0) Did you notice a problem with this approach? The program is not technically frozen as the sound plays, but it is effectively frozen while it mindlessly executes a loop over and over. Well see a way to use Visual Basics Timer control to get around this problem later in the demonstration program. Well also see how to use a callback function. First, lets look at some of the other MCI commands. The Seek CommandThe Seek command repositions the pointer in a device. The pointer indicates the current playing position. Play stops when we use Seek, and any subsequent Play command resumes playing from the new position. The syntax of this command is: Seek to position The most common use for Seek is to reset the file pointer to the beginning of the file, which is accomplished by using a value of zero for the position argument. If you want to seek to any location other than the beginning, you must specify the position argument in the devices current TimeFormat (discussed later in the chapter).
The Set CommandSet allows you to control certain |