Click here to Skip to main content
13,046,726 members (42,753 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 16 Feb 2005

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

You may also be interested in...

Comments and Discussions

QuestionRecording coinciding visualizing? Pin
fmmahdi2-Feb-10 7:40
memberfmmahdi2-Feb-10 7:40 
QuestionDoesn't work on web server but works on development machine. Pin
dnair92621-Aug-09 9:12
memberdnair92621-Aug-09 9:12 
GeneralFantastic! Pin
jharris49-Jul-09 11:43
memberjharris49-Jul-09 11:43 
QuestionCalling a method to execute sound Pin
selassie7478-Jul-09 1:18
memberselassie7478-Jul-09 1:18 
I tried out a code to play files stored in the resource folder. It is working alright, I wrote these codes to play these files in a Sub method for it to be executed when called. However its more than one method be called within the event of when a button is clicked.

What is happening is that, when the event takes place, that is when the button is clicked, only one method is executed.


These are examples of the codes:
I've got two different Subs I'll be calling in an event, both are to play sound files. Word() and Say()

Public Sub Word()

If My.Resources.mention.CanRead Then
Dim bStr(My.Resources.mention.Length) As Byte
My.Resources.mention.Read(bStr, 0, My.Resources.mention.Length)
My.Computer.Audio.Play(bStr, AudioPlayMode.Background)
End If

End Sub

Public Sub Say()

If My.Resources.pronounce.CanRead Then
Dim bStr(My.Resources.pronounce.Length) As Byte
My.Resources.pronounce.Read(bStr, 0, My.Resources.pronounce.Length)
My.Computer.Audio.Play(bStr, AudioPlayMode.Background)
End If

End Sub

Below is the event which is when the button 'btnStart' is clicked.

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

End Sub

When I execute this and click the 'btnStart' button the method that is executed is the second i.e. Word() only.

I can't understand why, what I want it to do is execute Say() then execute Word().

Is there a way to do this or is it that two methods can't be called within an event, can I get any help for this?????

I am really desperate here Confused | :confused: .
Questionplaying wave files in Pin
selassie7473-Jul-09 1:01
memberselassie7473-Jul-09 1:01 
QuestionIs there a way to change output device? Pin
Claudio Nicora26-May-09 5:58
memberClaudio Nicora26-May-09 5:58 
AnswerRe: Is there a way to change output device? Pin
Angelo Cresta26-May-09 10:33
memberAngelo Cresta26-May-09 10:33 
QuestionHow to stop the sound Pin
chaisutek8-Jun-08 11:10
memberchaisutek8-Jun-08 11:10 
Questionduration wave file Pin
Member 428349422-May-08 7:28
memberMember 428349422-May-08 7:28 
AnswerRe: duration wave file Pin
Code_Doctor22-May-08 21:37
memberCode_Doctor22-May-08 21:37 

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

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

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