Script to play a radio if nothing else is playing in mpc

Raspberry Pi related support

Script to play a radio if nothing else is playing in mpc

Postby maxsubs » 04 Nov 2020, 15:59

hey folks!

I'm trying to achieve this: when I boot up, I'd like to start playing this station (https://purenature-mynoise.radioca.st/stream). I have already added that to `Webradio/MyNoise-PureNature.pls`. Once it's playing, if I start playing some music it should obviously stop, but here is the tricky part, when I stop the music and nothing else is playing, I'd like to start playing that radio again, so it becomes some sort of background sound whenever system is up and nothing is playing. Now, I guess it should be pretty easy to add it at startup through systemd, but I have no idea how could I create a bash script with a conditional "if nothing else is playing in mpc, then play .pls". I think it'd be a nice thing to have, but no idea how to go about it. Anyone knows how to create such script.
Many thanks!
maxsubs
 
Posts: 8
Joined: 20 Oct 2020, 14:30

Re: Script to play a radio if nothing else is playing in mpc

Postby lakeuk » 11 Nov 2020, 21:30

Here's a crude example, should work with 0.4 / 0.5 version, probable won't work with +Re version as they do streaming / playlists differently. Not great using while & sleep, mpd python module probable better to use as you can include an idle wait.

Code: Select all
#!/usr/bin/env python3
import time
import subprocess

while True:
    station = subprocess.check_output("mpc current", stderr=subprocess.STDOUT, shell=True)
    if len(station) == 0:
        print('play my playlist track')
        subprocess.run(["mpc", "clear"])
        subprocess.run(["mpc", "load", "Radio Preset"])
        subprocess.run(["mpc", "play", "2"])       
    else:
        print('playing {}'.format(station))
    time.sleep(10)
(Pi 3, Ver. 0.4b build 20170229, usage m3u radio streams playlists)
lakeuk
 
Posts: 14
Joined: 20 Jun 2017, 16:54

support RuneAudio Donate with PayPal


Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 5 guests
cron