I have a clock I made and I'd like to make it an alarm clock.
Asked
Active
Viewed 8,984 times
7
-
1possible duplicate of [Play audio with python](http://stackoverflow.com/questions/260738/play-audio-with-python) – Marcelo Cantos Oct 24 '10 at 00:43
-
@Marcelo: sorry I answered, wasn't aware it was a dupe. – Rafe Kettler Oct 24 '10 at 00:51
-
@Rafe: It's no big deal; it happens all the time. Besides, it seems that not too many agree with me. ;-) – Marcelo Cantos Oct 24 '10 at 03:53
3 Answers
6
Assuming you're on Windows:
import winsound
winsound.PlaySound('alert.wav')
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()
Example using popen
, which executes a command as if you were in the terminal:
from os import popen
cmd = "mplayer alert.ogg"
popen(cmd)

Rafe Kettler
- 75,757
- 21
- 156
- 151
3
If you have the mp3play
module, and plan on playing an MP3 file, you can use this simple method.
import mp3play
filename = "C:/PATH/TO/FILE.mp3"
sound = mp3play.load(filename)
sound.play()
That code will play the entire MP3 file until it is done. If you want to only play that sound for a certain amount of time, use this:
import mp3play
import time
filename = "C:/PATH/TO/FILE.mp3"
sound = mp3play.load(filename)
time.sleep(min(30, sound.seconds())) # Plays the first 30 seconds of sound.
sound.stop()
The mp3play
module can be downloaded from the Python package index
0
On Debian/Ubuntu try this:
sudo apt-get install beep
and then:
import os
os.system('beep')

nbro
- 15,395
- 32
- 113
- 196

Tomasz Zieliński
- 16,136
- 7
- 59
- 83