Cin316
Posts: 1
Joined: Thu May 31, 2018 8:09 pm

Pygame won't play sound over Bluetooth, but other programs work

Thu May 31, 2018 8:19 pm

I having trouble getting a program I've written using the pygame sound mixer to play sound to work over Bluetooth. The program is able to work correctly over HDMI audio. The Raspberry Pi has no trouble connecting to the Bluetooth headphones, and aplay works just fine to play sound. After I attempt to play sound with pygame, aplay is broken and will not play sound until I do a reboot. Any ideas?

I'm using a Raspberry Pi Zero W and the built-in Bluetooth chip. More version info is at the bottom of the post.

Detailed information:

First, before connecting to Bluetooth headphones, I get the following error because there's no other sound output connected. This is expected.
Error when there's no device to output sound to:

Code: Select all

ALSA lib bluealsa-pcm.c:679:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA transport: No such device
aplay: main:788: audio open error: No such device
Next, I connected the bluetooth headphones to the Pi.

Output from "amixer":

Code: Select all

Simple mixer control 'Vidonn F1 - A2DP',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 127
  Mono:
  Front Left: Playback 127 [100%] [on]
  Front Right: Playback 127 [100%] [on]
Simple mixer control 'Vidonn F1 - SCO',0
  Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: Playback 0 - 15 Capture 0 - 15
  Mono: Playback 12 [80%] [on] Capture 15 [100%] [on]
"aplay -L"

Code: Select all

null
    Discard all samples (playback) or generate zero samples (capture)
default
sysdefault:CARD=ALSA
    bcm2835 ALSA, bcm2835 ALSA
    Default Audio Device
dmix:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct sample mixing device
dmix:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct sample mixing device
dsnoop:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct sample snooping device
dsnoop:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct sample snooping device
hw:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct hardware device without any conversions
hw:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct hardware device without any conversions
plughw:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Hardware device with all software conversions
plughw:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Hardware device with all software conversions
Using aplay to play a sound works just fine now. I can hear it on the Bluetooth headphones.

Here is my basic python test program:
simplePyGame.py

Code: Select all

import pygame.mixer

pygame.mixer.init(48000, -16, 1, 1024)

sndA = pygame.mixer.Sound("A.wav")

soundChannel1A = pygame.mixer.Channel(1)

soundChannel1A.play(sndA)
There's a file called A.wav in the working directory for simplePyGame.py to play.

Running the python program outputs the following:

"python simplePyGame.py"

Code: Select all

Traceback (most recent call last):
  File "simplePyGame.py", line 3, in <module>
    pygame.mixer.init(48000, -16, 1, 1024)
pygame.error: Couldn't set hardware audio parameters: Success
The only other info I could find about this error was this post on the forum from 2016 with no replies.
viewtopic.php?t=160164

Now, using "aplay" to play a WAV file outputs this error. It doesn't go away until the Raspberry Pi is restarted.

Code: Select all

Playing WAVE '../20second_sine.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
aplay: set_params:1363: Unable to install hw params:
ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 16
CHANNELS: 1
RATE: 8000
PERIOD_TIME: (85333 85334)
PERIOD_SIZE: (682 683)
PERIOD_BYTES: (1364 1366)
PERIODS: (5 7)
BUFFER_TIME: 511875
BUFFER_SIZE: NONE
BUFFER_BYTES: 8190
TICK_TIME: 0
"amixer" and "aplay -L" still have the same outputs as before.




Version info:

"python --version"

Code: Select all

Python 2.7.13
"aplay --version"

Code: Select all

aplay: version 1.1.3 by Jaroslav Kysela <[email protected]>
"pip show pygame"

Code: Select all

Name: pygame
Version: 1.9.3
Summary: Python Game Development
Home-page: http://www.pygame.org
Author: Pete Shinners, Rene Dudfield, Marcus von Appen, Bob Pendleton, others...
Author-email: [email protected]
License: LGPL
Location: /usr/lib/python2.7/dist-packages
Requires: 
The device is a Raspberry Pi Zero W.

OS release is:

Code: Select all

Raspbian GNU/Linux 9 (stretch)
"bluealsa --version"

Code: Select all

1.2.0

jdonald
Posts: 401
Joined: Fri Nov 03, 2017 4:36 pm

Re: Pygame won't play sound over Bluetooth, but other programs work

Sat Jul 20, 2019 4:07 am

Saw your bounty. I'm no expert but here are a couple troubleshooting ideas:

* If it's not mandatory to use bluealsa, any luck if you try running with SDL_AUDIODRIVER=dsp or SDL_AUDIODRIVER=pulse?

* Try it with PyGame-SDL2. Much like everything else, SDL_mixer changed immensely going from 1.2 to 2.0 so I think this is worth looking into.

Edit: Later I realized that the bounty on the Stack Exchange question was by someone other than the OP. Perhaps even a misclick rather than any renewed interest in this a year later.

Return to “Troubleshooting”