Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Product Lifecycle
  3. Free Tools
  4. Program to Play Wave File in Batch File, with Bonus for Programmers

Program to Play Wave File in Batch File, with Bonus for Programmers

Scheduled Pinned Locked Moved Free Tools
toolscsharpc++visual-studiocom
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David A Gray
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • D David A Gray

      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

      P Offline
      P Offline
      Pat OBrien
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • P Pat OBrien

        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

        D Offline
        D Offline
        David A Gray
        wrote on last edited by
        #3

        Though 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 = 0

        const 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 = 3

        const SLEEP_TIME_100_MILISECS = 100
        const WMP_PLAY_STATE_STOPPED = 1

        dim intExitCode : intExitCode = ERROR_SUCCESS ' Anticipate success.
        dim oArgs : Set oArgs = WScript.Arguments
        dim iArgLast : iArgLast = oArgs.Count

        if 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
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups