Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

windows - How to start a system "beep", from the built-in pc speaker, using a batch file?

I have written a batch script in an interactive mode, for making some tasks.

Sometimes, These tasks takes a long time to be finished, and then the batch asks if the user wants to go on to the next task, or back to the Batch's Main Menu or... etc

Now, what I want to do, is to add an "Interactive Alarm" command, that sounds a small short beep (Ex: Like the one when we turn on our PCs), to alert the batch user for new questions .

I don't know if this is possible or not, but the most important thing for me, NOT to use a GUI application like WMP or so.. I just want to do this from the Background, even If that beep has to be made from the free speaker, or by using a Third-Party CLI Application (Btw, I've Cygwin installed on my Win7-x64) .

Please note that, I will add that alarm command exactly before the interactive questions, waiting for user's answer to get to the next stage, so I can't just finish the batch, by making a real error beep !

So, would somebody please tell me how to do this ?

Appreciate your help :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
rundll32.exe Kernel32.dll,Beep 750,300

or

rundll32.exe cmdext.dll,MessageBeepStub

or

rundll32 user32.dll,MessageBeep

With rundll functions you''ll no need special symbols like ^G. With the first method you can also set the frequency and the time you want to beep.


UPDATE

another options are:

powershell "[console]::beep(500,300)"

or using systemSounds.bat

call systemsounds.bat  beep

rundll32.exe Kernel32.dll,Beep 750,300 - this does not work well on modern windows systems as rundll32 no longer accepts integer values through command line and this will play the beep with the default values which is too long (and frequency is irritating)

As the capability of beeping depends also on the mainboard and if the mainboard has a system speaker - which more and more becomes a rarity - the systems more and more depend on the normal speakers. So an option is to play sound through them. Here are some options:

Using the speaking capabilities of the SAPI.SpVoice:

mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('beep')))"

Here this is wrapped in a batch file and the words can be passed as an argument

SAPI.SpVoice can be used for playing wav files and you have some coming with the windows installation.You can use this script:

spplayer.bat  "C:WindowsMediaWindows Navigation Start.wav"

Another option - using the windows media player active-x objects to play a sound. On windows XP it was not installed by default but I think for the newer windows versions it is. It also can play mp3 files:

call mediarunner.bat "C:WindowsMediaRing03.wav"

And one a little bit obscure - using the <bgsound> tag from internet explorer (which also can play mp3 files). Here's the script:

call soundplayer.bat "C:WindowsMediaada.wav"

And here's a way to use the BEL character to produce sound with easy to copy-paste code (I've called it a beeper.bat):

@echo off
setlocal
::Define a Linefeed variable
(set LF=^
%=-=%
)
  
for /f eol^=^%LF%%LF%^ delims^= %%A in (
   'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x07"'
) do echo(%%A

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...