Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows (XP to Windows 7) audio playback with python?

Anyone have experience playing audio (right now specifically mp3s) with python using a any libs?

Details:

Use is in a wxPython app (yes I have tried wx.media.MediaCtrl)

Ok now is here things I have tried.

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

Doesn't work (no audio what so ever)

tried wxPython MediaCtrl: Works sometimes but recently only file playback works, urls play for a couple seconds and then no audio (but track keeps going, I know the file is downloaded fully also so it isn't the media not being downloaded). I was able to fix this with restarting then it worked for a bit then broke, tried restarting again and this time that didn't fix it, however other player that use windows media apis (a C# .NET app) work just fine and so does Windows Media Player. So it is some bug in the wxWidgets libs I guess

tried using mplayer, example: http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/ major problems mplayer doesn't like setting properties and so I can't ever pause because if I do it then won't let me set the state back to play (see code I use here http://paste.pocoo.org/show/574269/ )

On Linux I have used gstreamer, works after some headaches (though still has its problems also), MacOS X hasn't been tested yet but I am going to try quicktime and wx.media.MediaCtrl hoping that works)

like image 972
Zimm3r Avatar asked Mar 31 '12 23:03

Zimm3r


1 Answers

I've used PortAudio in a couple of projects, which is a free-cross-platform-open-source-audio library, but never with python. Don't worry, there are bindings for it:

PyAudio provides Python bindings for 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()
like image 64
karlphillip Avatar answered Nov 15 '22 05:11

karlphillip