How to Use Windows 3.1 APIs to Play Videos in Visual Basic

Last reviewed: February 6, 1997
Article ID: Q96090
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

You can play video .AVI files in Visual Basic by using Windows version 3.1 APIs.

NOTE: You must have the video for Windows driver installed on your system. Choose the Drivers icon from Control Panel to see what driver(s) you have installed. The driver you need is [MCI] Microsoft Video for Windows.

MORE INFORMATION

Use the following procedure to position and size the window where you want to play the .AVI file and to play the .AVI file:

  1. Run Visual Basic, or if Visual Basic is already running, choose New Project from the File menu (ALT, F, N). Form1 is created by default.

  2. Add a command button control (Command1) to Form1.

  3. Add the following code to the Command1_Click event of Form1:

       Sub Command1_Click ()
       Dim CmdStr As String
       Dim ret As Long
       Dim ErrorStr As String
    
       ErrorStr = Space(255)
       '*** This will open the AVIVideo and create a child window on the
       '*** form where the video will display. Animation is the device_id.
       '*** Replace c:\rbtndog.avi with the AVI you want to play. You must
       '*** put use the full path, for example, c:\tmp\avi\avifiletoplay.avi
       CmdStr =("open c:\tmp\rbtndog.avi type AVIVideo alias Animation parent "
               + LTrim$(Str$(form1.hWnd)) + " style " + LTrim$(Str$(WS_CHILD)))
       ret = mciSendString(CmdStr, 0&, 0, 0)
       If ret > 0 Then
          ret = mciGetErrorString(ret, ErrorStr, 255)
          MsgBox ErrorStr
       End If
       '*** Put the window at location 10 10 relative to the parent window
       '*** with a size of 200 200
       ret = mciSendString("put Animation window at 10 10 200 200", 0&, 0, 0)
       If ret > 0 Then
          ret = mciGetErrorString(ret, ErrorStr, 255)
          MsgBox ErrorStr
       End If
    
       '*** The wait tells the MCI command to complete before returning
       '*** control to the application.
       ret = mciSendString("play Animation wait", 0&, 0, 0)
       If ret > 0 Then
          ret = mciGetErrorString(ret, ErrorStr, 255)
          MsgBox ErrorStr
       End If
    
       '*** Close windows so they don't crash when you exit the application.
       ret = mciSendString("close Animation", 0&, 0, 0)
       If ret > 0 Then
          ret = mciGetErrorString(ret, ErrorStr, 255)
          MsgBox ErrorStr
       End If
    
    End Sub
    
    

  4. Choose New Module from the File menu (ALT, F, M). MODULE1.BAS is created by default. Add the following code to Module1. Enter the entire Declare on a single line:

    Global Const WS_CHILD = &H40000000

       Declare Function mciSendString Lib "mmsystem" (ByVal lpstrCommand$,
          ByVal lpstrReturnStr As Any, ByVal wReturnLen%, ByVal hCallBack%)
          As Long
       Declare Function mciGetErrorString Lib "mmsystem" (ByVal dwError&,
          ByVal lpstrReturnStr As Any, ByVal wReturnLen%) As Long
    
    

  5. From the Run menu, choose Start (ALT, R, S) or press the F5 key to run the program.

For more information on the sndSendString() function and command strings, see pages 3-26 and 7-23 to 7-93 in the "MultiMedia Programmer's Reference."


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 6, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.