我将如何在 python 中播放警报声? [英] How would I go about playing an alarm sound in python?

查看:62
本文介绍了我将如何在 python 中播放警报声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时钟,我想把它做成闹钟.

I have a clock I made and I'd like to make it an alarm clock.

推荐答案

假设您使用的是 Windows:

Assuming you're on Windows:

import winsound
winsound.PlaySound('alert.wav')

如果您使用的是 Linux(或我相信是 Mac OS X),您可以使用 pygame 或使用 popen 调用 Linux 程序(如 mplayer).pygame 示例:

If you're on Linux (or Mac OS X I believe), you can either use pygame or call a Linux program (like mplayer) using popen. pygame example:

import pygame
pygame.init()

pygame.mixer.music.load("alert.ogg")
pygame.mixer.music.play()
pygame.event.wait()

使用 popen 的示例,它会像在终端中一样执行命令:

Example using popen, which executes a command as if you were in the terminal:

from os import popen
cmd = "mplayer alert.ogg"
popen(cmd)

这篇关于我将如何在 python 中播放警报声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