如何通过批处理文件随机化声音?

时间:2018-12-09 12:21:51

标签: batch-file

我正在批量编写这个无害的程序,但是在声音有效负载的中间遇到了一个问题。无论随机变量是什么,它都会永远播放相同的声音(我确实希望它循环播放)。 这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

只需稍作更改,vbs脚本就会接受一个参数,因此您不必为每种声音重写sound.vbs

@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

答案 1 :(得分:0)

+ r属性(attrib + r命令)使文件进入只读状态(这样就无法删除/刷新/重写),因此,它是第一个文件,始终选择了第一个wav,然后执行了该文件,不会更改,因为无法通过设置命令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