Playing Sounds (Visual Basic)

The My.Computer.Audio object provides methods for playing sounds.

Playing Sounds

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. You can also play a sound and wait for it to complete.

In the following example, the My.Computer.Audio.Play method plays a sound. When AudioPlayMode.WaitToComplete is specified, My.Computer.Audio.Play waits until the sound completes before calling code continues. When using this example, you should ensure that the file name refers to a .wav sound file that is on your computer

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

In the following example, the My.Computer.Audio.Play method plays a sound. When using this example, you should ensure that the application resources include a .wav sound file that is named Waterfall.

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

Playing Looping Sounds

In the following example, the My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.BackgroundLoop is specified. When using this example, you should ensure that the file name refers to a .wav sound file that is on your computer.

Sub PlayLoopingBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav",
        AudioPlayMode.BackgroundLoop)
End Sub

In the following example, the My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.BackgroundLoop is specified. When using this example, you should ensure that the application resources include a .wav sound file that is named Waterfall.

Sub PlayLoopingBackgroundSoundResource()
    My.Computer.Audio.Play(My.Resources.Waterfall,
          AudioPlayMode.BackgroundLoop)
End Sub

The preceding code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Forms Applications > Sound. For more information, see Code Snippets.

In general, when an application plays a looping sound, it should eventually stop the sound.

Stopping the Playing of Sounds in the Background

Use the My.Computer.Audio.Stop method to stop the application's currently playing background or looping sound.

In general, when an application plays a looping sound, it should stop the sound at some point.

The following example stops a sound that is playing in the background.

Sub StopBackgroundSound()
    My.Computer.Audio.Stop()
End Sub

The preceding code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Forms Applications > Sound. For more information, see Code Snippets.

Playing System Sounds

Use the My.Computer.Audio.PlaySystemSound method to play the specified system sound.

The My.Computer.Audio.PlaySystemSound method takes as a parameter one of the shared members from the SystemSound class. The system sound Asterisk generally denotes errors.

The following example uses the My.Computer.Audio.PlaySystemSound method to play a system sound.

Sub PlaySystemSound()
    My.Computer.Audio.PlaySystemSound(
        System.Media.SystemSounds.Asterisk)
End Sub

See also