How can I play with the python alarm?

I have a watch that I made, and I would like to make it an alarm clock.

+5
source share
3 answers

Assuming you are on Windows:

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

If you are running Linux (or Mac OS X, I believe), you can use pygame or call a Linux program (e.g. mplayer) with popen. Pygame example:

import pygame
pygame.init()

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

An example of a use popenthat executes a command as if you were in a terminal:

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

If you have an MP3Play module and are planning 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()

MP # . , :

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()

:

http://pypi.python.org/pypi/mp3play/0.1.15#downloads  
+1

On Debian / Ubuntu, try the following:

sudo apt-get install beep

and then:

import os
os.system('beep')
0
source

All Articles