tags:

views:

4758

answers:

6

How can i play audio (it would be like a 1sec sound) from a python script?

It would be best if it was platform independent, but first of it needs to work on a mac.

I know i could just execute the 'afplay file.mp3' command from within python, but is it possible to do it in raw python? I would also be better if it diddnt rely on external libraries.

+5  A: 

You can find information about Python audio here: http://wiki.python.org/moin/Audio/

It doesn't look like it can play .mp3 files without external libraries. You could either convert your .mp3 file to a .wav or other format, or use a library like PyMedia.

yjerem
+6  A: 

Your best bet is probably to use pygame/SDL. It's an external library, but it has great support across platforms.

TML
+2  A: 

Also most of the modern GUI toolkits (wx, QT ...) have some sort of multimedia support

lazy1
+1  A: 

You can't do this without a nonstandard library.

for windows users who end up in this thread, try pythonwin. PyGame has some sound support. For hardware accelerated game audio, you'll probably need to call OpenAL or similar through ctypes.

Dustin Getz
+2  A: 

Pyglet has the ability to play back audio through an external library called AVbin. Pyglet is a ctypes wrapper around native system calls on each platform it supports. Unfortunately, I don't think anything in the standard library will play audio back.

technomalogical
+1  A: 

If you need portable Python audio library try PyAudio. It certainly has a mac port.

As for mp3 files: it's certainly doable in "raw" Python, only I'm afraid you'd have to code everything yourself :). If you can afford some external library I've found some PyAudio - PyLame sample here.

Grzegorz Gacek