If you haven't heard of MP3, I would be surprised. It is now the format of choice for music and sounds, as it has relitively good quality, and small size. This tutorial will show you how to add a small MP3 player directly into your form, for background music or sound effects. The 'source' that is posted here is NOT what is described in this tutorial. It is an ActiveX control, that was made the same way. It includes a VB5/6 project and example program.
This tutorial will be using the Microsoft Multimedia Control (NOT Windows Media Player), and programmers who have used MCI before should recognize the process.
Side-Note: MP3 is the acronym for MPEG Layer 3, as it is closely related to MPEG 1/2 video.
Setting up the Form
- Add a new form to your project, or select the one that you would like to add the audio to.
- Right click on the toolbox, and click 'Components...'
- Add 'Microsoft Multimedia Control' (v6.0 in VB6) to the toolbox
- Put a new Multimedia control on the form
- Rename the control from 'MMControl1' to 'mmcMP3'
- Right click on the control on your form. In the properties view, disable and set visible to false for all buttons except for 'Play' and 'Pause'.
(NOTE: You can leave 'Stop' enabled, but it doesn't rewind, it just pauses)
It is time for some code!! Go to the add this code to your form. You can substitute the Form_Load event for any you deem suitable to start the audio.
Private Sub Form_Load
mmcMP3.DeviceType = "MPEGVideo" '\\Change MCI device type to MPEG
mmcMP3.Filename = "C:\Myaudio.Mp3" '\\designate file to be played
mmcMP3.Command = "Open" '\\Open file for playing
mmcMP3.Command = "Play" '\\Play file
End Sub
Private Sub Form_Unload
mmcMP3.Command = "Stop" '\\Stop playing the file
mmcMP3.Command = "Close" '\\Close the file
End Sub
Comments