0

Is there an easy way to play an MP3 song automatically, and/or loop/or have it play through a series of songs in a folder? I am trying to do this on a Zero and am stuck. I have a Phat Dac connected to it that works brilliantly with lineout speakers.

Basically, I just want to turn on and have white noise machine without doing anything else.

It will play a wav file in the terminal but not loop or go through a folder.

1

Yes, use a shell script, that triggers your favorite mp3 wav player via command line.., you can customize a script file to do all sorts of nifty commands like cycle through all tracks.. and have that script load up in an rc.local file to trigger at startup.

Lots of ways you can do this, I'm sure you might be able to load a gui based player via command line with a playlist file of tracks or one track, and pass it parameters like volume, file, repeat all etc

You will need to find a good player to suit your needs and do a little research on scripting if you go that route.

I find the nifty Node-Red flowchart based programming IDE can open a wide world of awesome possibilities with interactive fun stuff, and makes programming things like that a little simpler, but might be Overkill for a simple scripts task.

1

You should use a systemd unit file to run a program at startup. Try this simple Unit to start with your project. If it runs on bootup you can improve it step by step.

Create a new service with

rpi ~$ sudo systemctl --force --full edit my-startup-song.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Play song at startup
After=multi-user.target

[Service]
ExecStart=/full/path/to/aplay /full/path/to/noise.wav

[Install]
WantedBy=multi-user.target

Enable the new service with:

rpi ~$ sudo systemctl --now enable my-startup-song.service
  • Yes, it's neat. – NVRM Oct 3 '19 at 18:43
1

To execute a command at system startup, without the need to login, so you don't need a screen at all, edit the file /etc/rc.local.

sudo nano /etc/rc.local

Paste any command there, and make sure the file end with exit 0.

Also make sure to use the full path to link your audio file.

#!/bin/sh -e   
aplay /home/pi/Music/noise.wav
exit 0

aplay is a command line player that should be installed by default.

Then give execution rights, so the file get executed at system startup:

sudo chmod +x /etc/rc.local

To specifically generate white/pink noise, add audio effects and many more, other advanced tools can do that, like ffmpeg or sox.

  • 2
    Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo Oct 1 '19 at 7:16
  • 2
    This is a somewhat contentious issue, which, based on the trajectory of community attitudes toward systemd over the past decade, means that the trend is toward abandoning non-legacy use of /etc/rc.local, something readers should be aware of. The Pi Foundation still condones it, although IMO they tend to be a bit behind the ball, linux wise.... – goldilocks Oct 1 '19 at 14:58
  • 1
    As for it being "how unix systems works", that's technically false: It's not specified by the UNIX or POSIX standards, and historically is a BSD convention which was inserted into SysV. – goldilocks Oct 1 '19 at 15:04
  • 1
    I believe you, but you must now provide a concise answer about the usage of systemd headless! Tx! – NVRM Oct 1 '19 at 15:16
  • 1
    @NVRM I have done it – Ingo Oct 2 '19 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.