![]() Click here for a complete OFFICIAL list of ALL MCI Commands Home AVI (Audio Visual) WAV (Sound/Audio) MIDI(Sequence/Music) CD (CD Audio) Download Links FAQ Email |
AVI Audio Video An AVI file is a movie file that can be played in Windows. If you have Visual Basic 4.0 or newer then copy and paste this code into a module. (eg. module1.bas)
Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
If you have Visual Basic 3.0 then copy and paste this code into a module. (eg. module1.bas) Declare Function mciSendString Lib "mmsystem" (ByVal lpstrCommand$, ByVal lpstrReturnStr As Any, ByVal wReturnLen%, ByVal hCallBack%) As Long OPEN We must OPEN an AVI file before we can do anything to with it. All of the other MCI commands listed on this web page require that the OPEN command to be used first, otherwise they will not work. The type of multimedia file we want to use is avivideo and the alias name that we will use to refer to the AVI file is video1 (Although we can use any alias name you desire, such as "Myvideo", "video2", "john1", etc... , we'll use video1 for easy reference in the examples that follow).
i = mciSendString("open c:\myvideo.avi type avivideo alias video1", 0&, 0, 0)
PLAYWhen an AVI is open, we can play it. The keyword in this command is PLAY. Notice that we use the alias video1. This is the same as typing in c:\myvideo.avi, but use the alias because you can always refer to the same AVI file throughout your entire VB program without using a global variable.
i = mciSendString("play video1", 0&, 0, 0)
Play from...You don't have to start playing an AVI file from the beginning. You can start anywhere within the file. This example starts at segment 4 (depends if set at milliseconds or frames, see below under FRAMES or MILLISECONDS) and plays to the end.
i = mciSendString("play video1 from 2000", 0&, 0, 0)
Play from to...Segments of an AVI file can be played. Perhaps you have an AVI file with several different scenes, and want to play only one of them (sample is frame 15 to frame 20).
i = mciSendString("play video1 from 15 to 20", 0&, 0, 0)
Wait...An AVI file can be played without any interruption from other Windows. The wait command sets the focus entirely on playing the AVI file until it has reached the end. It is a good idea not to use this command on long, time-consuming files. You are virtually "locked out" of your computer until the file completely finishes playing.
i = mciSendString("play video1 wait", 0&, 0, 0)
PauseWhile an AVI file is being played, you can pause it. Use the resume command to resume playing.
i = mciSendString("pause video1", 0&, 0, 0)
Resume...When an AVI file is paused, you can resume and continue playing it.
i = mciSendString("resume video1", 0&, 0, 0)
Stop...While an AVI file is being played, you can stop it. Use the play command to start playing the file again.
i = mciSendString("stop video1", 0&, 0, 0)
TIP:Milliseconds By default, the AVI file is set at milliseconds (or MS). That is, you can access any position within an AVI file in segments of milliseconds. The code below will set the AVI to milliseconds.
i = mciSendString("set video1 time format ms", 0&, 0, 0)
FramesYou can access any position within an AVI file in segments of picture frames. The code below will set the AVI to frames.
i = mciSendString("set video1 time format frames", 0&, 0, 0)
SeekAny position within an AVI can be set by using the seek command. This also sets the position of the AVI for PLAY. The example below sets the avi starting position to 1000 (milliseconds or frames).
i = mciSendString("seek video1 to 1000", 0&, 0, 0)
Seek to beginning of AVI
i = mciSendString("seek video1 to start", 0&, 0, 0)
Seek to end of AVI
i = mciSendString("seek video1 to end", 0&, 0, 0)
Close...Perhaps this is the most important MCI command code for your project. You must close the AVI file (unless you need it open for another program). Leaving it open could cause problems with Windows. The opened AVI file will remain open, even when you exit your software. This is known as "being used", or "hanging".
i = mciSendString("close video1", 0&, 0, 0)
Audio ON/OFFSome AVI files have an audio track. You can turn the audio on or off with the sample codes below.
i = mciSendString("set video1 audio all off", 0&, 0, 0)
OR
i = mciSendString("set video1 audio all on", 0&, 0, 0)
/-------------------------------------------------> Determine the STATUS of an AVI file How long is the AVI file? You will probably want to use this code to set the maximum value for a Progress Bar, Meter or a scroll bar. You will need to decide if you want to display the length of the AVI in Time or Frames. Notice that the STR() is used. Without it, the mssg variable would be 255 charactrers long. FRAMES:
Dim mssg As String * 255
i = mciSendString("set video1 time format frames", 0&, 0, 0)
i = mciSendString("status video1 length", mssg, 255, 0)
msgbox "There are " & Str(mssg) & " frames"
TIME:
Dim mssg As String * 255
i = mciSendString("set video1 time format ms", 0&, 0, 0)
i = mciSendString("status video1 length", mssg, 255, 0)
MsgBox "There are " & Str(mssg) & " milliseconds"
AVI Status (or Mode)The current status of an AVI can be used for many applications. Below is some code to determine if the AVI is paused, stopped or playing.
Dim mssg As String * 255
i = mciSendString("status video1 mode", mssg, 255, 0)
MsgBox mssg
/-------------------------------------------------> Display an AVI onto a Form in your Visual Basic Project Setup an AVI for a form (or picture box control) The sample code below assumes that you have a Form in your project, named Form1. Set the ScaleMode to 3 or Pixel. Copy and paste the code into your project. The AVI will be played on your form, 16 pixels from the left, and 10 pixels from the top. It will be stretched 124 pixels wide, and 120 pixels tall. You can change these settings to suite your needs.
Last$ = Form1.hWnd & " Style " & &H40000000
ToDo$ = "open c:\shut.avi Type avivideo Alias video1 parent " & Last$
i = mciSendString(ToDo$, 0&, 0, 0)
i = mciSendString("put video1 window at 16 10 124 120", 0&, 0, 0)
i = mciSendString("play video1 wait", 0&, 0, 0)
i = mciSendString("close video1", 0&, 0, 0)
How to find the actual width and height of an AVIThe Sub Routine below will open an AVI, onto Form1 and determine the width and height (as well as the TOP and LEFT which we do not actually need). It will play the AVI in it's original proportional dimensions.
Dim sReturn As String * 128
Dim lPos As Long
Dim lStart As Long
Last$ = Form1.hWnd & " Style " & &H40000000
ToDo$ = "open c:\shut.avi Type avivideo Alias video1 parent " & Last$
i = mciSendString(ToDo$, 0&, 0, 0)
i = mciSendString("Where video1 destination", ByVal sReturn, Len(sReturn) - 1, 0)
lStart = InStr(1, sReturn, " ") 'pos of top
lPos = InStr(lStart + 1, sReturn, " ") 'pos of left
lStart = InStr(lPos + 1, sReturn, " ") 'pos width
lWidth = Mid(sReturn, lPos, lStart - lPos)
lHeight = Mid(sReturn, lStart + 1)
i = mciSendString("put video1 window at 16 10 " & lWidth & " " & lHeight, 0&, 0, 0)
i = mciSendString("play video1 wait", 0&, 0, 0)
i = mciSendString("close video1", 0&, 0, 0)
|