Click here to Skip to main content
6,595,854 members and growing! (19,386 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Multimedia     Intermediate

Play Waves in VB.NET

By Angelo Cresta

A simple Wave Player class.
VB.NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:16 Feb 2005
Views:119,771
Bookmarked:41 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 5.38 Rating: 4.07 out of 5
1 vote, 4.8%
1
1 vote, 4.8%
2
1 vote, 4.8%
3
6 votes, 28.6%
4
12 votes, 57.1%
5

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


Member

Occupation: Chief Technology Officer
Location: Switzerland Switzerland

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
QuestionDoesn't work on web server but works on development machine. Pinmemberdnair92610:12 21 Aug '09  
GeneralFantastic! Pinmemberjharris412:43 9 Jul '09  
QuestionCalling a method to execute sound Pinmemberselassie7472:18 8 Jul '09  
Questionplaying wave files in vb.net Pinmemberselassie7472:01 3 Jul '09  
QuestionIs there a way to change output device? PinmemberClaudio Nicora6:58 26 May '09  
AnswerRe: Is there a way to change output device? PinmemberAngelo Cresta11:33 26 May '09  
GeneralHow to stop the sound Pinmemberchaisutek12:10 8 Jun '08  
Questionduration wave file vb.net PinmemberMember 42834948:28 22 May '08  
AnswerRe: duration wave file vb.net PinmemberCode_Doctor22:37 22 May '08  
QuestionDisplay of imgae files and .avi and .swf in VB.NET Pinmemberbijivn2:38 22 Apr '08  
Generalthanks Pinmemberx-files13:22 4 Dec '07  
Generalloop sound PinmemberI Hate My Computer19:21 27 May '07  
GeneralVery Good Pinmembermerdynn7:34 11 Apr '07  
GeneralRe: Very Good PinmemberAngelo Cresta21:50 12 Apr '07  
GeneralCool Pinmemberrexha23:37 18 Jul '06  
AnswerRe: Cool Pinmember98z2813:28 15 Sep '06  
GeneralHelp for AxWindiwsMediaPlayer with time PinmemberParth Bhatt8:37 22 Jun '06  
GeneralProblem playing .wmv files Pinmemberseva041219732:51 22 Jun '06  
GeneralRe: time frame in AxWindowsMediaPlayer Pinmemberseva041219732:49 22 Jun '06  
GeneralJust to Play a WAV File? PinmemberWD9DAN3:24 19 Jun '06  
GeneralRe: Just to Play a WAV File? Pinmember98z2813:17 15 Sep '06  
GeneralAny suggestion on how to record wav / mp3 files ? PinmemberParik Advani3:58 12 Jun '06  
AnswerRe: Any suggestion on how to record wav / mp3 files ? Pinmember98z2813:05 15 Sep '06  
GeneralIt's not working for me PinmemberRandomSkratch20:27 5 Apr '06  
GeneralRe: It's not working for me PinmemberJeff Meyers7:25 13 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Feb 2005
Editor: Smitha Vijayan
Copyright 2005 by Angelo Cresta
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project