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.
Try our HTML5-based media player that simulates the functionality of the VB application:
Select a media file to play
Current Media: None selected
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.
Design the form with the Windows Media Player control, a Common Dialog control, and command buttons:
' 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
In VB.NET, the process is similar but uses the .NET framework. Add the Windows Media Player control to your toolbox first.
' 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
Supports WAV, MP3, MP4, AVI, MPEG and more
Toggle full screen playback with one click
Play media directly from web URLs
Add your own playback controls
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: