What's the easiest way to play a sound file (.wav) in Python? By easiest I mean both most platform independent and requiring the least dependencies. pygame is certainly an option, but it seems overkill for just sound.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- Can we recover audio from MFCC coefficients?
- How to get the background from multiple images by
- Evil ctypes hack in python
wxPython has support for playing wav files on Windows and Unix - I am not sure if this includes Macs. However it only support wav files as far as I can tell - it does not support other common formats such as mp3 or ogg.
I just released a simple python wrapper around sox that will play a sound with Python. It's very easy to install as you need Python 2.6 or greater, sox (easy to get binaries for most architectures) and the wrapper ( https://github.com/standarddeviant/sound4python ). If you don't have sox, go here: http://sourceforge.net/projects/sox/files/sox/
You would play audio with it by:
Keep in mind, the only parts actually involved in playing audio are just these:
I like pygame, and the command below should work:
but it doesn't on either of my computers, and there is limited help on the subject out there. edit: I figured out why the pygame sound isn't working for me, it's not loading most sounds correctly, the 'length' attribute is ~0.0002 when I load them. maybe loading them using something other than mygame will get it morking more generally.
with pyglet I'm getting a resource not found error Using the above example, wigh both relative and full paths to the files.
using
pyglet.media.load()
instead ofpyglet.resource.media()
lets me load the files.but
sound.play()
only plays the first fraction of a second of the file, unless I runpyglet.app.run()
which blocks everything else...After the play() command add a delay of say 10 secs or so, it'll work
This also plays .mp3 files.
This seems ridiculous and far fetched but you could always use Windows (or whatever OS you prefer) to manage the sound for you!
Simple, no extensions, somewhat slow and junky, but working.
Definitely use Pyglet for this. It's kind of a large package, but it is pure python with no extension modules. That will definitely be the easiest for deployment. It's also got great format and codec support.