Videos and VB.NET 2012 Part 1 - Windows Media Player

Monday Jul 15th 2013 by Hannes Du Preez

Hannes du Preez shows you how to embed Windows Media Player into a program and play any form of media through it.


One of the first questions I get asked by my students is how to play movies or music through Visual Basic. I know of four different ways, depending on which Operating System you are on. Today I will cover the first option: Windows Media Player. Today we will learn how to embed Windows Media Player into our program and play any form of media through it.


Start a new Visual Basic Windows Application and add the following controls to it:

  • OpenFileDialog
  • Menu with the following Subitems
    • File
      • Browse
      • Resize
      • Exit
    • Player
      • Volume Up
      • Volume Down
      • Mute
      • Play
      • Stop
      • Mode
        • Invisible
        • None
        • Mini
        • Full
  • Windows Media Player Control. Follow these steps:
    • Right click your Toolbox
    • Select Choose Items…
    • Select the COM Tab
    • Scroll Down to Windows Media Player
    • If you don't find it on the list, Browse to the Wmp.dll file inside C:\Windows\System32

You can name all your objects as you wish; keep in mind though that mine would be different, so please look carefully before copying and pasting my code directly. Very important: Set your Form's KeyPreview property to True. This will allow the form to intercept all keyboard input, as you will see.

Your completed design should look more or less like Figure 1.

Our Design
Figure 1 - Our Design


Add the KeyDown event to your Form, and enter the following:

 Private Sub frmMedia_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

'Determine Key Pressed - Form KeyPreview Is Set To True
Select e.KeyCode

Case Keys.Add ' Plus Key

wmpPlayer.settings.volume = wmpPlayer.settings.volume + 2 ' Increase Volume

Case Keys.Subtract 'Minus Key

If wmpPlayer.settings.volume > 0 Then 'If Greater Than 0

wmpPlayer.settings.volume = wmpPlayer.settings.volume - 2 'Decrease Volume
wmpPlayer.settings.mute = False 'Enable Mute Button

Else '0 Or Less

wmpPlayer.settings.mute = True 'Muted Is True, Disable Mute Button

End If

Case Keys.Divide '/

wmpPlayer.settings.mute = Not wmpPlayer.settings.mute 'Mute

End Select

End Sub

As mentioned earlier, because the KeyPreview Property is set to true for the Form, the Form intercepts all keyboard input. This is the most apt place to handle any of our 'hotkeys' for our program.

When Plus ( + ) is pressed, it increases the volume in increments of 2.

When Minus ( - ) is pressed, it makes sure that the volume is greater than 0, before enabling the Mute button.

When Divide ( / ) is pressed, it mutes the volume.

Now let us proceed with the File Menu's submenus.

Add the following three events:

 Private Sub BrowseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BrowseToolStripMenuItem.Click

'Set Filter For OFD
ofdOpen.Filter = "AVI Files|*.avi|MPG Files|*.mpg|MP4 Files|*.mp4|WMV Files|*.wmv|All Files|*.*"

If ofdOpen.ShowDialog = Windows.Forms.DialogResult.OK Then 'If Valid File Selected

wmpPlayer.URL = ofdOpen.FileName 'Play
PlayPauseToolStripMenuItem.Text = "Pause" 'Change 'Play' To 'Pause' On Menu

End If

End Sub

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click

Application.Exit() 'Exit

End Sub

Private Sub ResizeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResizeToolStripMenuItem.Click

wmpPlayer.Size() = Me.ClientRectangle.Size 'Make Same Size As Form

End Sub

The Browse menu item allows us to select AVI, MPG, MP4 as well as WMV options. Obviously we can add MP3 here as well, specifically for audio. Just a note: MP 4 files may not work if Windows Media Player doesn't have the correct codec to play them.

Exit, exits our application.

Resize, resizes the Media Player according to the form's size in the event of the form being resized.

Now, let us add the Player menu's sub items:

 Private Sub VolumeUpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VolumeUpToolStripMenuItem.Click

wmpPlayer.settings.volume = wmpPlayer.settings.volume + 2 'Increase Volume

End Sub

Private Sub VolumeDownToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VolumeDownToolStripMenuItem.Click

'Same As When - Pressed
If wmpPlayer.settings.volume > 0 Then

wmpPlayer.settings.volume = wmpPlayer.settings.volume - 2
wmpPlayer.settings.mute = False


wmpPlayer.settings.mute = True

End If

End Sub

Private Sub MuteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MuteToolStripMenuItem.Click

wmpPlayer.settings.mute = Not wmpPlayer.settings.mute 'Swith Between Mute & UnMuted

End Sub

Private Sub PlayPauseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PlayPauseToolStripMenuItem.Click

If PlayPauseToolStripMenuItem.Text = "Play" Then 'Switch Between Playing & Pausing

PlayPauseToolStripMenuItem.Text = "Pause"



PlayPauseToolStripMenuItem.Text = "Play"

End If

End Sub

Private Sub StopToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StopToolStripMenuItem.Click

wmpPlayer.Ctlcontrols.stop() 'Stop

End Sub

Private Sub InvisibleToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InvisibleToolStripMenuItem.Click

''Windows Media Player is embedded without any visible user interface
wmpPlayer.uiMode = "invisible"

End Sub

Private Sub NoneToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NoneToolStripMenuItem.Click

'Windows Media Player is embedded without controls,
'and with only the video or visualization window displayed
wmpPlayer.uiMode = "none"

End Sub

Private Sub MiniToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MiniToolStripMenuItem.Click

'Windows Media Player is embedded with the status window,
'play/pause, stop, mute, and volume controls shown in
'addition to the video or visualization window
wmpPlayer.uiMode = "mini"

End Sub

The first few items concerning the Volume, are identical to our KeyDown event. Yes, I could have made one central sub procedure for all, but this is afterall a basic introduction.

The Play menu item will switch between 'Play' and 'Pause' depending on the state of the player. If it is playing an item, it shows 'Pause'. If it was paused, it should show 'Play'.

Stop stops all media.

The Mode setting determines how the Windows Media Player object looks. This means that depending on what setting has been chosen, the buttons, volume controls, etc. might be displayed or not. You can feel free to experiment with that setting. For more information regarding the uimode property, have a look here.


There you have it, not too complicated hey? Nope. I am including the source files with this article. Check out Part 2 - Playing Youtube and Flash videos from Visual Basic.NET. That is very exciting, but don't let me get ahead of myself here. Until then, Cheers!

About the Author:

Hannes du Preez is a Microsoft MVP for Visual Basic for the fifth year in a row. He is a trainer at a South African-based company providing IT training in the Vaal Triangle. You could reach him at hannes [at] ncc-cla [dot] com

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved