java7nerd
Posts: 6
Joined: Fri Jun 07, 2013 5:53 pm

Set the audio option (HDMI/headphones) in Python for pygame?

Tue Jul 29, 2014 6:27 pm

I'm trying to play a sound using pygame.

I found this solution online:

Code: Select all

import pygame
pygame.mixer.init()
sound = pygame.mixer.Sound("/usr/share/scratch/Media/Sounds/Animal/Meow.wav")
sound.play()
However, I hear nothing.

After messing around I found that if I run one of the Python Games that comes with Raspbian and select "Force HDMI", then my program plays sound correctly. When I exit the game, my program no longer works.

How do I set the audio option from Python? Or is there some other reason why my program is only working while I have another pygame game running? I'm assuming it's due to the audio option.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Set the audio option (HDMI/headphones) in Python for pyg

Tue Jul 29, 2014 6:31 pm

Are you trying to get the sound on your HDMI TV or from the analog headphone socket?
Have you set the volume above zero (set to eleven)?

http://www.raspberrypi.org/forums/viewt ... 66&t=24943
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

java7nerd
Posts: 6
Joined: Fri Jun 07, 2013 5:53 pm

Re: Set the audio option (HDMI/headphones) in Python for pyg

Tue Jul 29, 2014 7:00 pm

DougieLawson wrote:Are you trying to get the sound on your HDMI TV or from the analog headphone socket?
Have you set the volume above zero (set to eleven)?

http://www.raspberrypi.org/forums/viewt ... 66&t=24943
I'm using HDMI. have not yet tried headphones.

Volume is fine, as other sounds play and it plays when I'm running another pygame.

I will check this topic you've linked and see if I find anything, thanks.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Set the audio option (HDMI/headphones) in Python for pyg

Tue Jul 29, 2014 7:05 pm

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Set the audio option (HDMI/headphones) in Python for pyg

Wed Jul 30, 2014 9:47 am

java7nerd wrote:After messing around I found that if I run one of the Python Games that comes with Raspbian and select "Force HDMI", then my program plays sound correctly. When I exit the game, my program no longer works.

How do I set the audio option from Python? Or is there some other reason why my program is only working while I have another pygame game running? I'm assuming it's due to the audio option.
Best option would be to look at the source code of the program the does work correctly, work out how it's doing what it does, and then use that same code in your own program 8-)

And have a look also at http://www.raspberrypi.org/documentatio ... -config.md

java7nerd
Posts: 6
Joined: Fri Jun 07, 2013 5:53 pm

Re: Set the audio option (HDMI/headphones) in Python for pyg

Wed Jul 30, 2014 7:22 pm

Okay, I found out the problem. Audio doesn't work unless you input this shell command first if you want HDMI:

Code: Select all

amixer -c 0 cset numid=3 2
or if you want headphones:

Code: Select all

amixer -c 0 cset numid=3 1
The python games that come with the Pi runs a shell script first which prompts you to choose an option and then runs one of those commands before it starts running the game in Python.

java7nerd
Posts: 6
Joined: Fri Jun 07, 2013 5:53 pm

Re: Set the audio option (HDMI/headphones) in Python for pyg

Wed Jul 30, 2014 7:25 pm

Also, of note: after calling

Code: Select all

pygame.mixer.init()
It takes about one and a half seconds before the mixer is ready to start outputting sound. So if you play a sound immediately it gets eaten if the sound is short.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Set the audio option (HDMI/headphones) in Python for pyg

Wed Jul 30, 2014 7:49 pm

java7nerd wrote:Okay, I found out the problem.
Well done. Onwards and upwards! :D

Return to “Python”