How to play .wav sound files and system sounds by using the My namespace in Visual Basic 2005 (914353)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Studio 2005 Express Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Team System Architect Edition
  • Microsoft Visual Studio 2005 Team System Developer Edition
  • Microsoft Visual Studio 2005 Team System Team Foundation:
  • Microsoft Visual Studio 2005 Team System Test Edition

SUMMARY

This step-by-step article describes how to play .wav sound files and system sounds by using the My namespace in Microsoft Visual Basic 2005. This article also contains a code example that illustrates the concepts that are discussed in the article.

INTRODUCTION

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you must have:
  • Microsoft Windows Server 2003, Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows XP Service Pack 1 (SP1)
  • Visual Basic 2005
This article assumes that you are familiar with the following topics:
  • Visual Basic 2005 programming

MORE INFORMATION

To play .wav sound files and system sounds by using Visual Basic 2005, follow these steps:
  1. Create a Microsoft Windows application. To do this, follow these steps:
    1. Start Microsoft Visual Studio 2005.
    2. On the File menu, point to New, and then click Project.
    3. In the Project types list, click Visual Basic.
    4. In the Templates list, click Windows Application, and then click OK.

      Note By default, WindowsApplication1 is created and Form1.vb is opened in Design view.
  2. Add two control buttons to the Form1.vb form. To do this, follow these steps:
    1. Add two Button controls to the Form1.vb form.

      Note By default, the new buttons are named Button1 and Button2.
    2. Click Button1.
    3. In the Properties pane, change the Text property to Play.
    4. Click Button2.
    5. In the Properties pane, change the Text property of Button2 to Stop.
  3. Double-click Play, and then add the following code to the Button1_Click method:
    My.Computer.Audio.Play("<C:\Filename.wav>", AudioPlayMode.BackgroundLoop)
    Note <C:\Filename.wav> represents the file name of a .wav file.
  4. In Solution Explorer, double-click Form1.vb.
  5. Double-click Stop, and then add the following code to the Button2_Click method:
    My.Computer.Audio.Stop()
  6. Run the application. To do this, follow these steps:
    1. On the Debug menu, click Start Debugging.
    2. Click Play.

      Notes
      • The sound file plays continuously.
      • You hear audio output only if your computer has a sound card that is configured correctly. You must also use speakers, earphones, or headphones to hear the audio output.
    3. Click Stop.

      Note The sound stops playing.

REFERENCES

For more information about how to play audio files by using Microsoft Visual Basic .NET or Visual Basic 2005 and the Windows Media Player SDK, click the following article number to view the article in the Microsoft Knowledge Base:

821767 How to play audio files by using Visual Basic .NET or Visual Basic 2005

For more information about the My.Computer.Audio.Play method, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbhowto kbinfo KB914353 kbAudDeveloper