Play BBC Radio on the Raspberry Pi AIY Project UPDATED!

UPDATE!: I’ve made a massive update to this to use the vlc python bindings and to pause and resume on any trigger. The code can be found here: https://github.com/ktinkerer/aiyprojects-raspbian/tree/radio_player/src

To get it to work you will need to copy the main.py action.py and vlc.py files into your ~voice-recognizer-raspi/src/ folder (If you’ve made any changes to action.py then back it up first!). VLC needs to be installed:


sudo apt-get update
sudo apt-get install vlc

Double check that your main.py and your status-monitor.py files still have executable rights if not re-add them:


chmod +x voice-recognizer-raspi/src/main.py
chmod +x voice-recognizer-raspi/src/status-monitor.py

Restart the voice recognition service:
sudo systemctl restart voice-recognizer.service
Check the voice recogniser logs for errors!
sudo journalctl -u voice-recognizer -f

Now when the radio is playing and you use any trigger the radio will stop while the box responds to the command and resume again once it’s finished. To stop the radio say “radio stop” or “radio off”.

Listening from outside the UK

Unfortunately it seems that the high quality links won’t work from outside the UK so if you are having trouble getting this to work, you may see a 403 error, then try replacing the list of stations with this list:

'1': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p',
'2': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p',
'3': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio3_mf_p',
'4': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p',
'5': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5live_mf_p',
'5 sports': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5extra_mf_p',
'6': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p',
'1xtra': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1xtra_mf_p',
'4 extra': 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4extra_mf_p',

Other stations can be found here. Thanks to paulpoco in the comments for finding this out!

Using Voice Keywords – OK Google

This didn’t work with voice keywords as once the radio was playing it was hard to get the box to recognise the keyword or the command. To get around this I’ve come up with a method where if you want to use voice keywords you can get the box to then wait until the button is pressed and this will stop the radio.

To do this use the updated Github code and find these lines:

# Uncomment the following line if you want to use a voice trigger to start the radio and the button to stop it, radio will not pause and resume
# self.wait_for_button()

Delete the comment (and the space!) before self.wait_for_button()
Also, make sure you import the gpio module.

import RPi.GPIO as gpio

Pause and resume will no longer work with this method, the box will not respond to any commands until the button is pressed, then it will start listening for commands again.

Original Method

I wanted to play the radio through my AIY Projects Box and after a lot of experimentation I found a way to do it. I also got some hints from this post I found on the Raspberry Pi forum about playing tracks from Youtube.

This uses VLC to play the radio stream, I initially tried mpd but couldn’t get it to work nicely alongside the voice recognition. I may persevere with mpd / mopidy as it would be great to be able to play anything.

First of all install vlc:

sudo apt-get update
sudo apt-get install vlc

Then edit ~voice-recognizer-raspi/src/action.py

Add this to the imports:

import RPi.GPIO as gpio
import time

Further down edit this part to add this class (added as a gist as wordpress was messing up the formatting!):

Then further down add this:
# =========================================
# Makers! Add your own voice commands here.
# =========================================
actor.add_keyword(_('radio'), playRadio(say,_('radio')))

Restart the voice recognition service:
sudo systemctl restart voice-recognizer.service
Check the voice recogniser logs for errors!
sudo journalctl -u voice-recognizer -e

Your AIY Project Box should now respond to radio commands by playing the correct BBC radio station, eg “Radio 1”, “Radio 1 extra” etc.
I’ve set mine up to play Radio 6 music as the default for the command “radio” as we listen to that 99% of the time! Change that by editing
else:
station = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p?s=1494265223"
and replacing it with the stream for the station you want.

This should work with other radio streams, just add them to the elif list with the command you want to trigger them with.

Pressing the button stops the radio immediately and listens for another command.

110 Comments

Add a Comment

Your email address will not be published. Required fields are marked *