pygame.mixer - how to check/wait for audio device


3 posts
by tped » Wed May 17, 2017 2:38 pm
New to python and this board ... feel free to redirect me

I built a little python app for my grandbaby that plays sounds when she hits a button, but am struggling with a PI (jessie) that has other audio activity going on. Specifically I'd like to test to ensure the audio device is available (no shairport-sync activity) before attempting to initialize pygame.mixer and play my sound. Preferably wait for current audio activity to stop, play my sound and release the device again.

Currently if audio is active when I call pygame.mixer.init(), my script dies with "pygame.error: No Available audio device".
pygame_mixer.get_busy() does not seem to catch this.

any help would be appreciated
Posts: 2
Joined: Wed May 17, 2017 1:41 pm
by elParaguayo » Wed May 17, 2017 3:53 pm
Bit of a hack but you could just use a try...except... block to catch that exception and loop until the device becomes available.

Someone else here may know a way to programmatically check if the device is available.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.
User avatar
Posts: 1780
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK
by tped » Thu May 18, 2017 3:34 pm
elParaguayo wrote:Bit of a hack but you could just use a try...except... block to catch that exception and loop until the device becomes available.

Someone else here may know a way to programmatically check if the device is available.


Thank-you - the try...except did the trick! I tossed a sleep in and retry pygame.mixer.init() a couple of times a minute. Doesn't seem to impact other audio app or eat too much of the PI

I also had to add pygame.mixer.quit() to my playsound routine to release the audio device so other apps could use it between my sounds. Now basically using a mixer.init(), sound.play() and mixer.quit() sequence for each sound. Seems to work with shairport-sync, not sure about other apps yet

thanks for quick response
Posts: 2
Joined: Wed May 17, 2017 1:41 pm