isXander
isXander

Reputation: 137

How to randomize sound through batch file?

I'm in the middle of this harmless program I am writing in batch but I've come across a problem in the middle of the sound payload. No matter the random variable is, it plays the same sound forever (I do intend it to loop). Here is my code:

SET /A RAND=%RANDOM% %%5
echo %RAND%
if %RAND%==0 set file="\Windows Exclamation.wav"
if %RAND%==1 set file="\tada.wav"
if %RAND%==2 set file="\Windows Critical Stop.wav"
if %RAND%==3 set file="\Windows Error.wav"
if %RAND%==4 set file="\Windows Background.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
attrib +h +s sound.vbs
start /min sound.vbs
ping localhost -n 1 >NUL
goto top

Upvotes: 0

Views: 345

Answers (2)

Io-oI
Io-oI

Reputation: 2565

The attribute +r (attrib +r command) makes files entered in read only (not possible to delete/refresh/rewrite then), so, is the first file, with the first wav selected in all times then have executed, not change because not "editable" by setting command attrib +r.

@echo on & setlocal EnableDelayedExpansion

:: This is for write a new .vbs with new sound selected on loop:
( <nul dir /a .\sound.vbs && attrib -h -s .\sound.vbs & del /q /f .\sound.vbs ) 2>nul >nul

:_loop

:: Option [1] autentic random variable limited by 0-4 range by using: Set /a _rand=%random% * 5 / 32768 
:: Option [2] take last unit from millisecons number in time by using: Set _rand=%time:~-1%

Set /a _rand=!time:~-1!

if ["!_rand!"] gtr ["4"] (

     goto :_loop

    ) else (

    :: I´m not sure the of "\" in file path, by I've change to ".\" for test the wav's files in my folder:
    if ["!_rand!"]==["0"] set _file=".\Windows Exclamation.wav"
    if ["!_rand!"]==["1"] set _file=".\tada.wav"
    if ["!_rand!"]==["2"] set _file=".\Windows Critical Stop.wav"
    if ["!_rand!"]==["3"] set _file=".\Windows Error.wav"
    if ["!_rand!"]==["4"] set _file=".\Windows Background.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 wscript /nologo .\sound.vbs

:: add attribute after wscript use the file, or file .vbs can be not will not founded by wscript:
attrib +h +s .\sound.vbs

ping localhost -n 1 >NUL

) || ( 

echo/ E R R O R :  File .vbs not genereted^^! 

)

goto :top 

Upvotes: 0

Stephan
Stephan

Reputation: 56180

With a small change, the vbs script takes an argument, so you don't have to rewrite sound.vbs for each sound:

@echo off
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = WScript.Arguments(0^)
  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

:top
SET /A RAND=%RANDOM% %%5
if %RAND%==0 set "file=C:\Windows\media\Windows Exclamation.wav"
if %RAND%==1 set "file=C:\Windows\media\tada.wav"
if %RAND%==2 set "file=C:\Windows\media\Windows Critical Stop.wav"
if %RAND%==3 set "file=C:\Windows\media\Windows Error.wav"
if %RAND%==4 set "file=C:\Windows\media\Windows Background.wav"
echo %RAND% %file%    
start /min sound.vbs "%file%"
timeout 2 >nul
goto :top

Upvotes: 1

Related Questions