Jython: Making a simple beep on Windows
I'm working with Sikuli, which (I think) is build on Jython. I want to make a script that does a small gentle beep to attract the user's attention. (This is on Windows.)
How can I do this? I see that the winsound
module is not available on Jython.
(Note that I want to use the sound card, not 开发者_StackOverflow社区the built-in beeper.)
If its Jython, then just use any of the Java classes that play sound. There are tons of them.
from java import net from java.applet.Applet import newAudioClip from java import io url = io.File("fileName").toURL() audio = newAudioClip(url) audio.play()
import sun.audio
import java.io
inputStream = java.io.FileInputStream("test.wav")
audioStream = sun.audio.AudioStream(inputStream)
sun.audio.AudioPlayer.player.start(audioStream)
You may do the fllowing using command line:
Execute "copy con beep.txt" type [ctrl+G] as input and then [ctrl+Z] followed by [Enter] to exit
Then run "type beep.txt" and you will hear a beep.
You may place "type beep.txt" in a batch file or use Ctrl+G directly in batch (which would produce error in command line with sound)
Since we have access to the Java level in Sikuli (thanks to Jython), this should principally work:
import java.awt.Toolkit # only once per script
java.awt.Toolkit.getDefaultToolkit().beep()
Test passed on windows 7. You may get some detailed explanation here.
Since you are using Sikuli you can to the following.
Add any mediafile such as any .mp3 on the desktop of a windows machine, asociate the file to a media player. Capture the image and include:
click(pattern(desktopnoiseicon.png)
alternatley you could execute the same task with openApp(C:\noise.mp3)
Sikuli gives the ability to find numerous workarounds
in SikuluXrc2 you could even point to a URL from your code without the need of setting a bundle path
If you run Sikuli scripts from the command line, rather than through the IDE, then you can simply write the BEL
character to the console and it will beep. This also works through RDP.
Edit: on Windows 7, this will now beep through the sound card, as you asked for. On Windows XP it will beep to the internal speaker (if present).
E.g. the following beeps twice:
print("\007\007")
Since you asked for a simple Sikuli/Python script, I tested this one out myself on Windows 10:
import java.awt.Toolkit
class Main():
def __init__(self):
# Ask user input.
nValue = input('Please enter a value:')
# Run the beep definition.
self.beepAway(nValue)
def beepAway(self, nValue):
# Beep nValue number of times, after each beep wait 2 seconds.
for i in range(int(nValue)):
java.awt.Toolkit.getDefaultToolkit().beep()
wait(2)
# Run class
Main()
精彩评论