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 '14 at 20:02
  • A question further down says that Pyglet works on RPi. Can you update with your error? – Brian Bulkowski Apr 15 '17 at 0:30
up vote 27 down vote accepted

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
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue

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 '14 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 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.

player.stdin.write("q")

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.

  • 1
    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 '13 at 15:10
  • This is awfully slow. – Alex Jul 26 '14 at 9: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.

  • So how do you use it? – SDsolar Jan 15 at 19:04

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 '16 at 18:28
  • Oh I see. Hmm it probably won't, though I haven't tried it. – jjinking Jan 5 '16 at 1:31

protected by Ghanima Jul 25 '16 at 22:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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