mikerr
Posts: 2831
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK

APP: play tracks from youtube !

Mon May 08, 2017 11:03 am

No doubt you've found you can't play music on your AIYProjects box from Play Music :cry:

Well now you can play any track from youtube - yay :lol: !

- "Play <trackname>" and after a while (10 seconds) your track will start playing.

- Hold the button for 1 second to kill the music.

Install Intructions:

1. install required programs

First we need mps-youtube and youtube-dl installed

Code: Select all

sudo pip3 install mps-youtube youtube-dl
sudo apt-get update
sudo apt-get install vlc
Mps needs to be configured for VLC:

Code: Select all

mpsyt set player vlc, set playerargs ,exit
2. Setup the action.py file

Quick method:
Use this pre-edited action.py :
https://drive.google.com/open?id=0B5s6S ... 3FFdk11aVk
copy this to voice-recognizer-raspi/src/action.py and skip to step 8

or

Longer method:

Edit voice-recognizer-raspi/src/action.py :

Code: Select all

import RPi.GPIO as gpio
import time
further down:

Code: Select all

# =========================================
# Makers! Implement your own actions here.
# =========================================


playshell = None
class play(object):

    def __init__(self, say, keyword):
        self.say = say
        self.keyword = keyword

    def run(self, voice_command):

        track = voice_command.replace(self.keyword, '', 1)

        global playshell
        if (playshell == None):
            playshell = subprocess.Popen(["/usr/local/bin/mpsyt",""],stdin=subprocess.PIPE ,stdout=subprocess.PIPE)

        playshell.stdin.write(bytes('/' + track + '\n1\n', 'utf-8'))
        playshell.stdin.flush()

        gpio.setmode(gpio.BCM)
        gpio.setup(23, gpio.IN)

        while gpio.input(23):
             time.sleep(1)

        pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE)

Add this further down:

Code: Select all

        
    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================

    actor.add_keyword(_('play'), play(say,_('play')))
3. Restart the recognizer:

Code: Select all

sudo systemctl restart voice-recognizer 
Now press the button and say "Play blue monday" !

