Playing Music with Python

Wand to play a sound in Python but don’t know how? It's your problem No problems. I’ll show you some ways. Here is the list of popular Python libraries used to make noise with short descriptions and usage examples.


A cross-platform windowing and multimedia library for Python. Among it’s features: no external dependencies or installation requirements; can optionally use AVbin to play back audio formats such as MP3, OGG/Vorbis and WMA. Distributed under BSD open-source license.

#!/usr/bin/env python
import pyglet
song ='thesong.ogg')


Pygame is a set of Python modules designed for writing games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Pygame is highly portable and runs on nearly every platform and operating system. Pygame itself has been downloaded millions of times, and has had millions of visits to its website. Distributed under GPL.

#!/usr/bin/env python
import pygame
song = pygame.mixer.Sound('thesong.ogg')
clock = pygame.time.Clock()
while True:

GStreamer Python Bindings

GStreamer is a pipeline-based multimedia framework written in the C programming language with the type system based on GObject.

GStreamer allows a programmer to create a variety of media-handling components, including simple audio playback, audio and video playback, recording, streaming and editing. The pipeline design serves as a base to create many types of multimedia applications such asvideo editors, streaming media broadcasters and media players. Distributed under LGPL.

#!/usr/bin/env python
import pygst
import gst
import gobject
import os

mainloop = gobject.MainLoop()
pl = gst.element_factory_make("playbin", "player")


PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms.

PyAudio is still super-duper alpha quality. It has run on GNU/Linux 2.6, Microsoft Windows 7/XP, and Apple Mac OS X 10.5+—but it could use more testing.

I couldn’t get how to play ogg files, here is an example for wav.

#!/usr/bin/env python
import pyaudio
import wave

chunk = 1024
wf ='thesong.wav', 'rb')
p = pyaudio.PyAudio()

stream =
    format = p.get_format_from_width(wf.getsampwidth()),
    channels = wf.getnchannels(),
    rate = wf.getframerate(),
    output = True)
data = wf.readframes(chunk)

while data != '':
    data = wf.readframes(chunk)



PyMedia is a Python module for wav, mp3, ogg, avi, divx, dvd, cdda etc files manipulations. It allows you to parse, demutiplex, multiplex, decode and encode all supported formats. It can be compiled for Windows, Linux and cygwin.

#!/usr/bin/env python
import as acodec
import as sound
import pymedia.muxer as muxer

file_name = 'thesong.ogg'
dm = muxer.Demuxer(str.split(file_name, '.')[-1].lower())
f = open(file_name, 'rb')
snd = dec = None
s = 32000 )
while len(s):
    frames = dm.parse(s)
    if frames:
        for fr in frames:
            if dec == None:
                dec = acodec.Decoder(dm.streams[fr[0]])

            r = dec.decode(fr[1])
            if r and
                if snd == None:
                    snd = sound.Output(
                data =
    s =

while snd.isPlaying():

Looks like Pyglet is the obvious choise.

Share this page on Twitter or Reddit