Skip to main content

bash

Raspberry Pi audio playback from Python

Want to play audio on your Raspberry Pi running Raspbian, while logged in to a non-root user account? Me, too!

Seems like such a simple task.

Consider the following code snippet using PyGame:

import time
import pygame
pygame.mixer.init()
pygame.mixer.music.load("testfile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    time.sleep(.25)

Let's try it:

$ python soundtest.py
Traceback (most recent call last):
  File "./soundtest.py", line 3, in <module>
    pygame.mixer.init()
pygame.error: No available audio device
Syndicate content