Pythonshell play audio using Pygame

Hi,

I have a simple Python script to play audio:

#!/usr/bin/env python2
import sys
import pygame
print(sys.version)
file = '/home/pi/Musik/02-black_light_burns-animal.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)

which plays fine when called from terminal, I can hear the sound. From Nodered, the file also plays (pygame.busy returns 1) and I can see the loop is running, however, there is no sound to hear. Also the mixer does not show there is sound playing.

The Python envoirement and compiler are same:

pygame 1.9.4.post1 Hello from the pygame community. https://www.pygame.org/contribute.html
2.7.16 (default, Oct 10 2019, 22:02:15) [GCC 8.3.0]

also Nodered is started with same user as used in terminal.

I cannot see any difference.

On Stackoverflow I got one response in two days, telling me to try playing a wav instead of mp3, which did not make a difference.
Maybe here someone has an idea?

When you run the python script from terminal, where do you hear the sound? On something connected directly to the pi?

Have you tried any of the sound nodes?
What version of NR and node.js?

The PulseAudio Sound Driver is my default device ( aplay -L:
default
Playback/recording through the PulseAudio sound server)
which runs fine for all other sound playback so far, which is not hard-mapped to the ALSA backend.

/etc/asound.conf reads:

pcm.!default {
type pulse
}

ctl.!default {
type pulse
}

When playing from terminal, I see "python2: simple DirectMedia Layer" showing up within the PulseAudio Mixer. Nothing shows up when playing from NodeRed.

Also Darkice is able to catch the sound played by the terminal and sends it to my ESP-Multiroom-DACs. These two points are where I validate sound is playing or not.

I have a USB sound card, also checked if it plays when using the onboard device, with no luck.

I have not tried other sound nodes. All I found was a node which plays in browser. I can check it later, or maybe you have a better node to test with?

My Nodered Version is 0.19.6, Node.js is 12.18.2. Raspi OS is latest buster btw.

Hey, sorry to come in here with a request. I just started development on a set of nodes for sox a few weeks ago and i haven’t had anybody with a pulse audio setup for testing yet. The nodes include a play node that plays audio through sox/alsa and you can choose the output device from the nodes config. If you feel brave :wink: you can install those nodes following the instructions from the repos readme and be one of my testers. You could also test the backwards compatibility as the nodes were developed for nodered 1.0 and onwards but should be backwards compatible. So no promises :see_no_evil:

Johannes

1 Like

As soon as I have a stable os with everything running, I will make a backup. Then I can make a few tests :slight_smile:

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.