• Home
  • Blog/Personal
  • DesktopShots
  • FreeDOS
  • GEM
  • OSPlus
  • Transport Tycoon
  • Utility Base
  • Visual Basic
logo

Visual Basic

  • Home Page
  • Common Controls
  • 3D Controls
  • BIOS Object
  • Unzip Control

Creating a Video Player with Visual Basic

Information in this topic can be used with Visual Basic 2 or higher

DownloadDownload the Visual Basic 4.0 source code (10.9KB)

To make a Video for Windows AVI file (or any other type of video, for example, an MPEG movie) play in a picture box, or on a form, using Visual Basic 3.0 or higher, follow these steps:

1. Place a picture box, called Picture1, on a blank form. Also add three command buttons, like this:

buttons

2. If the Multimedia MCI control is not in the toolbox, add it.

3. Add a Common Dialog (CommonDialog1) control to the form.

4. Add an MCI control to the form. Click Custom... in the Properties window for the MCI control and set the values as follows:

MCI control

5. Paste the following code into the Play button (Command1)'s Click event (Command1_Click):

MMControl1.FileName = CommonDialog1.FileName
MMControl1.Command = "Open"

'Put window handle in CmdStr$
CmdStr$ = "window " + MMControl1.filename + " handle " + Format$(Picture1.hWnd)

'Send window handle to mciSendString
ReturnVal& = mciSendString(CmdStr$, 0&, 0, 0&)

'Put output in window
CmdStr$ = "put " + MMControl1.filename + "destination "
ReturnVal& = mciSendString(CmdStr$, 0&, 0, 0&)

MMControl1.Command = "Prev"
MMControl1.Command = "Play"
Command3.Caption = "Pause"

This plays the video in the picture box (change every occurance of Picture1 to Form1 for a form) and sets Command3's caption to Pause.

6. Now you need to paste the following code into the Command3_Click() event:

With Command3
[]If .Caption = "Pause" Then
[][].Caption = "Continue"
[]Else
[][].Caption = "Pause"
[]End If
End With
MMControl1.Command = "Pause"

This pauses the video if it is playing, or unpauses the video if it is paused.

7. Now place the following code into the Command2_Click() event:

MMControl1.Command = "Stop"
Command3.Caption = "Pause"

8. Place this code in the Command4_Click() event.

CommonDialog1.Flags = 1804
CommonDialog1.ShowOpen
If CommonDialog1.filename = "" Then Exit Sub
MMControl1.Command = "Close"
MMControl1.filename = CommonDialog1.FileName
Command1.Enabled = "True"

This displays the Open common dialog without a Read Only checkbox and sets the MCI control's FileName property to the file chosen in the dialog.

9. Now you need to declare the mciSendString DLL function. Place the following code into the Declarations section of the form, or a module:

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

That's all there is to it! Here is a picture of an AVI Viewer I created:

avi viewer

When I was creating this topic, I didn't realise that the MCI control had a hWndDisplay property, so much of this can be done by setting the hWndDisplay property! Also, I'm thinking of renaming 'Owen's AVI Player' to 'Owen's Media Player' because I just added multiple MCI device support to it, but it wasn't in a very neat way! Here is a screenshot of the program playing an MP3 file (device set to MPEGVideo). It may not look much, but it is playing the file:

playing mp3

Copyright © Owen Rudge 1998-2024. All Rights Reserved.
Legal Information | Privacy Policy