Function Reference


Plays a sound specified by the given file name, resource, or system event

#include <WinAPIMisc.au3>
_WinAPI_PlaySound ( $sSound [, $iFlags = $SND_SYSTEM_NOSTOP [, $hInstance = 0]] )


$sSound The string that specifies the sound to play. The maximum length is 255 characters. If $sSound is
empty, any currently playing waveform sound is stopped.
$iFlags [optional] The flags for sound playing. This parameter can be one or more of the following values.

Windows Vista or later

Three flags ($SND_ALIAS, $SND_FILENAME, and $SND_RESOURCE) determine whether the name is interpreted
as an alias for a system event, a file name, or a resource identifier. If none of these flags are
specified, _WinAPI_PlaySound() searches the registry or the WIN.INI file for an association with
the specified sound name. If an association is found, the sound event is played. If no association
is found in the registry, the name is interpreted as a file name.

If the $SND_ALIAS_ID flag is specified in $iFlags, the $sSound parameter must be one of the
$SND_ALIAS_* values.
(See MSDN for more information)
$hInstance [optional] Handle to the executable file that contains the resource to be loaded. If $iFlags does not
contain the $SND_RESOURCE, this parameter will be ignored.

Return Value

Success: True
Failure: False

See Also

Search PlaySound in MSDN Library.


#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIMisc.au3>


Func _Example()
        Local Const $sWav = @ScriptDir & '\Extras\Airplane.wav'

        ; Read Airplane.wav to memory
        Local $dWav = FileRead($sWav)
        If @error Then
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unable to read "' & $sWav & '"')
        Local $tWav = DllStructCreate('byte[' & BinaryLen($dWav) & ']')
        DllStructSetData($tWav, 1, $dWav)
        Local $pWav = DllStructGetPtr($tWav)

        ; Create GUI
        Local $hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 200, 200)
        #forceref $hForm
        Local $idButton = GUICtrlCreateButton('Play', 70, 70, 60, 60)

        Local $bPlay = False
        While 1
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                        Case $idButton
                                $bPlay = Not $bPlay
                                If $bPlay Then
                                        _WinAPI_PlaySound($pWav, BitOR($SND_ASYNC, $SND_LOOP, $SND_MEMORY))
;~                                      _WinAPI_PlaySound($sWav, BitOR($SND_ASYNC, $SND_LOOP))
                                        GUICtrlSetData($idButton, 'Stop')
                                        GUICtrlSetData($idButton, 'Play')
EndFunc   ;==>_Example