Pygame sound python not working


#1

I’ve tried to play a .wav in a Python script on a Grove Pi. I’ve set the correct amixer settings: amixer cset numid=3 1. When I start my script, nothing happens. No errors, but also no sound.
My script looks like this:

import pygame
pygame.init()
sound = pygame.mixer.Sound("crazy.wav)
sound.play()

When I play the sound on the command line (sudo aplay crazy.wav) I hear a nice sound.
Who can help me out?


#2

Hello,
Is it a mistake when you copied the code here?
pygame.mixer.Sound("crazy.wav) is missing the ending quote. It should end with ") , not just )

Hopefully it’s that easy!

Cleo


#3

No, it’s not that easy. I just typed it wrong. Sound is still not working!


#4

Is this the complete code that you posted here?
If so, then the sound doesn’t play simply because the program has ended, and pygame is just like that. Program ends, therefore kill every thing.

Add at the top
import time

Then the last line would be:
time.sleep(60)

That gives your audio 60 seconds to play before getting killed


#5

Thx, but the time.sleep function doesn’t solve the problem. I now hear noise for 60 seconds, but no wav file. Other suggestions?


#6

do you hear your file if you type in
omxplayer crazy.wav

Also you most likely need pygame.mixer.init()

This code works for me:

import pygame
import time
pygame.init()
pygame.mixer.init()
mysound = pygame.mixer.Sound("crazy.wav")
mysound.play()
time.sleep(60)


#7

No, omxplayer is not working either. What works is: aplay crazy.wav. I first have to set the output: amixer cset numid=3 1. That sets the sound output to the jack (phones or speakers). Then my sound is playing as it should do. The crazy thing is, when I play a Python game (also installed on my Jessie Pi image), sound is working fine. I ran trough the Pi games code, but didn’t discover anything special.


#8

Could you check if you have

dtparam=audio=on

in /boot/config.txt

If it’s missing, add it on its own line (anywhere in the file, but as a standalone line) and then reboot


#9

Yes, line is in boot/config.txt.


#10

Just found out: omxplayer -o local sound.wav forces sound to play trough speakers (jack).That works.


#11

Another question, just to be sure.
Are you starting the python program from the same directory as the one that contains crazy.wav?

I just tried my little program, and if the file is not found, I simply get 60 seconds of noise, no error message. Pygame doesn’t seem to think it needs to give out a warning…


#12

Yes, it\s in the same directory. I’ve placed my little program and the sound (.wav) in the /home/pi/Downloads dir. With and without the full path gives the same no-sound-but-noise issue. It’s really very strange, since the python games in my Jessie Dexter distribution of Raspbian are playing sounds. I tried to set the initial sound on my Pi to jack trough sudo raspi-config. No effect.


#13

At this poing I’m out of idea. This is a pygame puzzle, and I’m not that familiar with pygame.
Are you running Raspbian for Robots or straight Jessie from the Foundation?


#14

Hi Cleo,

I’ve found out what’s wrong. Just a very, very silly mistake. Just forgot the brackets () somewhere. Thank you for thinking with me!


#15

Great!! And thanks for letting me know the outcome.
Cleo


#16