Invisible batch script to play a sound file


If you need to play a sound file without opening your usual audio player and living it open after playback is finished, you can use this script. Copy and paste it in a plain text editor such as the Windows Notepad, and save it as whatever.bat

Notice the address of the audio file in the script: you don’t need to enclose it in brackets even if it contains spaces.

@echo off
set file=C:\Sounds Of Nature\waterdrop.wav
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.Controls.play
  echo do while Sound.currentmedia.duration = 0
  echo wscript.sleep 100
  echo loop
  echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs

If you prefer to play a random sound file, you can use this variation of the script:

@echo off
Set /a MYSOUND=(%Random% %%3)+1
IF %MYSOUND%==1 set file=C:\whateverfolder\whatever1.wav
IF %MYSOUND%==2 set file=C:\whateverfolder\whatever2.wav
IF %MYSOUND%==3 set file=C:\whateverfolder\whatever3.wav
(echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs

Or, if you prefer to replay / “loop” the sound file, use this variation:

@echo off
set file=C:\Sounds Of Nature\waterdrop.wav
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.settings.setMode "loop", True
  echo Sound.Controls.play
  echo While Sound.playState ^<^> 1
  echo wscript.sleep 100
  echo Wend
)>sound.vbs
start /min sound.vbs
exit /b