Play sound from Windows (XP to Windows 7) using python?

Does anyone have any experience playing audio (right now specifically mp3s) using python using any libs?

More details:

Using wxPython in the application (yes, I tried wx.media.MediaCtrl)

Ok, that’s what I tried.

tried code like http://www.daniweb.com/software-development/python/code/216465/play-mp3-files-via-pythons-win32com-support

Doesn't work (no sound ever)

tried wxPython MediaCtrl: it works sometimes, but recently only file playback has been working, the url is playing for a couple of seconds and then there is no sound (but the track continues, I know that the file is downloaded completely, so this is not the one that doesn't loading). I was able to fix this with a restart, then it worked for a bit, then tried to restart again, and this time it didn’t fix it, however another player that uses the Windows Media apis (C # .NET application) works fine, and so Windows Media Player So this is some error in wxWidgets libs, I think

tried to use mplayer, for example: http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/ Main problems mplayer does not like to set properties, and therefore I can never pause because if I do this and then I do not allow me to return the state back (see the code that I use here http://paste.pocoo.org/show/574269/ )

On Linux, I used gstreamer, it works after some headaches (although there are still problems), MacOS X has not been tested yet, but I'm going to try quicktime and wx.media.MediaCtrl, hoping this works)

+5
source share
1 answer

PortAudio , - , python. , :

PyAudio Python PortAudio.

""" Play a WAVE file. """

import pyaudio
import wave
import sys

chunk = 1024

if len(sys.argv) < 2:
    print "Plays a wave file.\n\n" +\
          "Usage: %s filename.wav" % sys.argv[0]
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

# open stream
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = wf.getframerate(),
                output = True)

# read data
data = wf.readframes(chunk)

# play stream
while data != '':
    stream.write(data)
    data = wf.readframes(chunk)

stream.close()
p.terminate()
+6

All Articles