How to Playback Movies and Videos by using VB and Visual Basic .NET

  • by

As you will see, playback of movies and/or videos of various formats is fairly easy to do. As you may have already guessed this article will be using the Windows MCI Command interface for the playback which is supported on all of the modern Windows OS including Windows XP and Windows Vista. Specifically the MCISendString function api will be used in this tutorial.

MCI should by default already support the older common formats like avi, mpg, mpeg, wmv ect… based videos. But you can actually program the device to playback nearly all formats that are used. All you usually need to do is install the Codec/Driver for that format.

Example: If you wanted to playback an Apple Quicktime .Mov file then simply install the codec for that format. Or if you want to play MPEG 4 DivX based videos then just install the DivX codec. After that just program the device like you normally would. Similarly, you can play H.264 Videos, Windows Media Video (.wmv) and so on just installing the codec for those formats.

This article only shows the basic playback for movies/videos and not a bunch of different features like getting the duration/length, position and status of the video. You should check out my MCI MultiMedia Tutorial at this link to learn how to program the device for adding your own custom features. The codes in the article are basically similar with All versions of Visual Basic. Mainly just change the variable types for the codes. The codes below are based around .NET but for them to work for VB 5.0 and 6.0 you just need to change, for example, Int32 or Integer types to “Long” types.

    'The Main API call that will be used for the playback. Simply change the “Integer” to "Long"
    'if your using Classic VB like VB 6.0.
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
        lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
            Integer, ByVal hwndCallback As Integer) As Integer

    'Will hold the path to the movie file.
    Dim filename As String

    'Holds the return value of mciSendstring. Not used for anything in this article though.
    Dim retVal As Integer

Now that the API and variables are setup its about that time to program the device.

Note: Be Sure to specify the file’s path in the filename variable for the video you want to play!

Important Info: There are basically Two(2) ways you can setup your movie or video. You can display the movie in its Own window which will popup a window separate from your application or you can specify the control/window where you want it to be displayed. For instance you can have the video play on your Form or on your Picturebox control.

You first need to open and setup a new device to playback your video. I normally use the MPEGVideo device since it will basically work with most all of the movie formats. The MPEGVideo device is usually DirectShow. If you want MCI to determine which device to use just take out the: “type mpegvideo” codes. The first code will setup the device to play in its Own window and the second will play the video on a control.

If you’ve seen my other MCI articles you will notice you should remember the need to add “Quotes” around the filename and path, or at least supply the Short path name for the files path.

        The path to the movie or video to play.
        filename = "c:\movies\MyMovie.wmv"

        Now add the quotes around the path.
        filename = Chr(34) & filename & Chr(34)

        Specify the mpegvideo driver to play the movies which should play most movie formats without any problems.
        This code will have the video open in its Own window and the alias name will be “movie”.
        retVal = mciSendString("open " & filename & " type mpegvideo alias movie", 0, 0, 0)

        This code below will open a new mpegvideo device and play the movie in the “movieWindow” control which is
        nothing more than a GroupBox/Frame control I used in a example app. Basically any control with a
        handle can be used. For VB 5/6 you might need to specify the controls .hWnd property instead of the
        Handle.ToInt32 property that .NET uses.
        retVal = mciSendString("open " & filename & " type mpegvideo alias movie parent " _
            & movieWindow.Handle.ToInt32 & " style child", 0, 0, 0)

This code below is used if you want to open a new video file using the same “movie” alias that has already been opened. Use this code, right before you open the new device with the ‘open filename’ code above, which will close the previous device and alias. This should also be called when your application is closing so all of the mci resources will be cleaned up.

Will make sure the previous alias is destroyed. If the alias “movie” hasn’t been created yet,
this code will NOT cause any errors or anything. So there is no need to worry about that.
retVal = mciSendString("close movie", 0, 0, 0)

Once that is taken care of (opened a device), all you have to do is call the Play command. The codes below will Play/Stop/Pause/Resume the movie.

        Start Playing the movie once you’ve setup the device with your file.
        retVal = mciSendString("play movie", 0, 0, 0)

        Will Stop the playback if its currently playing.
        retVal = mciSendString("stop movie", 0, 0, 0)

        Will Pause the playback if its playing.
        retVal = mciSendString("pause movie", 0, 0, 0)

        Will Resume the playback if it has been paused.
        retVal = mciSendString("resume movie", 0, 0, 0)

Well, thats basically all there is to it to do basic Movie and/or Video playback using Visual Basic 6.0 and Visual Basic.NET 2002/2003/2005/2008/2010. I just want to clarify that even though I specifically used I think Visual Basic 2008, these .NET codes will also work for the older VB.NET’s and the newer VB’s like VB 2010. I may add a new article in the coming days on how to play a video or movie on the Windows Desktop. Till then, Have Fun!


Leave a Reply

Your email address will not be published. Required fields are marked *