O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Play a WAV File

Problem

You need to play a .wav audio file.

Solution

Use the unmanaged sndPlaySoundA API function.

Discussion

The .NET Framework doesn’t include any managed classes for playing audio files. However, the winmm.dll library included with Windows includes a function named sndPlaySoundA that accepts the name of a WAV file and a parameter indicating how to play it. You can choose to play a sound synchronously, asynchronously, or in a continuous background loop. When you play a sound synchronously, the function interrupts the execution of the program until the sound is complete. If you play a sound asynchronously, the function will return immediately, and the sound will play in the background.

The following example form allows a sound to be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required