(code at https://github.com/mikerr/aiy-playmusic )
Last edited by mikerr on Sun May 14, 2017 5:28 pm, edited 9 times in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

rednuop
Posts: 4
Joined: Mon May 08, 2017 2:28 pm

Re: APP: play tracks from youtube !

Mon May 08, 2017 2:31 pm

I've followed your instructions but I just get a message back saying "Sorry something went wrong, please give it another try".

Am I missing something!? :lol:

Marsamver
Posts: 2
Joined: Mon May 08, 2017 8:56 am

Re: APP: play tracks from youtube !

Mon May 08, 2017 3:08 pm

Brilliant, works great for mine

User avatar
winkleink
Forum Moderator
Forum Moderator
Posts: 288
Joined: Mon Nov 07, 2011 9:12 am
Location: UK

Re: APP: play tracks from youtube !

Mon May 08, 2017 4:57 pm

This is fantastic
Also, a nice guide on adding a command.

I want to be able to play podcasts. I assume if i find a command line player that can take a podcast stream something similar would work.
Twitter:Winkleink
Google+: Winkleink

CJ0206
Posts: 25
Joined: Fri Aug 29, 2014 11:02 pm
Location: Wales, UK

Re: APP: play tracks from youtube !

Mon May 08, 2017 5:42 pm

rednuop wrote:I've followed your instructions but I just get a message back saying "Sorry something went wrong, please give it another try".

Am I missing something!? :lol:
Did you indent this line:

Code: Select all

actor.add_keyword(_('play'), play(say,_('play')))
I found I had to indent below:

Code: Select all

    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================
Don't forget to restart the recogniser or reboot.
Ras Pis: B | Zero
Boards: Zero4U | Voice

pilander
Posts: 7
Joined: Mon May 08, 2017 6:29 pm

Re: APP: play tracks from youtube !

Mon May 08, 2017 6:38 pm

Couple of things:
- Search results are odd, it often doesn't play what you'd expect. This might be an issue with the youtube dl/convert modules, however.
- to restart, simply: sudo systemctl restart voice-recognizer --- no need stop/start

Had a few other things on this, but resolved them. If you modify main.py to pass player directly to the make_actor() function, in theory we could use just youtube-dl and then convert to a wav file, and play directly with the player class. Not sure if you could abort it, though, like you have now. So maybe it'd be better to just get the time length of the file downloaded, and only loop looking for the GPIO event roughly that long to auto stop like an assistant response would?

Thanks for this, cool addon!

GiddyRaccoon
Posts: 6
Joined: Sat Jan 31, 2015 9:11 am

Re: APP: play tracks from youtube !

Mon May 08, 2017 7:54 pm

Code: Select all

p.kill()
this shows up as an error for me specifically the 'p' with a syntax error

If anyone could help that would be great

Giddy Racoon
Last edited by GiddyRaccoon on Mon May 08, 2017 8:25 pm, edited 1 time in total.
GiddyRaccoon

User avatar
winkleink
Forum Moderator
Forum Moderator
Posts: 288
Joined: Mon Nov 07, 2011 9:12 am
Location: UK

Re: APP: play tracks from youtube !

Mon May 08, 2017 7:58 pm

Can someone share a full action.py with this capability enabled.
I've tried to get it working and it isn't working.

I don't know python well enough to troubleshoot as looking at the the action.py file looks like it's not correct.

Seems wrong to me that return actor is below the Makers! Add your own voice commands here. comment.
If I added a new class here it would mean the return command would not be in the right place, or am I wrong. As I said my python is limited.

Code: Select all

    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================

    return actor
Also, I can't get it to accept any of the simple_command instructions.
If i say 'google home' it replied. 'sorry, I don't know how to help with that yet'

If makes me thing the actoin.py file isn't working as it's comments say it will.
Twitter:Winkleink
Google+: Winkleink

pilander
Posts: 7
Joined: Mon May 08, 2017 6:29 pm

Re: APP: play tracks from youtube !

Mon May 08, 2017 9:22 pm

winkleink wrote:Can someone share a full action.py with this capability enabled.
I've tried to get it working and it isn't working.

I don't know python well enough to troubleshoot as looking at the the action.py file looks like it's not correct.

Seems wrong to me that return actor is below the Makers! Add your own voice commands here. comment.
If I added a new class here it would mean the return command would not be in the right place, or am I wrong. As I said my python is limited.

Code: Select all

    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================

    return actor
Also, I can't get it to accept any of the simple_command instructions.
If i say 'google home' it replied. 'sorry, I don't know how to help with that yet'

If makes me thing the actoin.py file isn't working as it's comments say it will.
Two main things are to make sure you paste at the right locations as the code blocks above show, and make sure that the indents stay exactly the same, with the exception of the bottom one, it needs 4 spaces to indent over, which is not in his code block. Eg:

Code: Select all

    # =========================================
    # Makers! Add your own voice commands here.
    # =========================================

    actor.add_keyword(_('play'), play(say,_('play')))
    return actor
Also make sure you installed the needed packages, and you ran the restart command:
sudo systemctl restart voice-recognizer

If it's still not working, you can check the logs by this command:
sudo journalctl -u voice-recognizer -n 50 -f

mikerr
Posts: 2831
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK

Re: APP: play tracks from youtube !

Mon May 08, 2017 11:55 pm

pilander wrote:Couple of things:
- Search results are odd, it often doesn't play what you'd expect. This might be an issue with the youtube dl/convert modules, however.
Try using mpsyt from the command line to get a feel for what it's doing "behind the scenes" :

mpsyt
/track name
Press 1 <enter> to play 1st result.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

philkingmagpi
Posts: 19
Joined: Wed Sep 30, 2015 9:36 am

Re: APP: play tracks from youtube !

Tue May 09, 2017 8:50 am

Thanks so much for this, mikerr!

I rebooted the device first, thinking it wasn't working, but I forgot that there's a quite a long delay before it plays the track. It's working great now, although I turned the volume down to 80% to avoid distortion from the speaker.

mikerr
Posts: 2831
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK

Re: APP: play tracks from youtube !

Tue May 09, 2017 10:13 am

winkleink wrote:Can someone share a full action.py with this capability enabled.
I've tried to get it working and it isn't working.
I've added a pre-modified action.py to the first post.

Obviously you lose any custom actions you may have already set up if you do it that way.
philkingmagpi wrote: I forgot that there's a quite a long delay before it plays the track. It's working great now
I'm working on speeding that up - I can chop 10 seconds off (on my pi zero) by preloading the player app.
Last edited by mikerr on Wed May 10, 2017 8:50 am, edited 3 times in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

CJ0206
Posts: 25
Joined: Fri Aug 29, 2014 11:02 pm
Location: Wales, UK

Re: APP: play tracks from youtube !

Tue May 09, 2017 10:37 am

Maybe worth making a git hub :)

Nice work, I'll have to give it a try later.
Ras Pis: B | Zero
Boards: Zero4U | Voice

