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

Play Waves in VB.NET

By , 16 Feb 2005
 

Sample Image

Introduction

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
        Sound.PlayWaveFile("sn01088a.wav")
    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 ...

        '"SystemQuestion"
        '"SystemExclaimation"
        '"SystemHand"
        '"Maximize"
        '"MenuCommand"
        '"MenuPopup"
        '"Minimize"
        '"MailBeep"
        '"Open"
        '"Close"
        '"SystemAsterisk"
        '"RestoreUp"
        '"RestoreDown"
        '"SystemExit"
        '"SystemStart"

        Sound.PlayWaveSystem("SystemExit")
    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).

History

First release.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionunusable codememberchriscore31 Jul '12 - 5:08 
AnswerRe: unusable codememberAngelo Cresta1 Aug '12 - 21:05 
GeneralVB class for Sound PlayermemberMember 836841315 Nov '11 - 6:25 
QuestionCan't get it to workmemberxgonzalez8114 May '11 - 11:34 
GeneralGreat Class Thank youmemberCDPerez21 Sep '10 - 6:01 
GeneralGreat workmemberJohnAndre25 Aug '10 - 5:42 
GeneralMy vote of 5memberJohnAndre25 Aug '10 - 5:41 
QuestionRecording coinciding visualizing?memberfmmahdi2 Feb '10 - 7:40 
QuestionDoesn't work on web server but works on development machine.memberdnair92621 Aug '09 - 9:12 
GeneralFantastic!memberjharris49 Jul '09 - 11:43 
I was looking for a freeware app to download, heck with this I was able to right my own to do just what I need. good job.
 
JHarris

QuestionCalling a method to execute soundmemberselassie7478 Jul '09 - 1:18 
Questionplaying wave files in vb.netmemberselassie7473 Jul '09 - 1:01 
QuestionIs there a way to change output device?memberClaudio Nicora26 May '09 - 5:58 
AnswerRe: Is there a way to change output device?memberAngelo Cresta26 May '09 - 10:33 
QuestionHow to stop the soundmemberchaisutek8 Jun '08 - 11:10 
Questionduration wave file vb.netmemberMember 428349422 May '08 - 7:28 
AnswerRe: duration wave file vb.netmemberCode_Doctor22 May '08 - 21:37 
QuestionDisplay of imgae files and .avi and .swf in VB.NETmemberbijivn22 Apr '08 - 1:38 
Generalthanksmemberx-files4 Dec '07 - 12:22 
Generalloop soundmemberI Hate My Computer27 May '07 - 18:21 
GeneralVery Goodmembermerdynn11 Apr '07 - 6:34 
GeneralRe: Very GoodmemberAngelo Cresta12 Apr '07 - 20:50 
GeneralCoolmemberrexha18 Jul '06 - 22:37 
AnswerRe: Coolmember98z2815 Sep '06 - 12:28 
GeneralHelp for AxWindiwsMediaPlayer with timememberParth Bhatt22 Jun '06 - 7:37 
GeneralProblem playing .wmv filesmemberseva0412197322 Jun '06 - 1:51 
GeneralRe: time frame in AxWindowsMediaPlayermemberseva0412197322 Jun '06 - 1:49 
QuestionJust to Play a WAV File?memberWD9DAN19 Jun '06 - 2:24 
AnswerRe: Just to Play a WAV File?member98z2815 Sep '06 - 12:17 
QuestionAny suggestion on how to record wav / mp3 files ?memberParik Advani12 Jun '06 - 2:58 
AnswerRe: Any suggestion on how to record wav / mp3 files ?member98z2815 Sep '06 - 12:05 
GeneralIt's not working for mememberRandomSkratch5 Apr '06 - 19:27 
GeneralRe: It's not working for mememberJeff Meyers13 Apr '06 - 6:25 
GeneralGreat ArticlememberThad VDB29 Oct '05 - 19:48 
GeneralWaves in vb.net 2005membercave_troll26 Oct '05 - 1:20 
GeneralRe: Waves in vb.net 2005memberThad VDB29 Oct '05 - 19:39 
GeneralRe: Waves in vb.net 2005memberProgrammingPrincess30 Mar '06 - 12:52 
GeneralQuestion about waves in C++.Netmembersleff@yahoo.com26 Apr '05 - 12:15 
GeneralRe: Question about waves in C++.NetmemberAlexey A. Popov21 Nov '05 - 3:29 
GeneralRe: Question about waves in C++.NetmemberAlexey A. Popov21 Nov '05 - 3:30 
QuestionHow to Find the Duration of Wave File.memberDevangUdeshi3018213 Apr '05 - 23:25 
Question?One question?membertom_dx16 Feb '05 - 13:50 
AnswerRe: ?One question?sussDikkeBeis27 Feb '05 - 10:41 
AnswerRe: ?One question?membermarble_eater22 May '05 - 17:38 
AnswerRe: ?One question?memberMitch (Programmer2k4)10 Oct '05 - 9:40 

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

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