Knowledge Base Article # Q200118

How can you play a sound file within an ATEasy application

Summary: There are several methods for playing audio files within an ATEasy application. This article discusses one of them.
One of the simplest methods for playing a sound within an ATEasy application involves using the Windows API function sndPlaySound that resides in Winmm.dll.

The sndPlaySound function plays the waveform sound specified either by a file name or by an entry in the registry or the WIN.INI file.  The function is described in detail in the Microsoft Developers Network library under the path:
- Win32 and COM Development
    - Graphics and Multimedia
        - Windows Multimedia
            - Multimedia Reference
                - Multimedia Functions
The direct link is:  MSDN sndPlaySound.  The method for using the sndPlaySound function can be applied to any of the functions described in the MSDN library.

To use the sndPlaySound function within an ATEasy application you will need to:
  • Insert the Winmm.dll library under the Libraries sub-module of any ATEasy module (System, Driver or Program) - this example uses the Driver module. You can do this by right clicking on Libraries and selecting Insert Library Below. Select the DLL tab and for the DLL File Name parameter, enter:  Winmm.dll.  Since the DLL resides in the Windows System folder, the DLL file path can be omitted.

    How can you play a sound file within an ATEasy application

    Click Insert.
  • Declare in the libraries Procedures submodule the following function:  
    sndPlaySoundA(Val String : sSoundFile, Val DWord dwFlags) : Bool

    How can you play a sound file within an ATEasy application
    The constants for the dwFlags parameter are described in the MSDN Library and the header file Mmsystem.h.  The three parameters used in this example are:
    SND_SYNC = 0x0000 ! Play synchronously (default)
    SND_ASYNC = 0x0001  ! Play asynchronously
    SND_LOOP = 0x0008  ! Loop the sound until next sndPlaySound

    The ATEasy Getting Started manual explains how to define a DLL and DLL procedures in an ATEasy application.  There are also examples for calling DLL procedures and defining Windows API's in the ATEasy Language.prj example.
Assuming you have a WAV file in the "C:\Sounds" directory called Alert.wav, then the following ATEasy code can be used to play the sound:
sSoundFile: String
  sSoundFile="C:\\Sounds\\alert.wav" ! Define the WAV file path
  sndPlaySoundA(sSoundFile,SND_SYNC) ! Play the sound file

The flags can be combined (added) to produce additional effects, such as repeatedly looping the sound for awhile before stopping:
sSoundFile: String
  sSoundFile="C:\\Sounds\\alert.wav" ! Define the WAV file path
  sndPlaySoundA(sSoundFile,SND_LOOP+SND_ASYNC) ! Loop the sound indefinately
  Delay(2000) ! Allow sound loop to play for 2 seconds
  sndPlaySoundA(sSoundFile,SND_SYNC) ! Terminate the prior sound loop

Article Date: 10/7/2008
Keywords: sound, audio, media, ATEasy, wav, winmm.dll, mmsystem.h
0 ratings | 0 out of 5