This project assumes you have working AIY voice kit.
OverviewI picked the Google AIY so I could program some custom commands for my home automation, but I knew when I was getting it that the AIY doesn't have the same functionality of the Google Home. I had seen that I could use VLC to play media files or internet streams so with the help from this website I managed to get my AIY to play some of my favourite UK radio stations.
InstructionsInstall VLC from terminal
sudo apt-get install vlc
Install python bindings
sudo pip install python-vlc
Go to your AIY python script and add the VLC module::
import vlc
Firstly, we need to add to the other defined functions (power_off, Ip_address etc).
Stream List, which contains your radio stations:
def findstream(radiostream):
streamlist = {
'radio 1': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
'radio one': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
'planet rock': 'http://icy-e-bz-08-boh.sharp-stream.com/planetrock.mp3',
'talk sport': 'http://radio.talksport.com/stream?awparams=platform:ts-tunein;lang:en',
'talksport': 'http://radio.talksport.com/stream?awparams=platform:ts-tunein;lang:en'
}
return streamlist[radiostream]
And add a way to stop the radio:
def radiostop():
try:
player.stop()
except NameError as e:
print("Radio isn't on!")
Along with a way to turn the VLC player's volume up/down, firstly you'll need to add a global variable:
global vlc_volume
vlc_volume = 35
Now for the function:
def radiovoldown():
try:
global vlc_volume
vlc_volume = vlc_volume-10
print("Radio Volume: %s ", vlc_volume)
player.audio_set_volume(vlc_volume)
except NameError as e:
print("Radio Vol Down Failed")
def radiovolup():
try:
global vlc_volume
vlc_volume = vlc_volume+10
print("Radio Volume: %s ", vlc_volume)
player.audio_set_volume(vlc_volume)
except NameError as e:
print("Radio Vol Up Failed"))
Finally, the Play Radio function:
def radioplay(text):
print("Radio Stream Requested: %s ", text)
radiostream = (text.replace('play', '', 1)).strip()
print("Radio Volume: %s ", vlc_volume)
try:
stream = findstream(radiostream)
except KeyError as e:
print("Error finding stream")
radiostop()
return
print("Playing radio: %s ", stream)
instance = vlc.Instance()
global player
player = instance.media_player_new()
media = instance.media_new(stream)
player.set_media(media)
player.audio_set_volume(vlc_volume)
player.play()
This function receives the text from the voice recognizer function, the function will print the text, proceeds to remove the 'play' command and passes the rest of the text to the 'findstream' function, which in turn returns the URL for the requested radio stream. If the stream is not found then a message is printed to state there is a error. If the stream is found then the URL is passed to VLC to play.
Add the below into the 'process_event(assistant, event)' function along with the other pre-defined voice commands:
elif 'play' in text:
assistant.stop_conversation()
radioplay(text)
Additionally add the below, to stop and change the volume:
elif text == 'radio off':
assistant.stop_conversation()
radiostop()
elif text == 'radio volume down':
assistant.stop_conversation()
radiovoldown()
elif text == 'radio volume up':
assistant.stop_conversation()
radiovolup()
Note, I have problems with AIY not responding to voice commands when it is playing radio stations (not surprising), so I added the below function:
def _on_button_pressed():
radiostop()
And changed the main() function as below:
def main():
if platform.machine() == 'armv6l':
print('Cannot run hotword demo on Pi Zero!')
exit(-1)
credentials = aiy.assistant.auth_helpers.get_assistant_credentials()
with Assistant(credentials) as assistant:
aiy.voicehat.get_button().on_press(_on_button_pressed)
for event in assistant.start():
process_event(assistant, event)
This means you can press the button and the radio will stop.
So issuing voice command:
ok google, play talk sport
will play talk sport radio station!
Usage guidanceIf you want change the volume, this needs to be issued while the radio is off unless the volume is low then the AIY seems to be able to listen still for commands.
Issuing voice command 'play' essentially starts the chain you must then shortly after announce the Stream you wish to listen as defined in the stream list.
Comments