# Problem playing audio file from node red

**URL:** https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597
**Category:** General
**Created:** [11 December 2023 10:12 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597 "2023-12-11T10:12:17Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 10:12 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/1 "2023-12-11T10:12:17Z")

</div>

Hi,  
"aplay somefile.wav" with or without -D default works fine om my raspberry to play audio to paired bluetooth speaker.  
The same command from within exec node gives error: "aplay: main830: audio open error: unknown error 524"  
Other commands issued using exec node work fine.

I controll the moving parts of my christmas village this way but no music ...

What is going wrong?

Thanks in advance

6akaeddy2

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [11 December 2023 10:16 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/2 "2023-12-11T10:16:16Z")

</div>

This got me audio out of the headphone socket - something similar might work for Bluetooth

> [@\[SOLVED\]Not hearing sound using aplay via exec when running as service](https://discourse.nodered.org/t/solved-not-hearing-sound-using-aplay-via-exec-when-running-as-service/52924):
>
> Following on from how to get Node-RED to run as a service on a Pi I'm having trouble playing a sound via exec node So I'm trying to play a wav file out thru the Pi audio jack It works fine when I run Node-RED directly from a terminal As I was typing this I suddenly thought that the sound is probably defaulting to going out thru HDMI and with a bit of googling and experimenting I came up with this as my solution [image] which tells it to explicitly use the audio jack and all good …

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 13:44 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/3 "2023-12-11T13:44:28Z")

</div>

Thanks Cymplecy,  
I looked at your suggestion. Aplay should play via bluetooth. It does when invoked in a terminal session so I think this is the default now. Using "aplay -D default file.wav" using node red exec however gives the error. Tried other options then "default". "-D pulse" works from terminal. From nore red exec it gives an other error:

` ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused`

`aplay: main:830: audio open error: Connection refused`

using "paplay file.wav" from exec node results in "Connection failure: Connection refused  
pa\_context\_connect() failed: Connection refused"

I'm out of idees

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 December 2023 13:56 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/4 "2023-12-11T13:56:51Z")

</div>

How are you running node-red? If running it as a service try stopping it (node-red-stop) and run it directly in the terminal using  
`node-red`  
If it plays now then possibly the node-red user is not in the audio group.

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 14:26 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/5 "2023-12-11T14:26:13Z")

</div>

Hi Colin,  
I start node red as a service, but i have to login opening the web interface. Node red exec node "whoami" returns my username, so i think (p)aplay is called as me?

Yede

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 December 2023 14:54 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/6 "2023-12-11T14:54:16Z")

</div>

Have you tried what I suggested?

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 15:15 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/7 "2023-12-11T15:15:08Z")

</div>

Hi Colin,  
I thought i did without result, now for second time is does work.  
I see no user node-red in /etc/passwd? how to add it to group audio then?  
added myself to pulse and pulse-access. don't know is that was smart...  
What is my next step?  
Thanks so far!

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 December 2023 15:20 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/8 "2023-12-11T15:20:03Z")

</div>

Look in the systemd service to see what is the user running the service  
`cat /lib/systemd/system/nodered.service`  
There should be lines near the start setting the User and Group.

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 15:24 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/9 "2023-12-11T15:24:40Z")

</div>

[service]  
Type=simple  
#Run as normal pi user - change to the username you wish to ron NodepRED as  
User=yede  
Group=yede  
WorkingDirectory=/home/yede

yede is my username. instead of standard user pi

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 December 2023 15:31 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/10 "2023-12-11T15:31:49Z")

</div>

So is that the same user that you were logged in as when you ran `node-red`. If so then it is not the group, it must be something to do with the environment when logged in via ssh versus the service running.

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 15:38 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/11 "2023-12-11T15:38:45Z")

</div>

yes, same user as logged in via ssh (headless pi zero 1) and started node-red manualy

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [11 December 2023 17:41 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/12 "2023-12-11T17:41:51Z")

</div>

What OS are you running? You can find this by running `lsb_release -c`

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 19:43 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/13 "2023-12-11T19:43:23Z")

</div>

hello zenofmud,

Bullseye

