Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Play Sounds in Visual Basic

This example plays a sound in the background.

Background playing lets the application execute other code while the sound plays. The My.Computer.Audio.Play method allows the application to play only one background sound at a time; when the application plays a new background sound, it stops playing the previous background sound. If you want to play a sound and wait for it to complete, see How to: Play Sounds and Wait For Completion in Visual Basic.

In general, when an application plays a looping sound, it should eventually stop the sound. For more information, see How to: Stop Playing Sounds in the Background in Visual Basic.

The My.Computer.Audio.Play method plays a sound in the background when PlayMode.Background is specified.

Check that the file name references a .wav sound file on your system.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", _
End Sub

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Check that the application resources include a .wav sound file named Waterfall.

Sub PlayBackgroundSoundResource()
    My.Computer.Audio.Play(My.Resources.Waterfall, _
End Sub

These code examples can run only within a Windows Forms or console application. For more information, see My.Computer.Audio.Play Method.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources Object.

Community Additions

© 2015 Microsoft