Sound in a Visual Basic Program

There are a few classes that can be used to play sound files. The easiest way for use to play a sound file is to use the Play method of the Computer.Audio class.

The Play method of the Computer.Audio class will play a sound file that has been imported to the project Resources folder.

We use the properties dialog box of the project to add a sound file to the Resources folder.

  1. Double Click on My Project in the Solution Explorer task window.
  2. Choose Resources tab on the left
  3. Choose Audio from the leftmost drop down at the top of the dialog box
  4. Choose Add Existing File from the the Add Resource dropdown at the top of the dialog box.
  5. Travel to the location of the .wav file you want to import
  6. Select the .wav file click Open

 

Once the .wav file has been imported it is available to us to use in code. 

The following statement will play a .wav file named Cat.wav located in the Resources folder of the solution.

My.Computer.Audio.Play(My.Resources.CAT, AudioPlayMode.Background)

 

You will be prompted by the intellisense

image of audio.play intellesense