Home > python > Play sound

Play sound

In Python, for playing sound there are several options (see http://stackoverflow.com/search?q=python+play+sound). However, under Ubuntu I didn’t have much success with portable solutions :( I got most luck with wx.Sound('sound.wav') which used to work for a while but then it stopped… So I’ve decided to just call mplayer to do the job.

#!/usr/bin/env python

import os

SOUND = 'Gong.wav'

command = 'mplayer %s 1>/dev/null 2>&1' % SOUND

Not too elegant but at least it works…


If you want to launch mplayer in the background without any verbosity, here is how to do that:

mplayer music.mp3 &>/dev/null </dev/null &

Update (20101016): I forgot to mention that with the help of mplayer, you can play videos too. Just pass an AVI instead of an MP3, for instance.

Categories: python Tags: , , , , ,
  1. yves
    September 29, 2010 at 02:37

    Here is a Linux/Windows solution that should do the trick:

    p,s = map(__import__,["pyglet","sys"])
    p.media.load(s.argv[1], streaming=False).play()

    Tested with “Ozone – Dragostea din tei (original romanian).mp3” … ;-)

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: