As you will see, playing videos is easy using the Windows MCI Command interface for the playback which is supported in Windows XP, Windows Vista, Windows 7, Windows 8, and Windows 10. Specifically the MCISendString function will be used in this tutorial.
MCI should by default already support the common formats like avi, mpg, mpeg, wmv, etc. with a basic Windows installation. But you can actually get MCI to playback nearly all formats if the correct Codec/Driver is installed. For Example, you can playback Apple QuickTime .Mov, MPEG 4 DivX, or H.264 .MP4 by simply installing the codec for each format. After that just program the device like you normally would.
This article is only to show how to do basic playback of videos and not features like duration/length, position, or resizing the video. You can check out my MCI MultiMedia Tutorial to learn how to program MCI and add your own features.
If your using VB 6.0 select the correct MCISendString declare below, change any IntPtr.Zero code to 0, and use ‘.hWnd’ instead of ‘.Handle’.
Edit: 2014, December 17th: I decided to make a download available that uses all of the codes in this article with a couple extras to help resizing and such. I also changed some of the code in the program for Option Explicit.
Download Example: Click Here
View Screenshot: Click Here
Visual Basic 6.0 and Older
'The Main API call that will be used for the playback. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _ Long, ByVal hwndCallback As Long) As Long
Visual Basic.NET and Newer
'The Main API call that will be used for the playback. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _ Integer, ByVal hwndCallback As IntPtr) As Integer
OR You Can Use…
'The main API call. <DllImport("winmm.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Public Shared Function mciSendString(commandString As String, returnString As String, _ returnStringLength As Integer, _mciCallback As IntPtr) As Int32 End Function
Now make a couple variables.
'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
Then set a path to the file you want to play. If you’ve seen my other MCI articles you should remember about adding “Quotes” around the path and filename, or at least supply the file’s short pathname.
'The path to the movie or video to play. filename = "c:\movies\MyMovieFile.mp4" 'Now add the quotes around the path. filename = Chr(34) & filename & Chr(34)
Next thing to do is open and setup a playback device. I usually use the MPEGVideo device since it works with audio and video. The MPEGVideo device is DirectShow down under.
There are a couple ways you can open the video. You can have it display in its own popup window or have it displayed in a control like a Panel or Frame. Basically any control with a .hWnd or .Handle property.
This command will display open a new device and have the video display in a control window.
'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 an example app. Basically any control with a 'handle can be used. For VB 6 you would use the controls .hWnd property instead of the 'Handle.ToInt32 property. 'If you want MCI to select which device to use just take out "type mpegvideo" from the code. retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie parent " _ & movieWindow.Handle.ToInt32 & " style child", vbNullString, 0, IntPtr.Zero)
If you want the video to open in a separate window use this code instead.
'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 “myMovie”. If you want MCI to select which device to use just take out "type mpegvideo" from the code. retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie", vbNullString, 0, IntPtr.Zero)
OK, now that you’ve opened a device, all you have to do is call the Play command to begin playback. The codes below will Play, Stop, Pause, and Resume.
' 'Start Playing the movie once you’ve setup the device with your file. retVal = mciSendString("play myMovie", vbNullString, 0, IntPtr.Zero)
'Will Stop the playback if its currently playing. retVal = mciSendString("stop myMovie", vbNullString, 0, IntPtr.Zero)
'Will Pause the playback if its playing. retVal = mciSendString("pause myMovie", vbNullString, 0, IntPtr.Zero)
'Will Resume the playback if it has been paused. retVal = mciSendString("resume myMovie", vbNullString, 0, IntPtr.Zero)
You want to use this code when your done with your device and/or closing your program so the mci resources will be cleaned up. You could change ‘close myMovie’ and put ‘close all’ to free everything MCI has used.
'Will make sure the previous alias is destroyed. If the alias “myMovie” 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 myMmovie", vbNullString, 0, IntPtr.Zero)
Well, that’s all there is to it. Hope you found something useful from this article. Have Fun!