INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

Audio

Playing sounds with VB using API by MikeBronner
Posted: 22 Jun 01

During the development of my project, I recognized the need for audio feedback for the user. The most efficient and user-friendly way to do this would be making API calls to the Windows' Sound Events in the sound scheme.

This has two reasons:
1. You don't need to make your software package unnecessarily large with audio files.
2. The user gets the feedback he or she is used to, namely the sounds as they are set in the sound scheme (this works for Desktop Themes as well as for computers without Desktop Themes).

The first thing I did was to investigate how the API call works:
- The API call should be declared in a module, so it is publicly accessible throughout the entire program.
- The API call has quite a few parameters, which come in handy in different situations (list provided below).
- The API call does not always return a value. However, if specified, it can return error codes to be used when attempting playback and something goes wrong.

The following is the PlaySound API syntax:

PlaySound lpszName, hModule, dwFlags

lpszName
========
Can be one of the following:
- Resource Key Number
- Path String
- Sound Event Name (see Sound Event table below)
hModule
=======
This can most of the time, if not always, be set to 0, as it does not require any information. It is used for the memory resource used when playing the sound.

dwFlags
=======
Tells the API call how to play the sound. A combination of flags may also be used with the OR operator. (See Flags table below.)

Sound Events Table
==================
SystemAsterisk    - Asterisk
Default           - Default Beep
EmptyRecycleBin   - when recycle bin is emptied
SystemExclamation - when windows shows a warning
SystemExit        - when Windows shuts down
Maximize          - when a program is maximized
MenuCommand       - when a menu item is clicked on
MenuPopup         - when a (sub)menu pops up
Minimize          - when a program is minimized to taskbar
MailBeep          - when email is received
Open              - when a program is opened
SystemHand        - when a critical stop occurs
AppGPFault        - when a program causes an error
SystemQuestion    - when a system question occurs
RestoreDown       - when a program is restored to normal size
RestoreUp         - when a program is restored to normal size from taskbar
SystemStart       - when Windows starts up
Close             - when program is closed
Ringout           - when (fax) call is made outbound and the line is ringing
RingIn            - incoming (fax) call

Flags Table
===========
SND_ALIAS = &H10000:     lpszName is a string identifying the name of the system event sound to play.
SND_ALIAS_ID = &H110000: lpszName is a string identifying the name of the predefined sound identifier to play.
SND_APPLICATION = &H80:  lpszName is a string identifying the application-specific event association sound to play.
SND_ASYNC = &H1:         Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
SND_FILENAME = &H20000:  lpszName is a string identifying the filename of the .wav file to play.
SND_LOOP = &H8:          Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified.
SND_MEMORY = &H4:        lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
SND_NODEFAULT = &H2:     If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success.
SND_NOSTOP = &H10:       If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead.
SND_NOWAIT = &H2000:     If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
SND_PURGE = &H40:        Stop playback of any waveform sound. lpszName must be an empty string.
SND_RESOURCE = &H4004:   lpszName is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle.
SND_SYNC = &H0:          Play the sound synchronously -- do not return until the sound has finished playing.


The following is the code used:

Module
======
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule as Long, ByVal dwFlags as Long) As Long

Public Const [i]type flag to be used here[/]
Public Const [i]type flag to be used here[/]
Public Const [i]type flag to be used here[/]
Public Const [i]type flag to be used here[/]



Form
====
Private Sub Command1_Click()
    PlaySound [i][Name or Path of sound][/i], 0 , [i][Flag listed here][/i]
End Sub


Thats basically all there is to it. Pretty straight forward once you have used it once or twice. Here's an example of a call:

PlaySound "Exclamation", 0, SND_ASYNC And SND_ALIAS
Here the Sound Exclamation Event is played on its own thread (Windows does not wait for sound to complete to continue processing).

PlaySound "C:\Mysound.wav", 0, SND_LOOP Or SND_FILENAME
Sound file is played on a nonstop loop.

PlaySound 101, 0, SND_RESOURCE Or SND_NODEFAULT
Sound contained in resource file is played. However, if this sound does not exist or returns an error, the default sound is not played.

This pretty much wraps it up. Please send me any comments, suggestions, or other feedback you might have.

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close