Click here to Skip to main content
11,797,355 members (80,622 online)
Click here to Skip to main content
Add your own
alternative version

Play Waves in VB.NET

, 16 Feb 2005 237.1K 4.8K 47
A simple Wave Player class.
The Microsoft Sound.wav
Imports System
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.IO

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 

    Public Shared Sub PlayWaveFile(ByVal fileWaveFullPath As String)
            PlaySound(fileWaveFullPath, 0, SND_FILENAME)
        End Try
    End Sub

    Public Shared Sub PlayWaveResource(ByVal WaveResourceName As String)

        ' get the namespace 
        Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()

        ' get the resource into a stream
        Dim resourceStream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + WaveResourceName)
        If resourceStream Is Nothing Then Exit Sub

        ' bring stream into a byte array
        Dim wavData As Byte()
        ReDim wavData(CInt(resourceStream.Length))
        resourceStream.Read(wavData, 0, CInt(resourceStream.Length))

        ' play the resource
        PlaySound(wavData, 0, SND_ASYNC Or SND_MEMORY)
    End Sub

    Public Shared Sub PlayWaveSystem(ByVal SystemWaveName As String)
        PlaySound(SystemWaveName, 0&, SND_ALIAS Or SND_ASYNC Or SND_NODEFAULT)
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

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