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

Visual Basic Multimedia Player

Build a versatile media player in VB6 and VB.NET with complete code examples


This tutorial will guide you through creating a multimedia player that can play various media formats such as WAV, MIDI, MP3, MPEG video, AVI video, and more. You'll learn both VB6 and VB.NET implementations.

The player allows users to browse files from different drives, select media files of various types, and play them using either custom buttons or the built-in multimedia control interface.

In the VB6 version, we'll use the Microsoft Multimedia Control (MMControl), while in VB.NET we'll leverage the Windows Media Player component for a more modern approach.

Interactive Player Simulation

Experience how the multimedia player works with this interactive simulation:

VB Multimedia Player

File Browser

music.mp3
video.mpg
sound.wav
background.mid

Status: No file selected



VB6 Multimedia Player

Interface Design

VB6 Multimedia Player Interface

VB6 Multimedia Player Interface

Required Components

  • Microsoft Multimedia Control (MMControl)
  • Combo box (for file type selection)
  • DirListBox
  • DriveListBox
  • FileListBox
  • Picture box (for video display)
  • Text box (for file path display)
  • Command buttons (Play, Open, Stop, Exit)

Form_Load Event

Private Sub Form_Load()
    ' Center the form on screen
    Left = (Screen.Width - Width) \ 2
    Top = (Screen.Height - Height) \ 2
    
    ' Initialize file type combo box
    Combo1.Text = "*.wav"
    Combo1.AddItem "*.wav"
    Combo1.AddItem "*.mid"
    Combo1.AddItem "*.avi"
    Combo1.AddItem "*.mpeg;*.mpg;*.mp3"
    Combo1.AddItem "All files"
End Sub

Combo Box Change Event

Private Sub Combo1_Change()
    ' Set file pattern based on selection
    If Combo1.ListIndex = 0 Then
        File1.Pattern = "*.wav"
    ElseIf Combo1.ListIndex = 1 Then
        File1.Pattern = "*.mid"
    ElseIf Combo1.ListIndex = 2 Then
        File1.Pattern = "*.avi"
    ElseIf Combo1.ListIndex = 3 Then
        File1.Pattern = "*.mpeg;*.mpg;*.mp3"
    Else
        File1.Pattern = "*.*"
    End If
End Sub

File Handling

Private Sub File1_Click()
    ' Update pattern based on combo box selection
    If Combo1.ListIndex = 0 Then
        File1.Pattern = "*.wav"
    ElseIf Combo1.ListIndex = 1 Then
        File1.Pattern = "*.mid"
    ElseIf Combo1.ListIndex = 2 Then
        File1.Pattern = "*.avi"
    ElseIf Combo1.ListIndex = 3 Then
        File1.Pattern = "*.mpeg;*.mpg;*.mp3"
    Else
        File1.Pattern = "*.*"
    End If
    
    ' Build full file path
    If Right(File1.Path, 1) <> "\" Then
        filenam = File1.Path & "\" & File1.FileName
    Else
        filenam = File1.Path & File1.FileName
    End If
    
    ' Display file path in text box
    Text1.Text = filenam
End Sub

Player Controls

Private Sub Open_Click()
    ' Set device type based on file type
    If Combo1.ListIndex = 0 Then
        MMControl1.DeviceType = "WaveAudio"
    ElseIf Combo1.ListIndex = 1 Then
        MMControl1.DeviceType = "Sequencer"
    ElseIf Combo1.ListIndex = 2 Then
        MMControl1.DeviceType = "AVIVideo"
    ElseIf Combo1.ListIndex = 3 Then
        MMControl1.DeviceType = ""
    End If
    
    ' Set file and open the device
    MMControl1.FileName = Text1.Text
    MMControl1.Command = "Open"
End Sub

Private Sub play_Click()
    ' Start playing the media
    Timer1.Enabled = True
    MMControl1.Command = "Play"
    MMControl1.hWndDisplay = Picture1.hWnd
End Sub

Private Sub Stop_Click()
    ' Stop and close the media
    If MMControl1.Mode = 524 Then Exit Sub
    If MMControl1.Mode <> 525 Then
        MMControl1.Wait = True
        MMControl1.Command = "Stop"
    End If
    MMControl1.Wait = True
    MMControl1.Command = "Close"
End Sub

VB.NET Multimedia Player

In VB.NET, we can create a more modern multimedia player using the Windows Media Player component which provides better compatibility with modern media formats.

VB6 Approach
' VB6 uses MMControl component
MMControl1.DeviceType = "AVIVideo"
MMControl1.FileName = "video.avi"
MMControl1.Command = "Open"
VB.NET Approach
' VB.NET uses WMPLib
AxWindowsMediaPlayer1.URL = "video.avi"
AxWindowsMediaPlayer1.Ctlcontrols.play()

VB.NET Implementation Steps

  1. Add the Windows Media Player component to your toolbox (COM component: "Windows Media Player")
  2. Drag the AxWindowsMediaPlayer control to your form
  3. Add OpenFileDialog, buttons, and other UI elements
  4. Implement the file browsing and playback controls

Complete VB.NET Code

Public Class MediaPlayerForm
    ' Play button click event
    Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        AxWindowsMediaPlayer1.Ctlcontrols.play()
    End Sub

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

    ' Pause button click event
    Private Sub btnPause_Click(sender As Object, e As EventArgs) Handles btnPause.Click
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
    End Sub

    ' Open button click event
    Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
        Using openFileDialog As New OpenFileDialog()
            openFileDialog.Filter = "Media Files|*.wav;*.mp3;*.wma;*.avi;*.mpg;*.mpeg;*.wmv;*.mid|All Files|*.*"
            openFileDialog.FilterIndex = 1
            
            If openFileDialog.ShowDialog() = DialogResult.OK Then
                AxWindowsMediaPlayer1.URL = openFileDialog.FileName
                lblStatus.Text = "Playing: " & openFileDialog.FileName
            End If
        End Using
    End Sub

    ' Form closing event
    Private Sub MediaPlayerForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        AxWindowsMediaPlayer1.Ctlcontrols.stop()
        AxWindowsMediaPlayer1.close()
    End Sub
End Class

Key Advantages of VB.NET Approach

Key Differences: VB6 vs VB.NET

Feature VB6 VB.NET
Component Microsoft Multimedia Control (MMControl) Windows Media Player COM component
Media Format Support Limited (WAV, MIDI, AVI, MPEG) Extensive (MP3, WMA, WMV, MP4, etc.)
Code Complexity More complex, requires device type specification Simplified, URL-based playback
Video Display Requires PictureBox handle assignment Built-in display in media player component
Modern Features Limited Playlists, streaming, fullscreen, etc.

Practice Exercises

Exercise 1: Format Compatibility Enhancer

Extend the VB6 player to support additional formats like MP4 and FLAC:

  • Research how to register additional codecs for MMControl
  • Modify the file pattern to include new formats
  • Add error handling for unsupported formats

Exercise 2: Playlist Manager

Enhance the VB.NET player with playlist capabilities:

  • Add a ListBox to display the playlist
  • Implement "Add to Playlist" and "Remove from Playlist" buttons
  • Create "Play Next" and "Play Previous" functionality
  • Add shuffle and repeat modes