petercaunt
Posts: 2
Joined: Tue Jan 29, 2013 4:08 pm

pygame sound

Tue Jan 29, 2013 4:16 pm

I am new to python and have been trying to get it to play an mp3.
I tried the code below

import pygame, sys, time
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300))
pygame.display.set_caption('My MP3')
pygame.mixer.init()
pygame.mixer.music.load('Songbird.mp3')
pygame.mixer.music.set_volume(0.9)
pygame.mixer.music.play(0,0.0)
while True:
time.sleep(1)
pygame.display.set_caption('My MP3')
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if pygame.mixer.music.get_busy():
pygame.display.set_caption('Busy')

pygame.display.update()

This works fine on my windows machine. On the Pi it runs telling me it is busy for the right length of time for the mp3, but I cannot seem to hear anything from the HDMI output or the audio out.
I can type in the terminal - mpg123 <filename> - and this plays OK through the HDMI

How do I get python to select the HDMI output?

petercaunt
Posts: 2
Joined: Tue Jan 29, 2013 4:08 pm

Re: pygame sound

Wed Jan 30, 2013 8:05 am

Duh!!

I found http://www.pygame.org/docs/

This gives all sort of information/tutorials about pygame.

As I understand it pygame.init initialises everything including the mixer.

What is needed is to do a - pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffersize=4096) - before the pygame.init as the default frequency is 22050. Then remove the original call to pygame.init

This means that it uses the above parameters when pygame.init initialises the mixer. Since my mp3 was originally sampled at 44100, the mixer then understands it instead of trying to interpret it as a frequency of 22050. Not sure why it works on WIndows, but after the mods my mp3 plays fine through the HDMI on the Pi

:P

Return to “Python”