Visual Basic audio file player - Part 1
How to play audio files in Visual Basic
One nice thing about Visual Basic Express Edition is that it can use features that are built into Windows such as Windows Media Player. This lets you do things like play audio files without having to create a media player to do so. All you need to do is to add the player to your form and feed it the information it needs to play a file of your choice.
Here I'll show you how to not only play audio files using Windows Media Player but also some ways of using the My namespace in Visual Basic. The My namespace gives you access to Windows features such as its file system and your computer allowing you to perform quite complex processes in just a line or two of code. This makes developing applications easier than you might expect.
Create a basic audio player
To follow along with this project, launch Visual Basic Express Edition 2005, choose File > New Project and create a new Windows Application. Size your form to a largish size and add one label, two buttons and a listbox. Set the text property for each of these objects as follows:
Form1 Audio Player
Label1 Select a file to play
Button1 Load and play the file
The Windows Media Player control isn’t in the list of tools so you need to add it to the toolbox before you can use it in your project. To do this, in the toolbox right click the Components collection and choose Choose Items. When the Choose Toolbox Items dialog opens click the COM Components tab, enable the checkbox for Windows Media Player and click Ok. The Windows Media Player control now appears in your Components collection so you can click to select it and drag the player object onto your form.
Use the COM Components dialog to add the Windows Media Player control to your toolbox to use in your project.
To add the code to your project, double click the form and add this code inside the Form1_load event:
My.Computer.FileSystem.CurrentDirectory = "C:\windows\media" ListBox1.DataSource = My.Computer.FileSystem.GetFiles(My.Computer. FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.wav")
The code used to populate the list box with the files is placed in the form_load event and the player is managed with a button.
This code appears inside the Button1_Click event:
If ListBox1.SelectedItem <> "" Then Me.AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem.ToString Else MsgBox(;Please select a file", MsgBoxStyle.OkOnly, "Select a file") End If
Save your project and test it. When you click the Start Debugging button you'll see the Listbox is automatically populated with the WAV files from your C:\windows\media folder. Select any one of these files and click the Load and Play File button and the file will be loaded into the Windows Media Player and played automatically. You can continue and select and play any of the files in the list. When you are done click Close to finish.
In the code, the My.Computer.FileSystem.CurrentDirectory property is pointed to the folder that contains some of the WAV files used by the Windows operating system. The My.Computer.FileSystem.GetFiles method lets you obtain a list of files from a folder of your choice. The method lets you specify if only that folder is to be searched (as is done here) or if all subfolders are searched and you can specify the files to search for – here we're searching for just WAV files. These files are then used as a datasource to populate the Listbox dynamically when the form loads. When the user presses the button, the selected entry in the Listbox is passed to the Windows Media Player which plays the file.
Fine tuning the program
One change which would improve this application is for file names alone (and not the file and folder name) to appear in the Listbox. This requires a minor rewrite to the Form1_Load event and the Button1_click event.
This is the new Form1_load event code:
My.Computer.FileSystem.CurrentDirectory = "C:\windows\media" For Each File As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.wav") ListBox1.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name) Next
And the new Button1_click event code:
If ListBox1.SelectedItem <> "" Then Me.AxWindowsMediaPlayer1.URL = My.Computer.FileSystem.CurrentDirectory + "\" + ListBox1.SelectedItem.ToString Else MsgBox("Please select a file", MsgBoxStyle.OkOnly, "Select a file") End If
The Form1_load event code locates all the WAV files in the c:\windows\media folder but unlike before, it processes each of them to extract only the filename – discarding the path. These filenames are then used by the Listbox1.items.add method to populate the listbox with the filenames. When you run the program you will see filenames alone in the Listbox.
When the user presses the button problems will arise with this new code - if you just send the filename - the application doesn’t know where to find the file. The fix is to assemble a full filename and path before sending it to the Windows Media Player control in the Button1_click event.
This VB project is a player that will list and play sound files from your Windows\media folder.
(c) 2013, Helen Bradley, All Rights Reserved.