• Recent Posts

  • Meta

  • Tags

    #ziplib .net getfilename .net no titlebar 7-zip adding a splash screen in visual basic.net adding textbox autocomplete addition adjusttokenprivileges adware application filename application position applications path audio library autocomplete autocomplete combobox avi mpg basic math bass.dll bass sound system beep benchmark data calculator cdrom tray change unix to windows directory exists disable beep on enter doevents excel file exists getinputstate high cpu usage linq mcisendstring no doevents open webpage pinging play mp3 play wave scroll textbox shell stop beep on enter textbox vb ping visual studio windows mci
  • « | Home | »

    How-to Playback Movies/Videos in VB and Visual Basic .NET

    By admin | July 6, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
    Loading ... Loading ...


       
    Bookmark and Share

     


     

      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.

        

      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 Shortpathname 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 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 5.0/6.0 and Visual Basic.NET 2002/2003/2005/2008. I may add a new article in the coming days on how to play the video or movie on the Windows Desktop. Till then, Have Fun!

       

                           Jason

     


     

    Topics: - (.5.0/6.0), - (.NET All + 05/08), - .All VB (Related to All) | 1 Comment »

    One Response to “How-to Playback Movies/Videos in VB and Visual Basic .NET”

    1. Ed Says:
      August 29th, 2008 at 4:57 am

      Thank's for your information about MCI Tutorial

    Comments