Tell me more ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I am looking for a well maintained python library with allows me to play audio files on my raspberry using the standart 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. Is there any python library which has been proven as easy to use?

share|improve this question

3 Answers

up vote 2 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()

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

apt-get update
apt-get upgrade

and try again.

share|improve this answer

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!

share|improve this answer

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.

share|improve this answer
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 at 15:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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