47

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?

Peter Mortensen
  • 2,014
  • 2
  • 15
  • 17
Stein
  • 1,758
  • 3
  • 14
  • 13
  • Proven as easy to use is kind of relative, but I posted an alternative to pygame to similar question on StackOverflow [over here](http://stackoverflow.com/a/25899180/2801707). 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 00:30

7 Answers7

39

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.

xxmbabanexx
  • 3,248
  • 7
  • 33
  • 56
  • 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 09: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 '18 at 19:14
11

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!

user1616681
  • 211
  • 1
  • 2
  • 1
    This is really neat actually. [This adafruit tutorial](http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/overview) talks about playing sound but not stopping it. – gideon Jun 25 '13 at 08:09
7

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.

Bex
  • 2,919
  • 3
  • 25
  • 34
Brandorf
  • 171
  • 2
3

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.

Bex
  • 2,919
  • 3
  • 25
  • 34
0

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

mrpi64
  • 11
0

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.

Jacobm001
  • 11,878
  • 7
  • 46
  • 56
Dalen
  • 201
  • 2
  • 6
0

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

tonyslowdown
  • 151
  • 1
  • 3