Library code snippets

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

Comments

  1. 22 Aug 2007 at 04:08

    thank you, i have been looking everywhere for this, and it works!!!Smiley Face [:)]Big Smile [:D]

    do you also know how to have multiple music files, and the program runs throught them all one at a time, if you do i would be very gratefull

    thanks for all your help

  2. 29 Jul 2006 at 21:51
    does not work for me and i have vb6 pro i would really like this to? :( :(
  3. 10 Nov 2005 at 19:16

    Well, this would be very helpful if it still applied to visual studio.net, unfortunately it doesn't. Any word on where I can find an updated version of this article that works for visual studio.net?

  4. 14 Jan 2004 at 15:14
    I was under the impression that there were three flavours of VB (this is VB6 BTW)
    Learning
    Professional
    Enterprise.
    Is there a standard version before the Pro one?
  5. 14 Jan 2004 at 15:07
    hmm... what about VB "Standard" ?
  6. 14 Jan 2004 at 13:58
    You might want to mention that the MMC only comes with the Pro or Enterprise versions of VB. While I might be the only developer that uses the Learning Edition (I know it's pretty rubbish but I have VB5 Pro ) so some newbies could be stumped when it won't add.
    Anyway nice and simple.
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Add Quick MP3 Audio Functionality to your Programs.

Leave a comment

Sign in or Join us (it's free).

Cooper Sloan - VB6FREAK Was born. Now is living.

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback