代码完成时发出声音警报

时间:2013-05-15 19:04:58

标签: python alarm audio

我的情况是我的代码需要很长时间才能运行,我不想一直盯着它,但想知道它什么时候完成。

如何完成(Python)代码声音的“闹钟”?我正在考虑让它在到达代码末尾时播放.wav文件...

这甚至是一个可行的想法吗? 如果是这样,我怎么能这样做?

11 个答案:

答案 0 :(得分:169)

在Windows上

import winsound
duration = 1000  # milliseconds
freq = 440  # Hz
winsound.Beep(freq, duration)

其中freq是以Hz为单位的频率,持续时间以毫秒为单位。

在Linux和Mac上

import os
duration = 1  # seconds
freq = 440  # Hz
os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))

要使用此示例,您必须安装sox

在Debian / Ubuntu / Linux Mint上,在终端中运行:

sudo apt install sox

在Mac上,在终端中运行(使用macports):

sudo port install sox

Mac上的演讲

import os
os.system('say "your program has finished"')

Linux上的演讲

import os
os.system('spd-say "your program has finished"')

您需要在Ubuntu中安装speech-dispatcher包(或其他发行版上的相应包):

sudo apt install speech-dispatcher

答案 1 :(得分:23)

这个似乎适用于Windows和Linux *(from this question):

def beep():
    print "\a"
beep()

在Windows中,可以放在最后:

import winsound
winsound.Beep(500,1000)

where 500 is the frequency in Herz
      1000 is the duration in miliseconds

*:要在Linux上工作,您可能需要执行以下操作(来自QO的评论):

  • 在终端中输入'cd /etc/modprobe.d'然后'gksudo gedit blacklist.conf'
  • 评论“blacklist pcspkr”,然后重新启动
  • 还要检查终端首选项是否已选中“终端铃声”。

答案 2 :(得分:23)

 print('\007')

播放铃声

答案 3 :(得分:17)

可以使用ubuntu语音调度程序:

import subprocess
subprocess.call(['speech-dispatcher'])        #start speech dispatcher
subprocess.call(['spd-say', '"your process has finished"'])

答案 4 :(得分:8)

Kuchi's answer在OS X Yosemite(10.10.1)上没有为我工作。我找到了afplay命令(here),您可以从Python调用它。无论终端音响铃是否已启用且没有第三方库,这都可以正常工作。

import os
os.system('afplay /System/Library/Sounds/Sosumi.aiff')

答案 5 :(得分:6)

请参阅:Python Sound ("Bell")
当我想要做同样的事情时,这对我有所帮助 所有积分均转至gbc

引用:

你试过了吗?

import sys
sys.stdout.write('\a')
sys.stdout.flush()

这适用于我在Mac OS 10.5上

实际上,我认为您最初的尝试也可以进行一些修改:

print('\a')

(你只需要字符序列周围的单引号)。

答案 6 :(得分:5)

我假设您想要标准系统铃声,并且不想关注频率和持续时间等,您只需要标准的窗铃。

import winsound
winsound.MessageBeep()

答案 7 :(得分:4)

可以通过以下代码完成:

import time
time.sleep(10)   #Set the time
for x in range(60):  
    time.sleep(1)
    print('\a')

答案 8 :(得分:3)

为什么要使用python?您可能忘记将其删除并将其检入存储库。只需使用&&运行你的python命令和另一个运行警报的命令。

SELECT COUNT(*) AS Rows, rion FROM rdb_users
WHERE rion AND user_type = 'agent'
GROUP BY rion ORDER BY rion

或将函数放入.bashrc中。我在这里使用apython但你可以覆盖' python'

python myscript.py && 
    notify-send 'Alert' 'Your task is complete' && 
    paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga

答案 9 :(得分:2)

import subprocess

subprocess.call(['D:\greensoft\TTPlayer\TTPlayer.exe', "E:\stridevampaclip.mp3"])

答案 10 :(得分:0)

您的问题还要多

我使用gTTS包从文本生成音频,然后在学习网络爬网并创建了Coursera下载器(仅免费课程)时使用Playsound播放音频。

text2speech = gTTS("Your course " + course_name +
                                " is downloaded to " + downloads + ". Check it fast.")
text2speech.save("temp.mp3")
winsound.Beep(2500, 1000)
playsound("temp.mp3")