Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / Visual Basic
Article

Play Waves in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.53/5 (25 votes)
16 Feb 2005 302.1K   6.3K   46   47
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").

VB
    ...
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


Written By
Chief Technology Officer
Switzerland Switzerland
Senior Manager with broad experience in all aspects of information technology.
Direct experience with:
- project management and organization
- application architecture/development
- infrastructure and security design/implementation

Senior Security Architect
Senior Technical Information Security Officer

Comments and Discussions

 
QuestionWorks perfect in vb.net 2013 Pin
Appie van Zon7-Nov-16 19:59
Appie van Zon7-Nov-16 19:59 
GeneralMy vote of 5 Pin
Michael Kistic6-Oct-15 5:48
Michael Kistic6-Oct-15 5:48 
Questionunusable code Pin
chriscore 31-Jul-12 5:08
chriscore 31-Jul-12 5:08 
AnswerRe: unusable code Pin
Angelo Cresta1-Aug-12 21:05
professionalAngelo Cresta1-Aug-12 21:05 
GeneralVB class for Sound Player Pin
Member 836841315-Nov-11 6:25
Member 836841315-Nov-11 6:25 
QuestionCan't get it to work Pin
xgonzalez8114-May-11 11:34
xgonzalez8114-May-11 11:34 
GeneralGreat Class Thank you Pin
CDPerez21-Sep-10 6:01
CDPerez21-Sep-10 6:01 
GeneralGreat work Pin
JohnAndre25-Aug-10 5:42
JohnAndre25-Aug-10 5:42 
GeneralMy vote of 5 Pin
JohnAndre25-Aug-10 5:41
JohnAndre25-Aug-10 5:41 
QuestionRecording coinciding visualizing? Pin
Mahdi Mansouri2-Feb-10 7:40
Mahdi Mansouri2-Feb-10 7:40 
QuestionDoesn't work on web server but works on development machine. Pin
dnair92621-Aug-09 9:12
dnair92621-Aug-09 9:12 
GeneralFantastic! Pin
jharris49-Jul-09 11:43
jharris49-Jul-09 11:43 
QuestionCalling a method to execute sound Pin
selassie7478-Jul-09 1:18
selassie7478-Jul-09 1:18 
Questionplaying wave files in vb.net Pin
selassie7473-Jul-09 1:01
selassie7473-Jul-09 1:01 
QuestionIs there a way to change output device? Pin
Claudio Nicora26-May-09 5:58
Claudio Nicora26-May-09 5:58 
AnswerRe: Is there a way to change output device? Pin
Angelo Cresta26-May-09 10:33
professionalAngelo Cresta26-May-09 10:33 
QuestionHow to stop the sound Pin
chaisutek8-Jun-08 11:10
chaisutek8-Jun-08 11:10 
Questionduration wave file vb.net Pin
PCFAN22-May-08 7:28
PCFAN22-May-08 7:28 
AnswerRe: duration wave file vb.net Pin
Code_Doctor22-May-08 21:37
Code_Doctor22-May-08 21:37 
You'll have to extend the code yourself.

This can be done by using the same API winmm.dll that is being used.
Simply add some more declarations, and work out some additional methods.

Here are just a few, I'll suggest you research the API at msdn.microsoft.com



Private Declare Function waveOutOpen Lib "winmm.dll" Alias "waveOutOpen" (ByVal lphWaveOut As Long, ByVal uDeviceID As Long, ByVal lpFormat As WAVEFORMATEX, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
Private Declare Function waveOutWrite Lib "winmm.dll" Alias "waveOutWrite" (ByVal hWaveOut As Long, ByVal lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As Long
Private Declare Function waveOutPause Lib "winmm.dll" Alias "waveOutPause" (ByVal hWaveOut As Long) As Long
Private Declare Function waveOutRestart Lib "winmm.dll" Alias "waveOutRestart" (ByVal hWaveOut As Long) As Long
Private Declare Function waveOutSetPitch Lib "winmm.dll" Alias "waveOutSetPitch" (ByVal hWaveOut As Long, ByVal dwPitch As Long) As Long
Private Declare Function waveOutGetPosition Lib "winmm.dll" Alias "waveOutGetPosition" (ByVal hWaveOut As Long, ByVal lpInfo As MMTIME, ByVal uSize As Long) As Long

' Management of errors.
Private Declare Function waveOutGetErrorText Lib "winmm.dll" Alias "waveInGetErrorTextA" (ByVal err As Long, ByVal lpText As String, ByVal uSize As Long) As Long

Private Declare Function waveOutClose Lib "winmm.dll" Alias "waveOutClose" (ByVal hWaveOut As Long) As Long
Private Declare Function waveOutGetID Lib "winmm.dll" Alias "waveOutGetID" (ByVal hWaveOut As Long, ByVal lpuDeviceID As Long) As Long
Private Declare Function waveOutGetNumDevs Lib "winmm.dll" Alias "waveOutGetNumDevs" () As Long
Private Declare Function waveOutPrepareHeader Lib "winmm.dll" Alias "waveOutPrepareHeader" (ByVal hWaveOut As Long, ByVal lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As Long
Private Declare Function waveOutUnprepareHeader Lib "winmm.dll" Alias "waveOutUnprepareHeader" (ByVal hWaveOut As Long, ByVal lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As Long


Public Const WAVE_MAPPER = -1&
Public Const WAVE_FORMAT_PCM = 1

Public Structure WAVEHDR
    Dim lpData As Int32
    Dim dwBufferLength As Int32
    Dim dwBytesRecorded As Int32
    Dim dwUser As Int32
    Dim dwFlags As Int32
    Dim dwLoops As Int32
    Dim reserved As Int32
    Dim lpNext As Int32
End Structure

Public Structure WAVEFORMATEX
    Dim wFormatTag As Int16
    Dim nChannels As Int16
    Dim nSamplesPerSec As Int32
    Dim nAvgBytesPerSec As Int32
    Dim nBlockAlign As Int16
    Dim wBitsPerSample As Int16
    Dim cbSize As Int16
End Structure

Structure MMTIME
    Dim wType As Long
    Dim u As Long
End Structure


That should be enough to get ya started. It's a pretty easy API to get, have fun. Hope this helps.
QuestionDisplay of imgae files and .avi and .swf in VB.NET Pin
bijivn22-Apr-08 1:38
bijivn22-Apr-08 1:38 
Generalthanks Pin
x-files4-Dec-07 12:22
x-files4-Dec-07 12:22 
Generalloop sound Pin
I Love My Computer27-May-07 18:21
I Love My Computer27-May-07 18:21 
GeneralVery Good Pin
merdynn11-Apr-07 6:34
merdynn11-Apr-07 6:34 
GeneralRe: Very Good Pin
Angelo Cresta12-Apr-07 20:50
professionalAngelo Cresta12-Apr-07 20:50 
GeneralCool Pin
r3xha18-Jul-06 22:37
r3xha18-Jul-06 22: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.