Google Assistant(2/4): Integration of Youtube and Spotify with ReSpeaker 4-Mics Pi Hat on Raspberry Pi

Overview

YouTube is a free video-sharing website that makes it easy to watch online videos. You can even create and upload your own videos to share with others. Originally created in 2005, YouTube is now one of the most popular sites on the Web, with visitors watching around 6 billion hours of video every month.

Spotify is a digital music, podcast, and video streaming service that gives you access to millions of songs and other content from artists all over the world.

This post is the second tutorial in a 4-part series on how to set up Google Assistant on the Raspberry Pi. Now we will integrate YouTube and Spotify to Google Assistant with Raspberry Pi and ReSpeaker 4-Mics Pi Hat.

 

Hardware

  • Raspberry Pi 3B+ (Compatible on Raspberry Pi Zero and Zero W, Raspberry Pi B+, Raspberry Pi 2 B, Raspberry Pi 3 BRaspberry Pi 3 B+, Raspberry Pi 3 A+)

  • ReSpeaker 4-Mics Pi HAT
  • 
    
  • Speaker with 3.5mm audio jack
  •  

    Software

    • RPI terminal
    • Internet Browser (Example Chrome)

     

    Application Discussion

    With the YouTube Data API, you can add a variety of YouTube features to your application. One of the features is to use an embedded player to play videos directly in your app and customize the playback experience.

    Through the Spotify Web API, external applications retrieve Spotify content such as album data and playlists. To access user-related data through the Web API, an application must be authorized by the user to access that particular information.

    To be able to integrate YouTube and Spotify to the Google Assistant, we will be using their specific APIs in our Raspberry Pi.

     

    Setup the Hardware

    Mount the ReSpeaker 4-Mics Pi HAT to Raspberry pi like this.

     

    Setup the Software

    Google Assistant Setup

    • To set up the Google Assistant with Raspberry Pi, follow the first tutorial. You can check it here.

    Raspberry Pi with ReSpeaker 4-Mic Pi Hat Integration

    YouTube API Setup

    • To integrate YouTube to your Google Assistant. First, enable YouTube API on your Google Assistant. To that in your Google Console here:

    https://console.cloud.google.com/project

    • Next is to click your Project, then click the "Navigation Menu" (with 3 horizontal line icon) on the top left side, then hover to "API & Services", and lastly click "Credentials".

    • After clicking credentials go to "CREATE CREDENTIALS", then click "API Key" as you can see on the screenshot below. 

    • Take note of your API key as we will need this later. Click the "CLOSE" button.
    • After closing, click the "Library" on the left side as shown below.
    • On the library search for "YouTube Data API". Then click "YouTube Data API v3".

    • Then YouTube Data API home screen will appear. Click the "ENABLE" button to enable YouTube Data API on your project, then wait for it to finish enabling.

    • On a terminal window, input these commands to upgrade the needed libraries (to fix hanging issue)
    $ source env/bin/activate
    $ pip install youtube-dl --upgrade
    $ pip install gTTS --upgrade
    $ pip install gTTS-Token --upgrade
    • Edit config.yaml to update your "API_KEY"

           $ sudo nano GassistPi/src/config.yaml

    • In the config.yaml look for the following text by scrolling down or pressing down key. Then replace ENTER-YOUR-GOOGLE-CLOUD-API-KEY-HERE with your API key we got earlier. Press CTRL+X then Y to save.
    #API Key for YouTube and Kickstarter Search Engine
    Google_cloud_api_key: "ENTER-YOUR-GOOGLE-CLOUD-API-KEY-HERE"
    • Now you can use Google Assistant with YouTube. Remember to use this Phrase to play music "Play (your search name) from Youtube". Example, say "Ok Google" first, then say "Play Stereo from Youtube". To test this, input this command on your terminal.
    $ /home/pi/env/bin/python -u /home/pi/GassistPi/src/main.py
    • To stop music say "Ok Google" then "Stop Playing". To stop GassistPi library from running press Ctrl+C

     

    Spotify API

    • To integrate Spotify in Google Assistant. Click the link below to log into your Spotify developer account, or register if you don't have one already.
    https://developer.spotify.com/dashboard/login
    • After logging in the developer's dashboard, click "CREATE AN APP" as shown in the screenshot below.

     

    • Now fill in the "App name" and "App Description", then check the two checkboxes: Permission and Terms and Services. Then click the "Create" button.

    • You will be directed to your application dashboard. Take note of values for "Client ID", then click "SHOW CLIENT SECRET" to show the "Client Secret". Make sure to take note of both "Client Id" and "Client Secret"

    • Next is to take note of your account username. To get it click the link below and take note of your username.
    https://www.spotify.com/account/overview/

    • Open a terminal window and go to config.yaml by using this command
    $ sudo nano GassistPi/src/config.yaml
    • In the config.yaml look for this text by scrolling down or press down key.
    #Spotify credentials. Register for a spotify developer and get the credentials.
    Spotify:
      Spotify_Control: 'Enabled'
      client_id: 'ENTER YOUR SPOTIFY CLIENT ID HERE'
      client_secret: 'ENTER YOUR SPOTIFY CLIENT SECRET HERE'
      username: 'ENTER YOUR SPOTIFY USERNAME HERE'
    • Then replace ENTER YOUR SPOTIFY CLIENT ID HERE to your Client ID, replace ENTER YOUR SPOTIFY CLIENT SECRET HERE to your Client Secret, then ENTER YOUR SPOTIFY USERNAME HERE to your username. After replacing press CTRL+X and then press Y to save.
    • Now you can use Google Assistant with Spotify. Remember to use this phrase to play the playlist "Play (your playlist name) from Spotify". Example, say "Ok Google" first, then say "Play Ventures from Spotify". To test this, input this command on your terminal.
    $ /home/${USER}/env/bin/python -u /home/${USER}/GassistPi/src/main.py
    • To stop music say "Ok Google" then "Stop Playing". To stop GassistPi library from running press Ctrl+C.

     

    Libraries Used

    https://github.com/shivasiddharth/GassistPi

     

    Demo Video

    Conclusion

    With Youtube and Spotify integrated into your Google Assistant, you can now play your favorite music via voice command. Also with this guide, it also helps you how to use Google Assistant API keys. Now you can integrate other features on the GassistPi. You can check their Github library to enable other features.

     

    Check other tutorials in this series: Integrating Google Assistant to Raspberry Pi

    Raspberry Pi with ReSpeaker 4-Mic Pi Hat Integration

    Voice-activated Lights Control with IFFFT, Adafruit IO, NodeMCU, and Raspberry Pi

    Voice-Activated Sensor Reading and Light Control using NodeMCU and Raspberry Pi with Google Assistant and Dialogflow

     

    References

    https://github.com/shivasiddharth/GassistPi

    Google assistantMusicRaspberry piRespeakerSpotifySpotify apiVoice-activatedYoutubeYoutube api

    Leave a comment

    All comments are moderated before being published