Visual Basic

How To Play Sounds In Visual Basic

To play a WAV sound in a Visual Basic .NET Windows application, call My.Computer.Audio.Play with the file path. Choose background playback to keep the interface responsive, or a looping mode for repeated audio. Use System.Media.SoundPlayer when you need an explicit player object, and a suitable.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

Choose a method for your Visual Basic project

The examples below target VB.NET Windows Forms applications. Classic Visual Basic 6 and Office VBA use different APIs; a winmm.dll declaration copied from an older tutorial is not interchangeable with these examples. If you are assessing an existing application, this discussion of Visual Basic's role provides broader context.

  • For a short WAV notification or a repeating alert, start with My.Computer.Audio.
  • For a WAV player with loading methods and playback events, consider System.Media.SoundPlayer.
  • For MP3 music, independent volume control or overlapping tracks, choose an audio library that supports those requirements.

Neither My.Computer.Audio nor SoundPlayer is a general music player. In particular, passing an MP3 filename to a WAV playback method does not add MP3 support.

Play a WAV file when a button is clicked

  1. Open a Visual Basic Windows Forms project in Visual Studio.
  2. Add a button to the form and name it Button1.
  3. Choose a WAV file you can already play in Windows. For this example, place it at C:\Audio\notify.wav, or substitute its actual path.
  4. Double-click the button in the designer to create its Click event handler.
  5. Add the playback statement inside that handler, as shown below, then run the application and click the button.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

My.Computer.Audio.Play("C:\Audio\notify.wav", Microsoft.VisualBasic.AudioPlayMode.Background)

End Sub

If the designer has already created the handler, insert only the middle line. Do not paste a second procedure inside the existing one. Background playback lets subsequent code run while the sound plays. Microsoft's Audio.Play reference documents the supported file, stream and byte-array inputs.

The absolute path makes this first test easy to understand. Before distributing the application, replace that machine-specific location with a deployed file path or an embedded resource.

Control looping and playback timing

The playback mode is an argument to Play. There are three choices, described in Microsoft's AudioPlayMode reference:

  • Background plays the sound while the calling code continues.
  • BackgroundLoop repeats the sound while the calling code continues.
  • WaitToComplete waits for playback to finish before continuing on the calling thread.

To repeat the example sound, use this statement in the play button's handler:

My.Computer.Audio.Play("C:\Audio\notify.wav", Microsoft.VisualBasic.AudioPlayMode.BackgroundLoop)

Add a separate Stop button and put this statement in its Click handler:

My.Computer.Audio.Stop()

The Stop method ends background playback started through My.Computer.Audio. Give users a clear way to stop a repeating alert.

Avoid WaitToComplete in a form's Click handler when the window must remain interactive: it blocks that UI thread until playback finishes. My.Computer.Audio has no separate PlaySync or PlayLooping method; select the appropriate mode instead.

Include the sound with your application

An embedded resource avoids relying on a file remaining in a particular folder on the user's computer.

  1. Open the project's resources editor and add your WAV file as an audio resource.
  2. Give the resource a recognisable name, such as Notify.
  3. Replace the filename argument with My.Resources.Notify.
  4. Build and test the application from its output folder to confirm that the sound is included.

My.Computer.Audio.Play(My.Resources.Notify, Microsoft.VisualBasic.AudioPlayMode.Background)

Use the resource name generated by your project. If you prefer an external file, configure it to be copied to the output and included when publishing, then construct its path from the application's location rather than assuming the current working directory.

Use SoundPlayer for WAV loading and playback

System.Media.SoundPlayer offers Play, PlaySync, PlayLooping and Stop. It also provides Load and LoadAsync when you want to load a sound before playing it. The SoundPlayer reference describes these methods and the LoadCompleted event.

For a form that needs a reusable player, declare this field inside the form class, outside its event handlers:

Private ReadOnly notificationPlayer As New System.Media.SoundPlayer("C:\Audio\notify.wav")

Call notificationPlayer.Play() from the play button and notificationPlayer.Stop() from the stop button. Keep the object available for as long as you need it, and dispose of it when the form no longer needs it.

SoundPlayer has no Pause method or built-in volume property. My.Computer.Audio also has no SetVolume method. If those controls are requirements, choose a playback library that exposes them.

Play a simple tone without an audio file

For a basic notification, Visual Basic's Beep() takes no frequency or duration arguments. To specify those values on Windows, use System.Console.Beep instead:

System.Console.Beep(1000, 500)

This requests a 1,000 Hz tone lasting 500 milliseconds. The parameters are integers; see Microsoft's Console.Beep reference. The call blocks while the tone plays, so avoid a long sequence on the form's UI thread.

Handle MP3 playback and overlapping sounds

NAudio is an option for more involved Windows audio work. Its Windows Forms playback example demonstrates file playback and volume control. Check that the package you select supports your project's target framework.

Object lifetime matters with asynchronous playback. Creating a reader and output device inside a short Using block, calling Play, and immediately leaving the block disposes of objects still needed for playback. Keep references while audio is playing, then release resources through an appropriate completion or shutdown path. NAudio's PlaybackStopped guidance explains when cleanup is safe.

For simultaneous effects, use an audio engine with mixing support. Starting extra threads around My.Computer.Audio is not a substitute for a mixer.

Check file access and silent playback

  • Verify the exact path and that the application can read the file.
  • Check that the file contains supported WAV audio. Renaming an MP3 with a .wav extension does not convert it.
  • Check Windows volume, the application's mixer setting and the selected output device.
  • Handle loading and file-access errors with a useful message rather than silently ignoring them.
  • Test the published application, where missing external audio files often become apparent.

Once playback works, you can add a sound selector using the Visual Basic combo box guide, or find related lessons in the Visual Basic tutorials.

Start by wiring one known WAV file to a Play button and a Stop button. Test both controls before adding looping or music playback.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post