如何批量随机化声音?(不是bash)

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (25)

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

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
提问于
用户回答回答于

通过一个小的更改,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
用户回答回答于

属性+ r使文件只读(不可能删除/刷新/重写),因此,是第一个文件,第一个wav一直被选中然后执行,而不是因为不是“可编辑”而设置命令“attrib” + R”。@echo on && setlocal enableextensions 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 

扫码关注云+社区

领取腾讯云代金券