VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Windows Media Player in Visual Basic

Create a fully functional media player using VB6 and VB.NET with Windows Media Player control


This tutorial demonstrates how to create a Windows Media Player application using Visual Basic 6 and VB.NET. The Windows Media Player control allows you to play various media formats including WAV, MIDI, MP3, MPEG video, AVI video, and more.

When you launch the program, you can select media files from different sources such as your hard drives, CD, or even a URL. After selecting a file, you can play it using custom buttons or the built-in Windows Media Player controls.

Interactive Media Player Demo

Try our HTML5-based media player that simulates the functionality of the VB application:

Select a media file to play

Current Media: None selected

VB6 Implementation
VB.NET Implementation

Creating the Media Player in Visual Basic 6

In VB6, you need to insert the Windows Media Player control which is not available by default. Press CTRL+T to bring up the Components dialog and check "Windows Media Player" as shown below.

Adding Windows Media Player Control in VB6
Figure 1: Adding Windows Media Player Control
Adding Common Dialog Control
Figure 2: Adding Common Dialog Control

Design the form with the Windows Media Player control, a Common Dialog control, and command buttons:

VB6 Media Player Interface
Figure 3: VB6 Media Player Interface Design

VB6 Source Code

' Open button code
Private Sub cmdOpen_Click()
    Me.CommonDialog1.Filter = "WAV Files (*.wav)|*.wav|" & _
                          "MP3 Files (*.mp3)|*.mp3|" & _
                          "MP4 Files (*.mp4)|*.mp4|" & _
                          "Video Files (*.avi)|*.avi|" & _
                          "MPEG Files (*.mpg)|*.mpg|"
    CommonDialog1.ShowOpen
    Text1.Text = CommonDialog1.FileName
End Sub

' Play button code
Private Sub cmdPlay_Click()
    WindowsMediaPlayer1.URL = Text1.Text
End Sub

' Stop button code
Private Sub cmdStop_Click()
    WindowsMediaPlayer1.Close
End Sub

' Full Screen button code
Private Sub cmdMax_Click()
    WindowsMediaPlayer1.fullScreen = True
End Sub
VB6 Media Player File Selection
Figure 4: File Selection Dialog
VB6 Media Player in Action
Figure 5: Playing a Video in VB6

Creating the Media Player in VB.NET

In VB.NET, the process is similar but uses the .NET framework. Add the Windows Media Player control to your toolbox first.

Steps to Add Windows Media Player Control in VB.NET:

  1. Right-click on the Toolbox and select "Choose Items"
  2. In the COM Components tab, check "Windows Media Player"
  3. Click OK to add the control to your toolbox

VB.NET Source Code

' Import required namespace
Imports WMPLib

' Open button code
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Dim openDialog As New OpenFileDialog()
    
    openDialog.Filter = "Media Files|*.wav;*.mp3;*.mp4;*.avi;*.mpg|All Files|*.*"
    
    If openDialog.ShowDialog() = DialogResult.OK Then
        txtFilePath.Text = openDialog.FileName
    End If
End Sub

' Play button code
Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
    If Not String.IsNullOrEmpty(txtFilePath.Text) Then
        AxWindowsMediaPlayer1.URL = txtFilePath.Text
    Else
        MessageBox.Show("Please select a media file first.")
    End If
End Sub

' Stop button code
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub

' Full Screen button code
Private Sub btnFullScreen_Click(sender As Object, e As EventArgs) Handles btnFullScreen.Click
    AxWindowsMediaPlayer1.fullScreen = True
End Sub

Key Differences in VB.NET:

  • Uses the .NET Framework OpenFileDialog instead of CommonDialog
  • Requires importing the WMPLib namespace
  • Control methods are accessed through the Ctlcontrols property
  • Uses .NET event handling syntax

Media Player Features

Multiple Formats

Supports WAV, MP3, MP4, AVI, MPEG and more

Full Screen Mode

Toggle full screen playback with one click

Stream from URL

Play media directly from web URLs

Custom Controls

Add your own playback controls

Visual Basic Sample Code Book

VISUAL BASIC PROGRAMMING WITH CODE EXAMPLES

A Practical Guide authored by Dr. Liew Voon Kiong for Beginners and Intermediate Developers

This comprehensive guide covers both VB6 and VB.NET implementations of the Windows Media Player and many other practical applications. Learn how to create powerful multimedia applications with step-by-step instructions.

Key topics covered:

  • Windows Media Player integration in VB6 and VB.NET
  • Creating custom media playback controls
  • Working with different media formats
  • Adding playlists and media libraries
  • Advanced features like streaming and capture
Buy Now on Amazon