I am looking for a well maintained Python library with allows me to play audio files on my Raspberry Pi using the standard audio output.

So far I've tried several, but none of them seem to work. Although pyglet works on my regular computer fine, it causes an error on the Raspberry Pi. Is there a Python library which has been proven as easy to use?

  • Proven as easy to use is kind of relative, but I posted an alternative to pygame to similar question on StackOverflow over here. Basically that alternative is vlc.py (the libVLC Python module, which is very well maintained).
    – Ben
    Sep 17, 2014 at 20:02
  • A question further down says that Pyglet works on RPi. Can you update with your error? Apr 15, 2017 at 0:30

7 Answers 7


I recommend the widely popular Pygame. I may be wrong, but I believe that it is pre-installed on the Pi. You can use the Pygame Mixer Music Module to play audio files. I have included some example code below.

Assuming that we have an audio file called myFile.wav.

import pygame
while pygame.mixer.music.get_busy() == True:

NOTE: If this fails, please go to the terminal and update your system with

apt-get update
apt-get upgrade

and try again.

  • 2
    If you have more than one sound to play (which is likely) then it's better to create a pygame.mixer.Sound object for each one, then you can keep the definitions of the sound filenames in one place.
    – francis
    Jan 21, 2014 at 9:42
  • The is what I want, but my motherboard sound doesn't have a driver so I use a USB sound card. It plays espeak just fine, but not this.
    – SDsolar
    Jan 15, 2018 at 19:14

I needed a script to play a song from thirty seconds in in the background whilst responding to other user input. I then wanted it to end the song on some event.

I don't suppose it's particularly elegant, but I opened a pipe to a background MPlayer process.

import subprocess
player = subprocess.Popen(["mplayer", "song.mp3", "-ss", "30"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Then, when I wanted to terminate the MPlayer process, I simply wrote "q" for quit to the pipe.


Look at MPlayer documentation for all sorts of commands you can pass in this way to control playback.

Hopefully that's somewhat helpful!


Another option is to use mpg321 and invoke it from the command line.

apt-get install mpg321

Then in Python:

import os

os.system('mpg321 foo.mp3 &')

Pygame is almost certainly more robust, but it depends, I suppose, on what your needs are.

  • 2
    I thought about this myself, but I thought it wasn't very elegant, because it make pausing, volume control etc. a lot harder.
    – Stein
    Apr 18, 2013 at 15:10
  • This is awfully slow. Jul 26, 2014 at 9:58
  • subprocess is said to be the preferred method of executing commands. stackoverflow.com/a/450329
    – Nav
    Oct 28, 2020 at 3:58

I would recommend pyglet rather than pygame if you don't need graphics. In my view it is simpler, more elegant, and better maintained. Then again, I expect it might be a matter of taste.

  • 1
    So how do you use it?
    – SDsolar
    Jan 15, 2018 at 19:04
  • 1
    pyglet only seems to run when a display is available; running it remotely or as a daemon only for the purpose of playing sounds doesn't work.
    – oh7lzb
    Nov 20, 2018 at 19:39

Pygame gives you a nice and easy way to load and play sounds, on a range of channels.


You may use a number of external players through pipes. However, you will nearly always experience a loss of speed. There is also a problem with playback control.

I recommend you pyaudio, it works fine.

If you want to play compressed files as well, I'll be so free to point you to my module decoder.py. You can find it on PyPI.

It also uses subprocess, but calls decoders instead of players. They do not use a lot of CPU, and some of them know how to use hardware acceleration. You can then use pyaudio or something to output the raw audio data. If you want, you can even use pygame by utilizing pygame.sndarray. In that way you can play more formats than pygame supports.

I tried decoder.py in PyQT GUI environment, with pyaudio and also using aplay as an external player. That was on Raspberry Pi B.

Results were pretty satisfying in both cases.

The simplicity can be questioned.

decoder.py itself is very simple, but you have to combine it with other libs and that increases code lines.

There is an example on how to play files included with decoder.py.


I'm running OSMC (Debian) and I got sound to play through my TV speakers, which is connected by HDMI. https://youtu.be/p2ljmTE67gs

My method was based on the above solution posted by @xxmbabanexx as well as here. Basically, I had to add dtparam=audio=on to /boot/config.txt

  • I think the TS meant the 3.5 mm jack when referring to the "standard audio output". Does this work for the 3.5 jack as well?
    – Bex
    Jan 4, 2016 at 18:28
  • Oh I see. Hmm it probably won't, though I haven't tried it. Jan 5, 2016 at 1:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.