Add Quick MP3 Audio Functionality to your Programs

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

  1. Add a new form to your project, or select the one that you would like to add the audio to.
  2. Right click on the toolbox, and click 'Components...'
  3. Add 'Microsoft Multimedia Control' (v6.0 in VB6) to the toolbox
  4. Put a new Multimedia control on the form
  5. Rename the control from 'MMControl1' to 'mmcMP3'
  6. 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

You might also like...

Comments

Cooper Sloan - VB6FREAK Was born. Now is living.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann