Program to Play Wave File in Batch File, with Bonus for Programmers
-
In the course of investigating tools for reporting when a Windows computer is "ready for work," I discovered that there is no prepackaged way to cause a WAV file to play in a batch file or other script. Though I found [a solution](http://stackoverflow.com/questions/23313709/play-invisible-music-with-batch-file/43884629#43884629)), it was still in source code form, and was pretty Spartan. Since I have access to a good C compiler, I copied it off into a new Visual Studio project, converted the signature so that it would accept wide character inputs, and dressed it up a bit. The result is SoundOff.exe, available in binary form, from [^]](https://github.com/txwizard/SoundOff/blob/master/SoundOff\_Binary.ZIP). The source code is available under a three-clause BSD license from [GitHub - txwizard/SoundOff: System Utility to Play a Windows Sound (.WAV) File in a Script](https://github.com/txwizard/SoundOff). If you clone the project, you get not only the source for that program, but the C/C++ headers that define every entry point in the ten DLLs that come with it. Some of those will be discussed in forthcoming articles.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
In the course of investigating tools for reporting when a Windows computer is "ready for work," I discovered that there is no prepackaged way to cause a WAV file to play in a batch file or other script. Though I found [a solution](http://stackoverflow.com/questions/23313709/play-invisible-music-with-batch-file/43884629#43884629)), it was still in source code form, and was pretty Spartan. Since I have access to a good C compiler, I copied it off into a new Visual Studio project, converted the signature so that it would accept wide character inputs, and dressed it up a bit. The result is SoundOff.exe, available in binary form, from [^]](https://github.com/txwizard/SoundOff/blob/master/SoundOff\_Binary.ZIP). The source code is available under a three-clause BSD license from [GitHub - txwizard/SoundOff: System Utility to Play a Windows Sound (.WAV) File in a Script](https://github.com/txwizard/SoundOff). If you clone the project, you get not only the source for that program, but the C/C++ headers that define every entry point in the ten DLLs that come with it. Some of those will be discussed in forthcoming articles.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
Here is a sample windows script (VBS) file that will play a sound file - wav or mp3
Dim oPlayer
Set oPlayer = CreateObject("WMPlayer.OCX")' define the location / name of the file, or parameterize it for use in a bat file
' Play audio mp3 file
oPlayer.URL = "C:\Users\you\Desktop\Sounds\toilet.mp3"
'OR a wav file
oPlayer.URL = "C:\Users\you\Desktop\Sounds\applause.wav"oPlayer.controls.play
While oPlayer.playState <> 1 ' 1 = Stopped
WScript.Sleep 100
Wend' Release the audio file
oPlayer.close -
Here is a sample windows script (VBS) file that will play a sound file - wav or mp3
Dim oPlayer
Set oPlayer = CreateObject("WMPlayer.OCX")' define the location / name of the file, or parameterize it for use in a bat file
' Play audio mp3 file
oPlayer.URL = "C:\Users\you\Desktop\Sounds\toilet.mp3"
'OR a wav file
oPlayer.URL = "C:\Users\you\Desktop\Sounds\applause.wav"oPlayer.controls.play
While oPlayer.playState <> 1 ' 1 = Stopped
WScript.Sleep 100
Wend' Release the audio file
oPlayer.closeThough I could easily have implemented it as a Visual Basic script, I prefer a compiled EXE because its use does not depend upon a working Visual Basic Scripting interpreter, which is not a given in some locked down environments. Nevertheless, for fun, below is my improved implementation, which took maybe an hour to cobble together and test.
Option Explicit
' ============================================================================
'
' Script Name: SoundOff.VBS
'
' Objective: Cause a shell script to play a sound file to completion.
'
' Command Arguments: SoundFileName = Name of sound file to play
'
' Remarks: This script is a port of SountOff.exe.
'
' ----------------------------------------------------------------------------
' Revision History
' ----------------------------------------------------------------------------
'
' Date By Synopsis
' ---------- --- -------------------------------------------------------------
' 2018/05/28 DAG Script created, tested, and deployed
' ============================================================================const ARGS_NONE = 0
const ARRAY_FIRST_ELEMENT = 0const ERROR_SUCCESS = 0 ' The status code variable is initialized with the standard Windows "success" status code.
const ERROR_NEEDS_CSCRIPT = 1 ' Status code 1 is reserved for scripts run in Wscript that require CScript.
const ERROR_NO_ARGS = 2 ' Status code 2 is reserved for scripts that require one or more command line arguments to report being called without any.
const ERROR_FILE_NOT_FOUND = 3const SLEEP_TIME_100_MILISECS = 100
const WMP_PLAY_STATE_STOPPED = 1dim intExitCode : intExitCode = ERROR_SUCCESS ' Anticipate success.
dim oArgs : Set oArgs = WScript.Arguments
dim iArgLast : iArgLast = oArgs.Countif iArgLast > ARGS_NONE then
dim sFileSpec : sFileSpec = oArgs ( ARRAY_FIRST_ELEMENT )dim oFSO : set oFSO = CreateObject ( "Scripting.FileSystemObject" ) if oFSO.FileExists ( sFileSpec ) then dim oPlayer : Set oPlayer = CreateObject ( "WMPlayer.OCX" ) oPlayer.URL = sFileSpec ' Define the location / name of the file. oPlayer.controls.play