Create an audio player with VB6 and VB.NET
You can create an audio player that enables you to play MIDI and WAVE files from your hard drives or DVD-ROM. It allows you to change drives and search for the particular sound file that you wish to play.
In this program, you need to add a Combo box, a List Box, a Text Box, a Drive List Box and three command buttons. Change the label of one button to "PLAY", another to "STOP", and the last to "EXIT". Besides, you need to add the MMControl and make it invisible.
This page includes both VB6 and VB.NET implementations along with an interactive demo.
Private Sub Form_Load()
Top = (Screen.Height - Height) \ 2
Left = (Screen.Width - Width) \ 2
Combo1.Text = "*.wav"
Combo1.AddItem "*.wav"
Combo1.AddItem "*.mid"
Combo1.AddItem "All files"
End Sub
Private Sub Combo1_Change()
If Combo1.ListIndex = 0 Then
File1.Pattern = "*.wav"
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = "*.mid"
Else
File1.Pattern = "*.*"
End If
End Sub
Private Sub File1_Click()
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()
If Combo1.ListIndex = 0 Then
MMControl1.DeviceType = "WaveAudio"
ElseIf Combo1.ListIndex = 1 Then
MMControl1.DeviceType = "Sequencer"
End If
MMControl1.FileName = Text1.Text
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End Sub
Private Sub Stop_Click()
If MMControl1.Mode = 524 Then Exit Sub
If MMControl1.Mode <> 525 Then
MMControl1.Wait = True
MMControl1.Command = "Close"
End If
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
Call Combo1_Change
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
This VB6 implementation uses the Multimedia Control (MMControl) to play audio files. The interface includes drive, directory, and file selection components.
Public Class AudioPlayerForm
Private Sub AudioPlayerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ComboBox1.Items.AddRange({"*.wav", "*.mid", "All files"})
ComboBox1.SelectedIndex = 0
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedIndex
Case 0 : FileListBox1.Pattern = "*.wav"
Case 1 : FileListBox1.Pattern = "*.mid"
Case 2 : FileListBox1.Pattern = "*.*"
End Select
End Sub
Private Sub FileListBox1_Click(sender As Object, e As EventArgs) Handles FileListBox1.Click
TextBox1.Text = If(DirectoryListBox1.Path.EndsWith("\"),
DirectoryListBox1.Path & FileListBox1.FileName,
DirectoryListBox1.Path & "\" & FileListBox1.FileName)
End Sub
Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click
Try
If ComboBox1.SelectedIndex = 0 Then
AxWindowsMediaPlayer1.settings.autoStart = True
AxWindowsMediaPlayer1.URL = TextBox1.Text
ElseIf ComboBox1.SelectedIndex = 1 Then
' For MIDI files in VB.NET, use Windows Media Player or other library
AxWindowsMediaPlayer1.URL = TextBox1.Text
AxWindowsMediaPlayer1.Ctlcontrols.play()
End If
Catch ex As Exception
MessageBox.Show("Error playing file: " & ex.Message)
End Try
End Sub
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub
Private Sub DirectoryListBox1_Change(sender As Object, e As EventArgs) Handles DirectoryListBox1.Change
FileListBox1.Path = DirectoryListBox1.Path
Call ComboBox1_SelectedIndexChanged(Nothing, Nothing)
End Sub
Private Sub DriveListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DriveListBox1.SelectedIndexChanged
DirectoryListBox1.Path = DriveListBox1.Drive
End Sub
End Class
This VB.NET implementation uses the Windows Media Player control for audio playback. It provides similar functionality to the VB6 version with a more modern approach.
Browse drives and directories to find audio files
Filter by file type (WAV, MIDI, or all files)
Play, stop, and select tracks
VB6 and VB.NET code samples