Click here to Skip to main content
Click here to Skip to main content

Play Waves in VB.NET

, 16 Feb 2005
Rate this:
Please Sign up or sign in to vote.
A simple Wave Player class.

Sample Image


This is a simple class that shows how to play Wave files in a .NET project, using Windows API. Here, you will find the way to play embedded resources, external files, or Windows system Waves.

Using the code

Using the code is very simple, you have to choose if you want to play a file: Sound.PlayWaveFile("Filename.wav"), an embedded resource: Sound.PlayWaveResource("Embedded.wav"), or a system sound: Sound.PlayWaveSystem("SystemExit").

Public Class Sound
    Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
      As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer

    Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
      As Byte(), ByVal hmod As Integer, ByVal flags As Integer) As Integer

    Public Const SND_SYNC = &H0 ' play synchronously 
    Public Const SND_ASYNC = &H1 ' play asynchronously 
    Public Const SND_MEMORY = &H4  'Play wav in memory
    Public Const SND_ALIAS = &H10000 'Play system alias wav 
    Public Const SND_NODEFAULT = &H2
    Public Const SND_FILENAME = &H20000 ' name is file name 
    Public Const SND_RESOURCE = &H40004 ' name is resource name or atom 
End Class

'Using the code:

    Private Sub btnFile_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnFile.Click
        'File Located in executable dir, change it if you need
    End Sub

    Private Sub btnEmbed_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnEmbed.Click
        'Remeber to include the wave in the project 
        'and then in the "build action"
        'properties set it as: "Embedded Resource"
        Sound.PlayWaveResource("The Microsoft Sound.wav")
    End Sub

    Private Sub btnSystem_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSystem.Click

        'Here some ...


    End Sub

All the job is done by the PlaySound function.

Points of Interest

This is an "all in one" Sound class for your .NET applications (File, Resource, System sounds).


First release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Angelo Cresta
Chief Technology Officer
Switzerland Switzerland
No Biography provided

Comments and Discussions

QuestionJust to Play a WAV File? PinmemberWD9DAN19-Jun-06 3:24 
AnswerRe: Just to Play a WAV File? Pinmember98z2815-Sep-06 13:17 
QuestionAny suggestion on how to record wav / mp3 files ? PinmemberParik Advani12-Jun-06 3:58 
AnswerRe: Any suggestion on how to record wav / mp3 files ? Pinmember98z2815-Sep-06 13:05 
Hi, one of the easier ways to record is to use the MCI Command Interface. It is 'somewhat' flexible and gives you the ability to specify some of the quality settings.
I have a full tutorial on how to Learn to program the mciSendString Command Interface in your applications to be able to perform most media based functionality.
My site link is in my signature at the bottom of this post.
I do plan on releasing a new version of my csMusicLibrary Library for Visual Basic 2005 in the near future. It will have basic recording capabilities already incorporated in the library. The class is almost complete, but I have to finish debugging plus some tweaking here and there.
But you may not want to wait until the new library is released. So I suggest you go check out the tutorial I made.
Hope this helps Smile | :)



GeneralIt's not working for me PinmemberRandomSkratch5-Apr-06 20:27 
GeneralRe: It's not working for me PinmemberJeff Meyers13-Apr-06 7:25 
GeneralGreat Article PinmemberThad VDB29-Oct-05 20:48 
GeneralWaves in 2005 Pinmembercave_troll26-Oct-05 2:20 
GeneralRe: Waves in 2005 PinmemberThad VDB29-Oct-05 20:39 
GeneralRe: Waves in 2005 PinmemberProgrammingPrincess30-Mar-06 13:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 16 Feb 2005
Article Copyright 2005 by Angelo Cresta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid