PDA

View Full Version : Python: Playing sound


mevets
October 4th, 2007, 10:47 PM
I am making an alarm clock in python. This requires me to play a sound file just once. What is the easiest method to play a wav or ogg? I looked into the solutions and I saw people recommend pyMedia and ossaudiodev. Is pyGame a good idea?

Also, how would I even start to import something like pyGame? I know to add import pygame, but I do not know where I should put all the pygame code.

Billy the Kid
October 5th, 2007, 02:28 AM
You can browse Pygame's music API here:
http://www.pygame.org/docs/ref/music.html,
The most simple way to play a sound file with this module might be:
import pygame
pygame.init()

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

mevets
October 5th, 2007, 10:34 AM
Where is the lib folder for python so that I can import pyGame? I looked around found /var/lib/python2.5. Is that it?

bobbocanfly
October 5th, 2007, 02:23 PM
You shouldnt need a path to the pygame lib, typing

import pygame

should do it.

moephan
October 5th, 2007, 10:39 PM
For something this simple, assuming you have mplayer installed, you might consider just using popen to send mplayer the file to play. That should be quite easy and won't require you to install pygame or other libraries.

Cheers, Rick

Unterseeboot_234
October 6th, 2007, 03:57 PM
I was surprised to learn the 64-bit python excludes the python sound libraries. I have not investigated my BIOS to see if the mobo onboard speaker is disabled, but

in .msw32 python environment ==> bell() is the command.

on some python installations making a file ==> print_beep.py

print '\a' # the old ASCII bell, that I bet you thought you'd never use

and run python print_beep.py

or, you can type in an NT console, at the python prompt
>>> print '\a'

all of that works, but it appears to be WIN specific

This link shows a rather lengthy built module to play a single signal through OSS. This module does work for me through my sound card / headphones.

http://paste.pocoo.org/show/316/

And, finally, pygame is a separate org, separate development. The py modules are totally pyCool, e.g. you can micro-manage a CD playback. You download the source, or apt-get install it or Synaptic. I built from source so I could have it linked with Python2.5.

pyGame (http://www.pygame.org/news.html)

mevets
October 8th, 2007, 11:47 PM
a simple impot pygame does work but I had to

sudo apt-get install python-pygame

first

cime
October 9th, 2007, 08:19 AM
Maybe you should look after Pyxine.
http://pyxine.sourceforge.net/

Example:
>>> import pyxine
>>> xine = pyxine.Xine()
>>> stream = xine.stream_new()
>>> stream.open("music.mp3")
>>> stream.play()