I’m using my Raspberry PI as a MusicPlayer en most of the times I’m playing a radio stream from the Internet. However, when the Internet connection is lost the stream stops. (duh…) so I made a script that runs at startup and checks every 20 seconds if the Internet is still up. If not, mpc kicks in and loads a local playlist to play songs from the local storage. When Internet is back on, mpc kicks in again and starts my favourite radio stream.
This is the script:
#!/bin/bash
while true
do
if ping -c2 www.google.nl | grep -q 100%
then
if [ -f '/home/pi/playingstream' ];
then
rm /home/pi/playingstream
echo "Network unreacheable" > /home/pi/playinglocal
mpc stop
mpc clear
mpc load local
mpc play
fi
else
if [ -f '/home/pi/playinglocal' ];
then
rm /home/pi/playinglocal
echo "No network issues" > /home/pi/playingstream
mpc stop
mpc clear
mpc load Radio10Live
mpc play
fi
fi
sleep 20
done
mpc is the Music Player Client to be used for the Music Player Daemon (MPD)
The mentioned method of playing playlists with mpc is very old and based on Volumio version 1.5x
Since version 2.x of volumio, playing playlists is handled different.
It might still work with mpc but playing with this method does not show anything on the GUI.
It will be better to use the volumio API when using version 2.x of volumio.
It will the be something like this:
#!/bin/bash
while true
do
if ping -c2 8.8.8.8 | grep -q 100%
then
if [ -f '/home/volumio/playingstream' ];
then
rm /home/volumio/playingstream
echo "Network unreacheable" > /home/volumio/playinglocal
curl localhost:3000/api/v1/commands/?cmd=stop
curl localhost:3000/api/v1/commands/?cmd=clearQueue
curl localhost:3000/api/v1/commands/?cmd=playplaylist&name=usb
##curl localhost:3000/api/v1/commands/?cmd=play
fi
else
if [ -f '/home/volumio/playinglocal' ];
then
rm /home/volumio/playinglocal
echo "No network issues" > /home/volumio/playingstream
curl localhost:3000/api/v1/commands/?cmd=stop
curl localhost:3000/api/v1/commands/?cmd=clearQueue
curl localhost:3000/api/v1/commands/?cmd=playplaylist&name=radio10-80
##curl localhost:3000/api/v1/commands/?cmd=play
fi
fi
sleep 20
done
Make sure you’ve created the playlists named ‘usb’ and ‘radio10-80’ before running this script.
playlist ‘usb’ is a playlists that plays all songs from the USB stick
playlist ‘radio10-80’ is a webradio station saved in a playlist.
Playlists should be created in the queue.
Also, create an empty file with ‘touch /home/volumio/playinglocal’
This will then be replaced by ‘playingstream’ when you have Internet and running the script the first time.
UPDATE:
Place the following code in a file eg. /home/volumio/control.sh and give it execute flag (chmod +x)
#!/bin/bash
while true
do
if ping -c2 8.8.8.8 | grep -q 100%
then
if [ -f '/home/volumio/playingstream' ];
then
rm /home/volumio/playingstream
echo "Network unreacheable" > /home/volumio/playinglocal
curl "http://localhost:3000/api/v1/commands/?cmd=stop"
curl "http://localhost:3000/api/v1/commands/?cmd=clearQueue"
curl "http://localhost:3000/api/v1/commands/?cmd=playplaylist&name=usb"
fi
else
if [ -f '/home/volumio/playinglocal' ];
then
rm /home/volumio/playinglocal
echo "No network issues" > /home/volumio/playingstream
curl "http://localhost:3000/api/v1/commands/?cmd=stop"
curl "http://localhost:3000/api/v1/commands/?cmd=clearQueue"
curl "http://localhost:3000/api/v1/commands/?cmd=playplaylist&name=radio10-80"
fi
fi
sleep 20
done
Make sure you’ve created the playlists named ‘usb’ and ‘radio10-80’ before running this script.
playlist ‘usb’ is a playlists that plays all songs from the USB stick
playlist ‘radio10-80’ is a webradio station saved in a playlist.
Playlists should be created in the queue.
Also, create an empty file with ‘touch /home/volumio/playinglocal’
This will then be replaced by ‘playingstream’ when you have Internet and running the script the first time.
Run the script in the background with: ‘/home/volumio/control.sh &’