January 8th, 2004 10:12 AM
Constructing a Basic MP3 Player
Using the Multimedia Control in Visual Basic - Constructing a Basic MP3 Player
By the end of this tutorial you will know how to construct a basic MP3 player under Microsoft Visual Basic 6.0
This tutorial concentrates on the Microsoft Multimedia Control 6.0 and its BASIC application as an MP3 player. On a minor level, it also covers looping.
The Microsoft Multimedia Control 6.0, a control in Visual Basic 6.0, is a simple yet powerful tool that can be used to construct an MP3/CD/WAV/MIDI player. It allows the user control over most aspects of the file (playback, pause, stop, etc) without the need for introducing a plethora of other command buttons and controls. At the end of this article, you will find the source code attached in a .zip
Step 1 :: Constructing a form that can easily explore the local filesystem for MP3s.
This is possibly the simplest, yet most overlooked step. Having a MMControl (multimedia control) is all fine and dandy, but what’s the use of it if you cannot select the files which it loads dynamically?
i. Create a form
ii. Add the following controls
A Drive listbox (this will be called lstDrive)
A Directory Listbox (this will be called lstDir)
A File Listbox (this will be called lstFiles)
These are all accessible from the left hand control panel in VB 6.0. Now for the code.
Q. What do we need to accomplish?
A. We need the directory listbox to change when the drive listbox is changed, and the file listbox (which displays the files in a directory) to change when the directory listbox changes. Im already sick of the word listbox .
We can add another bit here – we can restrict *what* files the lstFiles object displays. We accomplish this with a combobox. This will be called lstPattern.
‘this caters for the Drive listbox changing.
Private Sub lstDrive_Change()
lstDir.Path = lstDrive.Drive
‘this caters for the directory listbox changing
Private Sub lstDir_Change()
lstFiles.Path = lstDir.Path
‘this bit isn’t necessary, but it’s a cool thing to have anyways.
lstFiles.ToolTipText = lstFiles.ListCount & " Files in directory"
In the properties of lstPattern, set “Text” to *.mp3 – this will be our default value. Be sure to make *.mp3 the default value for lstFiles.Pattern also. Under “List”, add as many options as you want. The add the following code;
Simple. Done. Out of the way
Private Sub lstPattern_Change()
lstFiles.Pattern = lstPattern.Text
Step 2 :: The MMControl
Add a Microsoft Multimedia Control 6.0 to the form. This can be found by going Project > Components > Check the desired control > Click OK. It should now be accessible via the left hand control panel.
Lets have a look at what we have so far; A form, which has a simple device to browse the local filesystem. A multimedia control, which thus far does nothing. Pretty useless at the moment.
Q. So what exactly must I do to make this program semi-functional?
A. Create a link between the two objects we now have. Ergo, find a way to load objects into the MMControl with the “Browser”.
“Load?”, some of you may ask. My fault – lets have a look at how the MMControl works.
Theory 1 :: Basic commands of the MMControl
Before a file can be played by the MMControl, it must first be loaded. Both of these functions and more are executed with the MMControl.Command call.
Example I; Loading a file
Example II; Closing a file
MMControl.FileName = “C:\ILoveRIAA\Random.mp3”
‘This sets the “target” of the .Command call to whatever is specified inside the quotes
MMControl.Command = “open”
‘This loads the file that we targeted. If you look at the form, you will notice that the “Play” button on the MMControl is now active. The file can be played at any time.
So now we can open, close, and play files. Lets have a look at putting this into practice on our current form.
‘This assumes that a file is “loaded”
MMControl.Command = “close”
‘NB: You must close a file before you can open a new one
Done. No seriously, that’s it. You can go home now.
Private Sub lstFiles_DblClick()
'this triggers when we double click the file listbox
For x = 0 To lstFiles.ListCount - 1
If lstFiles.Selected(x) = True Then
'this checks for a selected file
MMControl.Command = "close"
MMControl.FileName = lstDir.Path & "\" & lstFiles.List(x)
MMControl.Command = "open"
'if it finds one, it closes the currently open file then
'loads the selected one instead. the 'play' button should
'now be active.
'Done. Press play to play.
As I said, this was a very *basic* MP3 player. It can be expanded upon in numerous ways, which are simply to tedious to document here. Here are a few examples;
Show Elapsed/Remaining times
Add CD support
Add volume controls
Save your ‘sound’ directory as a home directory, so it always loads it straight into lstFiles
If you would like any help doing such things, just say and I will be more than happy to help. This is my first tutorial, excuse the tardiness/simplicity of it.