Click here to Skip to main content
11,412,516 members (70,127 online)
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

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

Share

About the Author

Angelo Cresta
Chief Technology Officer
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
Questionunusable code Pin
chriscore at 31-Jul-12 6:08
memberchriscore31-Jul-12 6:08 
AnswerRe: unusable code Pin
Angelo Cresta at 1-Aug-12 22:05
memberAngelo Cresta1-Aug-12 22:05 
GeneralVB class for Sound Player Pin
Member 8368413 at 15-Nov-11 7:25
memberMember 836841315-Nov-11 7:25 
QuestionCan't get it to work Pin
xgonzalez81 at 14-May-11 12:34
memberxgonzalez8114-May-11 12:34 
GeneralGreat Class Thank you Pin
CDPerez at 21-Sep-10 7:01
memberCDPerez21-Sep-10 7:01 
GeneralGreat work Pin
JohnAndre at 25-Aug-10 6:42
memberJohnAndre25-Aug-10 6:42 
GeneralMy vote of 5 Pin
JohnAndre at 25-Aug-10 6:41
memberJohnAndre25-Aug-10 6:41 
QuestionRecording coinciding visualizing? Pin
fmmahdi at 2-Feb-10 8:40
memberfmmahdi2-Feb-10 8:40 
QuestionDoesn't work on web server but works on development machine. Pin
dnair926 at 21-Aug-09 10:12
memberdnair92621-Aug-09 10:12 
GeneralFantastic! Pin
jharris4 at 9-Jul-09 12:43
memberjharris49-Jul-09 12:43 
QuestionCalling a method to execute sound Pin
selassie747 at 8-Jul-09 2:18
memberselassie7478-Jul-09 2:18 
Questionplaying wave files in vb.net Pin
selassie747 at 3-Jul-09 2:01
memberselassie7473-Jul-09 2:01 
QuestionIs there a way to change output device? Pin
Claudio Nicora at 26-May-09 6:58
memberClaudio Nicora26-May-09 6:58 
AnswerRe: Is there a way to change output device? Pin
Angelo Cresta at 26-May-09 11:33
memberAngelo Cresta26-May-09 11:33 
QuestionHow to stop the sound Pin
chaisutek at 8-Jun-08 12:10
memberchaisutek8-Jun-08 12:10 
Questionduration wave file vb.net Pin
Member 4283494 at 22-May-08 8:28
memberMember 428349422-May-08 8:28 
AnswerRe: duration wave file vb.net Pin
Code_Doctor at 22-May-08 22:37
memberCode_Doctor22-May-08 22:37 
QuestionDisplay of imgae files and .avi and .swf in VB.NET Pin
bijivn at 22-Apr-08 2:38
memberbijivn22-Apr-08 2:38 
Generalthanks Pin
x-files at 4-Dec-07 13:22
memberx-files4-Dec-07 13:22 
Generalloop sound Pin
I Hate My Computer at 27-May-07 19:21
memberI Hate My Computer27-May-07 19:21 
GeneralVery Good Pin
merdynn at 11-Apr-07 7:34
membermerdynn11-Apr-07 7:34 
GeneralRe: Very Good Pin
Angelo Cresta at 12-Apr-07 21:50
memberAngelo Cresta12-Apr-07 21:50 
GeneralCool Pin
rexha at 18-Jul-06 23:37
memberrexha18-Jul-06 23:37 
AnswerRe: Cool Pin
98z28 at 15-Sep-06 13:28
member98z2815-Sep-06 13:28 
GeneralHelp for AxWindiwsMediaPlayer with time Pin
Parth Bhatt at 22-Jun-06 8:37
memberParth Bhatt22-Jun-06 8:37 
GeneralProblem playing .wmv files Pin
seva04121973 at 22-Jun-06 2:51
memberseva0412197322-Jun-06 2:51 
GeneralRe: time frame in AxWindowsMediaPlayer Pin
seva04121973 at 22-Jun-06 2:49
memberseva0412197322-Jun-06 2:49 
QuestionJust to Play a WAV File? Pin
WD9DAN at 19-Jun-06 3:24
memberWD9DAN19-Jun-06 3:24 
AnswerRe: Just to Play a WAV File? Pin
98z28 at 15-Sep-06 13:17
member98z2815-Sep-06 13:17 
QuestionAny suggestion on how to record wav / mp3 files ? Pin
Parik Advani at 12-Jun-06 3:58
memberParik Advani12-Jun-06 3:58 
AnswerRe: Any suggestion on how to record wav / mp3 files ? Pin
98z28 at 15-Sep-06 13:05
member98z2815-Sep-06 13:05 
GeneralIt's not working for me Pin
RandomSkratch at 5-Apr-06 20:27
memberRandomSkratch5-Apr-06 20:27 
GeneralRe: It's not working for me Pin
Jeff Meyers at 13-Apr-06 7:25
memberJeff Meyers13-Apr-06 7:25 
GeneralGreat Article Pin
Thad VDB at 29-Oct-05 20:48
memberThad VDB29-Oct-05 20:48 
GeneralWaves in vb.net 2005 Pin
cave_troll at 26-Oct-05 2:20
membercave_troll26-Oct-05 2:20 
GeneralRe: Waves in vb.net 2005 Pin
Thad VDB at 29-Oct-05 20:39
memberThad VDB29-Oct-05 20:39 
GeneralRe: Waves in vb.net 2005 Pin
ProgrammingPrincess at 30-Mar-06 13:52
memberProgrammingPrincess30-Mar-06 13:52 
GeneralQuestion about waves in C++.Net Pin
sleff@yahoo.com at 26-Apr-05 13:15
membersleff@yahoo.com26-Apr-05 13:15 
GeneralRe: Question about waves in C++.Net Pin
Alexey A. Popov at 21-Nov-05 4:29
memberAlexey A. Popov21-Nov-05 4:29 
GeneralRe: Question about waves in C++.Net Pin
Alexey A. Popov at 21-Nov-05 4:30
memberAlexey A. Popov21-Nov-05 4:30 
QuestionHow to Find the Duration of Wave File. Pin
DevangUdeshi30182 at 14-Apr-05 0:25
memberDevangUdeshi3018214-Apr-05 0:25 
Question?One question? Pin
tom_dx at 16-Feb-05 14:50
membertom_dx16-Feb-05 14:50 
AnswerRe: ?One question? Pin
DikkeBeis at 27-Feb-05 11:41
sussDikkeBeis27-Feb-05 11:41 
AnswerRe: ?One question? Pin
marble_eater at 22-May-05 18:38
membermarble_eater22-May-05 18:38 
AnswerRe: ?One question? Pin
Mitch (Programmer2k4) at 10-Oct-05 10:40
memberMitch (Programmer2k4)10-Oct-05 10:40 

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.150427.1 | Last Updated 16 Feb 2005
Article Copyright 2005 by Angelo Cresta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid