Lesson 22: Creating a Multimedia Player in VB6

Build a versatile media player that handles both audio and video formats

Key Takeaway

By leveraging VB6's Multimedia Control (MMControl) and file system components, you can create a powerful media player that handles both audio and video formats with an intuitive interface.

Welcome to Lesson 22 of our Visual Basic 6 Tutorial! Building on your audio player from Lesson 20, you'll now learn how to create a full-featured multimedia player that handles both audio and video formats. This player will allow users to browse their storage devices, select media files, and play them with full playback controls.

22.1 Essential Components

To create a multimedia player, we'll use several key components:

File Browsing Controls

DriveListBox, DirListBox, and FileListBox for navigating files

Media Format Support

Supports WAV, MID, AVI, MPG, and other media formats

Playback Controls

MMControl for playing, pausing, and stopping media

22.1.1 Required Controls

Our multimedia player interface includes these essential controls:

1ComboBox

For selecting media file types (WAV, MID, AVI, etc.)

2DriveListBox

To select available drives on the system

3DirListBox

To navigate through directories and subdirectories

4FileListBox

To display available media files

5TextBox

To display the selected file path

6MMControl

The core component for media playback

7PictureBox

For displaying video content

Multimedia Player Interface
Figure 22.1: The Multimedia Player Interface

22.2 Building the Multimedia Player

Our media player follows a logical workflow to provide a seamless user experience:

1Select Media Type

User chooses the media formats they want to play

2Choose Drive

User selects the storage device containing media files

3Browse Directories

User navigates to the folder containing media files

4Select File

User chooses a media file from the list

5Play Media

User clicks "Play" to start playback

6Control Playback

User can stop playback or exit the application

Interactive Media Player

Ready to play

22.2.1 Implementing the Code

The core functionality is implemented through event handlers for each control. Here's the complete code for our multimedia player:

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

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;*.mpg")
    Else
        File1.Pattern = ("*.*")
    End If
End Sub

Private Sub Dir1_Change()
    ' Update file list when directory changes
    File1.Path = Dir1.Path
    ' Maintain current pattern setting
    Call Combo1_Change
End Sub

Private Sub Drive1_Change()
    ' Update directory when drive changes
    On Error Resume Next ' Handle possible drive errors
    Dir1.Path = Drive1.Drive
    If Err.Number <> 0 Then
        MsgBox "Drive error: " & Err.Description, vbExclamation
        Drive1.Drive = Dir1.Path ' Revert to previous drive
    End If
End Sub

Private Sub File1_Click()
    ' Handle file selection
    Dim filenam As String
    
    If Right(File1.Path, 1) <> "\" Then
        filenam = File1.Path & "\" & File1.FileName
    Else
        filenam = File1.Path & File1.FileName
    End If
    Text1.Text = filenam
End Sub

Private Sub play_Click()
    ' Play the selected media file
    If Text1.Text = "" Then
        MsgBox "Please select a media file first!", vbExclamation
        Exit Sub
    End If
    
    ' Close any previous media
    MMPlayer.Command = "Close"
    
    ' Set the file to play
    MMPlayer.FileName = Text1.Text
    
    ' Open the media file
    MMPlayer.Command = "Open"
    
    ' Set video display to PictureBox
    MMPlayer.hWndDisplay = videoscreen.hWnd
    
    ' Start playback
    MMPlayer.Command = "Play"
End Sub

Private Sub stop_Click()
    ' Stop playback and close media
    If MMPlayer.Mode = 524 Then Exit Sub ' Already stopped
    If MMPlayer.Mode <> 525 Then
        MMPlayer.Wait = True
        MMPlayer.Command = "Stop"
    End If
    MMPlayer.Wait = True
    MMPlayer.Command = "Close"
End Sub

22.2.2 Key Functionality Explained

The multimedia player relies on several important event handlers and techniques:

File System Navigation

The DriveListBox, DirListBox, and FileListBox work together to allow users to browse for media files.

Drive1_Change → Dir1_Change → File1_Click

Media Playback

The MMControl handles playback of both audio and video formats.

MMPlayer.Command = "Open" → "Play" → "Stop" → "Close"

File Pattern Handling

The ComboBox allows users to filter by specific media formats or show all files.

File1.Pattern = "*.wav;*.mid;*.avi;*.mpg"

Video Display

The PictureBox control serves as the display surface for video content.

MMPlayer.hWndDisplay = videoscreen.hWnd

Lesson Summary

In this lesson, you've learned how to create a versatile multimedia player in VB6:

File System Controls

Using DriveListBox, DirListBox, and FileListBox for navigation

Media Playback

Implementing playback controls with MMControl

Format Support

Handling multiple media formats (WAV, MID, AVI, MPG)

Video Display

Using a PictureBox as a video display surface

Important Note

The Multimedia Control (MMControl) must be added to your project through the Components dialog (Project → Components → Microsoft Multimedia Control). This component provides the core playback functionality.

Practice Exercises

Test your multimedia programming skills with these exercises:

Exercise 1: Playback Controls

Add pause, rewind, and fast forward buttons to the player.

Exercise 2: Volume Control

Implement a slider control to adjust playback volume.

Exercise 3: Playlist Support

Create a playlist feature that queues multiple media files.

Exercise 4: Status Display

Show current playback position and media duration.

Exercise 5: Fullscreen Mode

Add a button to toggle video playback to fullscreen.

Next Lesson

Continue your VB6 journey with Lesson 23: Creating a Database Application.

Related Resources

Full VB6 Tutorial Index

Complete list of all VB6 lessons with descriptions

Explore Tutorials

Visual Basic Examples

Practical VB6 code samples for real-world applications

View Examples

Picture Viewer Application

Learn to create an image viewer in VB6

Previous Lesson

Database Applications

Learn to create database-driven applications

Next Lesson