ZombieMedia
Posts: 1
Joined: Tue May 09, 2017 12:07 pm

Re: APP: play tracks from youtube !

Tue May 09, 2017 12:10 pm

Wicked - thanks for this. I'll be giving this a try later on tonight :D

Quick noob question - with the install instructions code - do I put them in the start dev terminal?

j.fordham83
Posts: 6
Joined: Sun May 07, 2017 11:05 am

Re: APP: play tracks from youtube !

Tue May 09, 2017 12:40 pm

Thats super cool, got this working on my AIY now :-).

J

User avatar
winkleink
Forum Moderator
Forum Moderator
Posts: 288
Joined: Mon Nov 07, 2011 9:12 am
Location: UK

Re: APP: play tracks from youtube !

Tue May 09, 2017 7:13 pm

Works using the google drive file.

I had things in the wrong place.
Twitter:Winkleink
Google+: Winkleink

Pipecock
Posts: 8
Joined: Tue Sep 04, 2012 8:35 pm
Location: South Wales

Re: APP: play tracks from youtube !

Tue May 09, 2017 9:17 pm

Brilliant, thank you! One of the main things I wanted my cardboard friend to do.

I used the uploaded action.py, worked first time. I had indentation issues.

mikerr
Posts: 2831
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK

Re: APP: play tracks from youtube !

Tue May 09, 2017 9:52 pm

Updated and now twice as fast (after first run) - so around 10 seconds

- keeps Mps-youtube loaded in the background,

(couldn't do this with omxplayer as there's some bug with omxplayer/mps-youtube where it keeps restarting
related to https://github.com/mps-youtube/mps-youtube/issues/156 )

- so moved to VLC for playback
Android app - Raspi Card Imager - download and image SD cards - No PC required !

BMS Doug
Posts: 4423
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: APP: play tracks from youtube !

Tue May 09, 2017 11:08 pm

Thanks Mike, really good.

I've been trying to figure out how to voice command a safe system shutdown but I havent had much of a chance yet.
Doug.
Building Management Systems Engineer.

ktinkerer
Posts: 36
Joined: Wed May 10, 2017 8:31 am

Re: APP: play tracks from youtube !

Wed May 10, 2017 8:55 am

Thanks for this, it works great, although I changed around the button press part so it reacts on any button press (I found that otherwise it would attempt to listen to the music if the button was pressed and react to that)

Code: Select all

while True:
    if gpio.input(23):
        pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE)
        break
    time.sleep(0.1)
I've set mine up to play the radio and this was a lot of help! (I shall attempt to start a seperate thread on that!)
Twitter: @k_tinkerer
Blog: http://www.ktinkerer.co.uk

kingosticks
Posts: 25
Joined: Mon Jun 24, 2013 9:25 am

Re: APP: play tracks from youtube !

Wed May 10, 2017 10:07 am

No doubt you've found you can't play music on your AIYProjects box from Play Music
I'm curious, why not? Is is not just a case of connecting it up to mopidy-gmusic (or similar)?

mikerr
Posts: 2831
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK

Re: APP: play tracks from youtube !

Wed May 10, 2017 10:38 am

kingosticks wrote:
No doubt you've found you can't play music on your AIYProjects box from Play Music
I'm curious, why not? Is is not just a case of connecting it up to mopidy-gmusic (or similar)?
Yes, but you'd have to write an action around that (like this thread).
I meant the functionality isn't built in - like it is on the Google Home
Android app - Raspi Card Imager - download and image SD cards - No PC required !

kingosticks
Posts: 25
Joined: Mon Jun 24, 2013 9:25 am

Re: APP: play tracks from youtube !

Wed May 10, 2017 10:42 am

Ahh OK, with you now.

It looks like it would be quite easy to add a bunch of MPD style actions and interface to Mopidy or mpd with python-mpd2. I would give it a go if my WH Smiths hadn't been cleaned out.

philkingmagpi
Posts: 19
Joined: Wed Sep 30, 2015 9:36 am

Re: APP: play tracks from youtube !

Thu May 11, 2017 9:32 am

It was working fine with omxplayer, but now I'm getting no audio using vlc. Yet I'm using the latter to play radio stations OK. Any ideas?

philkingmagpi
Posts: 19
Joined: Wed Sep 30, 2015 9:36 am

Re: APP: play tracks from youtube !

Thu May 11, 2017 9:44 am

Ah, I've fixed it now. I think it was due to the playerargs not being set to vlc. The command should be:

Code: Select all

mpsyt set player vlc, set playerargs vlc, exit

Return to “AIY Projects”