Home > Game making, Visual Basic > Music! And Sound! Simultaneously!

Music! And Sound! Simultaneously!

What is your favorite video game, ever?  Chances are, whatever game it is, it has background music. Also, it most likely has sounds.  Now, if you are going to make a game, you are going to want to make an awesome game.  And awesome games have music, and sound.

The problem with this is that Visual Basic classifies both background music and sound FX as “sounds”.  Now, that wouldn’t be a problem if it weren’t for the fact that VB won’t play two sounds simultaneously.  This means that you can have background music, but as soon as you play another sound, the music stops, and doesn’t start again.

I ran into this problem a couple of days ago.  I did several extensive Google searches, but to no avail. After a long time, I came up with a surprisingly easy solution.

Here is the code I used to play a sound effect.


'In the declarations.

Dim snd_Sound as New Media.SoundPlayer("Wherever the sound is")

...

'In the New() sub.

snd_Sound.Load()

...

'Wherever I want to play the sound.

snd_Sound.Play()

Now, to play the music, I added an AxWindowsMediaPlayer using these steps:

  1. In the Solution Explorer, double-click “My Project”.
  2. Go to the “References” tab.
  3. Click “Add…”.
  4. Click the “COM” tab.
  5. Scroll down until you find “Windows Media Player”.
  6. Select “Windows Media Player”.
  7. Click “Ok”.
  8. In the Solution Explorer, double-click the name of the form you want to play music in.
  9. Right-click anywhere in the Toolbox, and click “Choose items…”.
  10. Once the dialog appears, go to the “COM” tab.
  11. Scroll down until you find “Windows Media Player”.
  12. Check the box next to “Windows Media Player”.
  13. Click “Ok”.
  14. In the toolbox, double-click “Windows Media Player” (in General or All Windows Forms”
  15. Select the new Windows Media Player.
  16. Change the (Name) property to MusicPlayer.

And the code to play the music is:


'In the New() Sub.

'Remove all UI for the music player.

MusicPlayer.uiMode = "invisible"

'Set the media location to the correct path.

MusicPlayer.URL = "Path to your music"

'Make the music player invisible.

MusicPlayer.Location = New Point(-1000, -1000)

'Start the music.

MusicPlayer.settings.playCount = "500"

And voilà!  Your music will now play at the same time as your sound FX.

I hope you learned something!  If you couldn’t understand this, just leave me a comment and I’ll edit it to be in tutorial form.  Also, if you know an easier way to do this, please tell me in the comments!

See ya’ tomorrow!

  1. November 8, 2011 at 9:58 AM

    Very helpful post! Thanks!

  2. Administrator
    November 8, 2011 at 9:57 AM

    Very helpful post!

  3. Rebecca J Page
    March 21, 2011 at 4:30 PM

    Where there’s a will, there’s a way! Cool graphic, too 🙂

  4. March 21, 2011 at 3:49 PM

    Please, comment and rate!

  1. No trackbacks yet.

Leave a reply to Administrator Cancel reply