PlaySound API In Visual Basic

Level:
Level2

Create an EzPlay function for all your sound needs

From time to time, the Win32 API offers up a rare jewel, something that is both easy to use and provides seemingly complex functionality to your applications. The Playsound multimedia API function falls into this category.

Playsound can read files from standard WAV files on disk, by reference from the system registry, or from a resource file. For the application developer, this means that you can create a registry entry for your sound effects, which means that the end user can use the control panel to customize their preferences in sounds. You avoid designing another configuration screen or panel, and the user can still customize the application.

Playsound is also perfect for ActiveX control development. You can add go-faster sound effects to your control with an absolute minimal amount of code and virtually no head-aches. Developers will appreciate the added support of being able to easily include sound events to the controls that you produce.

I admire the simplicity and usefulness of this particular function. I think that after you use it, you may come to the same conclusion.

As you examine the code, it is going to seem awfully long for a one-line function call. I've provided as much detail about the function as possible and if you remove the comments and string constants, the code will shrink dramatically.

The only "gotcha" with Playsound is the creation of dwFlag. It is possible to add too many flags, which will result in an overflow error.

Option Explicit

'*-------------------------------------*
'* Playsound flags: store in dwFlags   *
'*-------------------------------------*
' lpszName points to a registry entry
' Do not use SND_RESOURSE or SND_FILENAME
Private Const SND_ALIAS& = &H10000
' Playsound returns immediately
' Do not use SND_SYNC
Private Const SND_ASYNC& = &H1
' The name of a wave file.
' Do not use with SND_RESOURCE or SND_ALIAS
Private Const SND_FILENAME& = &H20000
' Unless used, the default beep will
' play if the specified resource is missing
Private Const SND_NODEFAULT& = &H2
' Fail the call & do not wait for
' a sound device if it is otherwise unavailable
Private Const SND_NOWAIT& = &H2000
' Use a resource file as the source.
' Do not use with SND_ALIAS or SND_FILENAME
Private Const SND_RESOURCE& = &H40004
' Playsound will not return until the
' specified sound has played.  Do not
' use with SND_ASYNC
Private Const SND_SYNC& = &H0

Public Enum enSound_Source
    ssFile = SND_FILENAME&
    ssRegistry = SND_ALIAS&
End Enum

' These are common sounds available from the registry
Public Const elDefault = ".Default"
Public Const elGPF = "AppGPFault"
Public Const elClose = "Close"
Public Const elEmptyRecycleBin = "EmptyRecycleBin"
Public Const elMailBeep = "MailBeep"
Public Const elMaximize = "Maximize"
Public Const elMenuCommand = "MenuCommand"
Public Const elMenuPopUp = "MenuPopup"
Public Const elMinimize = "Minimize"
Public Const elOpen = "Open"
Public Const elRestoreDown = "RestoreDown"
Public Const elRestoreUp = "RestoreUp"
Public Const elSystemAsterisk = "SystemAsterisk"
Public Const elSystemExclaimation = "SystemExclaimation"
Public Const elSystemExit = "SystemExit"
Public Const elSystemHand = "SystemHand"
Public Const elSystemQuestion = "SystemQuestion"
Public Const elSystemStart = "SystemStart"

Private Declare Function PlaySound& Lib "winmm.dll" Alias_
    "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, _
    ByVal dwFlags As Long)
' hModule is only used if SND_RESOURCE& is set and represents
' an HINSTANCE handle.  This example doesn't support playing
' from a resource file.


' Plays sounds from the registry or a disk file
' Doesn't care if the file is missing
Public Function EZPlay(ssname As String, _
    sound_source As enSound_Source) As Boolean
    
    If PlaySound(ssname, 0&, sound_source + _
        SND_ASYNC + SND_NODEFAULT) Then
        EZPlay = True
    Else
        EZPlay = False
    End If
    
End Function

Originally written by Tim Kilgore

If you enjoyed this post, subscribe for updates (it's free)

inbalanced stock

I tried your function in VS 2010 and this is what happend.

A call to PInvoke function 'SoundTEst!SoundTEst.Form1::PlaySound' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

multi-channel

so far this is great, but is there a way to make it multi channel, so several sounds can play at the same time? do let me know, if you ever read this :p
many thanks
eveshka

PlaySound

I have been trying the very handy PlaySound routine in a VB6 app that
uses very frequent BitBlt and StretchBlt graphics. Even using your
SND_ASYNC the sounds often never play until way too late and, when they
do play, they slow down the graphics. I tried various SND combinations,
DoEvent, etc. but could not find a solution. I know that sounds can
appear in the background while the graphics execute but cannot find a
way to do it. Any suggestions?
I would greatly appreciate your help.
Aloha - George Losey

question

hello, can i download free software like vb6 from internet to my computer

mais me ne conprondre pas

mais me ne conprondre pas

ll

fast play only can you give an easy way plzzz