Play a Sound with Python [duplicate] Play a Sound with Python [duplicate] python python

Play a Sound with Python [duplicate]


For Windows, you can use winsound. It's built in

import winsoundwinsound.PlaySound('sound.wav', winsound.SND_FILENAME)

You should be able to use ossaudiodev for linux:

from wave import open as waveOpenfrom ossaudiodev import open as ossOpens = waveOpen('tada.wav','rb')(nc,sw,fr,nf,comptype, compname) = s.getparams( )dsp = ossOpen('/dev/dsp','w')try:  from ossaudiodev import AFMT_S16_NEexcept ImportError:  from sys import byteorder  if byteorder == "little":    AFMT_S16_NE = ossaudiodev.AFMT_S16_LE  else:    AFMT_S16_NE = ossaudiodev.AFMT_S16_BEdsp.setparameters(AFMT_S16_NE, nc, fr)data = s.readframes(nf)s.close()dsp.write(data)dsp.close()

(Credit for ossaudiodev: Bill Dandreta http://mail.python.org/pipermail/python-list/2004-October/288905.html)


The Snack Sound Toolkit can play wav, au and mp3 files.

s = Sound() s.read('sound.wav') s.play()


This seems ridiculous and far fetched but you could always use Windows (or whatever OS you prefer) to manage the sound for you!

import osos.system("start C:/thepathyouwant/file")

Simple, no extensions, somewhat slow and junky, but working.