Tag Archives: windows mci

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

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

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.

Play

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

 

Stop


'Will Stop the playback if its currently playing.
 retVal = mciSendString("stop myMovie", vbNullString, 0, IntPtr.Zero)

 

Pause


'Will Pause the playback if its playing.
 retVal = mciSendString("pause myMovie", vbNullString, 0, IntPtr.Zero)

 

Resume


'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!

Jason

Revised: 2015

How to Playback a Audio Compact Disk (Play/Stop/Pause a CD) using VB and VB.NET

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

Thankfully playing audio based .cda cds is an easy process and does not require having to rely on 3rd party controls. You can simply use the Microsoft Windows MCI Command Interface to do this. As you may be aware of you can do almost all of your Media based playback needs by using this interface including playing movie files. I do plan on making a new article on playing movie files in the future. I have a complete tutorial with some source code on how to use and program the mciSendString API so you can get a much deeper understanding on how it works and how to use it. Click on this link if you want to read it.

To get started you first need to setup the main API call that is required. Simply add the API below to your declarations section…

_____________________________

Visual Basic 5.0/6.0….

'
'Api call to send the commands to the mci device
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…

'
'Api call to send the commands to the mci device
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

_____________________________

Now that the main API call is setup and declared you need to start using it to program the device and send it commands. The first thing you need to do is create a new device and set the device to use the CDAudio driver. As you may have guessed this driver will do AudioCD playback. Below is how to create a new device using the CDAudio driver.

Note: In this article if your using VB 5/6 you only need to change Integer or Int32 types to Long types.

_____________________________

'
'This will hold the mci return value. The value in this variable will not be used in this article however.
Dim retVal As Integer
'
'This will be used to specify the drive letter of the CD or DVDRom drive to use.
Dim cdDrive As String
'
'This should be set to the CD or DVD drive your Music CD is in.
cdDrive = "D:\"
'
'Add quotes which will keep from having to get the short path since the mci device could fail if using the normal long file and path. Remember to set cdDrive to the CDRom or DVDRom optical drive. Specify like: D: or D:\ if D is your drive, or E: or E:\ and so on.
cdDrive = Chr(34) & cdDrive & Chr(34)
'
'Create a new CDAudio device with the alias CD. “CD” is how we will refer to the device  in this program.
retVal = mciSendString(“open “ & cdDrive & ” type cdaudio alias cd wait shareable”, vbNullString, 0, 0)
'
'Send the command to set the time format to the device default. Set this so when asking for time/track information we can get the value as a Tracks/Minutes/Seconds/Frames format.
retVal = mciSendString(“set cd time format tmsf”, vbNullString, 0, 0)

_____________________________

Now that a device has been created to use the CDAudio driver its time to start sending some commands.

_____________________________

The code below will return the total number of tracks on the audio cd in the cd drive…

'
'This variable is used to receive the value when requesting information from the mci device.
Dim buf As String
'
'Give it a 128 space buffer.
buf = Space(128)
'
'This will get the total number of tracks returned from the device.
Dim trackCount As Integer

retVal = mciSendString(“status cd number of tracks”, buf, 128, 0)
'
'Get the integer value from the buffer. It will return 0 (zero)  if no tracks are detected or if no audio cd is present.
trackCount = CInt(Val(buf))

_____________________________

If trackCount has the number 12 then there are 12 tracks starting at Track #1 through to Track #12. So when referring to a specific track you need to specify the exact number you want the information from.

Next you can now go ahead and start Playing, Stopping, and Pausing/Resuming a Audio CD’s tracks. First you want to tell the device which track you want to play. The next set of codes will do that.

_____________________________

'
'This will hold the track number to play. It is then set to track 2 below.
Dim trackToPlay As Integer
'
'Set to play Track number 2 on the compact disk.
trackToPlay = 2
'
'You need to set the time format to the cd devices default so it knows that 1, 2, 3, 4, and so on is the track number.
'Set the time format to the cd devices default Tracks, Minutes, Seconds, Frames when you are wanting to seek to a specific track.
retVal = mciSendString(“set cd time format tmsf”, vbNullString, 0, 0)
'
'This will tell the device to move to the track number specified. Simply put in the track number you want to play.
retVal = mciSendString(“seek cd to “ & trackToPlay, vbNullString, 0, 0)

_____________________________

You can now start Playing, Stopping, Pausing, and whatever it is you want to do.

_____________________________

'
'Start playing the cd at the track you already specified above.
retVal = mciSendString(“play cd”, vbNullString, 0, 0)
'
'Stop the cd from playing.
retVal = mciSendString(“stop cd”, vbNullString, 0, 0)
'
'Will pause the CD if its playing.
retVal = mciSendString(“pause cd”, vbNullString, 0, 0)
'
'Resumes the cd to playing if it was paused.
retVal = mciSendString(“resume cd”, vbNullString, 0, 0)

_____________________________

Thats ALL there is to it! IF the codes in this article was properly implemented then it should Start, Stop, Pause, and Resume an Audio Compact Disk (CD). There is MUCH more you can do with Audio CD’s as well like getting the length, position, fast forward, Opening/Closing the CD Door and so on. But some of those features might be implemented in a future article.

I suggest you really go check out my MCI Command tutorial this site, or an older revision but downloadable .doc version so you can try and learn how to actually implement your own custom functionality and possibly checking out the documentation at microsoft.msdn.com.

If you don’t care or have the time to create your own source code for audio CD based playback then you can check out my AudioCD based class libraries. My vbcodesource.com website has MANY libraries available that greatly simplifies the task of creating a CD player based application. I even recently released a brand new library named csAudioCD Library v2.5 Pro Enhanced for Visual Basic 2005 and VB 2008 that has Over 50 subs and properties. I have libraries for VB.NET 02/03 and VB 5.0/6.0 as well. I suggest you goto my main website under the controls and examples section if your interested. There are example applications available as well on how to use the audio cd libraries.

Anyways, I hope you got something useful from this article and it was easy to understand as well. Sometime in the future I may expand on this subject by showing how to get position, duration based information and such. Have Fun!

Jason