Spotify autoplay with mopidy on Raspbian

Hello ,

I followed the topic 4270 to install Mopidy with the MusicBox webclient on my Raspberry.
My goal is too autoplay my playlist when my Pi is powered on.
So, I installed mopidy-spotify and launched mopidy as a service, all is OK, I can play my playlist from the webclient.

But, I need your help for the last step.
I have my URI : spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl but I don’t know to use it to autoplay it.
I saw also the topic 2791 but I don’t understand.
I must create a bash (called in rc.local I supposed) but I don’t know what to write in this file.

Sorry for my poor level, I really try to understand and I think I’m not so far of my goal now.

Thank you very much for reading me and I hope someone can help me :blush:

Have a good day !

Yannick

Hi, easiest way is to add it to cron, this probably isn’t the best way as it relies on time rather than mopidy actually being up and running - anyway here goes.
This uses mpc which you will probably need to install.

assuming you are working in a terminal or ssh

sudo apt update
sudo apt install mopidy-mpd
sudo apt install mpc

Once that’s done, type mpc at a prompt and it should list what is playing like this;

pi@mopidy:~ $ mpc
Luke Wareham;Rachel Mason - Shadows
[playing] #1/5 0:08/4:30 (2%)
volume:100% repeat: off random: off single: off consume: off

You can also use it to play your existing playlist above, type into the terminal;

mpc add spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl;mpc play

if that’s working you can now add it to cron to start when you reboot.

Open the crontab file in your preferred editor;
sudo crontab -e
scroll down to the bottom and add the following.

@reboot sleep 90 ; mpc add spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl;mpc play

This waits 90 seconds from reboot before it starts playing, if it doesn’t work make the figure higher, try lowering it to get a quicker time. Save and close it then reboot and listen if it works.

Alternatively you can try this to add to /etc/rc.local
Use your choice of editor to open a new file and save it as startup.sh somewhere you can find it, I used /home/pi/startup.sh
Copy and paste the following into the document - you will need to put your own playlist details in near the bottom of the script.

#!/bin/bash
INI__musicbox__autoplaymaxwait=120
echo “Waiting for Mopidy to accept connections…”
waittime=0
while ! nc -q 1 localhost 6600 </dev/null;
do
sleep 1;
waittime=$((waittime+1));
if [ $waittime -gt $INI__musicbox__autoplaymaxwait ]
then
echo “WARNING: Timeout waiting for Mopidy to start, aborting”
break;
fi
done
if [ $waittime -le $INI__musicbox__autoplaymaxwait ]
then
echo “Mopidy startup complete, playing $INI__musicbox__autoplay”
mpc add spotify:playlist:put your playlist name here
mpc play
fi

Save and exit
Give it the correct permission

sudo chmod 755 /home/pi/startup.sh

Try and run it in a terminal
sudo /home/pi/startup.sh
You should get a line saying something like
“Waiting for Mopidy to accept connections…
OK MPD 0.19.0
Mopidy startup complete, playing”

If thats working then
sudo nano /etc/rc.local
and at the bottom, but before exit 0 add
sudo /home/pi/startup.sh
then exit, save and reboot to see if it works.

Tried both varieties on mine this evening and both seem to work, hope it does for you.

Hello,

Thank you so much for your help. All is perfect (I used the second method with rc.local).
Thanks again for your perfect answer. It was very clear.

Have a good day ! :slight_smile:

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