Tomorrow, tuesday 12-12 no time for reactions. have to go to office ;-(

---

<div class="post-metadata">

### Author: ![PdeJ](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@PdeJ](https://discourse.nodered.org/u/PdeJ)
#### Post date: [11 December 2023 22:01 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/14 "2023-12-11T22:01:22Z")

</div>

When using a headless rpi running Bullseye to get aplay to work through an Exec node for Bluetooth speakers I had to make PulseAudio specifically available to Node Red. Have a look at the section of my notes [PdeJ - Connect to Bluetooth](https://sites.google.com/view/pdej/pi2alexa/connect-to-bluetooth#h.t5u9mzfggg2x)

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [11 December 2023 22:41 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/15 "2023-12-11T22:41:03Z")

</div>

Thanks. Seems to the point. Will try this tomorrow evening.

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [13 December 2023 12:59 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/16 "2023-12-13T12:59:13Z")

</div>

A combination of the suggestions of PdJ and Colin seems to work. after rebooting the pizero i now issue "node-red-stop" and "node-red &". then "bluetoothctl power on" and "bluetoothctl connect mac-addres-speaker".  
Then the exec node "aplay -D pulse somevfile.wav" works.

Okay for now. Would like a solution whitout manualy restarting node-red in the future. probably start node red not as a service but from login script of user?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 December 2023 13:21 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/17 "2023-12-13T13:21:33Z")

</div>

You should be able to add those commands into the systemd startup script for node red.  
`sudo systemctl status nodered` will tell you where that is.

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [15 December 2023 13:30 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/18 "2023-12-15T13:30:01Z")

</div>

Hello Colin,

With my lack of experience I need a bit more guidance.  
could you give me another nudge please?

## sudo systemctl status nodered just after boot is:

```auto
yede@pizero1:~ $ sudo systemctl status nodered
Failed to get journal cutoff time: Bad message
● nodered.service - Node-RED graphical event wiring tool
     Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-12-15 10:12:17 CET; 1min 31s ago
       Docs: http://nodered.org/docs/hardware/raspberrypi.html
   Main PID: 247 (node)
      Tasks: 11 (limit: 414)
        CPU: 33.011s
     CGroup: /system.slice/nodered.service
             └─247 node --max_old_space_size=256 /usr/lib/node_modules/node-red/bin/../red.js

Dec 15 10:12:17 pizero1 systemd[1]: Started Node-RED graphical event wiring tool.
Dec 15 10:12:52 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info]
Dec 15 10:12:52 pizero1 Node-RED[247]: Welcome to Node-RED
Dec 15 10:12:52 pizero1 Node-RED[247]: ===================
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Node-RED version: v3.1.0
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Node.js version: v18.17.1
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Linux 6.1.21+ arm LE
Dec 15 10:13:29 pizero1 Node-RED[247]: 15 Dec 10:13:29 - [info] Loading palette nodes
Dec 15 10:13:50 pizero1 Node-RED[247]: 15 Dec 10:13:50 - [info] Dashboard version 3.6.2 started at /ui

```

## yede@pizero1:~ $ node-red-stop

## after stopping and starting like you told me it is:

```auto
● nodered.service - Node-RED graphical event wiring tool
     Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2023-12-15 10:33:24 CET; 3h 41min ago
       Docs: http://nodered.org/docs/hardware/raspberrypi.html
    Process: 247 ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 247 (code=exited, status=0/SUCCESS)
        CPU: 51.453s

Dec 15 10:13:57 pizero1 Node-RED[247]: 15 Dec 10:13:57 - [info] Starting flows
Dec 15 10:13:59 pizero1 Node-RED[247]: 15 Dec 10:13:59 - [info] Started flows
Dec 15 10:13:59 pizero1 Node-RED[247]: 15 Dec 10:13:59 - [info] [mqtt-broker:9ef57e0297253107] Connected to broker: mqtt://localhost:1883
Dec 15 10:33:24 pizero1 systemd[1]: Stopping Node-RED graphical event wiring tool...
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] Stopping flows
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] [mqtt-broker:9ef57e0297253107] Disconnected from broker: mqtt://localhost:1883
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] Stopped flows
Dec 15 10:33:24 pizero1 systemd[1]: nodered.service: Succeeded.
Dec 15 10:33:24 pizero1 systemd[1]: Stopped Node-RED graphical event wiring tool.
Dec 15 10:33:24 pizero1 systemd[1]: nodered.service: Consumed 51.453s CPU time.

```

* * *

## /lib/systemd/system/nodered.service is:

```auto
# systemd service file to start Node-RED

[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=yede
Group=yede
WorkingDirectory=/home/yede

Environment="NODE_OPTIONS=--max_old_space_size=256"
# define an optional environment file in Node-RED's user directory to set custom variables externally
EnvironmentFile=-/home/yede/.node-red/EnvironmentFile
# uncomment and edit next line if you need an http proxy
#Environment="HTTP_PROXY=my.httpproxy.server.address"
# uncomment the next line for a more verbose log output
#Environment="NODE_RED_OPTIONS=-v"
# uncomment next line if you need to wait for time sync before starting
#ExecStartPre=/bin/bash -c '/bin/journalctl -b -u systemd-timesyncd | /bin/grep -q "systemd-timesyncd.* Synchronized to time server"'

ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
#ExecStart=/usr/bin/env node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
RestartSec=20
# Tag things in the log
SyslogIdentifier=Node-RED
#StandardOutput=syslog

[Install]
WantedBy=multi-user.target
------

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 December 2023 12:21 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/19 "2023-12-16T12:21:02Z")

</div>

Actually there may be an easier way. What happens if you run an exec node containing  
`bluetoothctl power on ; bluetoothctl connect mac-addres-speaker`  
before your aplay exec node?

---

<div class="post-metadata">

### Author: ![6akaeddy2](https://avatars.discourse-cdn.com/v4/letter/6/b2d939/32.png) [@6akaeddy2](https://discourse.nodered.org/u/6akaeddy2)
#### Post date: [19 December 2023 09:03 UTC](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597/20 "2023-12-19T09:03:46Z")

</div>

Hi Colin,

Sorry for late reaction. Busy period of the year.  
I issue both commands. Manuallly when testing, or as part of the script that runs my music, steppermotors and servo's.  
For now I can live with the current situation: restart node-red manually after booting the pizero and don't switch it off until next year.  
happy hollidays.  
Yede

[Next page](https://discourse.nodered.org/t/problem-playing-audio-file-from-node-red/83597.md?page=2)
