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.
Experience how the multimedia player works with this interactive simulation:
Status: No file selected
VB6 Multimedia Player Interface
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
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
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
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
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 uses MMControl component MMControl1.DeviceType = "AVIVideo" MMControl1.FileName = "video.avi" MMControl1.Command = "Open"
' VB.NET uses WMPLib AxWindowsMediaPlayer1.URL = "video.avi" AxWindowsMediaPlayer1.Ctlcontrols.play()
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
| 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. |
Extend the VB6 player to support additional formats like MP4 and FLAC:
Enhance the VB.NET player with playlist capabilities: